PS:大一下学期C++课程设计
1.成绩管理系统
#include<stdio.h> #include<string> #include<iostream> #include<fstream> #include<ostream> #include<iomanip>//setw(),控制空格 #include<algorithm>//为了计算文件里面含有的对象个数 #include<windows.h> #include<cstdlib>//防止^Z导致程序异常 #include<mmsystem.h>//控制音乐播放的头文件 #pragma comment(lib, "Winmm.lib")//控制音乐播放的头文件 using namespace std; int N; class Cstudent { public: Cstudent()//构造函数,将成绩赋值为零 { string grade; grade = '0'; } void display()//控制输出,进行左对齐 { cout<<"姓名:"<<std::left<<setw(20)<<na; cout<<"学号:"<<std::left<<setw(20)<<id; cout<<"成绩:"<<std::left<<setw(20)<<gr; } void setdate(string name, string idnumber, string grade)//对私有变量进行复制 { na = name; id = idnumber; gr = grade; } string student_name() { return na; } string student_idnumber() { return id; } string student_grade() { return gr; } string find_idnumber()//按照学号查找时学号 { return id; } string find_NAME()//按照姓名查找时返回姓名 { return na; } int count() { int score; char* end; string grade; score = static_cast<int>(strtol(gr.c_str(), &end, 10));//使用strtol(string to long) 利用string的c_str()转化为C风格字符串 return score; } void revise() { char* end; int grade_bian; cout << "请根据以下提示进行修改。" << endl; cout << "姓名:"; cin >> na; cout << "学号:"; cin >> id; cout << "成绩(0~100):"; while(cin >> gr) { grade_bian=static_cast<int>(strtol(gr.c_str(), &end, 10)); if(grade_bian>=0&&grade_bian<=100) { break; } else { cout<<"输入有误!"<<endl; cout << "请输入成绩(0~100):"; } } } private: string na; string id; string gr; }; Cstudent *p;//将要创建的对象做成指针类型 void mainjiemian()//主界面 { cout << " ★-----★---------★---------★-----★" << endl; cout << " 欢迎进入学生成绩管理系统 " << endl; cout << " ☆ ☆" <<endl; cout << " 1 录入学生信息 " << endl; cout << " ☆ 2 输出学生信息 ☆" << endl; cout << " 3 查询学生信息 " << endl; cout << " ☆ 4 修改学生信息 ☆" << endl; cout << " 5 统计学生信息 " << endl; cout << " ☆ 6 文件录入信息 ☆" << endl; cout << " 7 文件读取信息 " << endl; cout << " ☆ 8 退出学生系统 ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << endl; cout << "请选择数字命令:"; } void help()//进入显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 制作小软件,我们是认真的! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " Made By 刘建伟" << endl; cout << endl; cout << "请输入学生数:"<<endl; } void jieshu()//结束显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 感谢您的使用! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; } void kaobei()//拷贝文件到test1.txt { char ch; ifstream file("H:\\student_in.txt");//读取H盘文件 ofstream file1("H:/test1.txt");//创建文本文件 while(file.get(ch))//读取文本内容 { //cout<<ch;//输出文本内容到控制台 file1<<ch;//写入内容到文件 } file.close();//关闭文件流 file1.close();//关闭文件流 cout<<endl; } void play() { // 注意PlaySound只能播放没有经过压缩的wav格式的声音文件 PlaySound("H:\\yinyue.wav",NULL,SND_FILENAME|SND_ASYNC);//控制音乐输出,带有三个参数,loop为阻塞 } int line() { ifstream file1("H:\\test1.txt");//没有就创建 string str; int line = 0; while (file1) { getline(file1, str); remove(str.begin(), str.end(), ' ');//删除从文件开始到结束中的空格 remove(str.begin(), str.end(), '\t');//删除从文件开始到结束中的空格 if (str.length() > 0)//如果有一行除去空格之后不为零就算作是一个对象 line++; } return line; } int main() { play();//播放音乐 system("color 57"); help(); int line_file;//文件里的学生数和总的学生数 int i; string in;//命令 int n;//键盘输入的学生数 int flag_case1 = 0;//是否执行了第一步 int flag_case7 = 0;//是否执行了第七步 int panduan; char* end;//末端指针 string sn; while(cin>>sn) { /*if (!cin) { cin.clear();//清除cin的错误状态 cin.sync();//清空cin缓冲区里面未读取的信息... cout << "输入错误,重新返回主界面。" << endl; //system("pause"); //cout<<flush; system("cls"); help(); continue; }*/ int flag_sn=0; int l_sn=sn.length(); n=static_cast<int>(strtol(sn.c_str(), &end, 10)); if(l_sn>0&&l_sn<5) { for(i=0;i<l_sn;i++) { if(sn.at(i)>='0'&&sn.at(i)<='9') { flag_sn=0; } else { flag_sn=1; } } } else { flag_sn=1; } if(flag_sn==1) { cout<<"输入有误!"<<endl; system("pause"); system("cls"); help(); } else { break; } } kaobei();//拷贝文件 line_file = line(); N = n + line_file; p = new Cstudent[N]; do { system("cls"); mainjiemian(); cin >> in; if (!cin) { cin.clear();//清除cin的错误状态 cin.sync();//清空cin缓冲区里面未读取的信息... cout << "输入错误,重新返回主界面。" << endl; system("pause"); //cin >>in; continue; } if(in.length()<2) panduan = static_cast<int>(strtol(in.c_str(), &end, 10)); else panduan=0; switch (panduan+48) { case'1'://**************************录入学生信息 { system("cls"); string name; string idnumber; string grade; for (i = 0; i < n; i++) { double grade_bian; cout<<"输入第"<<i+1<<"位同学的信息。"<<endl; cout << "请输入姓名:"; cin >> name; cout << "请输入学号:"; while(cin >> idnumber) { int flag_09=0; int l_idnumber=idnumber.length(); for(int j=0;j<l_idnumber;j++) { char linshi=idnumber[j]; if(linshi>='0'&&linshi<='9') { flag_09=1; } else { flag_09=0; break; } } if(flag_09==1) { int flag_idnumber=0; for(int j=0;j<N;j++) { if(idnumber==p[j].student_idnumber()) { flag_idnumber=1; } } if(flag_idnumber==0) { break; } else { cout<<"学号已经存在,输入有误"<<endl; cout << "请输入学号:"; } } if(flag_09==0) { cout<<"请输入数字类型的学号!"<<endl; cout<<"请输入学号:"; } } cout << "请输入成绩(0~100):";//判断成绩是否在规定的区间 while(cin >> grade) { int l_grade=grade.length(); grade_bian=static_cast<int>(strtol(grade.c_str(), &end, 10));//将成绩转化为int型来判断大小 if(l_grade>1) { if(grade_bian>=0&&grade_bian<=100&&l_grade<=3&&grade.at(l_grade-1)>='0'&&grade.at(l_grade-1)<='9'&&grade.at(l_grade-2)>='0'&&grade.at(l_grade-2)<='9') { //利用at()来返回字符串中某个位置处的函数 break; } else { cout<<"输入有误!"<<endl; cout << "请输入成绩(0~100):"; } } else { if(grade_bian>=0&&grade_bian<=100&&l_grade<=3&&grade.at(l_grade-1)>='0'&&grade.at(l_grade-1)<='9') { //利用at()来返回字符串中某个位置处的函数 break; } else { cout<<"输入有误!"<<endl; cout << "请输入成绩(0~100):"; } } } p[i].setdate(name, idnumber, grade);//将输入的数据传给私有变量 } flag_case1 = 1;//执行过第一步就标志为1 system("pause"); break; } case'2'://***************************输出学生信息 { system("cls"); if (flag_case7 == 1&&flag_case1==0) { for (i = n; i < N; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } else if(flag_case7 == 0&&flag_case1==1) { for (i = 0; i < n; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } else if(flag_case7 == 1&&flag_case1==1) { for (i = 0; i < N; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'3'://***********************查询学生信息 { system("cls"); if (flag_case1 == 1||flag_case7==1) { int flag_find; cout<<"请选择查询方式:1.按学号查询 2.按姓名查询"<<endl; cin>>flag_find; if(flag_find==1) { cout << "请输入要查询的学生学号:" << endl; string idnumber; int flag; cin >> idnumber; for (i = 0; i < N; i++) { if (p[i].find_idnumber() == idnumber) { p[i].display(); flag = 1; cout << endl; } } if (flag != 1) { cout << "输入信息有误,查询无果。" << endl; } } if(flag_find==2) { cout << "请输入要查询的学生姓名:" << endl; string NAME; int flag; cin>>NAME; for(i=0;i<N;i++) { if(p[i].find_NAME()==NAME) { p[i].display(); flag = 1; cout << endl; } } if (flag != 1) { cout << "输入信息有误,查询无果。" << endl; } } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'4'://****************************修改学生信息 { system("cls"); if (flag_case1 == 1||flag_case7==1) { int flag_4=0; cout << "请输入要修改的学生学号:" << endl; string idnumber; string name; string grade; cin >> idnumber; int grade_bian; cout<<endl; for (i = 0; i < N; i++) { if (p[i].find_idnumber() == idnumber) { cout<<"修改之前的信息:"<<endl; p[i].display(); cout<<endl<<endl; cout<<"请根据以下提示进行修改:"<<endl; cout << "请输入姓名:"; cin >> name; cout << "请输入学号:"; while(cin >> idnumber) { int flag_09=0; int l_idnumber=idnumber.length(); for(int j=0;j<l_idnumber;j++) { char linshi=idnumber[j]; if(linshi>='0'&&linshi<='9') { flag_09=1; } else { flag_09=0; break; } } if(flag_09==1) { int flag_idnumber=0; for(int j=0;j<N;j++) { if(idnumber==p[j].student_idnumber()) { flag_idnumber=1; } } if(flag_idnumber==0) { break; } else { cout<<"学号已经存在,输入有误"<<endl; cout << "请输入学号:"; } } if(flag_09==0) { cout<<"请输入数字类型的学号!"<<endl; cout<<"请输入学号:"; } } cout << "请输入成绩(0~100):";//判断成绩是否在规定的区间 while(cin >> grade) { int l_grade=grade.length(); grade_bian=static_cast<int>(strtol(grade.c_str(), &end, 10));//将成绩转化为int型来判断大小 if(l_grade>1) { if(grade_bian>=0&&grade_bian<=100&&l_grade<=3&&grade.at(l_grade-1)>='0'&&grade.at(l_grade-1)<='9'&&grade.at(l_grade-2)>='0'&&grade.at(l_grade-2)<='9') { //利用at()来返回字符串中某个位置处的函数 break; } else { cout<<"输入有误!"<<endl; cout << "请输入成绩(0~100):"; } } else { if(grade_bian>=0&&grade_bian<=100&&l_grade<=3&&grade.at(l_grade-1)>='0'&&grade.at(l_grade-1)<='9') { //利用at()来返回字符串中某个位置处的函数 break; } else { cout<<"输入有误!"<<endl; cout << "请输入成绩(0~100):"; } } } p[i].setdate(name, idnumber, grade);//将输入的数据传给私有变量 //p[i].revise(); //cout<<endl; cout << "修改成功!" << endl; cout<<"修改之后的信息:"<<endl; p[i].display(); ofstream ofile("H:\\student_in.txt", ios::out); //把三个私有变量信息插入到文件 ofile << p[i].student_name() << "\t"; ofile << p[i].student_idnumber() << "\t"; ofile << p[i].student_grade() << endl; cout<<endl<<endl; flag_4=1; } } if(flag_4==0) { cout<<"查询无果!无法进行修改。"<<endl; } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } cout<<endl; system("pause"); break; } case'5'://****************************统计输出学生信息 { system("cls"); if (flag_case1 == 1||flag_case7==1)//通过判断有没有执行第一步和第七步来判断统计输出时总数的大小 { int zongshu; if (flag_case7 == 1&&flag_case1==0) { zongshu=N; } if(flag_case7 == 0&&flag_case1==1) { zongshu=n; } if(flag_case7 == 1&&flag_case1==1) { zongshu=N; } cout << "学生成绩信息统计分布" << endl; Cstudent *q, *r, *s, *t, *z; q = new Cstudent[zongshu]; r = new Cstudent[zongshu]; s = new Cstudent[zongshu]; t = new Cstudent[zongshu]; z = new Cstudent[zongshu]; int qq = 0, rr = 0, ss = 0, tt = 0, zz = 0;//每个分数段的标志 if(flag_case1==1) i=0; else i=n; for (i; i < zongshu; i++) { if (p[i].count() < 60) { q[qq] = p[i]; qq = qq + 1; } else if (p[i].count() <= 69) { r[rr] = p[i]; rr = rr + 1; } else if (p[i].count() <= 79) { s[ss] = p[i]; ss = ss + 1; } else if (p[i].count() <= 89) { t[tt] = p[i]; tt = tt + 1; } else if (p[i].count() <= 100) { z[zz] = p[i]; zz = zz + 1; } } if (qq != 0) { cout << "低于60分的学生:" << endl << endl; for (int i = 0; i < qq; i++) { q[i].display(); cout << endl; } cout << endl; } else cout << "没有低于60分的学生" << endl << endl; if (rr != 0) { cout << "60分到69分的学生信息:" << endl << endl; for (int i = 0; i < rr; i++) { r[i].display(); cout << endl; } cout << endl; } else cout << "没有60分到69分的学生" << endl << endl; if (ss != 0) { cout << "70分到79分的学生信息:" << endl << endl; for (int i = 0; i < ss; i++) { s[i].display(); cout << endl; } cout << endl; } else cout << "没有70分到79分的学生" << endl << endl; if (tt != 0) { cout << "80分到89分的学生信息:" << endl << endl; for (int i = 0; i < tt; i++) { t[i].display(); cout << endl; } cout << endl; } else cout << "没有80分到89分的学生" << endl << endl; if (zz != 0) { cout << "90分到100分的学生信息:" << endl << endl; for (int i = 0; i < zz; i++) { z[i].display(); cout << endl; } cout << endl; } else cout << "没有90分到100分的学生" << endl << endl; if(flag_case1==1) i=0; else i=n; ofstream ofile("H:\\student_in.txt", ios::out); for (i ; i < zongshu; i++) {//把三个私有变量信息插入到文件 ofile << p[i].student_name() << "\t"; ofile << p[i].student_idnumber() << "\t"; ofile << p[i].student_grade() << endl; } cout << "即将返回页面主菜单,进行重新选择" << endl; } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'6'://******************************将学生信息保存到磁盘文件当中,利用追加的方式 { system("cls"); if (flag_case1 == 1||flag_case7==1) { for (int i = 0; i < n; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } ofstream ofile("H:\\student_in.txt", ios::app); for (i = 0; i < n; i++) {//把三个私有变量信息插入到文件 ofile << p[i].student_name() << "\t"; ofile << p[i].student_idnumber() << "\t"; ofile << p[i].student_grade() << endl; } cout << endl; cout << "所有学生信息都已保存完毕。" << endl; } else { cout << "请先执行第一步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'7'://*************************从磁盘文件中读入学生信息,并创建学生类对象 { system("cls"); ifstream ofile; string name; string idnumber; string grade; int i = n; ofile.open("H:\\student_in.txt", ios::in); if (ofile.is_open()) { for ( i = n; i < N; i++) {//依次提取学生三个基本私有变量信息 ofile >> name; ofile >> idnumber; ofile >> grade; p[i].setdate(name, idnumber, grade); if (i >= N) { cout << "文件数据已经全部导入。" << endl; cout << "当前学生数量:" << i << endl; cout << "当前存储容量已达到最大值!" << endl; break; } } if(flag_case1==1) { for (i = 0; i < N; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } else { for (i = n; i < N; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } /*for (i = 0; i < N; i++) { p[i].display(); cout << endl; }*/ cout << "已成功导入的学生数量:" << i << endl; flag_case7 = 1; } else { cout << "打开文件时出错!"; } ofile.close(); system("pause"); break; } case'8'://*************************系统退出功能 { system("cls"); jieshu(); break; } default: { cin.clear(); cin.sync(); cout << "输入错误,重新返回主界面。" << endl; system("pause"); break; } } } while (panduan != 8); return 0; }
2.复数
#include<iostream> #include<string> #pragma warning (disable: 4786) #include<vector> using namespace std; class CComplex; ostream& operator<<(ostream& os,CComplex&p); istream& operator>>(istream& is,CComplex&p); CComplex operator-(double d,const CComplex&c2); CComplex* p; int i; int n; int flag=0; int flag_case3=0,flag_case4=0,flag_case5=0,flag_case6=0,flag_case7=0; int l; class CComplex { public: friend ostream& operator<<(ostream&,CComplex&); friend istream& operator>>(istream&,CComplex&); CComplex operator+(const CComplex&c); CComplex operator-(const CComplex&c); CComplex operator+(double d); CComplex operator-(double d); CComplex(double re=0.0,double im=0.0) { real=re; imag=im; } friend CComplex operator-(double d,const CComplex&c2); CComplex operator=(const CComplex&c); CComplex operator[](int z); double Real() { return real; } double Imag() { return imag; } void Dispaly() { if(real==0) { if(imag>0) cout<<imag<<"i"<<endl; if(imag==0) cout<<real<<endl; if(imag<0) cout<<imag<<"i"<<endl; } else { if(imag>0) cout<<real<<"+"<<imag<<"i"<<endl; if(imag<0) cout<<real<<imag<<"i"<<endl; if(imag==0) cout<<real<<endl; } } private: double real; double imag; }; ostream& operator<<(ostream& os,CComplex&p) { if(p.real==0) { if(p.imag>0) os<<p.imag<<"i"<<endl; if(p.imag==0) os<<p.real<<endl; if(p.imag<0) os<<p.imag<<"i"<<endl; } else { if(p.imag>0) os<<p.real<<"+"<<p.imag<<"i"<<endl; if(p.imag<0) os<<p.real<<p.imag<<"i"<<endl; if(p.imag==0) os<<p.real<<endl; } return os; } istream& operator>>(istream& is,CComplex&p) { is>>p.real>>p.imag; return is; } CComplex CComplex::operator+(const CComplex& c) { CComplex t; t.real=real+c.real; t.imag=imag+c.imag; return t; } CComplex CComplex::operator-(const CComplex& c) { CComplex t; t.real=real-c.real; t.imag=imag-c.imag; return t; } CComplex CComplex::operator=(const CComplex& c) { real=c.real; imag=c.imag; return *this; } CComplex CComplex::operator+(double d) { CComplex t; t.real=real+d; t.imag=imag; return t; } CComplex CComplex::operator-(double d) { CComplex t; t.real=real-d; t.imag=imag; return t; } istream & operator>>(istream & in,vector<string>& s) { string str; in>>str; s.push_back(str); return in; } CComplex operator-(double d,const CComplex &c2) { return CComplex(d-c2.real,c2.imag); } CComplex CComplex::operator[](int z) { cout<<"liujianwei"<<endl; return (z-1); } void xianshi() { for(i=0;i<=n-1;i++) { cout<<"("<<i+1<<"):"; p[i].Dispaly(); } } void zhujiemian() { cout << " ************************************" << endl; cout << " 欢迎进入复数计算界面 " << endl; cout << endl; cout << " 1 输入复数信息 " << endl; cout << " 2 输出复数信息 " << endl; cout << " 3 两个复数加和 " << endl; cout << " 4 两个复数相减 " << endl; cout << " 5 实数复数相加 " << endl; cout << " 6 实数复数相减 " << endl; cout << " 7 复数之间赋值 " << endl; cout << " 8 获取特定复数 " << endl; cout << " 9 退出当前界面 " << endl; cout << endl; cout << " ************************************" << endl; cout << endl; cout << "请选择数字命令:"; } int main() { system("color 0b"); string c1,c2; int c1_int,c2_int; char* end; double shishu; char in; cout<<"注意:除了键盘输入复数的个数之外,系统将自动开辟6个空间,用于保存计算结果。"<<endl; cout<<"请输入复数个数:"; cin>>n; cout<<endl; p=new CComplex[n+7]; do { system("cls"); //system("color 1b"); zhujiemian(); cin >> in; switch(in) { case'1': { system("cls"); for(i=0;i<n;i++) { cout<<"输入第"<<i+1<<"个的实部和虚部:"<<endl; cin>>p[i]; } for(i=0;i<n;i++) { cout<<"("<<i+1<<"):"; cout<<p[i]; } system("pause"); break; } case'2': { system("cls"); xianshi(); if(flag_case3==1) { cout<<"("<<n+1<<"):"; p[n+1].Dispaly(); } if(flag_case4==1) { cout<<"("<<n+2<<"):"; p[n+2].Dispaly(); } if(flag_case5==1) { cout<<"("<<n+3<<"):"; p[n+3].Dispaly(); } if(flag_case6==1) { cout<<"("<<n+4<<"):"; p[n+4].Dispaly(); cout<<"("<<n+5<<"):"; p[n+5].Dispaly(); } if(flag_case7==1) { cout<<"("<<n+6<<"):"; p[n+6].Dispaly(); } system("pause"); break; } case'3': { system("cls"); xianshi(); cout<<"请输入想加和的两位复数的下标"<<endl; while(cin>>c1>>c2) { c1_int=static_cast<int>(strtol(c1.c_str(), &end, 10)); c2_int=static_cast<int>(strtol(c2.c_str(), &end, 10)); if(c1_int>n||c2_int>n) { cout<<"输入的下标不在所给范围内部!"<<endl; cout<<"请输入想加和的两位复数的下标"<<endl; } else { break; } } p[n+1]=p[c1_int-1]+p[c2_int-1]; cout<<"计算结果为:"<<p[n+1]; flag_case3=1; system("pause"); break; } case'4': { system("cls"); xianshi(); cout<<"请输入想相减的两位复数的下标"<<endl; while(cin>>c1>>c2) { c1_int=static_cast<int>(strtol(c1.c_str(), &end, 10)); c2_int=static_cast<int>(strtol(c2.c_str(), &end, 10)); if(c1_int>n||c2_int>n) { cout<<"输入的下标不在所给范围内部!"<<endl; cout<<"请输入想相减的两位复数的下标"<<endl; } else { break; } } p[n+2]=p[c1_int-1]-p[c2_int-1]; cout<<"计算结果为:"<<p[n+2]; flag_case4=1; system("pause"); break; } case'5': { system("cls"); xianshi(); cout<<"请输入想加和的一位实数"<<endl; cin>>shishu; cout<<"请输入想加和复数的下标"<<endl; while(cin>>c1) { c1_int=static_cast<int>(strtol(c1.c_str(), &end, 10)); if(c1_int>n) { cout<<"输入的下标不在所给范围内部!"<<endl; cout<<"请输入想加和复数的下标"<<endl; } else { break; } } p[n+3]=p[c1_int-1]+shishu; cout<<"复数和实数的计算结果为:"<<p[n+3]<<endl; cout<<"实数和复数的计算结果为:"<<p[n+3]<<endl; cout<<"满足交换律!"<<endl; flag_case5=1; system("pause"); break; } case'6': { system("cls"); xianshi(); cout<<"请输入想相减的一位实数"<<endl; cin>>shishu; cout<<"请输入想相减复数的下标"<<endl; while(cin>>c1) { c1_int=static_cast<int>(strtol(c1.c_str(), &end, 10)); if(c1_int>n) { cout<<"输入的下标不在所给范围内部!"<<endl; cout<<"请输入想相减复数的下标"<<endl; } else { break; } } p[n+4]=p[c1_int-1]-shishu; cout<<"当实数当作被减数时:"<<endl; cout<<"计算结果为:"<<p[n+4]; cout<<"当复数当作被减数时:"<<endl; p[n+5]=shishu-p[c1_int-1]; cout<<"计算结果为:"<<p[n+5]; if((p[n+4].Real()==p[n+5].Real())&&(p[n+4].Imag()==p[n+5].Imag())) { cout<<"满足交换律!"<<endl; } else { cout<<"不满足交换律"<<endl; } flag_case6=1; system("pause"); break; } case'7': { system("cls"); xianshi(); if(flag_case3==1) { cout<<"("<<n+1<<"):"; p[n+1].Dispaly(); } if(flag_case4==1) { cout<<"("<<n+2<<"):"; p[n+2].Dispaly(); } if(flag_case5==1) { cout<<"("<<n+3<<"):"; p[n+3].Dispaly(); } if(flag_case6==1) { cout<<"("<<n+4<<"):"; p[n+4].Dispaly(); cout<<"("<<n+5<<"):"; p[n+5].Dispaly(); } if(flag_case7==1) { cout<<"("<<n+6<<"):"; p[n+6].Dispaly(); } cout<<"请输入需要赋值的下标"<<endl; while(cin>>c1) { c1_int=static_cast<int>(strtol(c1.c_str(), &end, 10)); if(c1_int>n+6) { cout<<"输入的下标不在所给范围内部!"<<endl; cout<<"请输入需要赋值的下标"<<endl; } else { break; } } p[n+6]=p[c1_int-1]; cout<<"则第"<<n+6<<"个复数值为:"<<p[n+6]<<endl; flag=1; flag_case7=1; system("pause"); break; } case'8': { system("cls"); xianshi(); if(flag_case3==1) { cout<<"("<<n+1<<"):"; p[n+1].Dispaly(); } if(flag_case4==1) { cout<<"("<<n+2<<"):"; p[n+2].Dispaly(); } if(flag_case5==1) { cout<<"("<<n+3<<"):"; p[n+3].Dispaly(); } if(flag_case6==1) { cout<<"("<<n+4<<"):"; p[n+4].Dispaly(); cout<<"("<<n+5<<"):"; p[n+5].Dispaly(); } if(flag_case7==1) { cout<<"("<<n+6<<"):"; p[n+6].Dispaly(); } cout<<"请输入想获取复数的下标"<<endl; while(cin>>c1) { c1_int=static_cast<int>(strtol(c1.c_str(), &end, 10)); if(c1_int>n+6) { cout<<"输入的下标不在所给范围内部!"<<endl; cout<<"请输入需要赋值的下标"<<endl; } else { break; } } cout<<"第"<<c1_int<<"个复数值为:"<<p[c1_int-1]<<endl; system("pause"); break; } case'9': { cout << "即将退出界面" << endl; break; } default: { cin.clear(); cin.sync(); cout << "输入错误,请重新输入命令:" << endl; system("pause"); break; } } }while(in!='9'); return 0; }
3.几何形体
#include<iostream> #include<cmath> #include<string> using namespace std; double max(double x,double y) { return x>y?x:y; } const double PI=3.1415926535; class Geometric_shape { public: virtual double perimeter()=0;//周长 virtual double area()=0;//面积 virtual double volume()=0;//体积 virtual void Show(){}; }; class Circle:public Geometric_shape//圆 { double radius; public: Circle(){radius=0;}//构造函数 Circle(double vv){radius=vv;} double perimeter(){return 2.0*PI*radius;}//周长 double area(){return PI*radius*radius;}//面积 double volume(){return 0;} double ceshi(){return radius;} void Show(){cout<<"半径="<<radius<<endl;} }; class Rectangle:public Geometric_shape//长方形 { double width,length;//宽、长 public: Rectangle(){width=0;length=0;}//构造 长、宽 Rectangle(double wid,double len) { width=wid; length=len; } Rectangle(Rectangle& rr) { width=rr.width; length=rr.length; } double perimeter(){return 2.0*(width+length);}//周长 double area(){return width*length;}//面积 double volume(){return 0;}//体积 double ceshi1(){return width;} double ceshi2(){return length;} void Show(){cout<<"宽="<<width<<'\n'<<"长="<<length<<endl;} }; class Triangle:public Geometric_shape//三角形 { double a,b,c; public: Triangle(){a=0;b=0;c=0;} Triangle(double v1){a=v1;} Triangle(double v1,double v2,double v3){a=v1;b=v2;c=v3;} double perimeter(){return a+b+c;}//周长 double area()//面积 { double s=(a+b+c)/2.0; return sqrt(s*(s-a)*(s-b)*(s-c)); } double volume(){return 0;}//体积 void Show(){cout<<"边长a="<<a<<'\n'<<"边长b="<<b<<'\n'<<"边长c="<<c<<endl;} double ceshi1(){return a;} double ceshi2(){return b;} double ceshi3(){return c;} }; class Cube:public Rectangle//正方体 { double height; public: Cube(){} Cube(double wid,double len,double heg):Rectangle(wid,len){height=heg;} double area(){return Rectangle::area()*6;} double volume(){return sqrt(Rectangle::area())*Rectangle::area();} double ceshi(){return height;} void Show(){Rectangle::Show();cout<<"高="<<height<<endl;} }; class Box:public Rectangle//长方体 { double height; public: Box(){height=0;} Box(double wid,double len,double heg):Rectangle(wid,len){height=heg;} double area(){return Rectangle::area()*2+height*(Rectangle::perimeter());} double ceshi(){return height;} double volume(){return Rectangle::area()*height;}//体积 }; class Cylinder:public Circle//圆柱体 { double height; public: Cylinder(){height=0;} Cylinder(double vv,double heg):Circle(vv){height=heg;} double volume(){return Circle::area()*height;} double ceshi(){return height;} void Show(){Circle::Show();cout<<"高="<<height<<endl;} }; class Cone:public Circle//圆锥体 { double genera;//母线 public: Cone(){genera=0;} Cone(double vv,double gen):Circle(vv){genera=gen;} double area(){return Circle::area()+sqrt(Circle::area()/PI)*genera;} double volume(){return Circle::area()/3*sqrt(genera*genera-Circle::area()/PI);} void Show(){Circle::Show();cout<<"母线="<<genera<<endl;} double ceshi(){return genera;} }; class Three_pyramid:public Triangle//正三棱锥 { double dd;//侧棱长 public: Three_pyramid(){dd=0;} Three_pyramid(double a,double b,double c,double d):Triangle(a,b,c){dd=d;} double area(){return Triangle::area()+3*sqrt(Triangle::area()*4/sqrt(3))/2*sqrt(dd*dd-Triangle::area()/sqrt(3));} double volume(){return Triangle::area()/3*sqrt(dd*dd-Triangle::area()*4/sqrt(3)/3);} void Show(){Triangle::Show();cout<<"侧棱="<<dd<<endl;} double ceshi(){return dd;} }; void mainjiemian() { cout << " ★-----★---------★---------★-----★" << endl; cout << " 欢迎进入几何图形计算系统 " << endl; cout << " ☆ ☆" <<endl; cout << " 0 圆 " << endl; cout << " ☆ 1 圆锥 ☆" << endl; cout << " 2 圆柱体 " << endl; cout << " ☆ 3 矩形 ☆" << endl; cout << " 4 正方体 " << endl; cout << " ☆ 5 长方体 ☆" << endl; cout << " 6 三角形 " << endl; cout << " ☆ 7 正三棱锥 ☆" << endl; cout << " 8 比较面积 " << endl; cout << " ☆ 9 退出 ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << endl; cout << "请选择数字命令:"; } void jieshu()//结束显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 感谢您的使用! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; } int main() { system("color 0b"); int panduan; string in;//命令 char* end;//末端指针 Geometric_shape* gs; double c1r=0;//圆的半径 double co1r=0,co1ge=0;//圆锥半径与母线长 double cy1r=0,cy1h=0;//圆柱底面半径与高 double re1w=0,re2l=0;//矩形的宽与长 double cu1w=0,cu1l=0,cu1h=0;//正方体的棱长 double bo1w=0,bo1l=0,bo1h=0;//长方体的宽、长、高 double tr1a=0,tr1b=0,tr1c=0;//三角形的三边长度 double th1b=0,th1c=0,th1a=0,th1d=0;//正三棱锥底面边长与侧棱长 double bj[14]={0}; do { system("color DE"); panduan =10; system("cls"); mainjiemian(); cin >> in; if (!cin) { cin.clear();//清除cin的错误状态 cin.sync();//清空cin缓冲区里面未读取的信息... cout << "输入错误,重新返回主界面。" << endl; system("pause"); //cin >>in; continue; } if(in.length()<2) panduan = static_cast<int>(strtol(in.c_str(), &end, 10)); switch (panduan+48) { case'0': { system("cls"); cout<<"请输入圆的半径:"<<endl; cin>>c1r; Circle c1(c1r); try { double ceshi; ceshi=c1.ceshi(); if(ceshi<0) { cout<<"圆的半径:"<<ceshi<<endl; throw "圆的半径小于0,无法计算\n"; } else { gs=&c1; gs->Show(); cout<<"圆周长:"<<c1.perimeter()<<'\t'; cout<<"圆面积:"<<c1.area()<<'\t'; cout<<"圆体积:"<<c1.volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[0]=c1.area(); system("pause"); break; } case'1': { system("cls"); cout<<"请输入圆锥半径:"<<endl; cin>>co1r; cout<<"请输入圆锥母线长度"<<endl; cin>>co1ge; Cone co1(co1r,co1ge); try { double ceshi1,ceshi2; ceshi1=co1.Circle::ceshi(); ceshi2=co1.Cone::ceshi(); if(ceshi1<=0||ceshi2<=0||ceshi1>=ceshi2) { cout<<"底面半径:"<<ceshi1<<'\t'<<"圆锥母线:"<<ceshi2<<endl; throw "[1].底面半径与圆锥母线两项值存在小于0\n[2].母线长小于底面半径的,无法计算\n"; } else { gs=&co1; gs->Show(); cout<<"圆锥底周长:"<<co1.perimeter()<<'\t'; cout<<"圆锥表面积:"<<co1.area()<<'\t'; cout<<"圆锥体积:"<<co1.volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[1]=co1.Circle::area(); bj[2]=co1.area(); system("pause"); break; } case'2': { system("cls"); cout<<"请输入圆柱半径:"<<endl; cin>>cy1r; cout<<"请输入圆柱的高"<<endl; cin>>cy1h; Cylinder cy1(cy1r,cy1h); try { double ceshi1,ceshi2; ceshi1=cy1.Circle::ceshi(); ceshi2=cy1.Cylinder::ceshi(); if(ceshi1<=0||ceshi2<=0) { cout<<"底面半径:"<<ceshi1<<'\t'<<"圆柱的高"<<ceshi2<<endl; throw "底面半径与圆柱的高两项值存在小于0的,无法计算\n"; } else { gs=&cy1; gs->Show(); cout<<"圆柱底周长:"<<cy1.perimeter()<<'\t'; cout<<"圆柱表面积:"<<cy1.area()<<'\t'; cout<<"圆柱体积:"<<cy1.volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[3]=cy1.Circle::area(); bj[4]=cy1.area(); system("pause"); break; } case'3': { system("cls"); cout<<"请输入矩形的宽与长:"<<endl; cin>>re1w; cout<<"请输入巨熊的长:"<<endl; cin>>re2l; Rectangle re1(re1w,re2l); try { double ceshi1,ceshi2; ceshi1=re1.Rectangle::ceshi1(); ceshi2=re1.Rectangle::ceshi2(); if(ceshi1<=0||ceshi2<=0) { cout<<"矩形的宽:"<<ceshi1<<'\t'<<"矩形的长"<<ceshi2<<endl; throw "矩形的宽与长两项值存在小于0的,无法计算\n"; } else { gs=&re1; gs->Show(); cout<<"矩形周长:"<<re1.perimeter()<<'\t'; cout<<"矩形面积:"<<re1.area()<<'\t'; cout<<"矩形体积:"<<re1.volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[5]=re1.area(); system("pause"); break; } case'4': { system("cls"); cout<<"请输入正方体的棱长:"<<endl; cin>>cu1w; cu1l=cu1w; cu1h=cu1w; Cube cu1(cu1w,cu1l,cu1h); try { double ceshi1,ceshi2,ceshi3; ceshi1=cu1.Rectangle::ceshi1(); ceshi2=cu1.Rectangle::ceshi2(); ceshi3=cu1.Cube::ceshi(); if(ceshi1<=0||ceshi2<=0||ceshi3<=0) { cout<<"正方体的宽:"<<ceshi1<<'\t'<<"正方体的长:"<<ceshi2<<'\t'<<"正方体的高:"<<ceshi3<<endl; throw "正方体的棱长小于0,无法计算\n"; } else { gs=&cu1; gs->Show(); cout<<"正方体底周长:"<<cu1.perimeter()<<'\t'; cout<<"正方体表面积:"<<cu1.area()<<'\t'; cout<<"正方体体积:"<<cu1.volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[6]=cu1.Rectangle::area(); bj[7]=cu1.area(); system("pause"); break; } case'5': { system("cls"); cout<<"请输入长方体的宽:"<<endl; cin>>bo1w; cout<<"请输入长方体的长:"<<endl; cin>>bo1l; cout<<"请输入长方体的高:"<<endl; cin>>bo1h; Box bo1(bo1w,bo1l,bo1h); try { double ceshi1,ceshi2,ceshi3; ceshi1=bo1.Rectangle::ceshi1(); ceshi2=bo1.Rectangle::ceshi2(); ceshi3=bo1.Box::ceshi(); if(ceshi1<=0||ceshi2<=0||ceshi3<=0) { cout<<"长方体的宽:"<<ceshi1<<'\t'<<"长方体的长:"<<ceshi2<<'\t'<<"长方体的高:"<<ceshi3<<endl; throw "长方体的宽长高三项值存在小于0的,无法计算\n"; } else { gs=&bo1; gs->Show(); cout<<"长方体底周长:"<<bo1.perimeter()<<'\t'; cout<<"长方体表面积:"<<bo1.area()<<'\t'; cout<<"长方体体积:"<<bo1.volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[8]=bo1.Rectangle::area(); bj[9]=bo1.area(); system("pause"); break; } case'6': { system("cls"); cout<<"请输入三角形的边长a:"<<endl; cin>>tr1a; cout<<"请输入三角形的边长b:"<<endl; cin>>tr1b; cout<<"请输入三角形的边长c:"<<endl; cin>>tr1c; Triangle tr1(tr1a,tr1b,tr1c); try { double ceshi1,ceshi2,ceshi3; ceshi1=tr1.ceshi1(); ceshi2=tr1.ceshi2(); ceshi3=tr1.ceshi3(); if(ceshi1<=0||ceshi2<=0||ceshi3<=0||(ceshi1+ceshi2)<=ceshi3||(ceshi1+ceshi3)<=ceshi2||(ceshi3+ceshi2)<=ceshi1||sqrt(ceshi1-ceshi2)>=ceshi3||sqrt(ceshi1-ceshi3)>=ceshi2||sqrt(ceshi3-ceshi2)>=ceshi1) { cout<<"三角形边长a:"<<ceshi1<<'\t'<<"三角形边长b:"<<ceshi2<<'\t'<<"三角形边长c:"<<ceshi3<<endl; throw "[1].三角形的边长a,b,c三项值存在小于0的\n[2].三边长度无法构成三角形,无法计算\n"; } else { gs=&tr1; gs->Show(); cout<<"三角形周长:"<<gs->perimeter()<<'\t'; cout<<"三角形表面积:"<<gs->area()<<'\t'; cout<<"三角形体积:"<<gs->volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[10]=tr1.area(); system("pause"); break; } case'7': { system("cls"); cout<<"请输入正三角形底边边长"<<endl; cin>>th1a; cout<<"请输入正三棱锥侧棱长"<<endl; cin>>th1d; th1b=th1c=th1a; Three_pyramid th1(th1a,th1b,th1c,th1d); try { double ceshi1,ceshi2,ceshi3,ceshi4; ceshi1=th1.Triangle::ceshi1(); ceshi2=th1.Triangle::ceshi2(); ceshi3=th1.Triangle::ceshi3(); ceshi4=th1.Three_pyramid::ceshi(); if(ceshi1<=0||ceshi2<=0||ceshi3<=0||ceshi4<=0||ceshi1/sqrt(3)>=ceshi4) { cout<<"三棱锥底三角形边长a:"<<ceshi1<<'\t'<<"三棱锥底三角形边长b:"<<ceshi2<<'\n'<<"三棱锥底三角形边长c:"<<ceshi3<<'\t'<<"三棱锥侧棱d:"<<ceshi4<<endl; throw "[1].三棱锥边长a,b,c,d四项值存在小于0的;\n[2].底面正三角形的长度与侧棱长无法构成正三棱锥;\n无法计算\n"; } else { gs=&th1; gs->Show(); cout<<"正三棱锥底周长:"<<gs->perimeter()<<'\t'; cout<<"正三棱锥表面积:"<<gs->area()<<'\t'; cout<<"正三棱锥体积:"<<gs->volume()<<endl<<endl; } } catch(char* message) { cout<<message<<endl; } bj[11]=th1.Triangle::area(); bj[12]=th1.area(); system("pause"); break; } case'9': { system("cls"); jieshu(); break; } case'8': { system("cls"); cout<<"注意:没有输入过的数据一概初始为0!"<<endl<<endl; cout<<"[1]"<<"圆面积:"<<bj[0]<<endl; cout<<"[2]"<<"圆锥底面积:"<<bj[1]<<endl; cout<<"[3]"<<"圆锥表面积:"<<bj[2]<<endl; cout<<"[4]"<<"圆柱底面积:"<<bj[3]<<endl; cout<<"[5]"<<"圆柱表面积:"<<bj[4]<<endl; cout<<"[6]"<<"矩形面积:"<<bj[5]<<endl; cout<<"[7]"<<"正方体底面积:"<<bj[6]<<endl; cout<<"[8]"<<"正方体表面积:"<<bj[7]<<endl; cout<<"[9]"<<"长方体底面积:"<<bj[8]<<endl; cout<<"[10]"<<"长方体表面积:"<<bj[9]<<endl; cout<<"[11]"<<"三角形面积:"<<bj[10]<<endl; cout<<"[12]"<<"正三棱锥底面积:"<<bj[11]<<endl; cout<<"[13]"<<"正三棱锥表面积:"<<bj[12]<<endl; cout<<endl; cout<<"请输入相比较面积的下标:"<<endl; int x,y; while(cin>>x>>y) { if(x>13||y>13) { cout<<"下标输出不在正常范围之内!"<<endl; cout<<"请输入相比较面积的下标:"<<endl; } else { break; } } cout<<"比较结果中较大值为:"<<max(bj[x-1],bj[y-1])<<endl; system("pause"); break; } default: { cin.clear(); cin.sync(); cout << "输入错误,重新返回主界面。" << endl; system("pause"); break; } } }while (panduan != 9); return 0; }
4.教师与学生
#include<string> #include<iostream> #include<fstream> using namespace std; int q=0; enum Tsex{man,woman}; int S,T,n; struct course { char coursename[25]; int grade; }; struct Tcourse { char coursenumber[25]; char coursename[25]; int grade[8]; }; class CBirthday { public: CBirthday() { year=2000; strcpy(month,"01"); strcpy(day,"01"); } CBirthday(int y,char *m,char *d) { year=y; strcpy(month,m); strcpy(month,d); } void Input() { cout<<"年:"; cin>>year; cout<<"月:"; cin>>month; cout<<"日:"; cin>>day; } void Print() { cout<<year<<"-"<<month<<"-"<<day<<endl; } private: int year; char month[5],day[5]; }; class CPerson:public CBirthday { public: CPerson(char*,char*,Tsex,CBirthday&); CPerson(); ~CPerson(); void input(); void display(); private: char ID[10]; char Name[20]; Tsex Sex; CBirthday birthday; }; CPerson::CPerson (char *id,char *name,Tsex sex,CBirthday &d):birthday(d) { strcpy(ID,id); strcpy(Name,name); Sex=sex; } CPerson::CPerson ():birthday() { strcpy(ID,"00000000"); strcpy(Name," "); (int&)Sex=0; } CPerson::~CPerson () { //cout<<"调用CPerson的析构函数"<<endl; } void CPerson::input() { cout<<"身份:"; cin>>ID; cout<<"姓名:"; cin>>Name; cout<<"性别:"; cin>>(int&)Sex; Input(); } void CPerson::display() { cout<<"身份:"<<ID<<endl<<"姓名:"<<Name<<'\n'<<"性别:"; if(Sex==man) cout<<"男"<<'\n'; else cout<<"女"<<'\n'; cout<<"出生年月日:"; Print(); } class CStudent:public CPerson { public: void input() { cout<<"输入学生信息:"<<endl; CPerson::input (); cout<<"学号:";cin>>number; cout<<"班级名:";cin>>depart; inputCourse(); } CStudent(); CStudent(char *id,char *name,Tsex sex,CBirthday &d,char *num,char *dep); ~CStudent(); void inputCourse(); void displayS(); course cj[8]; private: char number[10]; char depart[30]; }; CStudent::CStudent() { CPerson(); strcpy(number,"00000000"); strcpy(depart," "); for(int i=0;i<8;i++) { strcpy(cj[i].coursename ," "); cj[i].grade =-1; } } CStudent::CStudent (char *id,char *name,Tsex sex,CBirthday &d,char *num,char *dep):CPerson(id,name,sex,d) { strcpy(number,num); strcpy(depart,dep); for(int i=0;i<8;i++) { strcpy(cj[i].coursename ," "); cj[i].grade =-1; } } CStudent::~CStudent () { //cout<<"调用学生类的析构函数"<<endl; } void CStudent::inputCourse () { cout<<"请输入各科成绩:(end结束)"<<'\n'; char coursename[20]; int score; while(1) { cin>>coursename; if(!strcmp(coursename,"end"))break; cin>>score; for(int i=0;i<8;i++) { if(strcmp(cj[i].coursename ," ")==0) { strcpy(cj[i].coursename ,coursename); cj[i].grade =score; break; } } } } void CStudent::displayS () { display(); cout<<"学号:"<<number<<endl; cout<<"班级名:"<<depart<<endl; cout<<"该生成绩:"<<endl; for(int i=0;i<8;i++) if(strcmp(cj[i].coursename," ")!=0) cout<<cj[i].coursename<<'\t'<<cj[i].grade<<'\n'; else break; cout<<"------------------"<<endl; } class CTeacher:public CPerson { public: void input() { cout<<"输入教师信息:"<<endl; CPerson::input (); cout<<"工号:"; cin>>number; cout<<"职称:"; cin>>prof; cout<<"部门:"; cin>>depart; SetScore(); } CTeacher(); CTeacher(char *id,char *name,Tsex sex,CBirthday &d,string num,string dep,string pro); ~CTeacher(); void SetScore(); void DispScore(); void SortScore(); void SetedStuScore(CStudent &); void displayT(); private: string number; string depart; string prof; Tcourse Tcj[2]; }; CTeacher::CTeacher () { CPerson(); number="00000000"; depart=" "; prof=" "; for(int i=0;i<2;i++) { strcpy(Tcj[i].coursename ," "); strcpy(Tcj[i].coursenumber ,"000000"); for(int j=0;j<8;j++) Tcj[i].grade [j]=-1; } } CTeacher::CTeacher(char *id,char *name,Tsex sex,CBirthday &d,string num,string dep,string pro):CPerson(id,name,sex,d) { number=num; depart=dep; prof=pro; for(int i=0;i<2;i++) { strcpy(Tcj[i].coursename ," "); strcpy(Tcj[i].coursenumber ,"000000"); for(int j=0;j<8;j++) Tcj[i].grade [j]=-1; } } CTeacher::~CTeacher () { //cout<<"调用教师类的析构函数"<<endl; } void CTeacher::SetScore () { cout<<"请输入所教课程数目:"<<endl; cin>>q; for(int i=0;i<q;i++) { cout<<"课程名"<<endl; cin>>Tcj[i].coursename; cout<<"各个学生的成绩"<<endl; for(int j=0;j<8;j++) cin>>Tcj[i].grade [j]; } } void CTeacher::DispScore () { for(int i=0;i<q;i++) { cout<<"课程名:"<<Tcj[i].coursename <<endl; for(int j=0;j<8;j++) cout<<"("<<j+1<<")"<<" "<<Tcj[i].grade [j]<<endl; } } void CTeacher::SortScore() { int a[8]; int b[8]; int c[8]; int d[8]; int e[8]; int f[8]; int g[8]; int h[8]; for(int i=0;i<8;i++) { a[i]=Tcj[0].grade [i]; b[i]=Tcj[1].grade [i]; c[i]=Tcj[2].grade [i]; d[i]=Tcj[3].grade [i]; e[i]=Tcj[4].grade [i]; f[i]=Tcj[5].grade [i]; g[i]=Tcj[6].grade [i]; h[i]=Tcj[7].grade [i]; } int t; for( i=0;i<8;i++) { for(int j=0;j<=8-i;j++) { if(a[j]<a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } if(b[j]<b[j+1]) { t=b[j]; b[j]=b[j+1]; b[j+1]=t; } if(c[j]<c[j+1]) { t=c[j]; c[j]=c[j+1]; c[j+1]=t; } if(d[j]<d[j+1]) { t=d[j]; d[j]=d[j+1]; d[j+1]=t; } if(e[j]<e[j+1]) { t=e[j]; e[j]=e[j+1]; e[j+1]=t; } if(f[j]<f[j+1]) { t=f[j]; f[j]=f[j+1]; f[j+1]=t; } if(g[j]<g[j+1]) { t=g[j]; g[j]=g[j+1]; g[j+1]=t; } if(h[j]<h[j+1]) { t=h[j]; h[j]=h[j+1]; h[j+1]=t; } } } cout<<Tcj[0].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<a[i]<<endl; cout<<Tcj[1].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<b[i]<<endl; cout<<Tcj[2].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<c[i]<<endl; cout<<Tcj[3].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<d[i]<<endl; cout<<Tcj[4].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<e[i]<<endl; cout<<Tcj[5].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<f[i]<<endl; cout<<Tcj[6].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<g[i]<<endl; cout<<Tcj[7].coursename <<" 成绩排序"<<endl; for( i=0;i<8;i++) cout<<h[i]<<endl; } void CTeacher::SetedStuScore (CStudent &s) { int score; for(int i=0;i<2;i++) if(strcmp(s.cj[i].coursename," ")!=0) { cout<<s.cj[i].coursename<<'\t'<<s.cj[i].grade<<endl; cout<<"请输入修改的成绩"<<endl; cin>>score; cout<<s.cj[i].coursename<<'\t'<<score<<endl; cout<<"---------------------"<<endl; } else break; } void CTeacher::displayT () { display(); cout<<"工号:"<<number<<endl; cout<<"部门:"<<depart<<endl; cout<<"职称:"<<prof<<endl; cout<<"----------------"<<endl; } CStudent *s;//将要创建的对象做成指针类型 CTeacher *t; void help()//进入显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 制作小软件,我们是认真的! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " Made By 刘建伟" << endl; cout << endl; cout << "请输入学生数:"<<endl; } void mainjiemian() { cout << " 欢迎进入学生教师信息系统 " << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << " " << endl; cout << " ☆ 1 输入学生信息 ☆" << endl; cout << " " << endl; cout << " ☆ 2 输入教师信息 ☆" << endl; cout << " " << endl; cout << " ☆ 3 显示人员信息 ☆" << endl; cout << " " << endl; cout << " ☆ 4 修改学生成绩 ☆" << endl; cout << " " << endl; cout << " ☆ 5 信息读入文件 ☆" << endl; cout << " " << endl; cout << " ☆ 6 退出当前系统 ☆" << endl; cout << " " << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << endl; cout << "请选择数字命令:"; } void jieshu()//结束显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 感谢您的使用! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; } int main() { system("color 0b"); help(); string sn; string in; int k,h; int i; int panduan; char* end;//末端指针 while(cin>>sn) { int flag_sn=0; int l_sn=sn.length(); k=static_cast<int>(strtol(sn.c_str(), &end, 10)); if(l_sn>0&&l_sn<5) { for(i=0;i<l_sn;i++) { if(sn.at(i)>='0'&&sn.at(i)<='9') { flag_sn=0; } else { flag_sn=1; } } } else { flag_sn=1; } if(flag_sn==1) { cout<<"输入有误!"<<endl; system("pause"); system("cls"); help(); } else { break; } } S=k; cout << "请输入教师数:"<<endl; while(cin>>sn) { int flag_sn=0; int l_sn=sn.length(); h=static_cast<int>(strtol(sn.c_str(), &end, 10)); if(l_sn>0&&l_sn<5) { for(i=0;i<l_sn;i++) { if(sn.at(i)>='0'&&sn.at(i)<='9') { flag_sn=0; } else { flag_sn=1; } } } else { flag_sn=1; } if(flag_sn==1) { cout<<"输入有误!"<<endl; system("pause"); cout << "请输入教师数:"<<endl; } else { break; } } T=h; s = new CStudent[k]; t = new CTeacher[h]; do { system("cls"); mainjiemian(); cin >> in; if (!cin) { cin.clear();//清除cin的错误状态 cin.sync();//清空cin缓冲区里面未读取的信息... cout << "输入错误,重新返回主界面。" << endl; system("pause"); //cin >>in; continue; } if(in.length()<2) panduan = static_cast<int>(strtol(in.c_str(), &end, 10)); else panduan=0; switch (panduan+48) { case'1'://输入学生信息 { system("cls"); for(i=0;i<k;i++) { cout<<"请输入第"<<i+1<<"位同学的信息:"<<endl; s[i].input(); } system("pause"); break; } case'2'://输入教师信息 { system("cls"); for(i=0;i<h;i++) { cout<<"请输入第"<<i+1<<"位教师的信息:"<<endl; t[i].input(); } system("pause"); break; } case'3'://显示学生与教师信息 { system("cls"); for(i=0;i<k;i++) { cout<<"["<<i+1<<"]"; s[i].displayS(); cout<<endl; } for(i=0;i<h;i++) { cout<<"["<<i+1<<"]"; t[i].displayT(); cout<<endl; t[i].DispScore(); cout<<endl; } system("pause"); break; } case'4'://修改学生成绩 { system("cls"); for(i=0;i<k;i++) { cout<<"["<<i+1<<"]"; s[i].displayS(); cout<<endl; } int m; cout<<"输入要修改的学生信息的学生下标:"<<endl; cin>>m; for(i=0;i<h;i++) { t[i].SetedStuScore (s[m-1]); } system("pause"); break; } case'5'://写入文件 { system("cls"); fstream iofile; iofile.open("C:\\Users\\liujianwei\\Desktop\\新建文本文档.txt",ios::in|ios::out|ios::binary ); if(!iofile) { cerr<<"file open error!"<<endl; return -1; } for(i=0;i<k;i++) { iofile.write((char*)&s[i],sizeof(s[i])); } for(i=0;i<h;i++) { iofile.write((char*)&t[i],sizeof(t[i])); } cout<<"保存成功!"<<endl; iofile.close(); system("pause"); break; } case'6'://退出结束 { system("cls"); jieshu(); break; } default: { cin.clear(); cin.sync(); cout << "输入错误,重新返回主界面。" << endl; system("pause"); break; } } }while(panduan!=6); return 0; }
5.职工信息
#include<iostream> #include<string> #include<iostream> #include<fstream> #include<ostream> #include<iomanip>//setw(),控制空格 #include<algorithm>//为了计算文件里面含有的对象个数 #include<windows.h> #include<cstdlib>//防止^Z导致程序异常 #include<mmsystem.h>//控制音乐播放的头文件 #pragma comment(lib, "Winmm.lib")//控制音乐播放的头文件 using namespace std; class Cworker { private: string wo; string na; string se; string ag; string xu; string sa; string ad; string ph; public: void setdate(string worknumber,string name,string sex,string age,string xueli,string salary,string adress,string phonenumber) { wo=worknumber; na=name; se=sex; ag=age; xu=xueli; sa=salary; ad=adress; ph=phonenumber; } void display() { cout<<"职工号:"<<std::left<<setw(15)<<wo; cout<<"姓名:"<<std::left<<setw(8)<<na; cout<<"性别:"<<std::left<<setw(5)<<se; cout<<"年龄:"<<std::left<<setw(5)<<ag; cout<<"学历:"<<std::left<<setw(20)<<xu; cout<<"工资:"<<std::left<<setw(17)<<sa; cout<<"地址:"<<std::left<<setw(18)<<ad; cout<<"电话号:"<<std::left<<setw(5)<<ph; } string work_worknumber() { return wo; } string find_worknumber() { return wo; } string find_NAME() { return na; } string worker_workernumber() { return wo; } string worker_name() { return na; } string worker_sex() { return se; } string worker_age() { return ag; } string worker_xueli() { return xu; } string worker_salary() { return sa; } string worker_adress() { return ad; } string worker_phonenumber() { return ph; } void revise() { cout << "请根据以下提示进行修改。" << endl; cout << "姓名:"; cin >> na; cout << "性别(男或女):"; cin >> se; cout << "年龄:"; cin >> ag; cout << "学历:"; cin >> xu; cout << "工资:"; cin >> sa; cout << "地址:"; cin >> ad; cout << "请输入电话号:"; cin >> ph; } int count() { int score; char* end; string salary; score = static_cast<int>(strtol(sa.c_str(), &end, 10));//使用strtol(string to long) 利用string的c_str()转化为C风格字符串 return score; } Cworker operator=(const Cworker&c); }; Cworker Cworker::operator=(const Cworker& c) { wo=c.wo; na=c.na; se=c.se; ag=c.ag; xu=c.xu; sa=c.sa; ad=c.ad; ph=c.ph; return *this; } Cworker *p;//将要创建的对象做成指针类型 void mainjiemian() { cout << " ★-----★---------★---------★-----★" << endl; cout << " 欢迎进入职工信息管理系统 " << endl; cout << " ☆ ☆" <<endl; cout << " 1 录入职工信息 " << endl; cout << " ☆ 2 输出职工信息 ☆" << endl; cout << " 3 查询职工信息 " << endl; cout << " ☆ 4 修改职工信息 ☆" << endl; cout << " 5 整理职工信息 " << endl; cout << " ☆ 6 文件录入信息 ☆" << endl; cout << " 7 文件读取信息 " << endl; cout << " ☆ 8 退出管理系统 ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << endl; cout << "请选择数字命令:"; } void help()//进入显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 制作小软件,我们是认真的! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " Made By 刘建伟" << endl; cout << endl; cout << "温馨提示:^C为强制退出程序"<<endl; cout << "请输入职工人数:"; } void jieshu()//结束显示 { cout << " ★-----★---------★---------★-----★" << endl; cout << endl; cout << " ☆ 感谢您的使用! ☆" << endl; cout << endl; cout << " ★-----★---------★---------★-----★" << endl; cout << endl; } void kaobei()//拷贝文件到test1.txt { char ch; ifstream file("H:\\worker_in.txt");//读取H盘文件 ofstream file1("H:/test1.txt");//创建文本文件 while(file.get(ch))//读取文本内容 { //cout<<ch;//输出文本内容到控制台 file1<<ch;//写入内容到文件 } file.close();//关闭文件流 file1.close();//关闭文件流 cout<<endl; } void play() { // 注意PlaySound只能播放没有经过压缩的wav格式的声音文件 PlaySound("C:\\Users\\liujianwei\\Desktop\\yinyue.wav",NULL,SND_FILENAME|SND_ASYNC);//控制音乐输出,带有三个参数,loop为阻塞 } int line() { ifstream file1("H:\\test1.txt");//没有就创建 string str; int line = 0; while (file1) { getline(file1, str); remove(str.begin(), str.end(), ' ');//删除从文件开始到结束中的空格 remove(str.begin(), str.end(), '\t');//删除从文件开始到结束中的空格 if (str.length() > 0)//如果有一行除去空格之后不为零就算作是一个对象 line++; } return line; } int main() { play();//播放音乐 system("color 0b"); help(); int line_file, N;//文件里的学生数和总的学生数 int i; string in;//命令 string sn; int n;//键盘输入的职工数 int flag_case1 = 0;//是否执行了第一步 int flag_case7 = 0;//是否执行了第七步 int panduan; char* end;//末端指针 while(cin>>sn) { if (!cin) { cin.clear();//清除cin的错误状态 cin.sync();//清空cin缓冲区里面未读取的信息... cout << "输入错误,重新返回主界面。" << endl; //system("pause"); //cout<<flush; system("cls"); help(); continue; } int flag_sn=0; int l_sn=sn.length(); n=static_cast<int>(strtol(sn.c_str(), &end, 10)); if(l_sn>0&&l_sn<5) { for(i=0;i<l_sn;i++) { if(sn.at(i)>='0'&&sn.at(i)<='9') { flag_sn=0; } else { flag_sn=1; } } } else { flag_sn=1; } if(flag_sn==1) { cout<<"输入有误!"<<endl; system("pause"); system("cls"); help(); } else { break; } /*if(l_sn>1) { if(n>=0&&n<=100&&l_sn<=3&&sn.at(l_sn-1)>='0'&&sn.at(l_sn-1)<='9'&&sn.at(l_sn-2)>='0'&&sn.at(l_sn-2)<='9') { break; } else { cout<<"输入有误!"<<endl; system("pause"); system("cls"); help(); } } else { if(n>=0&&n<=100&&l_sn<=3&&sn.at(l_sn-1)>='0'&&sn.at(l_sn-1)<='9') { break; } else { cout<<"输入有误!"<<endl; system("pause"); system("cls"); help(); } }*/ } cout<<flush; kaobei();//拷贝文件 line_file = line(); N = n + line_file; p = new Cworker[N]; do { system("cls"); mainjiemian(); cin >> in; if (!cin) { cin.clear();//清除cin的错误状态 cin.sync();//清空cin缓冲区里面未读取的信息... cout << "输入错误,重新返回主界面。" << endl; system("pause"); continue; } if(in.length()<2) panduan = static_cast<int>(strtol(in.c_str(), &end, 10)); switch (panduan+48) { case'1'://**************************录入职工信息 { system("cls"); string worknumber; string name; string sex; string age; string xueli; string salary; string adress; string phonenumber; //int now_n; cout<<"请输入现在想输入的学生数:"<<endl; for (i = 0; i < n; i++) { cout<<"输入第"<<i+1<<"位职工的信息。"<<endl; cout << "请输入职工号:"; cin >> worknumber; cout << "请输入姓名:"; cin >> name; cout << "请输入性别(男或女):"; cin >> sex; cout << "请输入年龄:"; cin >> age; cout << "请输入学历:"; cin >> xueli; cout << "请输入工资:"; cin >> salary; cout << "请输入地址:"; cin >> adress; cout << "请输入电话号:"; cin>>phonenumber; p[i].setdate(worknumber,name,sex,age,xueli,salary,adress,phonenumber);//将输入的数据传给私有变量 } flag_case1 = 1;//执行过第一步就标志为1 system("pause"); break; } case'2'://***************************输出职工信息 { system("cls"); if (flag_case7 == 1&&flag_case1==0) { for (i = n; i < N; i++) { cout << "worker" << "[" << i + 1 << "]" << ":"; p[i].display(); cout << endl; } } else if(flag_case7 == 0&&flag_case1==1) { for (i = 0; i < n; i++) { cout << "worker" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } else if(flag_case7 == 1&&flag_case1==1) { for (i = 0; i < N; i++) { cout << "student" << "[" << i + 1 << "]" << ":\t"; p[i].display(); cout << endl; } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'3'://***********************查询职工信息 { system("cls"); if (flag_case1 == 1||flag_case7==1) { int flag_find; cout<<"请选择查询方式:1.按职工号号查询 2.按姓名查询"<<endl; cin>>flag_find; if(flag_find==1) { cout << "请输入要查询的职工号:" << endl; string worknumber; int flag; cin >> worknumber; for (i = 0; i < N; i++) { if (p[i].find_worknumber() == worknumber) { p[i].display(); flag = 1; cout << endl; } } if (flag != 1) { cout << "输入信息有误,查询无果。" << endl; } } if(flag_find==2) { cout << "请输入要查询的职工姓名:" << endl; string NAME; int flag; cin>>NAME; for(i=0;i<N;i++) { if(p[i].find_NAME()==NAME) { p[i].display(); flag = 1; cout << endl; } } if (flag != 1) { cout << "输入信息有误,查询无果。" << endl; } } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'4'://****************************修改职工信息 { system("cls"); if (flag_case1 == 1||flag_case7==1) { int flag_4=0; cout << "请输入要修改的职工号:" << endl; string worknumber; cin >> worknumber; for (i = 0; i < N; i++) { if (p[i].find_worknumber() == worknumber) { p[i].revise(); cout << "修改成功!" << endl; p[i].display(); flag_4=1; } } if(flag_4==0) { cout<<"查询无果!无法进行修改。"<<endl; } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } cout<<endl; system("pause"); break; } case'5'://****************************整理输出学生信息 { system("cls"); if (flag_case1 == 1||flag_case7==1)//通过判断有没有执行第一步和第七步来判断统计输出时总数的大小 { int zongshu; if (flag_case7 == 1&&flag_case1==0) { zongshu=N; } if(flag_case7 == 0&&flag_case1==1) { zongshu=n; } if(flag_case7 == 1&&flag_case1==1) { zongshu=N; } cout<<"整理职工信息如下:"<<endl; if(flag_case1==1) i=0; else i=n; Cworker temp; for (i; i < zongshu; i++) { for(int j=i+1;j<zongshu;j++) { if(p[i].count()<p[j].count()) { temp=p[i]; p[i]=p[j]; p[j]=temp; } } } if(flag_case1==1) i=0; else i=n; for(i;i<zongshu;i++) { cout << "student" << "[" << i + 1 << "]" << ":"; p[i].display(); cout << endl; } if(flag_case1==1) i=0; else i=n; ofstream ofile("H:\\worker_in.txt", ios::in); for (i ; i < zongshu; i++) {//把三个私有变量信息插入到文件 ofile << p[i].worker_workernumber() << "\t"; ofile << p[i].worker_name() << "\t"; ofile << p[i].worker_sex() << "\t"; ofile << p[i].worker_age() << "\t"; ofile << p[i].worker_xueli() << "\t"; ofile << p[i].worker_salary() << "\t"; ofile << p[i].worker_adress() << "\t"; ofile << p[i].worker_phonenumber() << endl; } } else { cout << "请先执行第一步或第七步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'6'://******************************将学生信息保存到磁盘文件当中,利用追加的方式 { system("cls"); if (flag_case1 == 1||flag_case7==1) { for (int i = 0; i < n; i++) { cout << "work" << "[" << i + 1 << "]" << ":"; p[i].display(); cout << endl; } ofstream ofile("H:\\worker_in.txt", ios::app); for (i = 0; i < n; i++) {//把三个私有变量信息插入到文件 ofile << p[i].worker_workernumber() << "\t"; ofile << p[i].worker_name() << "\t"; ofile << p[i].worker_sex() << "\t"; ofile << p[i].worker_age() << "\t"; ofile << p[i].worker_xueli() << "\t"; ofile << p[i].worker_salary() << "\t"; ofile << p[i].worker_adress() << "\t"; ofile << p[i].worker_phonenumber() << endl; } cout << endl; cout << "所有学生信息都已保存完毕。" << endl; } else { cout << "请先执行第一步之后再执行此步骤!" << endl; cout << "即将返回主页面进行重新选择..." << endl; } system("pause"); break; } case'7'://*************************从磁盘文件中读入学生信息,并创建学生类对象 { system("cls"); ifstream ofile; string worknumber; string name; string sex; string age; string xueli; string salary; string adress; string phonenumber; int i = n; ofile.open("H:\\worker_in.txt", ios::in); if (ofile.is_open()) { for ( i = n; i < N; i++) {//依次提取学生三个基本私有变量信息 ofile >> worknumber; ofile >> name; ofile >> sex; ofile >> age; ofile >> xueli; ofile >> salary; ofile >> adress; ofile >> phonenumber; p[i].setdate(worknumber, name, sex,age,xueli,salary,adress,phonenumber); if (i >= N) { cout << "文件数据已经全部导入。" << endl; cout << "当前职工数量:" << i << endl; cout << "当前存储容量已达到最大值!" << endl; break; } } if(flag_case1==1) { for (i = 0; i < N; i++) { cout << "worker" << "[" << i + 1 << "]" << ":"; p[i].display(); cout << endl; } } else { for (i = n; i < N; i++) { cout << "worker" << "[" << i + 1 << "]" << ":"; p[i].display(); cout << endl; } } //cout << "已成功导入的职工人员数量:" << i << endl; flag_case7 = 1; } else { cout << "打开文件时出错!"; } ofile.close(); system("pause"); break; } case'8'://*************************系统退出功能 { system("cls"); jieshu(); break; } default: { cin.clear(); cin.sync(); cout << "输入错误,重新返回主界面。" << endl; system("pause"); break; } } } while (panduan != 8); delete[]p; return 0; }