链表实现对学生信息的相关操作

#include
#include
#define MAX 10
#define NE 4
using namespace std;
class Student { //学生类
public:
string name; //姓名
int num; //学号
float score[3]; //成绩
};
class SqList {
public:
int length; //表长
int maxlong; //开辟的最大长度
Student *head; //头指针

};
//建立一个顺序表,传入顺序表名
void setList(SqList &L) {
L.head=new Student[MAX]; //指针指向数组
L.length=0; //有元素表长加一
L.maxlong=MAX; //开辟的最大长度
}

//重载输入运算符(流提取)
istream& operator>>(istream &in,Student &stu) {
in >>stu.num >> stu.name >> stu.score[0] >> stu.score[1] >> stu.score[2];
return in;
}

//重载输出运算符(流插入)
ostream& operator<<(ostream &out,Student &stu) {

out << stu.num <<"\t"<< stu.name <<"\t"<< stu.score[0]<<"\t" << stu.score[1] <<"\t"<< stu.score[2];
return out;

}

//录入学生信息
void inPut(SqList &L,int n) {

while(n>L.maxlong) {
	Student *p;
	p=L.head;
	L.head=new Student[L.maxlong+NE];
	L.maxlong=L.maxlong+NE;
	delete(p);
}
int i;
cout <<"请输入学号	姓名	语文	英语	数学"<<endl;
for(i=0; i<n; i++) {
	cin >> L.head[i];
	L.length++;
}

}

//输出学生信息
void outPut(SqList L) {
int i;
cout <<“学号 姓名 语文 英语 数学”<<endl;
for(i=0; i<L.length; i++ ) {
cout << L.head[i] <<endl;
}
system(“PAUSE”);
}

//选择执行功能
void Function() {
system(“cls”);
cout << “1、插入学生信息” << endl << “2、删除学生信息” << endl << “3、修改学生信息” << endl << “4、查找学生信息” << endl << “5、排序学生信息” << endl;
cout << “请选择你要执行的功能:”;
}
//查找学生信息
void Search(SqList L) {
int num,i;
bool flag=false;
cout << “请输入你需要查找的学号:”;
cin >> num ;
cout << endl;
for(int i=0; i<L.length; i++) {
if(L.head[i].numnum) {
cout <<“学号 姓名 语文 英语 数学”<<endl;
cout << L.head[i];
flag = true;
}
}
if(flag
false) {
cout << “没有学号为” << num << “的学生!!” << endl;
}
}
// 插入学生信息子函数
void InsertFor(SqList &L,Student one) {
int length=L.length;
L.head[length]=one;
L.length++;
}
//插入学生信息
void Insert(SqList &L) {
int i;
Student one;
cout << “请输入要插入的学生个数:” << endl;
cin >> i;
while(L.length+i>L.maxlong) {
Student *p=L.head;
L.head=new Student[L.maxlong+NE];
L.maxlong=L.maxlong+NE;
delete p;
}
for(int j=0; j<i; j++) {
cout << “请输入第”<<j+1 <<“个要插入的学生信息:” << endl;
cout <<“学号 姓名 语文 英语 数学”<<endl;
cin >> one ;
InsertFor(L,one);
}
cout << “插入学生信息成功!” << endl;
outPut(L);

}

//删除学生信息
void Delete(SqList &L) {
int num;
cout << “请输入要删除学号:”;
cin >> num;
cout << endl;
bool flag = false;
for(int j=0; j<L.length; j++) {
if(num==L.head[j].num) {
for(int n=j; n<L.length; n++) {
L.head[n] = L.head[n+1];
}
L.length–;
flag = true;
cout << “删除成功!” << endl;
outPut(L);
}
}
if(flag == false) {
cout << “没有学号为”<< num <<“的学生” << endl;
}

}

//修改学生信息子函数
void ModificationGt(SqList &L,int j,int choice) {
if(choice1) {
int newnum;
cout <<“请输入新学号:”;
cin >> newnum;
L.head[j].num=newnum;
} else if(choice
2) {
string newname;
cout <<“请输入新姓名:”;
cin >> newname;
L.head[j].name= newname;
} else if(choice3) {
int newgrade;
cout <<“请输入新语文成绩:”;
cin >> newgrade;
L.head[j].score[0]= newgrade;
} else if(choice
4) {
int newgrade;
cout <<“请输入新数学成绩:”;
cin >> newgrade;
L.head[j].score[1]= newgrade;
} else if(choice5) {
int newgrade;
cout <<“请输入新英语成绩:”;
cin >> newgrade;
L.head[j].score[2]= newgrade;
}
cout << “修改成功!” << endl;
outPut(L);
}
//修改学生信息
void Modification(SqList &L) {
int num;
bool flag =false;
cout << “请输入要修改学号:”;
cin >> num;
cout << endl;
for(int j=0; j<L.length; j++) {
if(num
L.head[j].num) {
int choice;
cout << “1、修改学号\n” << “2、修改姓名\n” << “3、修改语文成绩\n” << “3、修改数学成绩\n” << “3、修改英语成绩\n” <<endl;
cout << “请选择需要修改的内容:”;
cin >> choice;
ModificationGt(L, j,choice);
flag = true;
}

}
if(flag == false) {
	cout << "没有学号为"<< num <<"的学生" << endl;
}

}

//排列学生顺序
void Rank(SqList &L)
{
for(int i=0;i<L.length;i++)
{
int min=i;
for(int j=i+1;j<L.length+1;j++){
if(L.head[j].num<L.head[min].num)
min = j;

	 }
	 	Student temp;
		temp = L.head[i];
		L.head[i] =L.head[min];
		L.head[min] = temp ;
 }
 	outPut(L);

}
int main() {
SqList L;
int n,choice;
cout <<“请输入学生个数:” <<endl;
cin >> n;
setList(L);
inPut(L,n);
outPut(L);
Function() ;
cin >> choice;
cout << endl;
switch(choice) {
case 1:
Insert(L);
break;
case 2:
Delete(L);
break;
case 3:
Modification(L);
break;
case 4:
Search(L);
break;
case 5:
Rank(L);
break;
}
return 0;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值