2条回答 默认 最新
- 小P聊技术 2021-06-28 16:49关注
#include <iostream> #include <fstream> #include <list> #include <cmath> #include <string.h> #define MAX_STU 100//读入学生的最大数目 /* *郑海波 blog.csdn.net/nuptboyzhb/ *email:zhb931706659@126.com */ using namespace std; class Student { public: char * name; char *ID; int grade; Student() { name = new char[strlen("Anonymous-----") + 1]; ID = new char[strlen("NoIdInput------") + 1]; grade = 0; } Student(char * pName,char * pID, int pgrade) :grade(pgrade) { name = new char[strlen(pName) + 1]; strcpy(name, pName); ID = new char[strlen(pID) + 1]; strcpy(ID, pID); } Student(const Student& rhs) :grade(rhs.grade) { name = new char[strlen(rhs.name) + 1]; strcpy(name, rhs.name); ID = new char[strlen(rhs.ID) + 1]; strcpy(ID, rhs.ID); } Student& operator=(const Student& rhs) { name = new char[strlen(rhs.name) + 1]; strcpy(name, rhs.name); ID = new char[strlen(rhs.ID) + 1]; strcpy(ID, rhs.ID); grade = rhs.grade; return *this; } // overload the == operator // for sorting purposes, we consider that two Student objects are "equal" // if they have the same grade bool operator==(const Student& rhs) const { return (grade == rhs.grade) ? true : false; } // overload the < operator // for sorting purposes, we consider that a Student object is "less than" another // if it's grade is less than the other object's grade bool operator<(const Student& rhs) const { return (grade < rhs.grade) ? true : false; } // overload the > operator // for sorting purposes, we consider that a Student object is "greater than" another // if it's grade is greater than the other object's grade bool operator>(const Student& rhs) const { return (grade > rhs.grade) ? true : false; } // 显示学生的信息 void print() { cout << name <<" " <<ID << " " << grade << endl; } //构造函数 ~Student() { delete []name; delete []ID; } }; list<Student> lst;//学生链表,用于存放学生数据 void print(list<Student> lst, char * name)//输入链表中所有的学生 { list<Student>::iterator it; cout << name << ":" << endl; for(it = lst.begin(); it != lst.end(); ++it) it->print(); cout << endl; } void screenA()//显示屏幕操作A { cout<<"****************************************"<<endl; cout<<" 1--------------查询"<<endl; cout<<" 2--------------排序"<<endl; cout<<" 3--------------插入"<<endl; cout<<" 4--------------删除"<<endl; cout<<" 5--------------显示"<<endl; cout<<" 6--------------保存"<<endl; cout<<" 7--------------清屏"<<endl; cout<<"****************************************"<<endl; } void screenB()//显示屏幕查询 { system("cls"); cout<<"****************************************"<<endl; cout<<" 1----------按学号查询"<<endl; cout<<" 2----------按姓名查询"<<endl; cout<<" 3----------按成绩查询"<<endl; cout<<" 4----------返回"<<endl; cout<<"****************************************"<<endl; } void searchByID()//按学号查找 { cout<<"-------请输入学号ID"<<endl; char tID[12]; memset(tID,0,12); cin>>tID; bool flag=false; list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { if (strcmp(it->ID,tID)==0) { cout<<"----查找到,该学生信息如下:-----"<<endl; it->print(); flag=true; break; } } if (flag==false) { cout<<"未找到!"<<endl; } } void searchByName()//按名字查找 { cout<<"-------请输入姓名:"<<endl; char tname[12]; memset(tname,0,12); cin>>tname; bool flag=false; list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { if (strcmp(it->name,tname)==0) { cout<<"----查找到,该学生信息如下:-----"<<endl; it->print(); flag=true; break; } } if (flag==false) { cout<<"未找到!"<<endl; } } void searchByGrade()//按分数查找 { cout<<"-------请输入分数:"<<endl; int tgrade; cin>>tgrade; bool flag=false; list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { if (it->grade==tgrade) { cout<<"----查找到,该学生信息如下:-----"<<endl; it->print(); flag=true; break; } } if (flag==false) { cout<<"未找到!"<<endl; } } void sortByGrade()//按分数进行排序,由高到低 { system("cls"); cout<<"-------按分数排序完毕,结果如下:"<<endl; lst.sort( greater<Student>() ); list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { it->print(); } } void insertStudent()//插入一个学生 { system("cls"); cout<<"-------请输入学号ID"<<endl; char tID[12]; memset(tID,0,12); cin>>tID; cout<<"-------请输入姓名:"<<endl; char tname[12]; memset(tname,0,12); cin>>tname; cout<<"-------请输入分数:"<<endl; int tgrade; cin>>tgrade; Student stu(tname,tID,tgrade); lst.push_back(stu); list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { it->print(); } } void deleteStudent()//按要求删除一个学生 { system("cls"); cout<<"-------请输入要删除学生的学号ID:"<<endl; char tID[12]; memset(tID,0,12); cin>>tID; bool flag=false; list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { if (strcmp(it->ID,tID)==0) { cout<<"----查找到,该学生信息如下:-----"<<endl; it->print(); lst.erase(it); cout<<"删除完毕!"<<endl; flag=true; break; } } if (flag==false) { cout<<"未找到!"<<endl; } } void inputData()//从文件中读取数据 { cout<<"正在从文件读入数据..."<<endl; ifstream ifile("student.dat"); if(!ifile) { cout<<"student.dat cannot be opened!"<<endl; return; } char ch; int i; for (i=0;i<MAX_STU;i++)//读取数目 { string s_name,s_id,s_grade; if(!ifile.get(ch)) { cout<<"文件已经读完!"<<endl; return; } while (ch!='#')//读取姓名 { if (ch==' ')//跳过空格 { ifile.get(ch); continue; } s_name+=ch; ifile.get(ch); } ifile.get(ch); while (ch!='#')//读取学号 { if (ch==' ') { ifile.get(ch);//跳过空格 continue; } s_id+=ch; ifile.get(ch); } ifile.get(ch); while(ch!='\n')//读取分数 { if (ch==' ') { ifile.get(ch); continue; } s_grade+=ch; if(!ifile.get(ch)) { cout<<"文件已经读完!"<<endl; return; } } Student temp; strcpy(temp.name,s_name.c_str()); strcpy(temp.ID,s_id.c_str()); temp.grade=atoi(s_grade.c_str()); lst.push_back(temp); } ifile.close(); system("cls"); } void SaveAsFile() { system("cls"); ofstream ofile("student.dat",ios::out); if (!ofile) { cout<<"打开文件失败!"<<endl; return; } list<Student>::iterator it; for(it = lst.begin(); it != lst.end(); ++it) { ofile<<it->name<<"#"<<it->ID<<"#"<<it->grade<<"\n"; } cout <<"保存完毕..."<< endl; ofile.close(); return ; } int main() { inputData();//从文件中读入数据 char ch; screenA(); while (cin>>ch) { switch(ch) { case '1': screenB(); while (cin>>ch) { int flag=0; switch(ch) { case '1': searchByID(); break; case '2': searchByName(); break; case '3': searchByGrade(); break; case '4': flag=1; break; default: flag=1; break; } if (flag==1) { break; } } break; case '2'://排序 sortByGrade(); break; case '3'://插入学生 insertStudent(); break; case '4'://删除学生 deleteStudent(); break; case '5'://显示当前信息 print(lst,"---------当前数据列表如下"); break; case '6'://将数据保存到文件 SaveAsFile(); break; case '7'://清屏 system("cls"); break; default: return 0; } screenA(); } cout<<"系统退出"<<endl; return 0; }
student.dat内容如下:
张山 # B11010101 # 98
李四 # B11010101 # 67
王五 # B11010101 # 88
李华 # B11010101 # 76
李阳 # B11010101 # 55
张伟 # B11010101 # 87
王大为 # B11010101 # 89
李小名 # B11010101 # 92
张山一 # B11010101 # 98
李四一 # B11010101 # 67
王五一 # B11010101 # 88
李华一 # B11010101 # 76
李阳一 # B11010101 # 55
张伟一 # B11010101 # 87
王大二 # B11010101 # 89
李小登 # B11010101 # 92本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 vs2022无法联网
- ¥15 TCP的客户端和服务器的互联
- ¥15 VB.NET操作免驱摄像头
- ¥15 笔记本上移动热点开关状态查询
- ¥85 类鸟群Boids——仿真鸟群避障的相关问题
- ¥15 CFEDEM自带算例错误,如何解决?
- ¥15 有没有会使用flac3d软件的家人
- ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
- ¥15 docker实践项目
- ¥15 利用pthon计算薄膜结构的光导纳