新微赢技术网

标题: 求助!C++的学生信息管理系统?? [打印本页]

作者: 释放压力    时间: 2009-11-4 00:04
标题: 求助!C++的学生信息管理系统??
要求:
1。建立学生信息数组,从文件中读入数据(已完成)
2。按姓名递增冒泡排序
3。按总分递减选择排序
4。显示按总分排序后学生信息
5。按学号查找学生平均分(已完成,但运行起来有问题)
6。按姓名查学生信息和平均分数(已完成,但运行起来有问题)
7。查各门课平均分
8。查男女生人数和总人数
9。显示学生信息表(已完成,但修改数据后不能完全显示)
10。将排序后的内容存为文件,以后显示时直接从文件中调出而不用再执行排序算法
那位大侠帮帮忙阿!!小妹不胜感激!!!
已编程序如下:

#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//定义结构体
struct date{
int year;
int month;
int day;
};
struct studentData
{
char stuNum[9];
char name[20];
char sex[3];
int age;
struct date birth;
float score[4];
};
float add(float a,float b,float c)
{
float z;
z=((a+b+c)/3);
return z;
}
//////////////////////
char *pFileName = 0;
//函数名英语字母顺序排列
void addRecord(fstream &);
void createNewFile();
void bsort(struct studentData pname[],int n);//按姓名递增冒泡程序
void ssort(struct studentData ptscore[],int n);//总分递减选择排序
void ssrch_stuNum(char *);//按学号查找和平均分数
void bsrch_name(char *);//按姓名查找和平均分
void find_ave(struct studentData*pstud,int n);//查各科均分
void find_pnum(struct studentData*pstud,int n);//查男女生总人数
void deleteRecord(char *);
void displayAllRecord(char *);
void findRecord(char *);
void functionMenu();
void initial(fstream &);
void insertRecord(char *);
void mainMenu();
void menu();
void operateExitedFile();
void updateRecord(char *);
///////////////////////////////////////////////////////////
//主函数
void main()
{
int recev;
studentData student();
mainMenu();
cout<<"请输入您的选择(0~2): ";
cin>>recev;
do
{
switch(recev)
{
case 0:
cout<<"\t\t\t谢谢使用!再见!"<<endl;
exit(1);
case 1:
createNewFile();
mainMenu();
break;
case 2:
operateExitedFile();
mainMenu();
break;
default:
cout<<"您输入的选项不存在,请查证后再选择!"<<endl;
}
cout<<"请输入您的选择(0~2): ";
}while(cin>>recev);
}
///////////////////////////////////////////////
///////////  函数区    //////////////////
//添加纪录函数
void addRecord(fstream &fInOut)
{
studentData student;
cout<<"请输入学号: ";
cin>>student.stuNum;
cout<<"请输入姓名: ";
cin>>student.name;
cout<<"请输入性别(m男§f女): ";
cin>>student.sex;
cout<<"请输入年龄(15~50): ";
cin>>student.age;
while(student.age <= 15 || student.age >= 50)
{
cout<<"年龄超出限定范围,请重新输入!"<<endl;
cout<<"请输入年龄(15~50): ";
cin>>student.age;
}
cout<<"请输入出生年: ";
cin>>student.birth.year;
cout<<"月:";
cin>>student.birth.month;
cout<<"日:";
cin>>student.birth.day;
cout<<"请输入数学,英语,C语言成绩: ";
for (int j=0;j<3;j++)
{
float m;
cin>>student.score[j];
m=add(student.score[0],student.score[1],student.score[2]);
student.score[3]=m;
}
cout<<"该学生的平均分数为:"<< student.score[3]<<endl;
fInOut.seekp( (atoi(student.stuNum) - 1) *sizeof(studentData) );
fInOut.write( reinterpret_cast<const char *>(&student),
sizeof(studentData) );
}
//创建新文件函数
void createNewFile()
{
cout<<"请输入文件全名(如copy.dat): ";
char fileName[20];
cin>>fileName;
fstream fout(fileName, ios::out |ios::ate);
initial(fout);//初始化文件记录
//打开FileName.dat文件
ofstream outPutFileName("FileName.dat", ios::out | ios::app);
if(!outPutFileName)
{
cerr<<"file could not be opend.\n";
exit(1);
}
outPutFileName<<fileName<<endl;//把新建文件的文件名写入FileName.dat
cout<<"开始输入数据 "<<endl;
int yn;
do
{
addRecord(fout);
cout<<"要继续输入数据吗?(1 yes|0 no)"<<endl;
cin>>yn;
}while(yn);
fout.close();
cout<<"您输入的数据已保存"<<endl;
//接下来判断是否对它进行操作(有待添加这些功能)
}
//删除记录函数
void deleteRecord(char *pFileName)
{
studentData student;
int sn;
cout<<"输入您想删除的记录号:"<<endl;
cin>>sn;
fstream fInOut(pFileName,ios::in | ios::out );
if(sn != 0 )
{
studentData blankRecord = {0," ",' ',0," "," "};
fInOut.seekp( (sn - 1)*sizeof(studentData) );
fInOut.write( reinterpret_cast<const char *>(&student),sizeof(studentData) );
cout<< "学生 #" << sn <<"已被删除"<<endl;
}
else
cout<< "学生 #" << sn <<"是空记录"<<endl;
fInOut.close();
}
//显示所有记录函数
void displayAllRecord(char *pFileName)
{
studentData student;
ifstream output(pFileName,ios::in ); // |ios::out
output.read(reinterpret_cast< char * >(&student),sizeof(studentData));
cout <<setiosflags(ios::left)<<setw(10)<<"学号"
<<setw(20)<<"姓名"<<setw(3)<<"性别"<<setw(6)<<"年龄"
<<setw(3)<<"生日年"<<setw(3)<<"月"<<setw(3)<<"日"<<setw(10)<<"数学"<<setw(10)<<"英语"<<setw(10)<<"C语言"<<endl;
while(output && !output.eof())
{
if((atoi(student.stuNum)) != 0)
cout <<setiosflags(ios::left)<<setw(10)<< student.stuNum
<<setw(20)<< student.name<<setw(3)<< student.sex<<setw(6)<< student.age
<<setw(7)<< student.birth.year<<setw(3)<<student.birth.month<<setw(3)<<student.birth.day<<setw(10)<<student.score[0]<<setw(10)<<student.score[1]<<setw(10)<<student.score[2]<<endl;
output.read(reinterpret_cast< char *>(&student),sizeof(studentData));
}
output.close();
}
//查找记录函数按学号查平均分
void findRecord(char *pFileName)
{
cout<<"请输入您要查找的记录的学号: ";
studentData student;
cin>>student.stuNum;
fstream fInOut(pFileName,ios::in );
fInOut.seekg( (strlen(student.stuNum - 1)*sizeof(studentData) ));
fInOut.read( reinterpret_cast< char *>(&student),sizeof(studentData) );
cout <<setiosflags(ios::left)<<setw(10)<<"学号"<<setw(20)<<"姓名"<<
setw(10)<<"数学"<<setw(10)<<"英语"<<setw(10)<<"C语言"<<setw(10)<<"平均"<<endl;
cout <<setiosflags(ios::left)<<setw(10)<< student.stuNum
<<setw(20)<< student.name
<<setw(10)<< student.score[0]<<setw(10)<<student.score[1]<<setw(10)<<student.score[2]<<setw(10)<<student.score[3]<<endl;
}
void bsrch_name(char *pFileName)//按姓名查找和平均分数
{
cout<<"请输入您要查找的记录的姓名: ";
studentData student;
cin>>student.name;
fstream fInOut(pFileName,ios::in );
fInOut.seekg( (strlen(student.stuNum-1)*sizeof(studentData) ));
fInOut.read( reinterpret_cast< char *>(&student),sizeof(studentData) );
cout <<setiosflags(ios::left)<<setw(10)<<"学号"
<<setw(20)<<"姓名"<<setw(10)<<"平均分"<<endl;
cout <<setiosflags(ios::left)<<setw(10)<< student.stuNum
<<setw(20)<< student.name<<setw(10)<<student.score[3]<<endl;
}

//功能菜单(选择"2.查看已有文件"后调用)
void functionMenu()
{
cout<<"现在操作的文件名是:";
cout<<pFileName<<endl;
menu();
cout<<"请输入您要的操作号: ";
int choice;
cin>>choice;
do{
switch(choice)
{
case 1:
displayAllRecord(pFileName);
cout<<endl;
menu();
cout<<"请输入您要的操作号: ";
cin>>choice;
break;
case 2:
insertRecord(pFileName);
cout<<endl;
menu();
cout<<"请输入您要的操作号: ";
cin>>choice;
break;
case 3:
updateRecord(pFileName);
cout<<endl;
menu();
cout<<"请输入您要的操作号: ";
cin>>choice;
break;
case 4:
deleteRecord(pFileName);
cout<<endl;
menu();
cout<<"请输入您要的操作号: ";
cin>>choice;
break;
case 5:
findRecord(pFileName);
cout<<endl;
menu();
cout<<"请输入您要的操作号: ";
cin>>choice;
break;
case 6:
bsrch_name(pFileName);
cout<<endl;
menu();
cout<<"请输入您要的操作号: ";
cin>>choice;
break;

case 0:
break;
default:
{
cout<<"您输入的选项不存在,请查证后再选择!"<<endl;
cout<<"请输入您要的操作号: ";
cin>>choice;
}
}//end of switch
}while(choice >= 0 && choice <= 5);
}
////////////////////
void initial(fstream &iniFile)
{
studentData student = {0,"\0",'m',0,"\0","\0"};//
for(int i = 0; i < 100; i++)
iniFile.write(reinterpret_cast<const char *>(&student),sizeof(studentData));
}
//插入数据函数
void insertRecord(char *pFileName)
{
fstream fInOut(pFileName,ios::out | ios::ate);
if(!fInOut)
{
cerr<<"file could not be opend.\n";
exit(1);
}
int yn;
do
{
addRecord(fInOut);
cout<<"要继续添加数据吗?(1 yes|0 no)"<<endl;
cin>>yn;
}while(yn);
fInOut.close();
}
//显示主菜单函数
void mainMenu()
{
cout<<"\t\t\t*****************************\n";
cout<<"\t\t\t* 学 生 信 息 管 理 *\n";
cout<<"\t\t\t*****************************\n";
cout<<"\t\t\t\t1.创建新文件\n\n\t\t\t\t2.查看已有文件\n\n\t\t\t\t0.退出系统"<<endl;
}
//功能菜单前显示的菜单函数
void menu()
{
cout <<"\t\t\t\t1.显示全部记录\n\n\t\t\t\t2.添加纪录\n\n\t\t\t\t3.修改记录"
<<"\n\n\t\t\t\t4.删除记录\n\n\t\t\t\t5.查找记录\n\n\t\t\t\t6.按姓名查平均分\n\n\t\t\t\t0.返回上级菜单"<<endl;
}
//查看已有文件函数
void operateExitedFile()
{
cout<<"已存在的文件名:"<<endl;
ifstream inPutFileName("FileName.dat",ios::in);
char fileNameA[20];
while(inPutFileName>>fileNameA)
{
cout<<fileNameA<<endl;
}
inPutFileName.close();
cout<<endl<<"请输入您要操作的文件全名: ";
char fileName[20];
cin>>fileName;
pFileName = fileName;//指针指向要操作的文件名,以备传递
fstream fInOut(fileName,ios::in | ios::out | ios::app);
if(!fInOut)
{
cerr<<"文件打不开!\n";
exit(1);
}
fInOut.close();
functionMenu();
}
//修改记录函数
void updateRecord(char *pFileName)
{
fstream fInOut(pFileName,ios::out | ios::ate);
cout<<"请输入您要修改的记录的学号"<<endl;
addRecord(fInOut);
fInOut.close();
}




欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2