C++学生成绩管理系统

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
int N=40;
struct student
{
char name[40];
int id;
char sex;
float chinese;
float math;
float english;
int i,n;
}ob[40];
void input()
{int i,n;
 cout<<"请输入学生个数:";
cin>>n;
for(i=1;i<=n;i++)
{cout<<"学号:"<<endl;
cin>>ob[i].id;

cout<<"姓名:"<<endl;
cin>>ob[i].name;


cout<<"性别(m/f):"<<endl;
cin>>ob[i].sex;

cout<<"语文:"<<endl;
cin>>ob[i].chinese;

cout<<"数学:"<<endl;
cin>>ob[i].math;

cout<<"英语:"<<endl;
cin>>ob[i].english;

}
}
void baochun()
{int i;
ofstream fout("list.txt");
if(!fout)
{cerr<<"open error!"<<endl;
exit(1);
}
for(i=1;i<=N;i++)
{
fout<<ob[i].id<<"        "<<ob[i].name<<"      "<<ob[i].sex <<"      "<<ob[i].chinese<<"    "<<ob[i].math<<"    "<<ob[i].english<<endl;                        
}
 fout.close();

}
void del()
{
 int id,i,t;
ifstream infile("list.txt");
if(!infile)
cout<<"文件打开失败";
for(i=1;i<=N;i++)
infile>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].chinese>>ob[i].math>>ob[i].english;
infile.close();
cout<<"请输入要删除学生信息的学号:"<<endl;
cin>>id;
for(i=1;i<=N;i++)
{if(ob[i].id==id)
{
 t=i;
}
}
for(i=t;i<=N;i++)
{
ob[i].id=ob[i+1].id;
strcpy(ob[i].name,ob[i+1].name);
ob[i].sex=ob[i+1].sex;
ob[i].chinese=ob[i+1].chinese;
ob[i].math=ob[i+1].math;
ob[i].english=ob[i+1].english;
}
N=N-1;
cout<<"删除后的学生信息:"<<endl;
for(i=1;i<=N;i++)
{cout<<ob[i].id<<" "<<ob[i].name<<" "<<ob[i].sex<<" "<<ob[i].chinese<<" "<<ob[i].math<<" "<<ob[i].english<<endl;
}
ofstream fout("list.txt");
if(!fout)
cout<<"open error";

for(i=1;i<=N;i++)
{
fout<<ob[i].id<<ob[i].name<<ob[i].sex<<ob[i].chinese<<ob[i].math<<ob[i].english<<endl;
}
fout.close();
}
void xiansi()
{int i;
 ifstream infile("list.txt");
if(!infile)
{cout<<"open error";
}
for(i=1;i<N;i++)
infile>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].chinese>>ob[i].math>>ob[i].english;
infile.close();
for(i=1;i<N;i++)
cout<<ob[i].id<<" "<<ob[i].name<<" "<<ob[i].sex<<" "<<ob[i].chinese<<" "<<ob[i].math<<" "<<ob[i].english<<endl;
}
void chaxun()
{ int i;
 ifstream fin("list.txt");
 if(!fin)
    cout<<"文件打开失败";
 for(i=1;i<=40;i++)
 {
  fin>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].chinese>>ob[i].math>>ob[i].english;
 }
 fin.close();
 
 int t,j;
cout<<"1 按学号查询"<<endl;
cout<<"2 按姓名查询"<<endl;
 cout<<"请选择查询方式:"<<endl;
cin>>j;
switch(j)
{case 1:
int m;
cout<<"请输入学号:";
cin>>m;
for(i=1;i<=40;i++)
{
if(ob[i].id==m)
{t=i;
}
}
cout<<ob[t].id<<"        "<<ob[t].name<<"      "<<ob[t].sex <<"      "<<ob[t].chinese<<"    "<<ob[t].math<<"    "<<ob[t].english<<endl; 
break;
case 2:
 char name1[40];
 cout<<"请输入姓名:";
 cin>>name1;
for(i=0;i<40;i++)
 {
 if(strcmp(ob[i].name,name1)==0)
 {t=i;
 }
}
 cout<<ob[t].id<<"        "<<ob[t].name<<"      "<<ob[t].sex <<"      "<<ob[t].chinese<<"    "<<ob[t].math<<"    "<<ob[t].english<<endl; 
break;
}
}
void xiugai()
{int i,m,id,id1;
char name2[40],sex;
float chinese,math,english;
 ifstream infile("list.txt");
if(!infile)
{cout<<"open error";
}
for(i=1;i<N;i++)
infile>>ob[i].id>>ob[i].name>>ob[i].sex>>ob[i].chinese>>ob[i].math>>ob[i].english;
infile.close();
cout<<"请输入将被修改学生信息的学号"<<endl;
cin>>id1;
cout<<"请输入修改后的学生信息:"<<endl;
cout<<"学号:"<<endl;
cin>>id;
cout<<"姓名:"<<endl;
cin>>name2;
cout<<"性别(m/f):"<<endl;
cin>>sex;
cout<<"语文:"<<endl;
cin>>chinese;
cout<<"数学:"<<endl;
cin>>math;
cout<<"英语:"<<endl;
cin>>english;
for(i=1;i<N;i++)
if(ob[i].id==id1)
{m=i;
}
ob[m].id=id;
strcpy(ob[m].name,name2);
ob[m].sex=sex;
ob[m].chinese=chinese;
ob[m].math=math;
ob[m].english=english;
ofstream fout("list.txt");
for(i=1;i<N;i++)
fout<<ob[i].id<<"        "<<ob[i].name<<"      "<<ob[i].sex <<"      "<<ob[i].chinese<<"    "<<ob[i].math<<"    "<<ob[i].english<<endl;
fout.close();
}
void menu()
{cout<<"学生成绩管理系统"<<endl;
 cout<<"1 输入和保存学生信息"<<endl;
 cout<<"2 删除学生信息"<<endl;
 cout<<"3 修改学生信息"<<endl;
 cout<<"4 显示显示信息"<<endl;
 cout<<"5 查询学生信息"<<endl;
 cout<<"6 退出系统"<<endl;
}

void main()
{
 while(1)
 { menu();

    int n;
     cin>>n;
switch(n)
 {
 case 1:
        input(); baochun();  break;
 case 2:
        del();      break;
 case 3:
      xiugai();   break;
 case 4:
  xiansi();   break;
 case 5:
  chaxun();   break;
 case 6:
   
 {cout<<"按任意键退出"<<endl;}
     exit(0);
default:
    cout<<"Bad input!!/n";
                  break;
  }

 }
}

相当不错的一个成绩管理系统 #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、付费专栏及课程。

余额充值