#include"iostream.h"
#include<string>
#include<iomanip>
#include<sstream>
using namespace std;
struct inf
{
char num[15]; //选用字符型的变量来接受输入避免在误输入非数字时陷入死循环
char name[10];
char native[6];
double score;
}x[101],t;
int n=0; //用于记录信息组数
void p(); //函数声明
void cininf();
void p() //将显示菜单作为一个函数
{
int a;
for(a=1;a<21;a++)cout<<" ";
cout<<"学生信息"<<endl;
for(a=1;a<49;a++)cout<<"*";
cout<<endl<<"***";
for(a=1;a<11;a++)cout<<" ";
cout<<"1.录入";
for(a=1;a<11;a++)cout<<" ";
cout<<"2.排序";
for(a=1;a<11;a++)cout<<" ";
cout<<"***";
cout<<endl<<"***";
for(a=1;a<11;a++)cout<<" ";
cout<<"3.查找";
for(a=1;a<11;a++)cout<<" ";
cout<<"4.删除";
for(a=1;a<11;a++)cout<<" ";
cout<<"***";
cout<<endl<<"***";
for(a=1;a<11;a++)cout<<" ";
cout<<"5.求总成绩和平均成绩";
for(a=1;a<13;a++)cout<<" ";
cout<<"***"<<endl;
for(a=1;a<49;a++)cout<<"*";
cout<<endl;
}
void cininf()
{
char b[18],d[5];
int i,j;
cout<<"请输入学号,姓名,籍贯,成绩(输入完100组后自动结束结束或以输入#号的方式结束):"<<endl<<endl;
for(i=n;i<100;i++)
{
cout<<"->准备录入第"<<n+1<<"组信息(输入#号结束录入)"<<endl;
cout<<"请输入学号或#号:";
a1: cin>>b;
if(b[0]=='#')
goto o3; //直接显示已输入的信息组数
for(j=0;j<strlen(b);j++)
{
//cout<<strlen(b)<<"b"<<j<<"="<<b[j]<<" "; //用于调试观察
if(strlen(b)>15||b[j]<'0'||b[j]>'9') //限制分数的输入为15位数字
{
cout<<"输入有误,请重新输入15位以内的阿拉伯数字学号:";
goto a1;
}
}
strcpy(x[i].num,b);
cout<<"请输入姓名:";
cin>>x[i].name;
cout<<"请输入籍贯:";
cin>>x[i].native;
cout<<"请输入成绩:";
a2: cin>>d; //选用字符型的变量来接受输入避免在误输入非数字时陷入死循环
for(j=0;j<strlen(d);j++) //限制分数的输入3位数字
{
// cout<<"d"<<j<<"="<<d[j]<<" "; //用于调试观察
if(strlen(d)>3||d[j]<'0'||d[j]>'9')
{
cout<<"输入有误,请重新输入3位以内的阿拉伯数字成绩:";
goto a2;
}
}
x[i].score=atof(d); //将学号转换为数字,用于排序时比较,
//cout<<"f="<<x[i].score; //用于调试观察
n++; //每输入一组信息,计数器+1
}
o3: cout<<endl<<"->共录入"<<n<<"组信息"<<endl; //显示已输入的信息的数量
}
void main()
{
o1: p(); //用于菜单的显示
int i,j;
char c[10];
cout<<"请输入1 2 3 4 or 5:"<<endl;
cin>>c;
if(c[0]!='1'&&c[0]!='2'&&c[0]!='3'&&c[0]!='4'&&c[0]!='5'||strlen(c)>1) //限定只能输入菜单中的选项
{
cout<<"输入错误,请按提示输入"<<endl;
goto o1; //返回到菜单
}
if(c[0]=='1') // *****录入学生信息
{
char c1[10];
if(n>0) //先判断是否有信息存在
{
o5: cout<<"************************************************"<<endl
<<" 已有信息存在,你想?(输入0返回目录)"<<endl
<<" ->1、重新录入(这将会清除所有现有的信息)"<<endl
<<" ->2、加入到后面"<<endl
<<"************************************************"<<endl;
cin>>c1;
if(c1[0]!='0'&&c1[0]!='1'&&c1[0]!='2'||strlen(c1)>1) //限定只能输入菜单中的选项
{
cout<<"输入错误,请按提示输入"<<endl;
goto o5;
}
if(c1[0]=='0') goto o4;
if(c1[0]=='1')
{
n=0;
cininf();
}
if(c1[0]=='2') cininf();
}
if(n==0) cininf();
goto o2; //将已输入的信息输出
}
if(c[0]=='2') //*****将信息排序
{
// cout<<"n1="<<n<<endl; //用于调试查看
if(n>0) //先判断是否有信息存在
{
char c2[10];
cout<<"请选择排序关键字(或按0键返回上层目录):"<<endl
<<"**** 1. 学号 ****"<<endl
<<"**** 2. 成绩 ****"<<endl
<<"**** 3. 籍贯 ****"<<endl;
o6: cin>>c2;
if(c2[0]!='0'&&c2[0]!='1'&&c2[0]!='2'&&c2[0]!='3'||strlen(c2)>1) //限定只能输入菜单中的选项
{
cout<<"输入错误,请按提示输入"<<endl;
goto o6; //返回到关键词选择
}
if(c2[0]=='0') //设置返回上层的开关。
goto o1;
if(c2[0]=='1')
{
long double t1,t2;
//cout<<"n2="<<n<<endl; //用于调试查看
for(i=0;i<n-1;i++) //按学号排序
for(j=0;j<n-(i+1);j++)
{
/*stringstream ps(x[j].num); //将学号转换为数字,用于排序时比较
ps>>t1;
stringstream qs(x[j+1].num);
qs>>t2;*/
t1=atof(x[j].num); //将学号转换为数字,用于排序时比较
t2=atof(x[j+1].num);
//cout<<"t1"<<i<<j<<"="<<t1<<" &t2"<<i<<j<<"="<<t2<<endl; //用于调试观察
if(t1>t2)
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
//cout<<"t1*"<<i<<j<<"="<<x[j].num<<" &t2*"<<i<<j<<"="<<x[j+1].num<<endl; //用于调试观察
}
cout<<"排序完毕,如下:"<<endl;
goto o2; //将学生信息输出(此时已排序)
}
if(c2[0]=='2') //按成绩排序
{
for(i=0;i<n-1;i++)
for(j=0;j<n-(i+1);j++)
{
//cout<<"t1"<<i<<j<<"="<<x[j].score<<" &t2"<<i<<j<<"="<<x[j+1].score<<endl; //用于调试观察
if(x[j].score>x[j+1].score)
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
//cout<<"t1*"<<i<<j<<"="<<x[j].score<<" &t2*"<<i<<j<<"="<<x[j+1].score<<endl; //用于调试观察
}
cout<<"排序完毕,如下:"<<endl;
goto o2; //将学生信息输出(此时已排序)
}
if(c2[0]=='3') cout<<c2[0]<<endl<<"排序完毕:"<<endl; //按籍贯排序
{
/*for(i=0;i<n;i++) //用于调试观察
cout<<x[i].native<<" ";
cout<<endl;*/
for(i=0;i<n;i++)
for(j=0;j<n-(i+1);j++)
{
// cout<<i<<j<<"o. "<<x[j].native<<" "<<x[j+1].native<<" "<<endl; //用于调试观察
if(strcmp(x[j].native,x[j+1].native)>0)
{
// cout<<i<<j<<"a. "<<x[j].native<<" "<<x[j+1].native<<" "<<endl; //用于调试观察
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// cout<<i<<j<<"b. "<<x[j].native<<" "<<x[j+1].native<<" "<<endl; //用于调试观察
}
// cout<<i<<j<<"p. "<<x[j].native<<" "<<x[j+1].native<<" "<<endl; //用于调试观察
}
}
cout<<"排序完毕,如下:"<<endl;
goto o2; //将学生信息输出(此时已排序)
}
else
{
cout<<"目前不存在学生信息,请先输入。"<<endl;
goto o4; //不存在信息时,准备返回菜单
}
}
if(c[0]=='3') //****查找信息
{
if(n>0) //先判断是否有信息存在
{
char c3[10];
c0: cout<<"请选择查找的关键字类别(或输入0返回上层):"<<endl
<<"**** 1. 学号 ****"<<endl
<<"**** 2. 姓名 ****"<<endl
<<"**** 3. 成绩 ****"<<endl;
o7: cin>>c3;
if(c3[0]!='0'&&c3[0]!='1'&&c3[0]!='2'&&c3[0]!='3'||strlen(c3)>1) //限定只能输入菜单中的选项
{
cout<<"输入错误,请按提示输入"<<endl;
goto o7; //返回到关键在选择
}
if(c3[0]=='0') goto o1; //设置返回上层的开关。
int m; //查找信息计数变量
if(c3[0]=='1')
{
char f[15];
c1: m=0;
cout<<"输入要查找的学号"<<endl;
cin>>f;
for(i=0;i<n;i++) //直接接收字符来进行比对
{
//cout<<"n="<<n<<"^^="<<f<<"&&="<<x[i].num<<endl; //用于调试观察
if(strcmp(f,x[i].num)==0)
{
cout<<m+1<<". "<<x[i].num<<" "<<x[i].name<<" "<<x[i].native<<" "<<x[i].score<<endl;
m++;
}
}
if(m>0)cout<<endl<<"->共找到"<<m<<"组信息"<<endl;
if(m==0)cout<<endl<<"->所找信息不存在"<<endl;
char f1[15];
cc1: cout<<"输入c继续查找,输入r返回查找关键字选择,0返回开始菜单:"<<endl;
cin>>f1;
if(f1[0]!='0'&&f1[0]!='c'&&f1[0]!='r'||strlen(f1)>1) //限定只能输入提示中的选项
{
cout<<"输入错误,请按提示输入"<<endl;
goto cc1;
}
if(f1[0]=='c') goto c1;
if(f1[0]=='r') goto c0;
goto o1; //查找完后返回菜单
}
if(c3[0]=='2')
{
char f[10];
c2: m=0;
cout<<"输入要查找的完整姓名"<<endl;
cin>>f;
for(i=0;i<n;i++)
{
//cout<<"n="<<n<<"^^="<<f<<"&&="<<x[i].num<<endl; //用于调试观察
if(strcmp(f,x[i].name)==0)
{
cout<<m+1<<". "<<x[i].num<<" "<<x[i].name<<" "<<x[i].native<<" "<<x[i].score<<endl;
m++;
}
}
if(m>0)cout<<endl<<"->共找到"<<m<<"组信息"<<endl;
if(m==0)cout<<endl<