#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
class student
{ private:
char name[20]; //姓名
double cpro,english,math,sport,law,hbpro,computer;//课程
int order, number; //名次,学号
public:
student(){}
student(char n[20],int nu,double cc,double eng,double ma,double sp,double l,double hb,double com)
{strcpy(name,n);
number=nu;
cpro=cc; english=eng;math=ma;sport=sp;law=l;hbpro=hb;computer=com;
}
friend void main();
};
void main()
{
cout<<" 欢迎进入**学生成绩管理系统**!"<<endl;
cout<<" ******************************************"<<endl;
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<<" **************************"<<endl;
cout<<" 选择0-6数字进行操作"<<endl;
char p;char w;
student *s[50]; //指针对象,最多存50个学生数据
ofstream *file[50]; //负责对文件插入操作
int i=0;
int j=0;
int flag=0;
do //flag判断输入是否有效
{
cin>>p;
if((p>='0'&&p<='6'))
flag=1;
else
cout<<" 指令错误!请重新输入:"<<endl;
}while(flag==0);
do{
switch(p) //接收功能选项
{
case '0': //输入数据
{
char c;
char name[20];int number;double cpro,english,math,sport,law,hbpro,computer;
do{
cout<<" 请输入姓名:";
cin>>name;
cout<<endl<<" 请输入学号:";
cin>>number;
cout<<" 请输入C++成绩:";
cin>>cpro;
cout<<endl<<" 请输入英语成绩:";
cin>>english;
cout<<endl<<" 请输入数学成绩:";
cin>>math;
cout<<endl<<" 请输入体育成绩:";
cin>>sport;
cout<<endl<<" 请输入网络基础成绩:";
cin>>law;
cout<<endl<<" 请输入C语言成绩:";
cin>>hbpro;
cout<<endl<<" 请输入数据库成绩:";
cin>>computer;
cout<<endl;
file[j]=new ofstream("D:\document",ios::ate);
*file[j]<<" 姓名 "<<name<<" 学号 "<<number<<" C++成绩 "<<cpro
<<" 英语成绩 "<<english<<" 数学成绩 "<<math<<" 体育成绩 "
<<sport<<" 网络基础成绩 "<<law<<" C成绩 "<<hbpro<<" 数据库成绩 "<<computer<<endl;
j++;
s[i]=new student(name, number,cpro,english,math,sport,law,hbpro,computer);
i++;
cout<<" 数据输入成功,想继续输入吗(y/n):";
cin>>c;
cout<<endl;
do
{
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl<<" ";
cin>>c;
}
else
flag=1;
}while(flag==0);
}while(c=='y');
break;
}
case '1': //增加数据
{
char name[20];
int number;double cpro,english,math,sport,law,hbpro,computer;
char c;
do
{
cout<<" 请输入您要增加的学生的姓名:";
cin>>name;
cout<<endl<<" 请输入学号:";
cin>>number;
cout<<endl<<" 请输入C++成绩:";
cin>>cpro;
cout<<endl<<" 请输入英语成绩:";
cin>>english;
cout<<endl<<" 请输入数学成绩:";
cin>>math;
cout<<endl<<" 请输入体育成绩:";
cin>>sport;
cout<<endl<<" 请输入网络基础成绩:";
cin>>law;
cout<<endl<<" 请输入C语言成绩:";
cin>>hbpro;
cout<<endl<<" 请输入数据库成绩:";
cin>>computer;
cout<<endl;
file[j]=new ofstream("d:\document",ios::ate);
*file[j]<<" 姓名 "<<name<<" 学号 "<<number<<" C++成绩 "<<cpro<<" 英语成绩 "<<english<<" 数学成绩 "<<math<<" 体育成绩 "<<sport<<" 网络基础成绩 "<<law<<" C成绩 "<<hbpro<<" 数据库成绩 "<<computer<<endl;
j++;
s[i]=new student(name, number, cpro,english,math,sport,law,hbpro,computer);
i++;
cout<<" 数据输入成功,想继续数入吗(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl<<" ";
cin>>c;
}
}while(c=='y');
break;
}
case '2': //修改数据
{
char name[20];int nu;double cc,eng,ma,sp,l,hb,com;flag=0;
char c;
if(i==0)
{
cout<<" 管理系统中没有输入数据!"<<endl;break;
}
do
{
cout<<" 请输入您要修改的学生的姓名:";
cin>>name;
cout<<endl;
for(int h=0;h<i;h++) //h纪录要修改学生的位置
{
if(strcmp(name,s[h]->name)==0)
{
flag=1;
cout<<" 请输入新的学号:";
cin>>nu;
cout<<endl<<" 请输入C++成绩:";
cin>>cc;
cout<<endl<<" 请输入英语成绩:";
cin>>eng;
cout<<endl<<" 请输入数学成绩:";
cin>>ma;
cout<<endl<<" 请输入体育成绩:";
cin>>sp;
cout<<endl<<" 请输入网络基础成绩:";
cin>>l;
cout<<endl<<" 请输入C语言成绩:";
cin>>hb;
cout<<endl<<" 请输入数据库成绩:";
cin>>com;
cout<<endl;
s[h]->cpro=cc;
s[h]->english=eng;
s[h]->math=ma;
s[h]->sport=sp;
s[h]->law=l;
s[h]->hbpro=hb;
s[h]->computer=com;
s[h]->number=nu;
cout<<" 数据修改成功!"<<endl;
}
}
if(flag==0)
{
cout<<" 您要修改的学生本来就不存在!请检查重新输入!"<<endl;
}
cout<<" 想继续修改吗(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl<<" ";
cin>>c;
}
}while(c=='y');
break;
}
case '3': //按姓名查询
{
char n[20];int j=0;char c;
if(i==0)
{
cout<<" 管理系统中没有输入数据!"<<endl;break;
}
do{
int flag=0;
cout<<" 请输入你要查询的学生姓名:";
cin>>n;
cout<<endl;
for(int j=0;j<i;j++)
{
if(strcmp(n,(*s[j]).name)==0)
{
flag=1;
cout<<" 您要查询的学生是:"<<(*s[j]).name<<endl;
cout<<(*s[j]).name<<"的成绩是: "<<" C++: "<<(*s[j]).cpro<<" 英语: "<<(*s[j]).english<<" 数学:"<<(*s[j]).math<<" 体育:"<<(*s[j]).sport<<" 法律:"<<(*s[j]).law<<" C:"<<(*s[j]).hbpro<<" 数据库 "<<(*s[j]).computer<<endl;
}
}
if(flag==0)
cout<<" 对不起!您要查询的学生不存在!"<<endl;
cout<<" 您想继续查询吗?(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl;
cin>>c;
}
}
while(c=='y');
break;
}
case '4': //按学号查询
{
int n,j=0;char c;
if(i==0){
cout<<" 管理系统中没有输入数据!"<<endl;break;
}
do{
int flag=0;
cout<<" 请输入你要查询的学生的学号:";
cin>>n;
cout<<endl;
for(int j=0;j<i;j++)
{
if(s[j]->number==n)
{
flag=1;
cout<<" 您要查询的学生是:"<<(*s[j]).name<<endl;
cout<<(*s[j]).name<<"的成绩是: "<<" C++:"<<(*s[j]).cpro<<" 英语:"<<(*s[j]).english<<" 数学:"<<(*s[j]).math<<" 体育:"<<(*s[j]).sport<<" 法律:"<<(*s[j]).law<<" C:"<<(*s[j]).hbpro<<" 数据库 "<<(*s[j]).computer<<endl;
}
}
if(flag==0)
cout<<" 对不起!您要查询的学生不存在!"<<endl;
cout<<" 您想继续查询吗?(y/n):";
cin>>c;
cout<<endl;
if(c!='y'&&c!='n')
{
cout<<" 指令错误!请重新输入!"<<endl;
cin>>c;
}
}
while(c=='y');
break;
}
case '5': //输出
{
cout<<" 本系统所有学生数据如下:"<<endl;
if(i==0)
cout<<" 管理系统中没有输入数据!"<<endl;
cout<<" 姓名 学号 c++ 英语 数学 体育 网络基础 C语言 数据库 "<<endl;
for(int k=0;k<i;k++)
{
cout<<s[k]->name<<setw(7)<<s[k]->number<<setw(6)
<<(*s[k]).cpro<<setw(6)<<(*s[k]).english<<setw(6)
<<(*s[k]).math<<setw(6)<<(*s[k]).sport<<setw(7)
<<(*s[k]).law <<setw(10)<<(*s[k]).hbpro<<setw(10)<<(*s[k]).computer<<setw(10)<<endl;
}
break;
}
case'6'://退出
{exit(0); cout<<"Bye bye!"<<endl;}
}
cout<<" 您想继续进行其他操作吗?(y/n):";
int flag=0;
do
{
cin>>w;
cout<<endl;
if(w!='y'&&w!='n')
cout<<" 指令错误!请重新输入!"<<endl;
else
flag=1;
}while(flag==0);
if(w=='y')
cout<<" 请输入操作代码:0 输入数据"<<endl;
cout<<" 1 增加数据"<<endl;
cout<<" 2 修改数据"<<endl;
cout<<" 3 按姓名查询"<<endl;
cout<<" 4 按学号查找"<<endl;
cout<<" 5 输出所有学生成绩"<<endl;
cout<<" 6 退出系统"<<endl;
cin>>p;
}while(w=='y');
}
参考资料:http://zhidao.baidu.com/question/158674661.html?an=3&si=2