实验任务3
#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("data.txt",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(); }
#include<iostream> #include<fstream> #include<array> #define N 5 int main(){ using namespace std; array<int,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"; }
#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"; }
实验任务4
Vector.h
#pragma once #include<iostream> using namespace std; template<typename T> class Vector{ public: Vector(int n):size{n}{ value=new T[n]; } Vector(int n,T v):size{n}{ value=new T[n]; for(int i=0;i<n;i++) value[i]=v; } Vector(const Vector<T> &a):size{a.size}{ value=new T[size]; for(int i=0;i<size;i++) value[i]=a.value[i]; } T &operator[](int i){ return value[i]; } int get_size() const{ return size; } T &at(int index){ return value[index]; } friend void output(const Vector<T> &a){ for(int i=0;i<a.size;i++) cout<<a.value[i]<<","; cout<<"\b\b\n"; } private: int size; T *value; };
task4.cpp
#include <iostream> #include "Vector.h" 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.5; output(x1); Vector<int> x2(n, 66); Vector<int> x3(x2); output(x2); output(x3); x2.at(0) = 88; output(x2); x3[0] = 99; output(x3); } int main() { test(); }
实验任务5
#include <iostream> #include<fstream> #include<string> using namespace std; void output() { string a[27][27] = { " " }; int i, j, n = 0; for (i = 1; i < 27; i++) { a[i][0] = to_string(i); a[0][i] = 97 + n; n++; } for (i = 1; i < 27; i++) { n = i; for (j = 1; j < 27; j++) { if (65 + n > 90) { a[i][j] = 65 + n - 26; } else { a[i][j] = 65 + n; } n++; } } ofstream out; out.open("cipher_ke.txt"); if (!out.is_open()) { cout << "fail to open cipher_key.txt to write" << endl; } for (i = 0; i < 27; i++) { for (j = 0; j < 27; j++) { cout << a[i][j] << " "; out << a[i][j] << " "; } cout << endl; out << endl; } } int main() { output(); }
标签:文件,int,namespace,Vector,实验,include,open,模板,out From: https://www.cnblogs.com/DeviIMayCry/p/16952006.html