task3
task3_1.cpp
#include <iostream> #include <fstream> #include <array> #define N 5 int main() { using namespace std; array<int, N> x {97, 98, 99, 100, 101}; ofstream out; out.open("data1.dat", ios::binary); if(!out.is_open()) { cout << "fail to open data1.dat\n"; return 1; } out.write(reinterpret_cast<char *>(&x), sizeof(x)); out.close(); }
task3_2.cpp
#include <iostream> #include <fstream> #include <array> #define N 5 int main() { using namespace std; array<char, N> x; ifstream in; in.open("data1.dat", ios::binary); if(!in.is_open()) { cout << "fail to open data1.dat\n"; return 1; } in.read(reinterpret_cast<char *>(&x), sizeof(x)); in.close(); for(auto i = 0; i < N; ++i) cout << x[i] << ", "; cout << "\b\b \n"; }
task4
Vector.hpp
#pragma once #include<iostream> using std::cout; using std::endl; template<class T> class Vector { public: Vector(int n) :num{ n }, data{ new T[n] } {}; Vector(int n, T x) :num{ n }, data{ new T[n] } { for (int i = 0; i <n; ++i) data[i] = x; } Vector(const Vector& obj); ~Vector() { delete data; } int get_size()const { return num; } T& at(int i) { return data[i]; } T& operator[](int i) { return data[i]; } friend void output(Vector& obj) { for (int i=0;i<obj.num;++i) cout << obj.data[i]<<","; cout << "\b\b" << endl; } private: T* data; int num; }; template<typename T> Vector<T>::Vector(const Vector& obj) :num{obj.num} { data = new T[obj.num]; for (int i = 0; i <obj.num; ++i) data[i] = obj.data[i]; }
task4.cpp
#include <iostream> #include "Vector.hpp" void test() { using namespace std; int n; cin >> n; Vector<double> x1(n); for (auto i = 0; i < n; ++i) x1.at(i) = i * 0.7; output(x1); Vector<int> x2(n, 42); Vector<int> x3(x2); output(x2); output(x3); x2.at(0) = 77; output(x2); x3[0] = 999; output(x3); } int main() { test(); }
task5
task5.cpp
#include<iostream> #include<fstream> #include<iomanip> using std::cout; using std::endl; using std::setw; void output(std::ostream &out) { out<<setw(4)<<" "; for(int i=0;i<26;++i) out<<setw(4)<<char('a'+i); out<<endl; for(int i=1;i<=26;++i) { out<<setw(4)<<i; for(int j=0;j<26;++j) out<<setw(4)<<char('A'+(i+j)%26); out<<endl; } } int main() { output(cout); std::ofstream fout; fout.open("cipher_key.txt"); output(fout); fout.close(); }
标签:std,int,Vector,实验,using,include,out From: https://www.cnblogs.com/gosj/p/16938634.html