顺序表(输入,插入,查询,删除。。。操作)学生信息
#include<iostream>
#include<string.h>
using namespace std;
#define MAXSIZE 100
int k=0;
struct student{
char name[20];
int num;
int score;
};
struct Sqlist{
student *elem;
int length;
};
void InitSqlist(Sqlist &L){
L.length=0;
L.elem=new student[MAXSIZE];
if(!L.elem)
cout<<"\t\t\t\t*******初始化失败!********"<<endl<<endl;
else
cout<<"\t\t\t\t*******初始化成功!********"<<endl<<endl;
}
void InputSqlist(Sqlist &L,int n){
int i=0,h=1;
for(i=0;i<n;i++){
h=1;
cout<<"\t\t\t*******请输入第"<<k+1<<"位同学的学生信息*******" <<endl<<endl;
cout<<"\t\t\t*******请按照:姓名 学号 成绩 顺序填写*******"<<endl<<endl;
cin>>L.elem[k].name;
while(h){
cin>>L.elem[k].num;
if(L.elem[k].num<0){
cout<<"\t\t\t*******您输入的数字不合法*******"<<endl;
cout<<"\t\t\t*******请重新输入*******"<<endl;
}
else
h=0;
}
h=1;
while(h){
cin>>L.elem[k].score;
if(L.elem[k].score<0){
cout<<"\t\t\t*******您输入的数字不合法*******"<<endl;
cout<<"\t\t\t*******请重新输入*******"<<endl;
}
else
h=0;
}
k++;
}
L.length=n+L.length;
system("PAUSE");
system("cls");
}
void OutSqlist(Sqlist &L){
system("cls");
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t****下面是学生信息:"<<endl<<endl;
int i;
for(i=0;i<L.length;i++){
cout<<"\t\t\t\t姓名 "<<L.elem[i].name<<" 学号 "<<L.elem[i].num<<" 成绩 "<<L.elem[i].score<<endl<<endl;
}
system("PAUSE");
system("cls");
}
void FindSqlist(Sqlist &L,char e[]){
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t****下面是学生信息:"<<endl<<endl;
int i,h=0;
for(i=0;i<L.length;i++){
if(strcmp(e,L.elem[i].name)==0){
cout<<"\t\t\t********该同学的学号是:"<<L.elem[i].num<<endl<<endl;
cout<<"\t\t\t********成绩是:"<<L.elem[i].score<<endl<<endl;
h++;
}
}
if(h==0){
cout<<"\t\t\t\t没有该同学!"<<endl<<endl;
}
system("PAUSE");
system("cls");
}
void LoacateSqList(Sqlist &L,int e){
if(e<=L.length&&e>=1){
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t****下面是学生信息:"<<endl<<endl;
cout<<"\t\t\t\t姓名:"<<L.elem[e-1].name<<"学号:"<<L.elem[e-1].num<<"成绩:"<<L.elem[e-1].score<<endl<<endl;
}
else{
cout<<"\t\t\t\t没有该同学!"<<endl<<endl;
}
system("PAUSE");
system("cls");
}
void InsertSqlist(Sqlist &L,int i){
int j;
if((i<1)||(i>L.length+1))
cout<<"\t\t\t*****您输入的数字不合法*****"<<endl<<endl;
if(L.length==MAXSIZE){
cout<<"\t\t\t****表已满,不可加入****"<<endl<<endl;
system("PAUSE");
system("cls");
}
else{
for(j=L.length-1;j>=i-1;j--){
L.elem[j+1]=L.elem[j];
}
cout<<"\t\t\t请输入你想要插入的学生信息:姓名 学号 成绩顺序输入"<<endl<<endl;
cin>>L.elem[i-1].name>>L.elem[i-1].num>>L.elem[i-1].score;
++L.length;
system("PAUSE");
system("cls");
}
}
void Del(Sqlist &L,int i){
int j;
if((i<1)||(i>L.length))
cout<<"\t\t\t*****您输入的数字不合法*****"<<endl<<endl;
for(j=i;j<=L.length-1;j++){
L.elem[j-1]=L.elem[j];
}
--L.length;
cout<<"\t\t\t\t删除成功!"<<endl;
system("PAUSE");
system("cls");
}
void start(){
cout<<"\t\t\t*****请选择您需要的操作"<<endl<<endl;
cout<<"\t\t\t*****输入学生信息,请按1"<<endl<<endl;
cout<<"\t\t\t*****显示信息,请按2"<<endl<<endl;
cout<<"\t\t\t*****按姓名查找学生信息,请按3"<<endl<<endl;
cout<<"\t\t\t*****按表中指定位置查找信息,请按4"<<endl<<endl;
cout<<"\t\t\t*****插入学生信息,请按5"<<endl<<endl;
cout<<"\t\t\t*****删除指定学生信息,请按6"<<endl<<endl;
cout<<"\t\t\t*****显示表中学生个数,请按7"<<endl<<endl;
cout<<"\t\t\t*****结束,请按0"<<endl<<endl;
}
int main(){
int i,j;
char e[20];
Sqlist L;
InitSqlist(L);
while(1){
system("cls");
cout<<endl<<endl<<endl<<endl<<endl;
start();
fflush(stdin);
cin>>i;
if(i==0)
break;
else if(i>=1&&i<=7){
switch(i){
case 1:system("cls");
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t*****请输入您想要输入的学生信息总数******"<<endl<<endl;
cin>>j;
InputSqlist(L,j);break;
case 2:system("cls");
cout<<endl<<endl<<endl<<endl<<endl;
OutSqlist(L);break;
case 3:system("cls");
cout<<endl<<endl<<endl<<endl<<endl;cout<<"\t\t\t\t请输入学生姓名:";
cin>>e;
FindSqlist(L,e);
break;
case 4: system("cls");//清屏break;
cout<<endl<<endl<<endl<<endl<<endl;cout<<"\t\t\t\t请输入指定的学生信息位置:";
cin>>j;
LoacateSqList(L,j);
break;
case 5:system("cls");//清屏break;
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t\t请输入插入学生信息的指定位置:";
cin>>j;
InsertSqlist(L,j);break;
case 6:system("cls");//清屏break;
cout<<endl<<endl<<endl<<endl<<endl;
cout<<"\t\t\t\t请输入删除学生信息的指定位置:";
cin>>j;
Del(L,j);
break;
case 7:cout<<"\t\t\t这个表一共有:"<<L.length<<"个同学"<<endl;
system("PAUSE");
system("cls");//清屏break;
break;
default:cout<<"\t\t\t*****没有该选项!"<<endl<<endl;break;
}
}
else if(i>7||i<0){
cout<<"\t\t\t*****没有该选项!"<<endl<<endl;
system("PAUSE");
system("cls");//清屏break;
}
}
}