⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 05060306张璐.cpp

📁 数据结构顺序表实验
💻 CPP
字号:
#include <iostream.h> 
#include <stdio.h>
#include <string.h>
#include <conio.h> 
#include <malloc.h> 
#include <process.h>
#define INITSIZE 100 
#define INCSIZE sizeof(student) 
typedef struct
//class student
{
long no; /*学生序号*/
int math; /*数学成绩*/
int program; /*程序设计成绩*/
int amount; /*总分*/
char name[30]; /*学生姓名*/
}student; 
int maxsize = INITSIZE; 
int num =0;
int dbnull = 1; 
enum 
{
Num, 
Name,
Math, 
Program, 
Amount 
}; 
int createset(student **t);
void addnew(student *t);
void deletestu(student *t);
void stuselect(student *t,int mode);
void scoresort(student *t,int mode);
int findno(student *t,int no);
int findmath(student *t,int math);
int findprogram(student *t,int program);
int findamount(student *t,int amount);
int findname(student *t,const char *name);
void display(student *t,int no);
void mathsort(student *t);
void programsort(student *t);
void amountsort(student *t);
void swap(student *t, int i,int j);
int createset(student **t)
{
char ask ;
if (num!=0) /*存在学生记录*/
{
cout<<"存在一个数据,是否覆盖?(Y/N)?"<<endl;
ask =getch(); 
if (ask == 'y'||ask=='Y')
{
free(*t);
num =0;
}
else
{
return 0; /*不选择覆盖,则退出*/
}
}
*t =(student *)malloc(INITSIZE*sizeof(student)); /*分配INITSIZE个学生记录所需空间*/
if (!t)
{
cout<<"内存不足,退出程序!"<<endl;
exit(0);
}
else
{
cout<<"分配成功!"<<endl; 
dbnull = 0; 
return 1;
}
}
void addnew(student *t)
{
student temp;
if (dbnull)
{
cout<<"无可用空间,按1分配空间!"<<endl;
return;
}
if (num+1>maxsize)
{
t =(student *)realloc(t,maxsize+INCSIZE); 
if (!t) /*内存不足,追加失败*/
{
cout<<"内存溢出."<<endl;
exit(0);
}
}
cout<<"输入学生的 No. , name, math score 和program score ."<<endl;
if ((cin>>temp.no), /*输入学生数据*/
temp.name,
&(temp.math),
&(temp.program))
{
if (findno(t,temp.no) == -1) 
{
t[num].no = temp.no; 
strcpy(t[num].name,temp.name);
t[num].math = temp.math;
t[num].program = temp.program;
t[num].amount = t[num].math + t[num].program;
num++; /*当前记录数加一*/
cout<<"添加成功!"<<endl;
}
else
{
cout<<"输入学号已经存在,添加记录失败"<<temp.no<<endl;
}
}
else
{
cout<<"输入函数出错,表示输入格式错误,添加失败!"<<endl; 
}
}
void deletestu(student *t) /*从数据库删除某条学生记录*/
{
long delno=0;
int index;
int i =0;
if (dbnull) 
{
cout<<"不存在学生信息,请按1创建信息..."<<endl;
return;
}
cout<<"请输入你想要删除的学生学号!"<<endl;
cin>>delno;
index = findno(t,delno); 
if (index != -1)
{
for (i = index+1; i<= num; i++) 
{
t[i-1].no = t[i].no;
strcpy(t[i-1].name,t[i].name);
t[i-1].math = t[i].math;
t[i-1].program = t[i].program;
t[i-1].amount = t[i].amount;
}
num--; /*当前记录数减一*/
cout<<"删除成功!";
}
else
{
cout<<"无该学号的学生,删除失败!"; 
}
}
void stuselect(student *t,int mode)
{
long tempno =0;
char tempname[30];
int tempmath;
int tempprogram;
int tempamount;
int count =0;
if (dbnull)
{
cout<<"不存在学生信息,请按1创建信息..."<<endl;
return;
}
switch (mode) /*判断查询方式*/
{
case Num: /*按学号查询*/
cout<<"请输入你想要查找的学生的学号!";
cin>>tempno; /*输入学号*/
tempno =findno(t,tempno); /*查找该学生*/
if ( tempno!= -1 )
{
cout<<"查询成功,打印之!"<<endl;/*查询成功,打印之*/
display(t,tempno);
}
else
{
cout<<"你所输入的学号不存在,查找失败!"<<endl; /*查找失败*/
}
break;
case Name: /*按姓名查询*/
cout<<"请输入你要查找的学生的姓名!"<<endl;
*tempname ='\0';
cin>>tempname;
count = findname(t,tempname); /*返回查询姓名为name的学生记录个数*/
cout<<"按你所输入的姓名查找成功!"<<count<<endl;
break;
case Math: /*按数学成绩查询*/
cout<<"输入一个成绩,程序将查找比该数学成绩高的个数!"<<endl;
cin>>tempmath;
count = findmath(t,tempmath);
cout<<"按你所输入的数学成绩查找成功!"<<count<<endl;
break;
case Program: /*按程序设计成绩查询*/
cout<<"输入一个成绩,程序将查找比该程序设计成绩高的个数!"<<endl;
cin>>tempprogram;
count = findprogram(t,tempprogram);
cout<<"按你所输入的程序设计成绩查找成功!"<<count<<endl;
break;
case Amount: /*按总分查询*/
cout<<"输入一个成绩,程序将查找比该总分成绩高的个数!"<<endl;
cin>>tempamount;
count = findamount(t,tempamount);
cout<<"按你所输入的总分成绩查找成功!"<<count<<endl;
break;
default:
break;
}
}
void scoresort(student *t,int mode) /*学生记录排序*/
{
int count =0; 
switch (mode) /*选择不同排序方式进行成绩排序*/
{
case Math:
mathsort(t); /*按数学成绩排序*/
break;
case Program: /*按程序设计成绩排序*/
programsort(t);
break;
case Amount: /*按总分排序*/
amountsort(t);
break;
}
cout<<"排序已经完成,结果是:"<<endl;
for (count =0;count< num; count++) /*排序完成后输出排序结果*/
{
display(t,count);
}
}
int findno(student *t,int no)/*按学号查找学生记录*/
{
int count =0;
for (count =0; count<num; count++)
{
if ((t+count)->no == no) /*逐个搜索,若该学生记录的学号等于需要查找的学号,则返回该学号*/
{
return count;
}
}
return -1; /*搜索完毕,仍没有匹配学号,则返回-1*/
}
int findmath(student *t,int math)
{
int count =0; 
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->math > math)
{
display (t,count); /*显示查找结果*/
i++;
}
}
return i; /*返回符合查询条件的学生记录数目*/
}
int findprogram(student *t,int program)
{
int count =0;
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->program > program)
{
display(t,count);
i++;
}
}
return i;
}
int findamount(student *t,int amount)
{
int count =0;
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->amount > amount)
{
display(t,count);
i++;
}
}
return i;
}
int findname(student *t,const char *name) 
{
int count =0;
int i =0;
for (count =0; count<num; count++)
{
if (!strcmp((t+count)->name,name))
{
display(t,count);
i++;
}
}
return i;
}
void display(student *t,int no) /*打印指定学生记录*/
{
cout<<"NO.:"<< "Name:"<<" Math :"<<" Programing:"<< "Sum: "<<endl;
t[no].no,
t[no].name,
t[no].math,
t[no].program,
t[no].amount;
}
void mathsort(student *t) /*数学成绩排序,使用选择排序算法*/
{
int i;
int j;
for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].math > t[i].math )
{
swap(t,j,i);
}
}
}
void programsort(student *t) /*类似数学成绩排序*/
{
int i;
int j;
for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].program > t[i].program )
{
swap(t,j,i);
}
}
}
void amountsort(student *t) /*类似数学成绩排序*/
{
int i;
int j;
for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].amount > t[i].amount )
{
swap(t,j,i);
}
}
}
void swap(student *t, int i,int j) /*交换两个学生的记录内容*/
{
student temp; /*定义一个中间记录*/
temp.no = t[j].no; /*逐个交换记录的数据项*/
t[j].no = t[i].no;
t[i].no = temp.no;
strcpy(temp.name , t[j].name);
strcpy(t[j].name , t[i].name);
strcpy(t[i].name , temp.name);
temp.math = t[j].math;
t[j].math = t[i].math;
t[i].math = temp.math;
temp.program = t[j].program;
t[j].program = t[i].program;
t[i].program = temp.program;
temp.amount = t[j].amount;
t[j].amount = t[i].amount;
t[i].amount = temp.amount;
}
void main() /*主模块*/
{
student *t; 
int Menu =0,submenu =0;
cout<<"********学生信息管理系统********"<<endl;
while ( Menu!= '6' ) 
{
fflush(stdin); /*清除输入缓冲区*/
submenu =0; /*重置子菜单的选中项*/
cout<<"1>.分配空间."<<endl;
cout<<"2>.添加学生信息."<<endl;
cout<<"3>.删除学生信息."<<endl;
cout<<"4>.排序."<<endl;
cout<<"5>.查找."<<endl;
cout<<"6>.系统退出"<<endl;
cout<<"请输入菜单选择(1-6)..."<<endl;
Menu = getchar(); /*选择菜单*/
switch (Menu) /*按选择的菜单项,执行相应模块*/
{
case '1':
createset(&t);
break;
case '2':
addnew(t);
break;
case '3':
deletestu(t);
break;
case '4':
if (dbnull) /*数据库不存在,不予以处理*/
{
cout<<"无法操作,请选1分配空间..."<<endl;
break;
}
while (submenu != '4' )/*进入排序方式的子菜单*/
{
fflush(stdin);
cout<<"****排序****"<<endl;
cout<<"1>.数学成绩排序"<<endl;
cout<<"2>.程序设计排序"<<endl;
cout<<"3>.总成绩排序"<<endl;
cout<<"4>.返回主菜单"<<endl;
cout<<"输入菜单选择"<<endl;
submenu = getchar();
switch ( submenu )
{
case '1':
scoresort(t,Math);
break;
case '2':
scoresort(t,Program);
break;
case '3':
scoresort(t,Amount);
break;
case '4':
break;
default:
break;
}
}
break;
case '5':
if (dbnull)
{
cout<<"无法操作,请选1分配空间...";
break;
}
while (submenu != '6') /*进入查询子菜单*/
{
fflush(stdin);
cout<<"****学生查找*****"<<endl;
cout<<"1>学号查找"<<endl;
cout<<"2>姓名查找"<<endl;
cout<<"3>数学成绩查找"<<endl;
cout<<"4>程序设计成绩查找"<<endl;
cout<<"5>总成绩查找"<<endl;
cout<<"6>返回主菜单"<<endl;
cout<<"输入菜单选择"<<endl;
submenu = getchar();
switch (submenu)
{
case '1':
stuselect(t,Num);
break;
case '2':
stuselect(t,Name);
break;
case '3':
stuselect(t,Math);
break;
case '4':
stuselect(t,Program);
break;
case '5':
stuselect(t,Amount);
break;
case '6':
break;
default:
break;
}
}
case '6':
break;
default:
break;
}
}
cout<<"结束 ************学生信息管理系统*****"<<endl;
cout<<"按任意键退出...";
fflush(stdin);
getch(); /*按任意键返回*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -