收藏 分销(赏)

c++项目设计.doc

上传人:快乐****生活 文档编号:10522816 上传时间:2025-06-01 格式:DOC 页数:26 大小:91.01KB
下载 相关 举报
c++项目设计.doc_第1页
第1页 / 共26页
c++项目设计.doc_第2页
第2页 / 共26页
点击查看更多>>
资源描述
//学生管理系统代码 #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; enum {SUBJECT=5};//一共五门 typedef struct { char subject[10];//科目名称 int score;//科目成绩 }markinfo; typedef struct studentnode { markinfo mark[SUBJECT]; int totalmark; char name[10];//学生姓名 studentnode * next; }studentnode; class student { studentnode * head; public: student(); int addstudent(); ~student(); int countmark(); int sortbymark(); int save(); int show(); int display(); int readfiletolist(); int searchbyname(); }; student::student() //用构造函数来初始化。 { head=new studentnode; head->next=NULL; } //1.输入学生姓名、成绩等数据,并保存在链表中。 int student::addstudent() { studentnode * p; int i; char check; system("cls"); cout<<"**********************"<<endl; cout<<"请输入学生信息:"<<endl; do { p=new studentnode; cin.ignore(); cout<<"姓名:"; gets(p->name); i=0; p->totalmark=0; do { cout<<"科目:"; gets(p->mark[i].subject); cout<<"成绩(0--100):"; do { cin>>p->mark[i].score; } while(p->mark[i].score>100||p->mark[i].score<0); p->totalmark=p->totalmark+p->mark[i].score; getchar(); } while(++i!=SUBJECT); if(head->next==NULL) { head->next=p;p->next=NULL; } else { p->next=head->next; head->next=p; } cout<<"继续添加?y or n :"; check=getchar(); } while(check!='n' &&check!='N'); return 0; } //2.计算每位学生总成绩。 int student::countmark() { studentnode * p=head->next; if(p==NULL) { cout<<"没有学生,请重新输入"<<endl;system("pause");return 0; } else { cout<<"***************"<<endl; cout<<"学生成绩汇总:"<<endl; while(p) { cout<<"姓名:"<<p->name<<" 总成绩:"<<p->totalmark<<endl; p=p->next; } } system("pause"); return 0; } //4.输出所有学生成绩到一个文件中。 int student::save() { char address[35]; int i; studentnode * p=head->next; cout<<"请输入保存的地址"<<endl; cin.ignore(); gets(address); ofstream fout; fout.open(address,ios::app|ios::out); while(p) { fout<<"*"; fout<<p->name<<"*"; i=0; while(i!=SUBJECT) { fout<<p->mark[i].subject<<"*"; fout<<p->mark[i].score; i++; } //fout<<"*"; p=p->next; } fout.flush(); fout.close(); cout<<"已经保存,请查阅"; system("pause"); return 0; } student::~student() //析构函数 { studentnode * p,* s; p=head->next; while(p) { s=p->next; delete p; p=s; } delete head; } //3.按照总成绩大小对记录进行排序 int student::sortbymark() { studentnode *move1=head->next; studentnode *move2,*max,*pre1,*pre2,*maxpre,*s=move1; if(head->next==NULL) { cout<<"没有记录,请添加"<<endl;system("pause");return 0; } for(pre1=head,max=move1,maxpre=pre1;move1->next!=NULL;pre1=move1,maxpre=pre1,move1=move1->next,max=move1) { for(pre2=move1,move2=move1->next;move2!=NULL;pre2=move2,move2=move2->next) if(move2->totalmark>max->totalmark) { maxpre=pre2; max=move2; } if(move1->next==max) //交换max和move1。 { pre1->next=max; move1->next=max->next; max->next=move1; move1=max; } else { s=move1->next; move1->next=max->next; max->next=s; maxpre->next=move1; pre1->next=max; move1=max; } } cout<<"已经按照从大到小排序"<<endl; system("pause"); return 0; } //5输出输入的信息 int student::show() { studentnode * p=head->next; int i; if(head->next==NULL){cout<<"没有学生记录,请添加"<<endl;system("pause"); return 0;} else { while(p) { cout<<"姓名:"<<p->name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<<p->mark[i-1].subject; cout<<" 成绩:"<<p->mark[i-1].score; i++; } cout<<endl; p=p->next; } } system("pause"); return 0; } //6:从文件按读取记录 int student::display() { ifstream fin; char buf[100]; char str[25]; cout<<"请输入路径及文件名:"<<endl; cin.ignore(); gets(str); fin.open(str); if(!fin) { cout<<"没有此文件"<<endl; system("pause"); return 0; } while(fin) { fin.getline(buf,sizeof(buf)); cout<<buf<<endl; } system("pause"); return 0; } //8从文件中读取数据,并将数据保存在链表中 int student::readfiletolist() { ifstream fin; int i; char str[25]; cout<<"请输入路径及文件名:"<<endl; cin.ignore(); gets(str); fin.open(str); if(!fin) { cout<<"没有此文件"<<endl; system("pause"); return 0; } studentnode * p; fin.ignore(100,'*'); while(fin) { p=new studentnode; p->totalmark=0; fin.getline(p->name,100,'*'); i=0; while(i!=SUBJECT) { fin.getline(p->mark[i].subject,100,'*'); fin>>p->mark[i].score; p->totalmark+=p->mark[i].score; i++; } if(head->next==NULL) { head->next=p; p->next=NULL; } else { p=head->next; head->next=p; } } cout<<"信息已经保存在链表中"<<endl; system("pause"); return 0; } //9根据姓名进行查找 int student::searchbyname() { if(head->next==NULL) { cout<<"没有学生,请添加或者从文件中读取"<<endl; system("pause"); return 0; } studentnode * p=head->next; char findname[10]; int i; cout<<"请输入姓名:"; cin.ignore(); gets(findname); while(p) { if(!strcmp(p->name,findname)) { cout<<"经查找,找到该生信息如下:"<<endl<<endl; cout<<"姓名:"<<p->name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<<p->mark[i-1].subject; cout<<" 成绩:"<<p->mark[i-1].score; i++; } cout<<endl; system("pause"); return 1; } p=p->next; } cout<<"没有此学生,请添加或者从文件中读取"<<endl; system("pause"); return 0; } int showmenu() { int choice; char * menu[9]={ "1:输入学生成绩保存到链表\n", "2:计算每位学生总成绩\n", "3:按照总成绩大小对记录进行排序\n", "4:输出所有学生成绩到一个文件中\n", "5:显示新输入的学生信息\n", "6:从文件中读取信息\n", "7:将文件信息保存在链表中\n", "8:根据姓名查找学生记录\n", "9:结束程序\n" }; cout<<" "<<"*****************************************************"<<endl; cout<<" *"<<" "<<"学生成绩管理系统"<<" *"<<endl; cout<<" "<<"*****************************************************"<<endl; for(choice=0;choice<9;choice++) cout<<" "<<menu[choice]; cout<<" "<<"*****************************************************"<<endl; cout<<"please choose to continue"<<endl; do { cin>>choice; } while(choice>9||choice<1); return choice; } int main() { int menuitem,flag=1; student stu; while(flag) { system("cls"); menuitem=showmenu(); switch(menuitem) { case 1:{stu.addstudent();break;} case 2:{stu.countmark();break;} case 3:{stu.sortbymark();break;} case 4:{stu.save();break;} case 5:{stu.show();break;} case 6:{stu.display();break;} case 7:{stu.readfiletolist();break;} case 8:{stu.searchbyname();break;} case 9:{flag=0;break;} } } return 0; } //管理系统代码2 #include <iostream> #include <fstream> #include <stdio.h> #include <ctype.h> #include <cstdlib> #define N 10 using namespace std; char * course[N]= { "高等数学(上)", "高等数学(下)", "大学英语(上)", "大学英语(下)", "体育(上)", "体育(下)", "C++程序设计", "通信原理", "课程为数据库", "通信应用系统开发" }; typedef struct studentNode { string studentNumber; string name; int score[N]; studentNode *next; }; class Student { studentNode * head; public: Student(); ~Student(); void createStudentInfo(); void addStudentInfo(); void delStudentInfo(); void modificationStudentInfo(); void write2File(); void show(); void referCourseScore(); void referStudentInfo(); void referAverageScore(); void referByName(); void referByStudentNumber(); void referTheHightScore(); void referTheFallStudent(); }; Student::Student() //用构造函数来初始化。 { head=new studentNode; head->next=NULL; } Student::~Student() //析构函数 { studentNode * p,* s; p=head->next; while(p) { s=p->next; delete p; p=s; } delete head; } void Student::referTheHightScore() { studentNode *p = head->next; int i; string recordstudentNumber[N]; int theHighScore[N]; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { i = 0; while( i < N) { recordstudentNumber[i] = p -> studentNumber; theHighScore[i] = p->score[i]; i++; } p = p -> next; i = 1; while(p) { int j ; for(j = 0; j < N; j++) { if(theHighScore[j] < p->score[j]) { theHighScore[j] = p->score[j]; recordstudentNumber[j] = p -> studentNumber; } } p = p->next; } } p = head->next; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { for(int j = 0; j < N; j++) { while(p) { i = 0; if(recordstudentNumber[j] == p->studentNumber) { cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; while( i < N) { cout<<"\n科目:"<<course[i]; cout<<"成绩:" <<p->score[i]; i++; } } cout<<endl; p = p->next; } p = head->next; } } system("pause"); } void Student::referTheFallStudent() { studentNode *p = head->next; int i; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { while(p) { while( i < N) { if(p->score[i] < 60) { cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; cout<<"\n科目:"<<course[i]; cout<<"成绩:" <<p->score[i]; } i++; } i = 0; cout<<endl<<endl; p = p->next; } } system("pause"); } void Student::show() { studentNode *p = head->next; int i; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { while(p) { cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; i = 0; while( i < N) { cout<<"\n科目:"<<course[i]; cout<<"成绩:" <<p->score[i]; i++; } cout<<endl<<endl; p = p->next; } } system("pause"); } void Student::createStudentInfo() { if(head -> next == NULL) { cout<<"增加学生信息"<<endl; system("pause"); } addStudentInfo(); } void Student::addStudentInfo() { studentNode * p; int i; char check; system("cls"); cout<<"**********************"<<endl; cout<<"请输入学生信息:"<<endl; do { i = 0; p=new studentNode; cin.ignore(); cout<<"姓名:"; cin >>p -> name; cout<<"学号:"; cin >>p -> studentNumber; do { cout<<"科目:"<<course[i]; cout<<"成绩(0--100):"; do { cin>> p->score[i]; }while(p -> score[i]>100||p -> score[i]<0); }while(++i != N); if(head->next == NULL) { head->next=p; p->next=NULL; } else { p->next = head->next; head->next = p; } cout<<"继续添加?y or n :"; cin.ignore(); check=getchar(); }while(check != 'n' && check!= 'N'); } void Student::delStudentInfo() { studentNode *p = head->next; studentNode *pFlag = head; int i; int flag = 0; string str; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { cout<< "输入你要删除学生的学号:"; cin>> str; while(p) { i = 0; if(str == p->studentNumber) { if(p ->next != NULL) { pFlag->next = p -> next; delete p; } else { pFlag->next = NULL; } return; } pFlag = p; p = p->next; } if(flag == 0) { cout <<"查无此人"<< endl; } } system("pause"); } void Student::modificationStudentInfo() { studentNode *p = head->next; int i; int flag = 0; string str; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { cout<< "输入你要修改成绩学生的学号:"; cin>> str; while(p) { i = 0; if(str == p->studentNumber) { int numericalOrder; flag = 1; cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; while( i < N) { cout<< "\n"<<i<<"科目:"<<course[i]; cout<< "成绩:" <<p->score[i]; i++; } cout<< "请输入修改科目的课程序号:"; cin>> numericalOrder; cout<< "\n请输入修改后的成绩"; cin>> p -> score[numericalOrder]; } cout<<endl; p = p->next; } if(flag == 0) { cout <<"查无此人"<< endl; } } system("pause"); } void Student::referByName() { studentNode *p = head->next; int i; int flag = 0; string str; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { cout<< "输入你要查询学生的姓名:"; cin>> str; while(p) { i = 0; if(str == p->name) { flag = 1; cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; while( i < N) { cout<<"\n科目:"<<course[i]; cout<<"成绩:" <<p->score[i]; i++; } } cout<<endl; p = p->next; } if(flag == 0) { cout <<"查无此人"<< endl; } } system("pause"); } void Student::referByStudentNumber() { studentNode *p = head->next; int i; int flag = 0; string str; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { cout<< "输入你要查询学生的学号:"; cin>> str; while(p) { i = 0; if(str == p->studentNumber) { flag = 1; cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; while( i < N) { cout<<"\n科目:"<<course[i]; cout<<"成绩:" <<p->score[i]; i++; } } cout<<endl; p = p->next; } if(flag == 0) { cout <<"查无此人"<< endl; } } system("pause"); } void Student::referStudentInfo() { int i; while(1) { system("cls"); cout<<"\n***********************选择查询方式***************************"<<endl; cout<<" 0. 查询全部学生信息"<<endl; cout<<" 1. 通过姓名查询信息"<<endl; cout<<" 2. 通过学号查询信息"<<endl; cout<<" 3. 单科最高成绩学生"<<endl; cout<<" 4. 不 及格学生信 息"<<endl; cout<<" 5. 返回上级菜单查询"<<endl; cout<<"*************************************************************************"<<endl; cout<<"请 输 入 你 的 选 择< 按数字0~2>:"<<endl; cin >> i; switch(i) { case 0: show(); break; case 1: referByName(); break; case 2: referByStudentNumber(); break; case 3: referTheHightScore(); break; case 4: referTheFallStudent(); break; case 5: return; default: cout<< "输入错误"<< endl; system("pause"); } } } void Student::referAverageScore() { studentNode *p = head->next; int i; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { while(p) { int totalScore = 0; cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; i = 0; while( i < N) { totalScore += p->score[i]; i++; } cout<<"\n平均成绩:"<< totalScore / N; cout<<endl; p = p->next; } } system("pause"); } void Student::referCourseScore() { studentNode *p = head->next; int i; string str; if(head->next==NULL) { cout<<"没有学生记录,请添加"<<endl; system("pause"); return ; } else { i = 0; int numericalOrder; while( i < N) { cout<< "\n"<<i<<"科目:"<<course[i]; i++; } cout<< "请输入查询科目的课程序号:"; cin>> numericalOrder; while(p) { cout<<"姓名:"<< p->name; cout<<"学号:"<< p->studentNumber; cout<< p -> score[numericalOrder]; p = p->next; } } } void Student::write2File() { char address[35]; int i; studentNode * p=head->next; cout<<"请输入保存的地址"<<endl; cin.ignore(); gets(address); ofstream fout; fout.open(address,ios::app|ios::out); while(p) { fout<<" "; fout<<p->studentNumber<<" "; fout<<p->name<<" "; i=0; while(i < N) { fout<<p ->score[i]<<" "; i++; } //fout<<"*"; p=p->next; } fout.flush(); fout.close(); cout<<"已经保存,请查阅"; system("pause"); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服