首页 > 其他分享 >实验六 模板类和文件

实验六 模板类和文件

时间:2022-12-03 16:26:02浏览次数:29  
标签:std 文件 int Vector 实验 output include 模板 size

实验三

task3.1

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 int main() {
 6 using namespace std;
 7 array<int, N> x {97, 98, 99, 100, 101};
 8 ofstream out;
 9 out.open("data1.dat", ios::binary);
10 if(!out.is_open()) {
11 cout << "fail to open data1.dat\n";
12 return 1;
13 }
14 // 把从地址&x开始连续sizeof(x)个字节的数据块以字节数据块方式写入文件data1.txt
15 out.write(reinterpret_cast<char *>(&x), sizeof(x));
16 out.close();
17 }

task3.2

 1 #include <iostream>
 2 #include <fstream>
 3 #include <array>
 4 #define N 5
 5 int main() {
 6 using namespace std;
 7 array<int, N> x;
 8 ifstream in;
 9 in.open("data1.dat", ios::binary);
10 if(!in.is_open()) {
11 cout << "fail to open data1.dat\n";
12 return 1;
13 }
14 // 从文件流对象in关联的文件data1.dat中读取sizeof(x)字节数据写入&x开始的地址单元
15 in.read(reinterpret_cast<char *>(&x), sizeof(x));
16 in.close();
17 for(auto i = 0; i < N; ++i)
18 cout << x[i] << ", ";
19 cout << "\b\b \n";
20 }

 

 array<int, n=""> x; ----> 修改成: array<char, n=""> x

 

 读取97的ascii码为97,int型占四个字节,<int,5>一共占20个字节,<char,5>占五个字节,所以先输出97对应的a,然后空出三个字节的空再读取下一个98ascii码对应的字母b.

 

实验四

Vector.hpp

 1 #include<cassert>
 2 using namespace std;
 3 template<typename T>
 4 class Vector{
 5     public:
 6         Vector(int n) {
 7         p=new T[n];
 8         size = n;
 9     }
10     Vector(int n,T value):size{n}{
11     p=new T[size];
12     for(int i=0;i<size;i++){
13         p[i]=value;
14         }
15     }
16     Vector(const Vector<T> &a):size{a.size}{
17     p=new T[size];
18     for(int i=0;i<size;i++){
19         p[i]=a.p[i];
20         }
21     }
22         ~Vector(){
23             delete p;
24         }
25         T &at(int index){
26         assert(index>=0&&index<size);
27         return p[index];
28         }
29         int get_size(){
30             return size;
31         }
32         friend void output(Vector &v){
33         int i;
34         for(i=0;i<v.size-1;i++){
35             cout << v.p[i] << ",";
36             }
37         cout << v.p[i] << endl;
38         }
39         T& operator[](int i){ //运算符重载 
40         return p[i];
41         }
42     private:
43         T size;
44         T *p;
45 
46 };

task4.hpp

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 void test() {
 4 using namespace std;
 5 int n;
 6 cin >> n;
 7 Vector<double> x1(n);
 8 for(auto i = 0; i < n; ++i)
 9 x1.at(i) = i * 0.7;
10 output(x1);
11 Vector<int> x2(n, 42);
12 Vector<int> x3(x2);
13 output(x2);
14 output(x3);
15 x2.at(0) = 77;
16 output(x2);
17 x3[0] = 999;
18 output(x3);
19 }
20 int main() {
21 test();
22 }

 

 

实验五

 1 #include<iostream>
 2 #include<iomanip>
 3 #include<string>
 4 #include<fstream>
 5 using namespace std;
 6 void output(std::ostream &out)
 7 {
 8     char a[26][26];
 9     cout<<"  ";
10     out<<"  ";
11     for(char i='a';i<='z';i++)
12     {
13         cout<<setw(2)<<setfill(' ')<<i;
14         out<<setw(2)<<setfill(' ')<<i;
15     }
16     cout<<endl;
17     out<<endl;
18     for(int j=0;j<26;j++)
19     {
20         cout<<setw(2)<<setfill(' ')<<j+1;
21         out<<setw(2)<<setfill(' ')<<j+1;
22         for(int k=0;k<26;k++)
23         {
24             a[j][k]='A'+char((j+k+1)%26);
25             cout<<setw(2)<<setfill(' ')<<a[j][k];
26             out<<setw(2)<<setfill(' ')<<a[j][k];
27         }
28         out<<endl;
29         cout<<endl;
30     }
31 }
32 int main()
33 {
34     ofstream out;
35     out.open("cipher_key.txt",ios::out);
36     if(!out.is_open())
37     {
38         cout<<"fail to open cipher_ke.text"<<endl;
39         return 0;
40     }
41     output(out);
42     out.close();
43 }

 

 

 

标签:std,文件,int,Vector,实验,output,include,模板,size
From: https://www.cnblogs.com/ljhakuna/p/16948223.html

相关文章

  • 实验6
    TASK3:#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};......
  • 实验六
    task3_1.cpp#include<iostream>#include<fstream>#include<array>#defineN5intmain(){usingnamespacestd;array<int,N>x{97,98,99,100,101};of......
  • C++学习------csetjmp头文件的源码学习
    引言csetjmp是C++对setjmp.h头文件的封装,通过这个头文件提供的工具允许程序员通过提供执行跳转的方法来绕过正常的函数调用和返回规程,从而保留调用环境。其中定义了一些函数......
  • oop 实验6 模板类和文件I/O
    task3程序源码task3_1.cpp1#define_CRT_SECURE_NO_WARNINGS12#include<iostream>3#include<fstream>4#include<array>5#defineN56usingnames......
  • gps.conf文件解读
    https://www.cnblogs.com/zhuwei0901-yanwu/p/9485607.html 手机adbshell修改gps.conf文件后,导入记得更改权限。adbshell里面要有/system/framework里面的com.g......
  • 阿里云盘不能分享压缩文件
    阿里云盘不能分享压缩文件的方法一、下载压缩文件伪装工具地址:压缩文件伪装工具 提取码:tn77二、使用工具 把需要伪装的文件拖到对话框里面按回车就会文件地址......
  • python实验报告(第13章)
    一、实验目的1.掌握Pygame的基础知识。二、实验环境python版本:3.10(64-bit)三、实验内容1.实例1  实验结果:  四、实验分析:1.掌握了Pygame的基础知识。......
  • java FileReader FileWriter修改文件内容
    javaFileReader FileWriter修改文件内容 publicstaticvoideditContent(FilefileParamOld,FilefileParamNew)throwsIOException{FileReaderfr=n......
  • 头文件<numeric>
    累加函数accumulate(first,last,val,op);first和last表示序列(可以是数组、vector、array等)的首尾指针/迭代器,val表示操作的初始值,op表示运算符,进行累加、累减......
  • 2022/12/3 Python实验报告
      实验报告1、实验目的和要求了解并掌握Pygame的基本应用2、实验环境笔记本与Python书本3、实验过程实例01制作一个跳跃的小球游戏创建一个游戏......