#include "stdafx.h" #include "iostream" #include "string" #define OK 1 #define ERROR 0 using namespace std; typedef int status; typedef struct { int examnum; string name; string gender; int age; int Chinese; int Maths; int English; float everage; int sum; }ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList; LinkList L; void CreateList(LinkList &L, int n); status ListInsert(LinkList &L, int i, ElemType e); status ListDelete(LinkList &L, int i, ElemType &e); int LocateElem(LinkList L, ElemType e); status GetElem(LinkList L, int i, ElemType &e); int ListLength(LinkList L); void ListTraverse(LinkList L); void UpdateByNo(LinkList &L, int examnum); void Operation(); void Flunk(LinkList L, int i); void Ranking(LinkList &L); //创建考生信息的线性表(带头结点的单链表) void CreateList(LinkList &L, int n) { L = new LNode; //新结点 L->next = NULL; //头结点 空 ElemType e; LinkList s, p = L; cout << endl << "\t初始化链表完成并生成了头结点!" << endl; for (int i = 0; i < n; i++) { s = new LNode; cout << "\t请输入第" << i + 1 << "个考生的信息:" << endl; cout << "\t 准考证号:"; cin >> s->data.examnum; cout << "\t 考生姓名:"; cin >> s->data.name; cout << "\t 考生性别:"; cin >> s->data.gender; cout << "\t 考生年龄:"; cin >> s->data.age; cout << "\t 语文成绩:"; cin >> s->data.Chinese; cout << "\t 数学成绩:"; cin >> s->data.Maths; cout << "\t 英语成绩:"; cin >> s->data.English; s->data.sum = s->data.Chinese + s->data.English + s->data.Maths; s->data.everage = s->data.sum / 3.0; p->next = s; p = s; } p->next = NULL; } //在线性表的某个位置上插入考生信息 status ListInsert(LinkList &L, int i, ElemType e) { LinkList p = new LNode; p = L;//p指向头结点 int j = 0; while (p&&j < i - 1) { p = p->next; ++j; } if (!p&&j > i - 1) { cout << endl << "\t插入失败" << endl << endl; return ERROR; } LNode *s; s = new LNode; s->data = e; s->next = p->next; p->next = s; cout << endl << "\t插入成功" << endl << endl; return OK; } //删除线性表中第i个考生的信息 status ListDelete(LinkList &L, int i, ElemType &e) { LinkList p = new LNode; LinkList q = new LNode; p = L; int j = 0; while (p->next&&j < i - 1) { p = p->next; j++; } if (!(p->next) || j > i - 1) { return ERROR; } q = p->next; p->next = q->next; e = q->data; delete q; return OK; } //根据考号查询某考生的位序 int LocateElem(LinkList L, ElemType e) {//在带头结点的单链表L中查找元素e int i = 1, length = 0; LinkList p = new LNode; p = L->next; while ((i <= ListLength(L)) && (e.examnum != p->data.examnum)) { i++; p = p->next; } if (i <= ListLength(L) && (e.examnum == p->data.examnum)) return i; return 0; } //根据位序查询线性表中的考生信息 status GetElem(LinkList L, int i, ElemType &e) { LinkList p = new LNode; p = L->next; int j = 1; while (p&&j < i) { p = p->next; j++; } if (!p || j > i) return ERROR; e = p->data; return OK; } //求线性表的长度 int ListLength(LinkList L) { int length = 0; LinkList p = new LNode; p = L->next; while (p) { length++; p = p->next; } return length; } //输出线性表中的考生信息 void ListTraverse(LinkList L) { int i = 1; LNode *p = L->next; cout << endl << "\t考生信息如下:" << endl; while (p) { cout << "\t第" << i << "个考生的信息为:" << endl; cout << "\t 准考证号:" << p->data.examnum << endl; cout << "\t 考生姓名:" << p->data.name << endl; cout << "\t 考生性别:" << p->data.gender << endl; cout << "\t 考生年龄:" << p->data.age << endl; cout << "\t 语文成绩:" << p->data.Chinese << endl; cout << "\t 数学成绩:" << p->data.Maths << endl; cout << "\t 英语成绩:" << p->data.English << endl; cout << "\t 平均成绩:" << p->data.everage << endl; cout << "\t 总成绩:" << p->data.sum << endl << endl; p = p->next; i++; } cout << "\t输出完毕!" << endl << endl; } //根据考号修改线性表中的考生信息 void UpdateByNo(LinkList &L, int examnum) { LinkList p = new LNode; p = L->next; int i = 1; while (p&&p->data.examnum != examnum) { p = p->next; i++; } if (i <= ListLength(L) && (examnum == p->data.examnum)) { cout << "\t请重新输入该考生的信息:" << endl << endl; cout << "\t 准考证号:"; cin >> p->data.examnum; cout << "\t 考生姓名:"; cin >> p->data.name; cout << "\t 考生性别:"; cin >> p->data.gender; cout << "\t 考生年龄:"; cin >> p->data.age; cout << "\t 语文成绩:"; cin >> p->data.Chinese; cout << "\t 数学成绩:"; cin >> p->data.Maths; cout << "\t 英语成绩:"; cin >> p->data.English; p->data.sum = p->data.Chinese + p->data.Maths + p->data.English; p->data.everage = p->data.sum / 3.0; cout << endl << "\t修改成功!" << endl << endl; } else cout << endl << "\t学号有误!" << endl << endl; } void Flunk(LinkList L, int i) { LinkList p = new LNode; p = L->next; int flunkNum = 0; switch (i) { case 1: cout << "\t 语文不及格名单如下:" << endl << endl; while (p) { if (p->data.Chinese < 60) { cout << "\t\t姓名:" << p->data.name; cout << " 语文:" << p->data.Chinese << endl << endl; flunkNum++; } p = p->next; } if (!flunkNum) cout << "\t 语文成绩全部及格!" << endl << endl; break; case 2: cout << "\t 数学不及格名单如下:" << endl << endl; while (p) { if (p->data.Maths < 60) { cout << "\t\t姓名:" << p->data.name; cout << " 数学:" << p->data.Maths << endl << endl; flunkNum++; } p = p->next; } if (!flunkNum) cout << "\t 数学成绩全部及格!" << endl << endl; break; case 3: cout << "\t 英语不及格名单如下:" << endl << endl; while (p) { if (p->data.English < 60) { cout << "\t\t姓名:" << p->data.name; cout << " 英语:" << p->data.English << endl << endl; flunkNum++; } p = p->next; } if (!flunkNum) cout << "\t 英语成绩全部及格!" << endl << endl; break; default: cout << endl << "\t 输入有误!" << endl << endl; break; } } void Ranking(LinkList &L) { LinkList p = new LNode, q = new LNode; ElemType e; p = L->next; for (int i = 0; i < ListLength(L); i++) { if (p->next) q = p->next; for (int j = i + 1; j < ListLength(L); j++) { if (p->data.sum < q->data.sum) { e = p->data; p->data = q->data; q->data = e; } if (q) q = q->next; } if (p) p = p->next; } } void Operation() {//用户可以循环选择操作类型 int j = -1; while (j) { cout << "\t 1为插入\t\t 2为删除\t\t 3为查找" << endl; cout << "\t 4为修改\t\t 5为统计\t\t 6列出成绩单" << endl; cout << "\t 7查询不及格成绩\t 8根据总分排名\t\t 0为退出" << endl; cout << endl << " 请选择您要进行的操作:"; cin >> j; cout << endl; switch (j) { case 1: { //插入 cout << " 请输入你要插入的考生的位置:"; int i; cin >> i; ElemType e; cout << " 请输入考生信息:" << endl << endl; cout << "\t 准考证号:"; cin >> e.examnum; cout << "\t 考生姓名:"; cin >> e.name; cout << "\t 考生性别:"; cin >> e.gender; cout << "\t 考生年龄:"; cin >> e.age; cout << "\t 语文成绩:"; cin >> e.Chinese; cout << "\t 数学成绩:"; cin >> e.Maths; cout << "\t 英语成绩:"; cin >> e.English; e.sum = e.Chinese + e.Maths + e.English; e.everage = e.sum / 3.0; ListInsert(L, i, e); break; } case 2: {//删除 cout << " 请输入你要删除的考生的考号:"; ElemType e; cin >> e.examnum; int i = LocateElem(L, e); if (i) if (ListDelete(L, i, e)) cout << endl << " 删除成功!" << endl << endl; else cout << endl << " 删除失败!" << endl << endl; else cout << endl << " 不存在此考生!" << endl << endl; break; } case 3: {//查找 cout << " 请输入你要查询的考生的序号:"; int i; ElemType e; cin >> i; if (GetElem(L, i, e)) { cout << "\t第" << i << "个考生的信息为:" << endl; cout << "\t 准考证号:" << e.examnum << endl; cout << "\t 考生姓名:" << e.name << endl; cout << "\t 考生性别:" << e.gender << endl; cout << "\t 考生年龄:" << e.age << endl; cout << "\t 语文成绩:" << e.Chinese << endl; cout << "\t 数学成绩:" << e.Maths << endl; cout << "\t 英语成绩:" << e.English << endl << endl; } else { cout << "\t 序号有误!" << endl << endl; } break; } case 4: {//修改 cout << " 请输入你要修改信息的考生的考号:"; int examnum; cin >> examnum; UpdateByNo(L, examnum); break; } case 5: {//统计 cout << "\t 报考人数为:" << ListLength(L) << endl << endl; ListTraverse(L); break; } case 6:ListTraverse(L); break;//输出 case 7://列出不及格成绩 cout << "\t 1语文\t 2数学\t 3英语" << endl << endl; cout << "\t请输入您要查询不及格科目的序号:"; cin >> j; Flunk(L, j); break; case 8://根据总分排名 Ranking(L); cout << endl << " 排名完成!成绩单如下:" << endl << endl; ListTraverse(L); break; case 0: return; default: cout << endl << "该操作不存在,请输入正确操作!" << endl << endl; break; } } } //主函数,调用各个函数实现相应功能 void main() { int n; cout << "\t\t\t\t*\t\t\t\t\t*"; cout << "\t\t\t\t*****************************************" << endl << endl; cout << " 正在为您准备初始化链表,请输入您要输入的学生个数:"; cin >> n; CreateList(L, n);//创建初始链表 cout << endl << " 初始化成功," << n << "个考生的信息已存储。" << endl << endl; Operation(); }
已有 18279 位网友参与,快来吐槽:
发表评论