📄 0009.cpp
字号:
# include<iostream.h>
# include<string.h>
# include<stdio.h>
# include<stdlib.h>
# include<fstream.h>
const int s=2;
class student{
public:
char *name;
int id;
char sex;
float math, eng, comp, totll, aver;
void setup()
{
char n[20]; char s;
int b;
do { cout<<" 学号: ";
cin>>b;
if(b>1020||b<1001)
cout<<"Bad data input!!"<<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<<"Bad data input!!"<<endl<<endl;
cout<<" 性别(m/f):";
cin>>s;
}
sex=s;
float m, e, co;
cout<<" 数学: ";
cin>>m;
while (m<0 || m>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 数学: ";
cin>>m;
}
math=m;
cout<<" 英语: ";
cin>>e;
while (e<0 || e>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 英语: ";
cin>>e;
}
eng=e;
cout<<" 计算机: ";
cin>>co;
while (co<0 || co>100)
{
cout<<"Bad data input!!"<<endl<<endl;
cout<<" 计算机: ";
cin>>co;
}
comp=co;
totll=math+eng+comp;
aver=(math+eng+comp)/3;
}
void 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 input()
{ int n;
cout<<endl<<"输入将要录入的学生数目: ";
cin>>n;
int j;
for(j=1; j<=n; j++)
{ cout<<" 输入学生信息 "<<j<<endl;
ob[j].setup();
}
int i;
i=n;
system("cls");
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
for(i=1; i<=n; i++)
{
ob[i].show();
}
cout<<endl;
cout<<" 是否保存? (Y/N): ";
char Y;
cin>>Y;
}
//-------------------------------------------------------------------------------------
void Query()
{
int x , i;
cout<<endl<<" 输入要查询学生的学号: ";
cin>>x;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
for(i=1;i<=s;i++)
{ if (x==ob[i].id)
{
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;
}
}
getchar();
}
//---------------------------------------------------------------------------------------
void save()
{ int i;
ofstream fout("list_1.tet");
if(!fout)
{
cout<<"Cannot open output file!\n,";
}
for(i=1; i<=s; i++)
{ fout<<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;
}
fout.close();
}
void read()
{ int i;
system("cls");
cout<<endl<<"----------------------------- 学生信息表 ------------------------------------"<<endl;
cout<<endl<<" 学号 姓名 性别 数学 英语 计算机 总分 平均分"<<endl;
for(i=1; i<=s; i++)
{
ob[i].show();
}
getchar();
}
//-------------------------------------------------------------------------------------
void add()
{
cout<<"输入要添加学生的信息:"<<endl;
cout<<" 学号: ";
}
//--------------------------------------------------------------------------------------
void del()
{
}
//--------------------------------------------------------------------------------------
void order()
{
int k,j;
float t; char n[20];
for(j = 0; j<=(2-1); j++)
for(k=1; k<=(2-j); k++)
if(ob[k].totll < ob[k + 1].totll)
{
t = ob[k].totll;
ob[k].totll = ob[k+1].totll;
ob[k+1].totll = t;
strcpy(n, ob[k].name);
strcpy(ob[k].name, ob[k+1].name);
strcpy(ob[k+1].name, n);
}
cout<<" 成绩排名:"<<endl;
cout<<" 姓名 总成绩 名次"<<endl;
for(k=1; k<=s; k++)
{
cout<<" ";
cout.setf(ios::left);
cout.width(9);
cout<<ob[k].name;
cout.width(9);
cout<<ob[k].totll<<k<<endl;
}
getchar();
}
}ob[s];
//--------------------------------------------------------------------------------------
void menu()
{
cout<<"\n\n";
cout<<"------------------ 学生成绩系统 -----------------"<<endl<<endl;
cout<<"\t\t1.录入与保存学生信息.\n";
cout<<"\t\t2.读取学生信息.\n";
cout<<"\t\t3.删除学生信息.\n";
cout<<"\t\t4.追加学生信息.\n";
cout<<"\t\t5.查询学生信息.\n";
cout<<"\t\t6.显示成绩名次.\n";
cout<<"\t\t7.退出系统......\n\n\n";
cout<<"\t\t请选择功能项: ";
}
//---------------------------------------------------------------------------------------
void main()
{
while(1)
{ student a;
int SEL;
system("cls");
menu();
cin>>SEL;
switch(SEL)
{
case 1:
system("cls"); a.input(); a.save(); break;
case 2:
system("cls"); a.read(); break;
case 3:
system("cls"); break;
case 4:
system("cls"); break;
case 5:
system("cls"); a.Query(); break;
case 6:
system("cls"); a.order(); break;
case 7:
cout<<endl<<" 按任意键退出.... "<<endl;
getchar();
exit(0);
default:
cout<<"Bad input!!\n";
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -