简易版学生成绩管理系统(c++)

#include<iostream>//基本输入输出流

#include<fstream>//文件操作流

#include<algorithm>//算法

#include<string>//string类

using namespacestd;

struct Student//结构体

{

      char name[20];//姓名

      double chi;//语文成绩

      double math;//数学成绩

      double english;//英语成绩

}out[1000];//in用于存储输入数据,out用于输出数据。

int top;//读取文件的栈头

int user;//判断是否跳出菜单标量

intsort_method(Student a,Student b)//定义排序方式

{

      returna.chi+a.math+a.english>b.chi+b.math+b.english;

}

void fileget()//读取文件

{

      top=-1;

      ifstreamin("c://database.dat",ios::binary|ios::out);

      while(!in.eof())//每次文件读取到文件尾

      {

           top++;

           in.read((char*)&out[top],sizeof(out[top]));//文件读取操作

      }

      in.close();//关闭

}

voidstudent_print()//打印学生成绩表

{

    fileget();

      if(top<1)

           cout<<"信息表无信息"<<endl;

      cout<<"姓名    语文 数学 英语 平均分 总分"<<endl;

      for(int i=0;i<top;i++)

      {

           cout<<out[i].name<<""<<out[i].chi<<""<<out[i].math<<""<<out[i].english<<"";

           cout<<(out[i].chi+out[i].math+out[i].english)/3<<""<<out[i].chi+out[i].math+out[i].english<<endl;

      }

}

voidstudent_insert()//插入操作,文件保存操作

{

      cout<<"请输入学生姓名 三科成绩并用空格隔开"<<endl;

    Student A;

   cin>>A.name>>A.chi>>A.math>>A.english;

      ofstreamout("c://database.dat",ios::binary|ios::app);//二进制打开,ios:app不覆盖插入

      out.write((char*)&A,sizeof(A));

      out.close();

      cout<<"插入成功,新的信息表为"<<endl;

      student_print();//执行插入语句再次输出学生信息表

}

voidstudent_print_sort()//打印学生排序后成绩表

{

      fileget();

      cout<<"姓名    语文 数学 英语 平均分 总分"<<endl;

      sort(out,out+(top-1),sort_method);//C++STLsort()排序函数

      for(int i=0;i<top;i++)

      {

           cout<<out[i].name<<""<<out[i].chi<<""<<out[i].math<<""<<out[i].english<<"";

           cout<<(out[i].chi+out[i].math+out[i].english)/3<<""<<out[i].chi+out[i].math+out[i].english<<endl;

      }

}

voidstudent_select()//学生信息查询

{

      string name_find;

      fileget();

      cout<<"请输入欲查询的学生"<<endl;

    cin>>name_find;

      int pos=-1;//定位学生的下标

      for(int i=0;i<top;i++)

      {

           if(name_find==out[i].name)

           {

                 pos=i;

                 break;

           }

      }

      if(pos==-1)

      {

           cout<<"查无此人"<<endl;

      }

      else

      {

           cout<<"姓名    语文 数学 英语 平均分 总分"<<endl;

           cout<<out[pos].name<<""<<out[pos].chi<<""<<out[pos].math<<""<<out[pos].english<<"";

      cout<<(out[pos].chi+out[pos].math+out[pos].english)/3<<""<<out[pos].chi+out[pos].math+out[pos].english<<endl;

      }

}

voidstudent_delete()

{

      top=-1;

      Student out1[1000];//执行write时,该结构体必须于该函数内定义

      ifstream in("c://database.dat",ios::binary|ios::out);

      while(!in.eof())//每次文件读取到文件尾

      {

           top++;

           in.read((char*)&out1[top],sizeof(out1[top]));//文件读取操作

      }

      in.close();//关闭

      string name_find;

      fileget();

      cout<<"请输入欲删除的学生"<<endl;

    cin>>name_find;

      int pos=-1;//定位学生的下标

      for(int i=0;i<top;i++)

      {

           if(name_find==out1[i].name)

           {

                 pos=i;

                 break;

           }

      }

      if(pos==-1)//当语句不执行,即pos变量未被赋值,此时可确认查无信息

      {

           cout<<"查无此人"<<endl;

      }

      else

      {

           ofstreamout("c://database.dat",ios::binary|ios::ate);//二进制打开,ios:app不覆盖插入,反之是覆盖插入

           for(int i=0;i<top;i++)

           {

                 if(i!=pos)//忽略删除一行

                 {

                      out.write((char*)&out1[i],sizeof(out1[i]));//覆盖更新插入

                 }

           }

           out.close();

           cout<<"删除成功,新的学生信息表为:"<<endl;

           student_print();//再次输出学生表信息

      }

     

}

void menu()//主菜单

{

      string menu_order;//字符串指令,防止用户输入指令出现多个字符

      cout<<"************学生成绩管理************"<<endl;

      cout<<"1.学生信息添加"<<endl;

      cout<<"2.学生信息输出"<<endl;

      cout<<"3.学生信息删除"<<endl;

      cout<<"4.学生信息查询"<<endl;

      cout<<"5.学生成绩排序"<<endl;

    cout<<"请按一个数字1-5,进行一项操作,0表示退出"<<endl;

mark:  //输入指令非法可goto到此处

      cin>>menu_order;

      if(menu_order=="0")

      {

           user=0;//表示退出菜单

           cout<<"欢迎下次使用"<<endl;

      }

      if(menu_order<"0"||menu_order>"5")

      {

           cout<<"输入有误,请重新输入"<<endl;

           goto mark;

      }

      if(menu_order=="1")

      {

           student_insert();

      }   

      if(menu_order=="2")

      {

           student_print();

      }

      if(menu_order=="3")

      {

           student_delete();

      }

      if(menu_order=="4")

      {

           student_select();

      }

      if(menu_order=="5")

      {

           student_print_sort();

      }

}

int main()

{

      user=1;

    while(user)

      {

           menu();

      }

      return 0;

}

相当不错的一个成绩管理系统 #include #include #include #include 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<name); i=0; p->totalmark=0; do { cout<mark[i].subject); cout<>p->mark[i].score; } while(p->mark[i].score>100||p->mark[i].scoretotalmark=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<next; if(p==NULL) { cout<<"没有学生,请重新输入"<<endl;system("pause");return 0; } else { cout<<"***************"<<endl; cout<<"学生成绩汇总:"<<endl; while(p) { cout<<"姓名:"<name<<" 总成绩:"<totalmark<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<name<<"*"; i=0; while(i!=SUBJECT) { fout<mark[i].subject<<"*"; fout<mark[i].score; i++; } //fout<next; } fout.flush(); fout.close(); cout<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<<"没有记录,请添加"<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<<"已经按照从大到小排序"<next; int i; if(head->next==NULL){cout<<"没有学生记录,请添加"<<endl;system("pause"); return 0;} else { while(p) { cout<<"姓名:"<name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<mark[i-1].subject; cout<<" 成绩:"<mark[i-1].score; i++; } cout<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<<"没有此文件"<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<<"信息已经保存在链表中"<next==NULL) { cout<<"没有学生,请添加或者从文件中读取"<next; char findname[10]; int i; cout<name,findname)) { cout<<"经查找,找到该生信息如下:"<<endl<<endl; cout<<"姓名:"<name; i=1; while(i!=SUBJECT+1) { cout<<"科目:"<mark[i-1].subject; cout<<" 成绩:"<mark[i-1].score; i++; } cout<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"<>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; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值