实验题目(1): 在多态性实验第2题的基础上,在复数类中增加输入流运算符>>和输出流运算符<<的重载,主函数通过“cin>>对象名”输入对象的值,通过“cout<<对象名”输出对象的值,输出复数值时将原来主函数中“对象名.print( )”改成“cout<<对象名”形式。 在上一步完成的基础上,将复数类改成一个类模板,只设一个模板参数,即实部和虚部用同一种类型,修改相应的代码,完成输入、输出功能。
#include<iostream> using namespace std; template<class T> class Complex { private: T real; T imag; public: Complex(T r = 0, T i = 0) { real = r; imag = i; } template<class T> friend Complex<T> operator +(const Complex<T> &a, const Complex<T> &b); template<class T> friend Complex<T> operator -(const Complex<T> &a, const Complex<T> &b); template<class T> friend ostream & operator <<(ostream & stream, Complex<T> & a); template<class T> friend istream & operator >>(istream & stream, Complex<T> & a); Complex<T> operator *(Complex<T> &a); Complex<T> operator /(Complex<T> &a); }; template<class T> Complex<T> Complex<T>::operator *(Complex<T> &a) { real = real * a.real; imag = imag * a.imag; return *this; } template<class T> Complex<T> Complex<T>::operator /(Complex<T> &a) { Complex temp; real = real / a.real; imag = imag / a.imag; return *this; } template<class T> istream & operator >>(istream & stream, Complex <T> & a) { cout << "real = "; stream >> a.real; cout << "imag = "; stream >> a.imag; return stream; } template<class T> ostream & operator <<(ostream & stream, Complex <T> & a) { cout << a.real; if (a.imag != 0) { if (a.imag > 0) { cout << "+"; } cout << a.imag << " i"; } cout << endl; return stream; } template<class T> Complex <T> operator +(const Complex <T> &a, const Complex <T> &b) { Complex <T> temp; temp.real = a.real + b.real; temp.imag = a.imag + b.imag; return temp; } template<class T> Complex <T> operator -(const Complex <T> &a, const Complex <T> &b) { Complex <T> temp; temp.real = a.real - b.real; temp.imag = a.imag - b.imag; return temp; } int main() { Complex <double> a1, a2; Complex <double> a3, a4, a5, a6; cout << "a1 :" << endl; cin >> a1; cout << "\na2 :" << endl; cin >> a2; cout << "\na1 is:" << a1 ; cout << "a2 is:" << a2 << endl; a3 = a1 + a2; cout << "a3 = a1+a2 = " << a3 ; a4 = a1 - a2; cout << "a4 = a1-a2 = " << a4 ; a5 = a1 * a2; cout << "a5 = a1*a2 = " << a5 ; a6 = a1 / a2; cout << "a6 = a1/a2 = " << a6 ; while (1) { getchar(); } return 0; } #include <iostream> using namespace std; template<class T> class Complex { T real; T imag; public: Complex(T r = 0, T i = 0) { real = r; imag = i; } template<class T> friend ostream & operator <<(ostream & stream, Complex <T> & a); template<class T> friend istream & operator >>(istream & stream, Complex <T> & a); template<class T> friend Complex<T> operator +(const Complex <T> &a, const Complex <T> &b); }; template<class T> istream & operator >>(istream & stream, Complex <T> & a) { cout << "real = "; stream >> a.real; cout << "imag = "; stream >> a.imag; return stream; } template<class T> ostream & operator <<(ostream & stream, Complex <T> & a) { cout << a.real; if (a.imag != 0) { if (a.imag > 0) { cout << " + "; } cout << a.imag << " i"; } cout << endl; return stream; } template<class T> Complex <T> operator +(const Complex <T> &a, const Complex <T> &b) { Complex <T> temp; temp.real = a.real + b.real; temp.imag = a.imag + b.imag; return temp; } int main() { Complex<int> a1, a2, a3; cout << "a1 :\n"; cin >> a1; cout << "\na2 : \n"; cin >> a2; cout << "\na1 = : " << a1; cout << "a2 = : " << a2; a3 = a1 + a2; cout << "a3 = a1 + a2 = " << a3 << endl; while (1) { getchar(); } return 0; }实验题目2:事先用Windows的记事本建立一个文本文件ff.txt。 ① 编写一个函数void ReadFile(char* s)实现读取以s串为文件名的文本文件的内容在屏幕上显示。 ② 编写一个函数void Change(char s1,char s2)将文本文件中的小写字母全部改写成大写字母生成一个新文件ff2.txt。 ③ 主函数中调用ReadFile(“ff.txt”);显示ff.txt的内容,调用Change (“ff.txt” ,“ff2.txt”);根据ff.txt文件作修改生成一个新的文件ff2.txt,最后再调用ReadFile(“ff2.txt”);显示新文件的内容。
#include<iostream> #include<fstream> using namespace std; //编写一个函数void ReadFile(char* s)实现读取以s串为文件名的文本文件的内容在屏幕上显示 void ReadFile(char *s) { char ch; ifstream inf(s); if (!inf) { cout << "Cannot open the file!\n"; return; } while (inf.get(ch)) { cout << ch ; } inf.close(); } //编写一个函数void Change(char *s1,char *s2)将文本文件中的小写字母全部改写成大写字母生成一个新文件ff2.txt。 void Change(char *s1,char *s2) { ifstream ifile(s1); if(!ifile) { cout<<"ifile cannot be openned!"<<endl; return; } ofstream ofile(s2); if (!ofile) { cout << "ofile cannot be openned!" << endl; return; } char ch; while(ifile.get(ch)) { ch=ch-32; ofile.put(ch); } ifile.close(); ofile.close(); } //主函数中调用ReadFile("ff.txt");显示ff.txt的内容,调用Change ("ff.txt" ,"ff2.txt"); //根据ff.txt文件作修改生成一个新的文件ff2.txt,最后再调用ReadFile("ff2.txt");显示新文件的内容。 int main() { ReadFile("d:\\ff.txt"); Change("d:\\ff.txt", "d:\\ff2.txt"); ReadFile("d:\\ff2.txt"); while (1) { getchar(); } return 0; }实验题目3(选做):定义学生类,该类包含学生的一些基本信息:学号、姓名、性别、成绩。定义流对象,实现用write函数将学生信息以二进制方式写到磁盘文件stu.dat中。再用read将磁盘中的学生信息读到内存显示在屏幕上。
#include<iostream> #include<fstream> using namespace std; #pragma warning(disable:4996) class Student { char num[10]; char name[10]; char sex[3]; int score; public: Student(char *nu=" ", char *na=" ", char *se=" ", int s = 0); friend ostream & operator<<(ostream &out, const Student &s); }; Student::Student(char *nu , char *na , char *se , int s) { strcpy(name, nu); strcpy(name, na); strcpy(name, se); score = s; } ostream & operator<<(ostream &out, const Student &s) { out << "\n Number:" << s.num << " Name:" << s.name << " Sex:" << s.sex << " Score:" << s.score; return out; } void CreateBiFile(char *filename) { ofstream out(filename); Student stu[3] = { Student("001","Sewsa","女",100), Student("002","Gewsa","女",90) , Student("003","Wewsa","女",95) }; out.write((char *)stu, sizeof(Student) * 3); out.close(); } void ReadBiFile(char *filename) { Student stu[3]; int i = 0; ifstream in(filename); while (!in.eof()) { in.read((char*)&stu[i++], sizeof(Student)); } for (int j = 9; j < i - 1; j++) { cout << stu[j]; } in.close(); } int main() { CreateBiFile("D:\\ff.dat"); ReadBiFile("D:\\ff.dat"); while (1) { getchar(); } return 0; }