从开学到现在,老师叫我们用C++编写了几个程序,现在贴一个上来,是关于学生信息管理的。其中代码的“骨架”是从网上找的,自己也加工了下,里面也有很多自己的心血,主要是增加了输入数据的合法性,还有变得更加人性化吧。代码如下:
# include <iostream>
# include <string.h>
# include <stdio.h>
# include <stdlib.h>
# include <fstream>
# include <conio.h>
using namespace std;
//*****定义一个学生原子的的数据结构*****//
struct stuatom
{
char *name;
int id;
char sex;
float math, eng, comp, totll, aver;
void show();
void setup();
};
//*********定义一系列对学生的操作**********//
class student
{
private:
stuatom ob[100];
int stulen;
public:
student();
void input();
void order();
void save();
void check();
void clean();
void query();
void read();
void add();
void del();
void modify();
};
//********对学生数据的初始化(类的构造函数)**********//
student::student()
{
//用for循环对全部数组中的数据初始化
for(int i=0;i <100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =NULL;
}
this->stulen =0;
}
//********输入学生的数据,并判断是否在规定数据域内*******//
void stuatom::setup()
{
char n[20]; char s;
int b;
//如果输入学号在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
cout<<" 学号(1001与1020之间): ";
cin>>b;
if(b>1020||b <1001)
cout<<" 数据有误!!重新输入.."<<endl<<endl;
}while (b <1001||b>1020);
id=b;
//如果输入姓名在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
name=new char[strlen(n)+1];
cout<<" 姓名: ";
cin>>n;
/*if( strlen(n)>6 || strlen(n) <4 )
cout<<"Bad data input!!" <<endl <<endl; */
}while ( strlen(n)>6 && strlen(n) <4 ); //等于没判断
strcpy(name,n);
cout<<" 性别(m/f):" ;
cin>>s;
//如果输入性别在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (s!='m' && s!='f')
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" 性别(m/f):";
cin>>s;
}
sex=s;
float m, e, co;
cout<<" 数学(0-100): ";
cin>>m;
//如果输入成绩在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (m <0 || m>100)
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" 数学(0-100): ";
cin>>m;
}
math=m;
cout<<" 英语(0-100): ";
cin>>e;
while (e <0 || e>100)
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" 英语(0-100): ";
cin>>e;
}
eng=e;
cout<<" C++(0-100): ";
cin>>co;
while (co <0 || co>100)
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" C++(0-100): ";
cin>>co;
}
comp=co;
totll=math+eng+comp;
aver=(math+eng+comp)/3;
}
//*******按照规定格式把该学生的数据显示在屏幕上******//
void stuatom::show()
{
cout.setf(ios::left);
cout.width(6);
cout<<""<<id<<" ";
cout.width(8);
cout<<name;
cout.width(10);
cout<<sex;
cout.width(9);
cout<<math;
cout.width(9);
cout<<eng;
cout.width(11);
cout<<comp;
cout.width(10);
cout<<totll<<aver<<endl;
}
//**************输入学生的数据***********************//
void student::input()
{
int n;
cout <<endl <<" 输入将要录入的学生数目: ";
cin>>n;
int j,i;
//通过循环输入要求输入学生个数的学生的数据。
for(j=(this->stulen+1) ; j <=(this->stulen+n); j++)
{
cout <<" 输入学生信息 " <<j <<endl;
ob[j-1].setup();
}
//检查学号是否有重复
int a[100];
int b=0,c=0,d;
char Y;
if(this->stulen!=0)
{
for (i=0;i<this->stulen ;i++)
{
for(j=this->stulen ;j<(this->stulen+n);j++)
{
if (ob[i].id==ob[j].id)
{
if (c==0) //c只是为了美观而已。。
{
cout<<" 错误!系统检测到第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同/n";
a[b]=j;
b++;c++;
}
else
{
cout<<" 第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同/n";
a[b]=j;
b++;c++;
}
}
}
}
for(c=0;c<b;c++)
{
cout << " 是否重新输入第" << (a[c]+1)<< "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
cin>>Y;
while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
{
cout<<" 无效的命令,请重新输入../n";
cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
cin>>Y;
}
if(Y=='y'||Y=='Y')
{
d=a[c];
ob[d].setup();
}
else
{
n--;
for(d=a[c];d<(this->stulen+n);d++)
ob[d]=ob[d+1];
for(d=(c+1);d<b;d++)
a[d]--;
}
}
}
else
{
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(ob[i].id==ob[j].id)
{
if (c==0)
{
cout<<" 错误!系统检测到第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同/n";
a[b]=j;
b++;c++;
}
else
{
cout<<" 第"<<(j+1)<<"位学生学号与第"<<(i+1)<<"位学生学号相同/n";
a[b]=j;
b++;c++;
}
}
}
}
for(c=0;c<b;c++)
{
cout << " 是否重新输入第" << (a[c]+1) << "位学生成绩?(Y/N--选N则第" << (a[c]+1)<<"位学生成绩将不保存)";
cin>>Y;
while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
{
cout<<" 无效的命令,请重新输入../n";
cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(Y/N--选N则第"<<(a[c]+1)<<"位学生成绩将不保存)";
cin>>Y;
}
if(Y=='y'||Y=='Y')
{
d=a[c];
ob[d].setup();
}
else
{
n--;
for(d=a[c];d<n;d++)
ob[d]=ob[d+1];
for(d=(c+1);d<b;d++)
a[d]--;
}
}
}
//学生数据显示格式
system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
//通过循环输出所有学生数据。
for(j=this->stulen ; j <(this->stulen+n); j++)
{
ob[j].show();
}
this->stulen+=n; //学生个数赋值
/*cout <<endl;
cout <<" 是否保存? (Y/N): ";
char Y;
cin>>Y;
system("cls"); */
}
//***********把学生成绩排序******************//
void student::query()
{
int x;
cout <<endl <<" 输入要查询学生的学号: ";
cin>>x;
int i;
for(i=0;i <this->stulen ;i++)
{
if (x==ob[i].id)
break;
/*{
cout <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
cout.setf(ios::left);
cout.width(6);
cout <<"" <<ob[i].id <<" ";
cout.width(8);
cout <<ob[i].name;
cout.width(10);
cout <<ob[i].sex;
cout.width(9);
cout <<ob[i].math;
cout.width(9);
cout <<ob[i].eng;
cout.width(11);
cout <<ob[i].comp;
cout.width(10);
cout <<ob[i].totll <<ob[i].aver <<endl;
} */
}
if(i<this->stulen )
{
cout <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
cout.setf(ios::left);
cout.width(6);
cout <<"" <<ob[i].id <<" ";
cout.width(8);
cout <<ob[i].name;
cout.width(10);
cout <<ob[i].sex;
cout.width(9);
cout <<ob[i].math;
cout.width(9);
cout <<ob[i].eng;
cout.width(11);
cout <<ob[i].comp;
cout.width(10);
cout <<ob[i].totll <<ob[i].aver <<endl;
}
else
cout<<" 没有找到该学生成绩...";
_getch();
}
//*******************保存学生数据**************************//
void student::save()
{
int i;
ofstream outfile;
outfile.open("list.txt",ios::trunc);
if(!outfile)
{
cout <<" 无法打开数据文件!/n";
}
//通过循环把所有的学生数据保存在list.txt里边。
for(i=0; i <this->stulen; i++)
{
outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
ob[i].math <<" " <<ob[i].eng <<" " <<ob[i].comp <<" " <<ob[i].totll <<" " <<ob[i].aver <<endl;
}
outfile.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
}
//********重新初始化********//
void student::clean()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in);
if(!infile)
{
for(int i=(this->stulen-1);i <100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =NULL;
}
this->stulen=0;
ifstream infile1;
infile1.open("stulen.txt",ios::in);
infile1>>this->stulen ;
infile1.close();
}
else
{
ifstream infile1;
infile1.open("stulen.txt",ios::in);
infile1>>this->stulen ;
infile1.close();
for(i=0;i<this->stulen ;i++)
{
infile>>ob[i].id;
ob[i].name =new char[strlen(b)+1];
infile>>b;
strcpy(ob[i].name,b);
infile>>ob[i].sex ;
infile>>ob[i].math ;
infile>>ob[i].eng;
infile>>ob[i].comp;
infile>>ob[i].totll;
infile>>ob[i].aver ;
}
infile.close ();
}
}
//*************显示所有学生数据*********************//
void student::read()
{
int i;
float averm,avere,averc;
averm=avere=averc=0;
//system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
for(i=0; i <this->stulen; i++)
{
ob[i].show();
}
cout <<"/n/n/n";
for(i=0; i <this->stulen; i++)
{
averm +=ob[i].math;
}
if(i!=0)
cout <<" 数学平均成绩为" <<averm/i <<endl;
/*else
cout <<" 数学平均成绩为0" <<endl;*/
for(i=0; i <this->stulen; i++)
{
avere +=ob[i].eng;
}
if(i!=0)
cout <<" 英语平均成绩为" <<avere/i <<endl;
/*else
cout <<" 英语平均成绩为0" <<endl;*/
for(i=0; i <this->stulen; i++)
{
averc +=ob[i].comp;
}
if(i!=0)
cout <<" C++平均成绩为" <<averc/i;
/*else
cout <<" C++平均成绩为0" <<endl;*/
_getch();
}
//*******************一个学生的数据****************//
void student::add()
{
int i, d=this->stulen ;
int c=0;
cout <<" 输入要添加学生的信息:" <<endl;
do
{
cout<<"/n";
ob[d].setup();
for(i=0;i<d;i++)
{
if(ob[i].id==ob[d].id )
{
cout<<" 系统检测到此次输入的学号和已有学生学号相同,请核实再输..";
_getch();
ob[d].math=ob[d].eng=ob[d].comp =ob[d].totll =ob[d].aver =0;
ob[d].id =0;
ob[d].sex =' ';
ob[d].name =NULL;
c=1;
break;
}
else c=0;
}
}while(c!=0);
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
for(i=0; i <=d; i++)
{
ob[i].show();
}
this->stulen++;
ofstream fout("list.txt",ios::app);
if(!fout)
{
cout <<" 无法打开数据文件!/n,";
}
//把添加的学生数据添加到list.txt里边去。
fout <<ob[d].id <<" " <<ob[d].name <<" " <<ob[d].sex <<" " <<
ob[d].math <<" " <<ob[d].eng <<" " <<ob[d].comp <<" " <<ob[d].totll <<" " <<ob[d].aver <<endl;
fout.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
_getch();
}
//*********************删除指定名字学生的数据*******************//
void student::del()
{
int i,p;
int x;
p=-1;
if(this->stulen==0)
{
cout<<" 目前没有学生成绩!";
_getch();
}
else
{
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
for(i=0; i <this->stulen; i++)
{
ob[i].show();
}
char a;
cout <<"/n/n 输入要删除的学生学号:" ;
cin>>x;
//通过for循环查找要删除学生的学号
for(i=0; i <stulen; i++)
{
if(x==ob[i].id )
{
p=i;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
cout <<" " <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<ob[i].math <<" " <<ob[i].eng <<" " <<ob[i].comp <<" " <<ob[i].totll <<" " <<ob[i].aver <<endl;
break;
}
else
continue;
}
if(p==-1)
{
cout<<" 没有此学生成绩!";
_getch();
}
else
{
cout<<" 确定删除此学生成绩?<Y/N>";
cin>> a;
if (a=='y'|| a=='Y')
{
cout <<" 删除成功!" <<endl;
/*cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl; */
//删除了之后,应该把后面的数据往前移,把要删除的数据覆盖,并且学生长度减1
stulen--;
for(i;i <stulen;i++)
{
ob[i]=ob[i+1];
}
ofstream outfile;
outfile.open("list.txt",ios::trunc);
for(i=0; i <this->stulen; i++)
{
outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
ob[i].math <<" " <<ob[i].eng <<" " <<ob[i].comp <<" " <<ob[i].totll <<" " <<ob[i].aver <<endl;
}
outfile.close();
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc );
outfile1<<this->stulen ;
outfile1.close();
this->read ();
//_getch();
}
else
{
cout<<" 回到主界面...";
_getch();
}
}
}
}
//********************排序*****************//
void student::order()
{
int k,j;
float t; char n[20];
cout <<" 成绩排名:" <<endl;
cout <<" 姓名 总成绩 名次" <<endl;
//排序算法。
for(j = 0; j <=(stulen-2); j++)
{
for(k=j+1; k <=(stulen-1); k++)
{
if(ob[j].totll < ob[k].totll)
{
t = ob[j].totll;
ob[j].totll = ob[k].totll;
ob[k].totll = t;
strcpy(n, ob[j].name);
strcpy(ob[j].name, ob[k].name);
strcpy(ob[k].name, n);
}
}
}
for(k=0; k <=(stulen-1); k++)
{
cout <<" ";
cout.setf(ios::left);
cout.width(9);
cout <<ob[k].name;
cout.width(9);
cout <<ob[k].totll <<(k+1) <<endl;
}
_getch();
}
//********修改学生信息********//
void student::modify()
{
int a,i,p,b;
p=-1;
system("cls");
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
for(i=0; i <this->stulen; i++)
{
ob[i].show();
}
cout <<"/n/n 请输入要修改学生的学号:";
cin>>a;
for(i=0; i <stulen; i++)
{
if(a==ob[i].id)
{
p=i;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
cout <<" " <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<ob[i].math <<" " <<ob[i].eng <<" " <<ob[i].comp <<" " <<ob[i].totll <<" " <<ob[i].aver <<endl;
break;
}
else
continue;
}
if(p==-1)
{
cout<<" 没有此学生成绩!";
_getch();
}
else
{
cout<<"/n/n 请输入要修改的课程名的编号(1.数学, 2.英语,3.C++):";
cin>>b;
while(b!=1 &&b!=2 &&b!=3)
{
cout<<" 无效输入,请重新输入!/n";
cout<<"/n/n 请输入要修改的课程名的编号(1.数学, 2.英语,3.C++):";
cin>>b;
}
switch(b)
{
case 1:
{
float m;
cout<<" 数学(0-100): ";
cin>>m;
//如果输入成绩在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (m <0 || m>100)
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" 数学(0-100): ";
cin>>m;
}
ob[p].math=m;break;
}
case 2:
{
float e;
cout<<" 英语(0-100): ";
cin>>e;
while (e <0 || e>100)
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" 英语(0-100): ";
cin>>e;
}
ob[p].eng=e; break;
}
case 3:
{
float co;
cout<<" C++(0-100): ";
cin>>co;
while (co <0 || co>100)
{
cout<<" 数据有误!!重新输入.."<<endl<<endl;
cout<<" C++(0-100): ";
cin>>co;
}
ob[p].comp=co;
}
}
ob[p].totll=ob[p].math+ob[p].eng+ob[p].comp;
ob[p].aver=(ob[p].math+ob[p].eng+ob[p].comp)/3;
ofstream outfile;
outfile.open("list.txt",ios::trunc);
for(i=0; i <this->stulen; i++)
{
outfile <<ob[i].id <<" " <<ob[i].name <<" " <<ob[i].sex <<" " <<
ob[i].math <<" " <<ob[i].eng <<" " <<ob[i].comp <<" " <<ob[i].totll <<" " <<ob[i].aver <<endl;
}
outfile.close();
cout<<" 成绩修改成功!回车返回主界面...";
_getch();
}
}
//***************程序运行前先检查以前是否有数据存在*****************//
void student::check()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in );
if(!infile)
{
cout<<" 系统没有检测到以前输入的数据,此次操作将建立一个新的数据文件...";
_getch();
}
else
{
ifstream infile1;
infile1.open("stulen.txt",ios::in);
infile1>>this->stulen ;
infile1.close();
for(i=0;i<this->stulen ;i++)
{
infile>>ob[i].id;
ob[i].name =new char[strlen(b)+1];
infile>>b;
strcpy(ob[i].name,b);
infile>>ob[i].sex ;
infile>>ob[i].math ;
infile>>ob[i].eng;
infile>>ob[i].comp;
infile>>ob[i].totll;
infile>>ob[i].aver ;
}
infile.close ();
//学生数据显示格式
system("cls");
cout<<" 系统检测到以前输入过数据,数据内容如下:/n/n";
cout <<endl <<"----------------------------- 学生信息表 ------------------------------------" <<endl;
cout <<endl <<" 学号 姓名 性别 数学 英语 C++ 总分 平均分" <<endl;
//通过循环输出所有学生数据。
for(i=0; i<this->stulen; i++)
{
ob[i].show();
}
cout<<"/n/n/n";
cout<<" 原始数据是否覆盖?(Y/N)";
char q;
cin>>q;
while(q!='y'&& q!='Y'&& q!='N'&& q!='n')
{
cout<<" 无效的命令,请重新输入../n";
cout<<" 原始数据是否覆盖?(Y/N)";
cin>>q;
}
if(q=='y'||q=='Y')
{
for(int i=0;i <100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =NULL;
}
this->stulen =0;
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc);
outfile1<<this->stulen ;
outfile1.close();
system("del list.txt");
}
/* else*/
}
}
//**************按照一定格式显示所要查询学生的信息。**************//
void menu()
{
cout <<"/n/n";
cout <<"------------------ 学生成绩系统 -----------------" <<endl <<endl;
cout <<"/t1.录入与保存学生成绩:/n";
cout <<"/t2.读取学生成绩:/n";
cout <<"/t3.删除学生成绩:/n";
cout <<"/t4.追加学生成绩:/n";
cout <<"/t5.查询学生成绩:/n";
cout <<"/t6.修改学生成绩:/n";
cout <<"/t7.显示成绩名次./n";
cout <<"/t8.退出系统....../n/n/n";
cout <<"/t请选择要实现的功能: ";
}
//---------------------------------------------------------------------------------------
void main()
{
student a;
a.check();
while(1)
{
int SEL;
system("cls");
menu();
cin>>SEL;
switch(SEL)
{
case 1:
system("cls");
a.input();
cout <<endl;
cout <<" 是否保存? (Y/N): ";
char Y;
cin>>Y;
while(Y!='y'&& Y!='Y'&& Y!='N'&& Y!='n')
{
cout<<" 无效的命令,请重新输入../n";
cout<<" 是否保存? (Y/N)";
cin>>Y;
}
system("cls");
if(Y=='y'||Y=='Y')
a.save();
else/* if(Y=='n'||Y=='N')*/
a.clean();
break;
case 2:
system("cls"); a.read(); break;
case 3:
system("cls"); a.del(); break;
case 4:
system("cls"); a.add();break;
case 5:
system("cls"); a.query();break;
case 6:
system("cls"); a.modify();break;
case 7:
system("cls"); a.order();break;
case 8:
/*cout <<endl <<" 按任意键退出.... ";
_getch(); */
exit(0);
default:
cout <<" 无效的命令!";
_getch();
break;
}
}
}
这段程序中个人感觉最重要的变量是stulen,一切程序的操作都离不开它,也希望有人可以提出更多的问题,让这个程序变得更完美。