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<int, 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"; }
修改后
#include<iostream> #include<fstream> #include<array> #define N 5 int main(){ using namespace std; array<char,N>x; ifstream in; in.open("data.txt",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"; }
Vector.hpp #pragma once #include<iostream> #include<cassert> using namespace std; // delaration of template class template<typename T> class Vector { public: Vector(int n) :size{ n } { p = new T[n]; } Vector(int n, T value) :size{ n } { p = new T[n]; for (int i = 0; i < size; i++) { p[i] = value; } } Vector(const Vector& v) :size{ v.size } { p = new T[size]; for (int i = 0; i < size; i++) { p[i] = v.p[i]; } } ~Vector() { delete[] p; } int get_size() { return size; } T& at(int index) { assert(index >= 0 && index < size); return p[index]; } T& operator[](int index) { assert(index >= 0 && index < size); return p[index]; } template<typename T1> friend void output(const Vector<T1>& v); private: int size; T* p; }; template<typename T1> void output(const Vector<T1>& v) { for (int i = 0; i < v.size; i++) { cout << v.p[i] << " ,"; } cout << endl; } 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.cpp #include<iostream> #include <fstream> #include<string> #include<iomanip> using namespace std; void output(std::ostream& out) { string a[27][27] = { " " }; int n = 0; for (int i = 1; i < 27; i++) { a[i][0] = to_string(i); a[0][i] = 97 + n; n++; } for (int i = 1; i < 27; i++) { n = i; for (int j = 1; j < 27; j++) { if (65 + n > 90) { a[i][j] = 65 + n - 26; } else { a[i][j] = 65 + n; } n++; } } for (int i = 0; i < 27; i++) { for (int j = 0; j < 27; j++) { out <<right<< setw(2) << a[i][j] << " "; } out << endl; } } int main() { ofstream out; out.open("cipher_key.txt", ios::out); if (!out.is_open()) { cout << "fail to open file\n"; return 1; } output(out); output(cout); out.close(); }
标签:index,int,++,Vector,实验,include,size From: https://www.cnblogs.com/2021zxq/p/16962713.html