实验任务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("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(); }.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"; }.cpp
实验任务4
#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(); }.cpp
#pragma once #include<cassert> using namespace std; template<typename T> class Vector { private: T l; T *p; public: Vector(int n) { p=new T[n]; l = n; } ~Vector() { delete p; } Vector(const Vector<T> &a):l{a.l} { p=new T[l]; for(int i=0;i<l;i++) { p[i]=a.p[i]; } } Vector(int n,T m):l{n} { p=new T[l]; for(int i=0;i<l;i++) { p[i]=m; } } T &at(int index) { assert(index>=0&&index<l); return p[index]; } int get_size() { return l; } T& operator[](int i) { return p[i]; } friend void output(Vector &v) { int i; for(i=0;i<v.l-1;i++) { cout << v.p[i] << ","; } cout << v.p[i] << endl; } };.hpp
实验任务5
#include<iostream> #include<iomanip> #include<string> #include<fstream> using namespace std; void output(std::ostream &out) { char a[26][26]; cout<<" "; out<<" "; for(char i='a';i<='z';i++) { cout<<setw(2)<<setfill(' ')<<i; out<<setw(2)<<setfill(' ')<<i; } cout<<endl; out<<endl; for(int j=0;j<26;j++) { cout<<setw(2)<<setfill(' ')<<j+1; out<<setw(2)<<setfill(' ')<<j+1; for(int k=0;k<26;k++) { a[j][k]='A'+char((j+k+1)%26); cout<<setw(2)<<setfill(' ')<<a[j][k]; out<<setw(2)<<setfill(' ')<<a[j][k]; } out<<endl; cout<<endl; } } int main() { ofstream out; out.open("cipher_key.txt",ios::out); if(!out.is_open()) { cout<<"fail to open cipher_ke.text"<<endl; return 0; } output(out); out.close(); }.cpp
标签:std,int,Vector,实验,output,include,out From: https://www.cnblogs.com/shun16/p/16960564.html