cpp文件需要与名为‘class.txt’,'student.txt','score.txt'三个文件放在一个文件夹才能正常运行,如图:
代码如下:
//使用此程序时需将源程序整个文件夹拖到桌面才可使用
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
#include <iomanip>
#include <iostream>
using namespace std;
typedef struct
{
int num;
char name[10];
float score;
char teacher[10];
}Cls;
typedef struct
{
Cls *elem;
int length;
}Class;
typedef struct
{
int num;
char name[10];
char clas[10];
char birth[10];
long long QQ;
}Std1;
typedef int Status;
typedef Std1 ElemType;
typedef struct
{
ElemType *elem;
int length;
}Std;
typedef struct
{
int snum;
int cnum;
float score;
int trem;
}Sco;
typedef struct
{
Sco *elem;
int length;
}Score;
Status menu();
Status ReadStu( Std &stu );
Status ReadCls( Class &cls );
Status ReadSco( Score &sco );
Status InitList( Std &stu );
Status InitList( Class &cls );
Status InitList( Score &sco );
Status InputStd( Std &stu );
Status InputClass( Class &cls );
Status InputScore( Score &sco );
Status InputMenu( Std &stu, Class &cls, Score &sco );
Status OutputStd( Std &stu );
Status OutputCls( Class &cls );
Status OutputSco( Score &sco );
Status OutputMenu( Std &stu, Class &cls, Score &sco );
Status SearchStd( Std &stu );
Status SearchCls( Class &cls );
Status SearchSco( Score &sco );
Status SearchMenu( Std &stu, Class &cls, Score &sco );
Status ModifyStd( Std &stu );
Status ModifyCls( Class &cls );
Status ModifySco( Score &sco );
Status ModifyMenu( Std &stu, Class &cls, Score &sco );
Status DeleteStd( Std &stu );
Status DeleteCls( Class &cls );
Status DeleteSco( Score &sco );
Status DeleteMenu( Std &stu, Class &cls, Score &sco );
Status SaveStd( Std &stu );
Status SaveCls( Class &cls );
Status SaveSco( Score &sco );
Status SaveMenu( Std &stu, Class &cls, Score &sco );
Status SearchOsco( Score &sco , Class &cls , Std &stu );
Status SearchCSco( Std &stu, Class &cls, Score &sco );
Status Searchlsco( Std &stu, Class &cls, Score &sco, char *clas );
int main()
{
int i;
Std stu;
Class cls;
Score sco;
char *clas;
InitList( stu );
InitList( cls );
InitList( sco );
ReadStu( stu );
ReadCls( cls );
ReadSco( sco );
short int flag = 0;
menu();
do
{
flag=1;
switch( getch() )
{
case '1' :
InputMenu( stu, cls, sco );//添加信息
break;
case '2' :
OutputMenu( stu, cls, sco );//输出信息
break;
case '3' :
SearchMenu( stu, cls, sco );//查询信息
break;
case '4' :
ModifyMenu( stu, cls, sco );//修改成绩
break;
case '5' :
DeleteMenu( stu, cls, sco );//删除信息
break;
case '6' :
SearchOsco( sco , cls , stu );//查询个人成绩
break;
case '7' :
SearchCSco( stu, cls, sco ); //查询某门课程所有成绩
break;
case '8' :
Searchlsco( stu, cls, sco ,clas );//查询某班级某门课所有成绩
break;
case '9' :
SaveMenu( stu, cls, sco ); //保存信息
break;
case '0' :
cout<<"已成功退出,欢迎您下次使用^-^"<<endl;
exit( 0 );
default :
cout<<"输入有误"<<endl;
}
cout<<"按任意键继续"<<endl;
getch();
menu();
}
while( flag == 1 );
}
Status menu()
{
system("cls");
cout<<endl;
cout<<"*********欢迎进入学生成绩管理系统**************"<<endl;
cout<<" 菜单 "<<endl;
cout<<"录入信息-------------------------------------1"<<endl;
cout<<"输出信息-------------------------------------2"<<endl;
cout<<"查询信息-------------------------------------3"<<endl;
cout<<"修改信息-------------------------------------4"<<endl;
cout<<"删除信息-------------------------------------5"<<endl;
cout<<"查询个人成绩---------------------------------6"<<endl;
cout<<"查询某门课程所有人成绩-----------------------7"<<endl;
cout<<"查询某班级某门课所有成绩---------------------8"<<endl;
cout<<"保存信息-------------------------------------9"<<endl;
cout<<"退出系统-------------------------------------0"<<endl;
cout<<"**********************************************"<<endl;
cout<<"请输入您的选择"<<endl;
}
Status ReadStu( Std &stu )
{
FILE *fp;
int i;
if( ( fp = fopen( "student.txt" , "r" ) ) == NULL )
{
cout<<"打开失败!"<<endl;
return ERROR;
exit( 0 );
}
for( i = 0; !feof(fp); i++ )
{
fscanf( fp ,"%d %s %s %s %lld", &stu.elem[i].num, &stu.elem[i].name, &stu.elem[i].clas
, &stu.elem[i].birth, &stu.elem[i].QQ );
++stu.length;
}
fclose( fp );
stu.length--;
return OK;
}
Status ReadCls( Class &cls )
{
FILE *fp;
int i;
if( ( fp = fopen( "class.txt" , "r" ) ) == NULL )
{
cout<<"打开失败!"<<endl;
return ERROR;
exit( 0 );
}
for( i = 0; !feof(fp); i++ )
{
fscanf( fp ,"%d %s %f %s", &cls.elem[i].num, &cls.elem[i].name
, &cls.elem[i].score, &cls.elem[i].teacher );
++cls.length;
}
fclose( fp );
cls.length--;
return OK;
}
Status ReadSco( Score &sco )
{
FILE *fp;
int i;
if( ( fp = fopen( "score.txt" , "r" ) ) == NULL )
{
cout<<"打开失败!"<<endl;
return ERROR;
exit( 0 );
}
for( i = 0; !feof(fp); i++ )
{
fscanf( fp ,"%d %d %f %d", &sco.elem[i].snum, &sco.elem[i].cnum
, &sco.elem[i].score, &sco.elem[i].trem );
++sco.length;
}
fclose( fp );
sco.length--;
return OK;
}
Status InitList( Std &stu )
{
stu.elem = new ElemType[MAXSIZE];
if( !stu.elem )
exit( OVERFLOW );
stu.length = 0;
return OK;
}
Status InitList( Class &cls )
{
cls.elem = new Cls[MAXSIZE];
if( !cls.elem )
exit( OVERFLOW );
cls.length = 0;
return OK;
}
Status InitList( Score &sco )
{
sco.elem = new Sco[MAXSIZE];
if( !sco.elem )
exit( OVERFLOW );
sco.length = 0;
return OK;
}
Status InputStd( Std &stu )
{
int i, n, k, a = 0;
cout<<"请输入学生总数:"<<endl;
if( cin>>n && n > 0 )
{
for( i = 0; i < n ; i++ )
{
cout<<"请输入学生"<<i+1<<"的学号"<<endl;
cin>>stu.elem[stu.length].num;
for( k = 0; k < stu.length; k++ )
{
if( stu.elem[k].num == stu.elem[stu.length].num )
{
cout<<"学号重复,已有该学生!"<<endl;
a = 1;
break;
}
}
if( a == 1 )
break;
cout<<"请输入学生"<<i+1<<"的姓名"<<endl;
cin>>stu.elem[stu.length].name;
cout<<"请输入学生"<<i+1<<"的班级"<<endl;
cin>>stu.elem[stu.length].clas;
cout<<"请输入学生"<<i+1<<"的出生日期"<<endl;
cin>>stu.elem[stu.length].birth;
cout<<"请输入学生"<<i+1<<"的QQ"<<endl;
cin>>stu.elem[stu.length].QQ;
++stu.length;
}
}
}
Status InputClass( Class &cls )
{
int i, n, k ,a;
cout<<"请输入课程总数:"<<endl;
if( cin>>n && n > 0 )
{
for( i = 0; i < n; i++ )
{
cout<<"请输入课程"<<i+1<<"的课程号"<<endl;
cin>>cls.elem[cls.length].num;
for( k = 0; k < cls.length; k++ )
{
if( cls.elem[k].num == cls.elem[cls.length].num )
{
cout<<"课程号重复,已有该课程!"<<endl;
a = 1;
break;
}
}
if( a == 1 )
break;
cout<<"请输入课程"<<i+1<<"的课程名"<<endl;
cin>>cls.elem[cls.length].name;
cout<<"请输入课程"<<i+1<<"的学分"<<endl;
cin>>cls.elem[cls.length].score;
cout<<"请输入课程"<<i+1<<"的任课教师"<<endl;
cin>>cls.elem[cls.length].teacher;
++cls.length;
}
}
}
Status InputScore( Score &sco )
{
int i, n, a, b, k;
cout<<"请输入录入成绩总数:"<<endl;
if( cin>>n && n > 0 )
{
for( i = 0; i < n; i++ )
{
cout<<"请输入学号"<<endl;
cin>>sco.elem[sco.length].snum;
cout<<"请输入课程号"<<endl;
cin>>sco.elem[sco.length].cnum;
for( k = 0; k < sco.length; k++ )
{
if( sco.elem[k].cnum == sco.elem[sco.length].cnum
&& sco.elem[k].snum == sco.elem[sco.length].snum )
{
cout<<"学号和课程号重复,该学生在该课程上已有成绩!"<<endl;
a = 1;
break;
}
}
if( a == 1 )
break;
cout<<"请输入分数"<<endl;
cin>>sco.elem[sco.length].score;
cout<<"请输入学期数"<<endl;
cin>>sco.elem[sco.length].trem;
++sco.length;
}
}
}
Status InputMenu(Std &stu,Class &cls, Score &sco )
{
int i;
cout<<endl;
cout<<"1.录入学生信息 || 2.录入课程信息 "<<endl;
cout<<"3.录入成绩信息 || 4.取消 "<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>i;
switch( i )
{
case 1:
InputStd( stu );
break;
case 2:
InputClass( cls );
break;
case 3:
InputScore( sco );
break;
case 4:
cout<<"取消成功!"<<endl;
break;
default :
cout<<"输入有误"<<endl;
break;
}
return OK;
}
Status OutputStd( Std &stu )
{
int i;
cout<<"学号 姓名 班级 生日 QQ "<<endl;
for( i = 0; i < stu.length; i++ )
{
cout<<std::left<<setw(12)<<stu.elem[i].num<<std::left<<setw(12)<<stu.elem[i].name
<<std::left<<setw(12)<<stu.elem[i].clas<<std::left<<setw(12)<<stu.elem[i].birth
<<std::left<<setw(12)<<stu.elem[i].QQ<<endl;
}
return OK;
}
Status OutputCls( Class &cls )
{
int i;
cout<<"课程号 课程名 学分 任课教师 "<<endl;
for( i = 0; i < cls.length ; i++ )
{
cout<<std::left<<setw(12)<<cls.elem[i].num<<std::left<<setw(12)
<<cls.elem[i].name<<std::left<<setw(12)<<cls.elem[i].score
<<std::left<<setw(12)<<cls.elem[i].teacher<<endl;
}
return OK;
}
Status OutputSco( Score &sco )
{
int i;
cout<<"学号 课程号 分数 学期数 "<<endl;
for( i = 0; i < sco.length; i++ )
{
cout<<std::left<<setw(12)<<sco.elem[i].snum<<std::left<<setw(12)
<<sco.elem[i].cnum<<std::left<<setw(12)<<sco.elem[i].score
<<std::left<<setw(12)<<sco.elem[i].trem<<endl;
}
return OK;
}
Status OutputMenu( Std &stu, Class &cls, Score &sco )
{
int i;
cout<<endl;
cout<<"1.查看学生信息 || 2.查看课程信息 "<<endl;
cout<<"3.查看成绩信息 || 4.取消 "<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>i;
switch( i )
{
case 1:
OutputStd( stu );
break;
case 2:
OutputCls( cls );
break;
case 3:
OutputSco( sco );
break;
case 4:
cout<<"取消成功!"<<endl;
break;
default :
cout<<"输入有误"<<endl;
break;
}
return OK;
}
Status SearchStd( Std &stu )
{
int i, n;
cout<<"请输入要查找的学生的学号"<<endl;
cin>>n;
for( i = 0; i < stu.length; i++ )
{
if( stu.elem[i].num == n)
{
cout<<"查找到学号:"<<n<<"的学生的信息为:"<<endl;
cout<<"学号 姓名 班级 生日 QQ "<<endl;
cout<<std::left<<setw(12)<<stu.elem[i].num<<std::left<<setw(12)<<stu.elem[i].name
<<std::left<<setw(12)<<stu.elem[i].clas<<std::left<<setw(12)<<stu.elem[i].birth
<<std::left<<setw(12)<<stu.elem[i].QQ<<endl;
return OK;
}
}
cout<<"未查找到此学生,请输入正确的学号!"<<endl;
return ERROR;
}
Status SearchCls( Class &cls )
{
int i, n;
cout<<"请输入要查找的课程的课程号:"<<endl;
cin>>n;
for( i = 0; i < cls.length; i++ )
{
if( cls.elem[i].num == n )
{
cout<<"查找到课程号为"<<n<<"的课程信息为:"<<endl;
cout<<"课程号 课程名 学分 任课教师 "<<endl;
cout<<std::left<<setw(12)<<cls.elem[i].num<<std::left<<setw(12)
<<cls.elem[i].name<<std::left<<setw(12)<<cls.elem[i].score
<<std::left<<setw(12)<<cls.elem[i].teacher<<endl;
return OK;
}
}
cout<<"未查找到此课程,请输入正确的课程号!"<<endl;
return ERROR;
}
Status SearchSco( Score &sco )
{
int i, m, n, a;
cout<<"请分别输入学号和课程号:"<<endl;
cin>>m>>n;
for( i = 0; i < sco.length; i++ )
{
if( sco.elem[i].snum == m )
{
for( a = 0; a < sco.length; a++ )
{
if( sco.elem[i].cnum == n )
{
cout<<"查找到成绩为:"<<endl;
cout<<"学号 课程号 分数 学期数 "<<endl;
cout<<std::left<<setw(12)<<sco.elem[i].snum<<std::left<<setw(12)
<<sco.elem[i].cnum<<std::left<<setw(12)<<sco.elem[i].score
<<std::left<<setw(12)<<sco.elem[i].trem<<endl;
return OK;
}
}
}
}
cout<<"未查找到此成绩,请输入正确的学号和课程号!"<<endl;
return ERROR;
}
Status SearchMenu( Std &stu, Class &cls, Score &sco )
{
int i;
cout<<endl;
cout<<"1.查询学生信息 || 2.查询课程信息 "<<endl;
cout<<"3.查询成绩信息 || 4.取消 "<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>i;
switch( i )
{
case 1:
SearchStd( stu );
break;
case 2:
SearchCls( cls );
break;
case 3:
SearchSco( sco );
break;
case 4:
cout<<"取消成功!"<<endl;
break;
default :
cout<<"输入有误"<<endl;
break;
}
return OK;
}
Status ModifyStd( Std &stu )
{
int n, i;
cout<<"请输入学生的学号"<<endl;
cin>>n;
for( i = 0; i < stu.length; i++ )
{
if( stu.elem[i].num == n )
{
cout<<"查找到学号为"<<n<<"的学生信息为;"<<endl;
cout<<"学号 姓名 班级 生日 QQ "<<endl;
cout<<std::left<<setw(12)<<stu.elem[i].num<<std::left<<setw(12)<<stu.elem[i].name
<<std::left<<setw(12)<<stu.elem[i].clas<<std::left<<setw(12)<<stu.elem[i].birth
<<std::left<<setw(12)<<stu.elem[i].QQ<<endl;
cout<<"请重新输入该学生的信息"<<endl;
cout<<"学号 姓名 班级 生日 QQ "<<endl;
cin>>stu.elem[i].num>>stu.elem[i].name>>stu.elem[i].clas
>>stu.elem[i].birth>>stu.elem[i].QQ;
cout<<"修改成功!"<<endl;
return OK;
}
cout<<"未查找到此学生,请输入正确的学号!"<<endl;
return ERROR;
}
}
Status ModifyCls( Class &cls )
{
int n, i;
cout<<"请输入课程的课程号"<<endl;
cin>>n;
for( i = 0; i < cls.length; i++ )
{
if( cls.elem[i].num == n )
{
cout<<"查找到课程号为"<<n<<"的课程信息为;"<<endl;
cout<<"课程号 课程名 学分 任课教师 "<<endl;
cout<<std::left<<setw(12)<<cls.elem[i].num<<std::left<<setw(12)
<<cls.elem[i].name<<std::left<<setw(12)<<cls.elem[i].score
<<std::left<<setw(12)<<cls.elem[i].teacher<<endl;
cout<<"请重新输入该课程的信息"<<endl;
cout<<"课程号 课程名 学分 任课教师 "<<endl;
cin>>cls.elem[i].num>>cls.elem[i].name>>cls.elem[i].score
>>cls.elem[i].teacher;
cout<<"修改成功!"<<endl;
return OK;
}
cout<<"未查找到此课程,请输入正确的课程号!"<<endl;
return ERROR;
}
}
Status ModifySco( Score &sco )
{
int n, m, i, a;
cout<<"请输入学生的学号和课程号"<<endl;
cin>>m>>n;
for( i = 0; i < sco.length; i++ )
{
if( sco.elem[i].snum == m )
{
for( a = 0; a < sco.length; a++ )
{
if( sco.elem[i].cnum == n )
{
cout<<"查找到成绩为:"<<endl;
cout<<"学号 课程号 分数 学期数 "<<endl;
cout<<std::left<<setw(12)<<sco.elem[i].snum<<std::left<<setw(12)
<<sco.elem[i].cnum<<std::left<<setw(12)<<sco.elem[i].score
<<std::left<<setw(12)<<sco.elem[i].trem<<endl;
cout<<"请重新输入成绩信息"<<endl;
cout<<"学号 课程号 分数 学期数 "<<endl;
cin>>sco.elem[i].snum>>sco.elem[i].cnum
>>sco.elem[i].score>>sco.elem[i].trem;
cout<<"修改成功!"<<endl;
return OK;
}
}
}
}
cout<<"未查找到此成绩,请输入正确的学号和课程号!"<<endl;
return ERROR;
}
Status ModifyMenu( Std &stu, Class &cls, Score &sco )
{
int i;
cout<<endl;
cout<<"1.修改学生信息 || 2.修改课程信息 "<<endl;
cout<<"3.修改成绩信息 || 4.取消 "<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>i;
switch( i )
{
case 1:
ModifyStd( stu );
break;
case 2:
ModifyCls( cls );
break;
case 3:
ModifySco( sco );
break;
case 4:
cout<<"取消成功!"<<endl;
break;
default :
cout<<"输入有误"<<endl;
break;
}
return OK;
}
Status DeleteStd( Std &stu )
{
int i, n, a;
char choice;
cout<<"请输入要删除的学生的学号:"<<endl;
cin>>n;
for( i = 0; i < stu.length; i++ )
{
if( stu.elem[i].num == n )
{
cout<<"查找到学号为"<<n<<"的学生信息为:"<<endl;
cout<<"学号 姓名 班级 生日 QQ "<<endl;
cout<<std::left<<setw(12)<<stu.elem[i].num<<std::left<<setw(12)<<stu.elem[i].name
<<std::left<<setw(12)<<stu.elem[i].clas<<std::left<<setw(12)<<stu.elem[i].birth
<<std::left<<setw(12)<<stu.elem[i].QQ<<endl;
cout<<"确定要删除该学生的信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
for( a = i; a < stu.length; a++ )
{
stu.elem[a].num = stu.elem[a+1].num;
strcpy( stu.elem[a].name, stu.elem[a+1].name );
strcpy( stu.elem[a].clas, stu.elem[a+1].clas );
strcpy( stu.elem[a].birth, stu.elem[a+1].birth );
stu.elem[a].QQ = stu.elem[a+1].QQ;
}
stu.length--;
cout<<"已成功删除"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"您取消了删除"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
}
cout<<"未查找到此学生,请输入正确的学号!"<<endl;
return ERROR;
}
}
Status DeleteCls( Class &cls )
{
int i, n, a;
char choice;
cout<<"请输入要删除课程的课程号:"<<endl;
cin>>n;
for( i = 0; i < cls.length; i++ )
{
if( cls.elem[i].num == n )
{
cout<<"查找到课程号为"<<n<<"的课程信息为:"<<endl;
cout<<"课程号 课程名 学分 任课教师 "<<endl;
cout<<std::left<<setw(12)<<cls.elem[i].num<<std::left<<setw(12)
<<cls.elem[i].name<<std::left<<setw(12)<<cls.elem[i].score
<<std::left<<setw(12)<<cls.elem[i].teacher<<endl;
cout<<"确定要删除该课程的信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
for( a = i; a < cls.length; a++ )
{
cls.elem[a].num = cls.elem[a+1].num;
strcpy( cls.elem[a].name, cls.elem[a+1].name );
strcpy( cls.elem[a].teacher, cls.elem[a+1].teacher );
cls.elem[a].score = cls.elem[a+1].score;
}
cls.length--;
cout<<"已成功删除"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"您取消了删除"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
}
cout<<"未查找到此课程,请输入正确的课程号!"<<endl;
return ERROR;
}
}
Status DeleteSco( Score &sco )
{
int i, n, m, a;
char choice;
cout<<"请分别输入学号和课程号:"<<endl;
cin>>m>>n;
for( i = 0; i < sco.length; i++ )
{
if( sco.elem[i].snum == m )
{
for( a = 0; a < sco.length; a++ )
{
if( sco.elem[i].cnum == n )
{
cout<<"查找到成绩为:"<<endl;
cout<<"学号 课程号 分数 学期数 "<<endl;
cout<<std::left<<setw(12)<<sco.elem[i].snum<<std::left<<setw(12)
<<sco.elem[i].cnum<<std::left<<setw(12)<<sco.elem[i].score
<<std::left<<setw(12)<<sco.elem[i].trem<<endl;
cout<<"确定要删除该成绩信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
for( a = i; a < sco.length; a++ )
{
sco.elem[a].snum = sco.elem[a+1].snum;
sco.elem[a].cnum = sco.elem[a+1].cnum;
sco.elem[a].score = sco.elem[a+1].score;
sco.elem[a].trem = sco.elem[a+1].trem;
}
sco.length--;
cout<<"已成功删除"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"您取消了删除"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
}
}
}
}
cout<<"未查找到此成绩,请输入正确的学号!"<<endl;
return ERROR;
}
Status DeleteMenu( Std &stu, Class &cls, Score &sco )
{
int i;
cout<<endl;
cout<<"1.删除学生信息 || 2.删除课程信息 "<<endl;
cout<<"3.删除成绩信息 || 4.取消 "<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>i;
switch( i )
{
case 1:
DeleteStd( stu );
break;
case 2:
DeleteCls( cls );
break;
case 3:
DeleteSco( sco );
break;
case 4:
cout<<"取消成功!"<<endl;
break;
default :
cout<<"输入有误"<<endl;
break;
}
return OK;
}
Status SaveStd( Std &stu )
{
FILE *fp;
int i;
fp = fopen( "student.txt","w" );
for( i = 0; i< stu.length; i++ )
{
fprintf( fp, "%-12d%-12s%-12s%-12s%-12d\n", stu.elem[i].num, stu.elem[i].name
, stu.elem[i].clas, stu.elem[i].birth, stu.elem[i].QQ );
}
fclose(fp);
return OK;
}
Status SaveCls( Class &cls )
{
FILE *fp;
int i;
fp = fopen( "class.txt","w" );
for( i = 0; i< cls.length; i++ )
{
fprintf( fp, "%-12d%-16s%-12f%-12s%\n",cls.elem[i].num, cls.elem[i].name
,cls.elem[i].score, cls.elem[i].teacher );
}
fclose(fp);
return OK;
}
Status SaveSco( Score &sco )
{
FILE *fp;
int i;
fp = fopen( "score.txt","w" );
for( i = 0; i< sco.length; i++ )
{
fprintf( fp, "%-12d%-12d%-12f%-12d\n", sco.elem[i].snum, sco.elem[i].cnum
, sco.elem[i].score, sco.elem[i].trem );
}
fclose(fp);
return OK;
}
Status SaveMenu( Std &stu, Class &cls, Score &sco )
{
int i;
char choice;
cout<<endl;
cout<<"1.保存学生信息 || 2.保存课程信息 "<<endl;
cout<<"3.保存成绩信息 || 4.保存全部信息 "<<endl;
cout<<"5.取消保存 || "<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>i;
switch( i )
{
case 1:
cout<<"你所要保存的学生信息为:"<<endl;
OutputStd( stu );
cout<<"你确定要保存学生信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
SaveStd( stu );
cout<<"保存成功!"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"取消成功!"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
break;
case 2:
cout<<"你所要保存的课程信息为:"<<endl;
OutputCls( cls );
cout<<"你确定要保存课程信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
SaveCls( cls );
cout<<"保存成功!"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"取消成功!"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
break;
case 3:
cout<<"你所要保存的成绩信息为:"<<endl;
OutputSco( sco );
cout<<"你确定要保存成绩信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
SaveSco( sco );
cout<<"保存成功!"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"取消成功!"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
break;
case 4:
cout<<"你所要保存的学生信息,课程信息,成绩信息分别为:"<<endl;
OutputStd( stu );
cout<<endl;
OutputCls( cls );
cout<<endl;
OutputSco( sco );
cout<<"你确定要所有信息吗?(y/n)"<<endl;
cin>>choice;
if( choice == 'y' )
{
SaveStd( stu );
SaveCls( cls );
SaveSco( sco );
cout<<"保存成功!"<<endl;
return OK;
}
else if( choice =='n' )
{
cout<<"取消成功!"<<endl;
return OK;
}
else
{
cout<<"请输入正确的选项!"<<endl;
return ERROR;
}
break;
case 5:
cout<<"取消成功!"<<endl;
break;
default :
cout<<"输入有误"<<endl;
break;
}
return OK;
}
Status SearchOsco( Score &sco , Class &cls , Std &stu )
{
int i, n, m, a, b;
cout<<"请输入学生的学号:"<<endl;
cin>>n;
for( i = 0; i < sco.length; i++ )
{
if( sco.elem[i].snum == n )
{
for( m = 0; m < stu.length; m++ )
{
if( stu.elem[m].num = n )
{
b = m;
break;
}
}
for( a = 0; a < cls.length; a++ )
{
if( sco.elem[i].cnum == cls.elem[a].num )
{
cout<<std::left<<setw(12)<<stu.elem[b].name<<std::left<<setw(12)
<<cls.elem[a].name<<std::left<<setw(12)<<sco.elem[i].score<<endl;
}
}
}
}
}
Status SearchCSco( Std &stu, Class &cls, Score &sco )
{
int i, n, m, a, b;
cout<<"请输入课程号"<<endl;
cin>>n;
for( m = 0; m < sco.length; m++ )
{
for( i = 0; i < cls.length; i++ )
{
if( cls.elem[i].num == n )
a = i;
}
if( sco.elem[m].cnum == n )
{
for( b = 0; b < stu.length; b++ )
{
if( stu.elem[b].num == sco.elem[m].snum )
cout<<std::left<<setw(12)<<cls.elem[a].name<<std::left<<setw(12)
<<stu.elem[b].name<<std::left<<setw(12)<<sco.elem[m].score<<endl;
}
}
}
return OK;
}
Status Searchlsco( Std &stu, Class &cls, Score &sco , char *clas )
{
int i, n, a, b, m, x;
cout<<"请输入班级名和课程号"<<endl;
cin>>clas>>m;
for( i = 0; i < sco.length; i++ )
{
for( n = 0; n < stu.length; n++ )
{
if( strcmp( stu.elem[n].clas, clas ) == 0 )
{
for( b = 0; b < sco.length; b++ )
{
if( sco.elem[b].snum == stu.elem[n].num && sco.elem[b].cnum == m )
{
for( x = 0; x < cls.length; x++ )
{
if( cls.elem[x].num == m )
{
cout<<std::left<<setw(12)<<clas<<std::left<<setw(12)
<<cls.elem[x].name<<std::left<<setw(12)<<stu.elem[n].name
<<std::left<<setw(12)<<sco.elem[b].score<<endl;
}
}
}
}
}
}
break;
}
return OK;
}
可供初学者学习,做得不好的地方望指正。