📄 5555.cpp
字号:
#define ERROR 0
#define OK 1
#include<math.h>
#include<iostream.h>
#include<malloc.h>
#include<string.h>
typedef int Status;
struct student
{
int stuno;
char name[10];
int age;
}STU[50],*q,*p;
struct LIST
{
struct student STU[50];
int length;
}L;
int choose(int m,int n)
{
char k[10];
int g,f=0;
while(f==0)
{ cin>>k;
if(k[0]>=(char)(m+'0')&&k[0]<=(char)(n+'0')&&strlen(k)==1)
{ g=(int)(k[0]-'0');f++;}
else cout<<"你的输入不正确!\n请重新输入:";
}
return g;
}
int change()
{
char k[10];
int g=0,i,f=0;
while(f==0)
{
cin>>k;
for(i=0;i<strlen(k)&&k[i]>='0'&&k[i]<='9';i++);
if(i<strlen(k))
cout<<"你的输入不正确!\n请重新输入:";
else
{
for(i=0;i<strlen(k);i++)
g+=((int)(k[i]-'0'))*(int)pow(10,(double)(strlen(k)-i-1));
f++;
}
}
return g;
}
void Menu() //菜单
{
cout<<" 主菜单\n";
cout<<" ********************************************\n";
cout<<" 1.创建数据;\n";
cout<<" 2.查找数据;\n";
cout<<" 3.插入数据;\n";
cout<<" 4.添加数据;\n";
cout<<" 5.删除数据;\n";
cout<<" 6.排序数据;\n";
cout<<" 7.退出菜单;\n";
cout<<" ********************************************\n";
cout<<"请选择你所需要的操作:";
}
void print(struct LIST L) //打印表
{
int i=0;
if(L.length!=0)
{
cout<<" 年龄情况表:\n";
cout<<" --------------------------------------------\n";
cout<<" 学号 姓名 年龄\n" ;
for(i=0;i<L.length;i++)
{
cout<<" "<<(i+1)<<". "<<L.STU[i].stuno<<" "<<L.STU[i].name<<" "<<L.STU[i].age<<endl;
}
cout<<" --------------------------------------------\n";
}
else cout<<"年龄情况表中没有任何数据!\n";
}
void create(struct LIST &L) //创建表
{
int i=0;
cout<<"请输入创建的数据个数:";
L.length=change();
for(i=0;i<L.length;i++)
{
cout<<"第"<<(i+1)<<"个同学的"<<"学号:";
L.STU[i].stuno=change();
cout<<"第"<<(i+1)<<"个同学的"<<"姓名:";
cin>>L.STU[i].name;
cout<<"第"<<(i+1)<<"个同学的"<<"年龄:";
L.STU[i].age=change();
}
print(L);
}
void finds(struct LIST &L) //利用学号查找数据
{
int data=0,i=0,s=0;
cout<<"学号:";
data=change();
for(i=0;i<L.length;i++)
if(L.STU[i].stuno==data)
{
cout<<"\n您要查找的数据是:\n" ;
cout<<" 学号 姓名 年龄\n" ;
cout<<" "<<L.STU[i].stuno<<" "<<L.STU[i].name<<" "<<L.STU[i].age<<endl;
s=1;
}
if(s==0)cout<<"对不起,没有找到你所要查找的数据!\n";
}
void findn(struct LIST &L) //利用姓名查找数据
{
int i=0,s=0;
char data[10]=" ";
cout<<"姓名:";
cin>>data;
for(i=0;i<L.length;i++)
if(strcmp(L.STU[i].name,data)==0)
{
cout<<"\n您要查找的数据是:\n" ;
cout<<"学号 姓名 年龄\n" ;
cout<<L.STU[i].stuno<<" "<<L.STU[i].name<<" "<<L.STU[i].age<<endl;
s=1;
}
if(s==0)cout<<"对不起,没有找到你所要查找的数据!\n";
}
void finda(struct LIST &L) //利用年龄查找数据
{
int data=0,i=0,s=0;
cout<<"年龄:";
data=change();
cout<<"\n学号 姓名 年龄\n" ;
for(i=0;i<L.length;i++)
if(L.STU[i].age==data)
{
cout<<"\n您要查找的数据是:\n" ;
cout<<"学号 姓名 年龄\n" ;
cout<<L.STU[i].stuno<<" "<<L.STU[i].name<<" "<<L.STU[i].age<<endl;
s=1;
}
if(s==0)cout<<"对不起,没有找到你所要查找的数据!\n";
}
void find(struct LIST &L) //查找数据
{
int j=0,k;
cout<<"查找按照:\n";
cout<<" 1.学号;\n";
cout<<" 2.姓名;\n";
cout<<" 3.年龄;\n";
cout<<" 4.返回主菜单;\n";
cout<<"请选择:";
k=choose(1,4);
switch(k)
{
case 1:finds(L);break;
case 2:findn(L);break;
case 3:finda(L);break;
case 4:break;
default:{cout<<"你输入的不符合要求!\n请重新选择:";
j=1;
}
}
if(j==1)
find(L);
}
void insert(struct LIST &L) //插入数据
{
int i,j=0,m=0,k;
if(j==0)
{
cout<<"1.从前面插入:\n";
cout<<"2.从后面插入:\n";
cout<<"请选择:";
}
k=choose(1,2);
switch(k)
{
case 1:break;
case 2: m=1;break;
default:{cout<<"你的输入不正确!\n请重新选择:\n";
j=1;
}
}
if(j==1)
insert(L);
cout<<"在第几个位置插入:";
i=choose(1,L.length);
i+=m;
q=&L.STU[i-1];
for(p=&(L.STU[L.length-1]);p>=q;p--)
*(p+1)=*p;
++L.length;
cout<<"请输入插入的数据:\n";
cout<<"学号:";
L.STU[i-1].stuno=change();
cout<<"姓名:";
cin>>L.STU[i-1].name;
cout<<"年龄:";
L.STU[i-1].age=change();
print(L);
}
void add(struct LIST &L) //添加数据
{
int i=0,j=0;
cout<<"请输入添加的数据个数:";
j=change();
L.length+=j;
for(i=L.length-j;i<L.length;i++)
{
cout<<"学号:";
L.STU[i].stuno=change();
cout<<"姓名:";
cin>>L.STU[i].name;
cout<<"年龄:";
L.STU[i].age=change();
}
print(L);
}
Status Delete(struct LIST &L) //删除数据
{
int i=0,data=0,j=-5,s=0,m=0,k;
char datan[10]=" ";
cout<<"请输入要删除数据按照:\n";
cout<<" 1.学号;\n 2.姓名;\n 3.年龄;\n";
cout<<" 4.返回主菜单\n";
cout<<"请选择:";
k=choose(1,4);
switch(k)
{
case 1:
{
print(L);
cout<<"请输入该数据:";
data=change();
for(i=0;i<L.length;i++)
if(L.STU[i].stuno==data)
{
j=i;
s=1;
}
if(s==0)cout<<"对不起,没有找到你所要删除的数据!\n";
}break;
case 2:
{
print(L);
cout<<"请输入该数据:";
cin>>datan;
for(i=0;i<L.length;i++)
if(strcmp(L.STU[i].name,datan)==0)
{
j=i;
s=1;
}
if(s==0)cout<<"对不起,没有找到你所要删除的数据!\n";
}break;
case 3:
{
print(L);
cout<<"请输入该数据:";
data=change();
for(i=0;i<L.length;i++)
if(L.STU[i].age==data)
{
j=i;
s=1;
}
if(s==0)cout<<"对不起,没有找到你所要删除的数据!\n";
}break;
case 4:break;
default:{cout<<"你的输入不正确!\n请重新选择:";
m=1;
}
}
if(m==1)Delete(L);
q=L.STU+L.length-1;
if(j!=-5)
{
for(p=&(L.STU[j-1]);p<=q;++p)
{
p->stuno=(p+1)->stuno;
strcpy(p->name,(p+1)->name);
p->age=(p+1)->age;
}
L.length--;
}
print(L);
return OK;
}
Status sorts(struct LIST &L) //按照学号进行排序
{
int i=0,j=0;
char data[10]=" ";
for(i=0;i<L.length-1;i++)
for(j=0;j<L.length-1-i;j++)
if(L.STU[j].stuno>L.STU[j+1].stuno)
{
L.STU[j].stuno=L.STU[j].stuno+L.STU[j+1].stuno;
L.STU[j+1].stuno=L.STU[j].stuno-L.STU[j+1].stuno;
L.STU[j].stuno=L.STU[j].stuno-L.STU[j+1].stuno;
strcpy(data,L.STU[j].name);
strcpy(L.STU[j].name,L.STU[j+1].name);
strcpy(L.STU[j+1].name,data);
L.STU[j].age=L.STU[j].age+L.STU[j+1].age;
L.STU[j+1].age=L.STU[j].age-L.STU[j+1].age;
L.STU[j].age=L.STU[j].age-L.STU[j+1].age;
}
print(L);
return OK;
}
Status sortn(struct LIST &L) //按照姓名进行排序
{
int i=0,j=0;
char data[10]=" ";
for(i=0;i<L.length-1;i++)
for(j=0;j<L.length-1-i;j++)
if(strcmp(L.STU[i].name,L.STU[j+1].name)>0)
{
L.STU[j].stuno=L.STU[j].stuno+L.STU[j+1].stuno;
L.STU[j+1].stuno=L.STU[j].stuno-L.STU[j+1].stuno;
L.STU[j].stuno=L.STU[j].stuno-L.STU[j+1].stuno;
strcpy(data,L.STU[j].name);
strcpy(L.STU[j].name,L.STU[j+1].name);
strcpy(L.STU[j+1].name,data);
L.STU[j].age=L.STU[j].age+L.STU[j+1].age;
L.STU[j+1].age=L.STU[j].age-L.STU[j+1].age;
L.STU[j].age=L.STU[j].age-L.STU[j+1].age;
}
print(L);
return OK;
}
Status sorta(struct LIST &L) //按照年龄进行排序
{
int i=0,j=0;
char data[10]=" ";
for(i=0;i<L.length-1;i++)
for(j=0;j<L.length-1-i;j++)
if(L.STU[j].age>L.STU[j+1].age)
{
L.STU[j].stuno=L.STU[j].stuno+L.STU[j+1].stuno;
L.STU[j+1].stuno=L.STU[j].stuno-L.STU[j+1].stuno;
L.STU[j].stuno=L.STU[j].stuno-L.STU[j+1].stuno;
strcpy(data,L.STU[j].name);
strcpy(L.STU[j].name,L.STU[j+1].name);
strcpy(L.STU[j+1].name,data);
L.STU[j].age=L.STU[j].age+L.STU[j+1].age;
L.STU[j+1].age=L.STU[j].age-L.STU[j+1].age;
L.STU[j].age=L.STU[j].age-L.STU[j+1].age;
}
print(L);
return OK;
}
void sort(struct LIST &L) //对表进行排序
{
int j=0,k;
cout<<"排序按照:\n 1.学号;\n 2.姓名;\n 3.年龄;\n";
cout<<" 4.返回主菜单\n";
cout<<"请选择:";
k=choose(1,4);
switch(k)
{
case 1: sorts(L);break;
case 2: sortn(L);break;
case 3: sorta(L);break;
case 4:break;
default :{cout<<"你的输入不正确!\n请重新选择:";
j=1;}
}
if(j==1)
sort(L);
}
void main()
{
int exit=0,k;
Menu();
k=choose(1,7);
switch(k)
{
case 1:if(L.length!=0)
cout<<"你已经创建数据!请选择其它项:\n";
else create(L);break;
case 2:if(L.length==0)
{ cout<<"对不起,你还没创建数据!\n请先创建数据:\n";
create(L);
}
else find(L);break;
case 3:if(L.length==0)
{cout<<"对不起,你还没创建数据!\n请先创建数据:\n";
create(L);
}
else insert(L);break;
case 4:if(L.length==0)
{cout<<"对不起,你还没创建数据!\n请先创建数据:\n";
create(L);
}
else add(L);break;
case 5:if(L.length==0)
{cout<<"对不起,你还没创建数据!\n请先创建数据:\n";
create(L);
}
else Delete(L);break;
case 6:if(L.length==0)
{cout<<"对不起,你还没创建数据!\n请先创建数据:\n";
create(L);
}
else sort(L);break;
case 7: exit=1;break;
default: cout<<"你的输入不正确!\n请重新选择:\n";
}
if(exit==0)
main();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -