C语言作业-成绩管理
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> // 内存操作头文件
#include <string.h> // 字符串操作头文件
typedef struct student {
char Id[30]; // 学号 备注:不可重复
char Name[30]; // 名字
char Gender[10]; // 性别
int Age; // 年龄
int AE; // analog electronic (模拟电子)
int C; // C Language (C语言)
int singlechip; // 单片机
int CAD; // 电子CAD
int OSHD; // Open Source Hardware Development(开源硬件开发)
struct student * pNext;
}Student, *Pstudent;
// 定义函数
void UI(void);
void UI2(void);
Pstudent createLisr(int len); // 新建数据档案
void printList(Pstudent pHead); // 输出
bool insertList(Pstudent pHead, int pos); // 插入 pos:链表下标值 注:数据不可为空!
bool deleteList(Pstudent pHead, int pos); // 删除
int lengthList(Pstudent pHead); // 判断长度
void sortList(Pstudent pHead, int subject); // 排序
void inquireList(Pstudent pHead, char ID[]); // 查询
int main (void)
{
int i = 0, j = 0, pos = 0, subject = 0;
Pstudent student;
char ID[30];
while (true) {
UI ();
printf("请输入:");
scanf("%d",&i);
switch (i) {
case 0:
return 0;
case 1:
printf("请输入新建数据档案人数:");
scanf("%d",&j);
student = createLisr(j);
break;
case 2:
insertList(student, 1);
break;
case 3:
printList(student);
printf("\n请输入删除的数据的编号:");
scanf("%d",&pos);
deleteList(student, pos);
break;
case 4:
UI2();
printf("\n请输入:");
scanf("%d",&subject);
sortList(student, subject);
break;
case 5:
printf("\n请输入要查询的学生学号:");
scanf("%s",&ID);
inquireList(student, ID);
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
void UI () {
system("cls");
printf("**********学生成绩管理系统**********\n");
printf("\n");
printf(" 1:新建数据档案\n");
printf(" 2:添加数据\n");
printf(" 3:删除数据\n");
printf(" 4:排序\n");
printf(" 5:查询\n");
printf(" 0:退出\n");
printf("\n");
printf("************************************\n");
printf("\n");
printf("请输入序号:");
printf("\n");
}
void UI2 () {
system("cls");
printf("**********学生成绩单科排名**********\n");
printf("\n");
printf(" 1:模拟电子\n");
printf(" 2:C语言程序设计\n");
printf(" 3:单片机程序设计与制作\n");
printf(" 4:电子CAD\n");
printf(" 5:开源硬件开发\n");
printf("\n");
printf("************************************\n");
printf("\n");
printf("请输入序号:");
printf("\n");
}
Pstudent createLisr(int len) {
system("cls");
Pstudent Head, Node, End;
Head = (Pstudent)malloc(sizeof(Student));
if (NULL == Head) {
printf("分配失败\n");
exit(-1);
}
End = Head;
printf("提示:请按(格式)格式依次输入\n");
for (int i = 0; i < len; i++) {
Node = (Pstudent)malloc(sizeof(Student));
if (NULL == Node) {
printf("分配失败\n");
exit(-1);
}
printf("请输入第%d的值。\n", i + 1);
printf("提示:请按(学号 名字 性别 年龄)格式依次输入\n");
scanf("%s %s %s %d", &Node->Id ,&Node->Name ,&Node->Gender, &Node->Age);
printf("提示:请按(模拟电子 C语言 单片机 CAD 开源硬件开发)格式依次输入\n");
scanf("%d %d %d %d %d", &Node->AE, &Node->C, &Node->singlechip, &Node->CAD, &Node->OSHD);
End->pNext = Node;
End = Node;
}
End->pNext = NULL;
return Head;
}
void printList(Pstudent pHead) {
system("cls");
Pstudent temp = pHead->pNext;
while (temp != NULL) {
printf("\n");
printf("学号\t\t姓名\t性别\t年龄\t模拟电子\tC语言\t单片机\t电子CAD\t开源硬件开发\n\n");
printf("%s\t%s\t%s\t%d\t%d\t\t%d\t%d\t%d\t%d\t\n",
temp->Id, temp->Name, temp->Gender, temp->Age,
temp->AE, temp->C, temp->singlechip, temp->CAD, temp->OSHD);
temp = temp->pNext;
}
}
bool insertList(Pstudent pHead, int pos) {
system("cls");
int i = 0;
Pstudent p = pHead;
while (NULL != p && i < pos - 1) {
p = p->pNext;
i ++;
}
if (i > pos - 1 || NULL == p) {
return false;
}
Pstudent pNew = (Pstudent)malloc(sizeof(Student));
if (NULL == pNew) {
printf("分配失败\n");
exit(-1);
}
printf("提示:请按(学号 名字 性别 年龄)格式依次输入\n");
scanf("%s %s %s %d", &pNew->Id ,&pNew->Name ,&pNew->Gender, &pNew->Age);
printf("提示:请按(模拟电子 C语言 单片机 CAD 开源硬件开发)格式依次输入\n");
scanf("%d %d %d %d %d", &pNew->AE, &pNew->C, &pNew->singlechip, &pNew->CAD, &pNew->OSHD);
Pstudent q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
return true;
}
bool deleteList(Pstudent pHead, int pos) {
int i = 0;
Pstudent p = pHead;
while (NULL != p && i < pos - 1) {
p = p->pNext;
i ++;
}
if (i > pos - 1 || NULL == p->pNext) {
return false;
}
Pstudent q = p->pNext;
p-> pNext = p->pNext->pNext;
free(q);
q = NULL;
return true;
}
int lengthList(Pstudent pHead) {
int length = 0;
Pstudent temp = pHead->pNext;
while (temp != NULL) {
temp = temp->pNext;
length ++;
}
return length;
}
void sortList(Pstudent pHead, int subject) {
system("cls");
Pstudent p, q;
int i, j, t, len = lengthList(pHead);
char t1[30], t2[30];
for (
i = 0, p = pHead->pNext;
i < len -1;
i ++, p = p->pNext) {
for (
j = i + 1, q = p->pNext;
j < len;
j++, q = q->pNext) {
// analog electronic (模拟电子)
if (subject == 1) {
if (p->AE < q->AE) {
t = p->AE;
p->AE = q->AE;
q->AE = t;
strcpy(t1, p->Id);
strcpy(p->Id, q->Id);
strcpy(q->Id, t1);
strcpy(t2, p->Name);
strcpy(p->Name, q->Name);
strcpy(q->Name, t2);
}
}
// C Language (C语言)
if (subject == 2) {
if (p->C < q->C) {
t = p->C;
p->C = q->C;
q->C = t;
strcpy(t1, p->Id);
strcpy(p->Id, q->Id);
strcpy(q->Id, t1);
strcpy(t2, p->Name);
strcpy(p->Name, q->Name);
strcpy(q->Name, t2);
}
}
// 单片机
if (subject == 3) {
if (p->singlechip < q->singlechip) {
t = p->singlechip;
p->singlechip = q->singlechip;
q->singlechip = t;
strcpy(t1, p->Id);
strcpy(p->Id, q->Id);
strcpy(q->Id, t1);
strcpy(t2, p->Name);
strcpy(p->Name, q->Name);
strcpy(q->Name, t2);
}
}
// 电子CAD
if (subject == 4) {
if (p->CAD < q->CAD) {
t = p->CAD;
p->CAD = q->CAD;
q->CAD = t;
strcpy(t1, p->Id);
strcpy(p->Id, q->Id);
strcpy(q->Id, t1);
strcpy(t2, p->Name);
strcpy(p->Name, q->Name);
strcpy(q->Name, t2);
}
}
// Open Source Hardware Development(开源硬件开发)
if (subject == 5 ){
if (p->OSHD < q->OSHD) {
t = p->OSHD;
p->OSHD = q->OSHD;
q->OSHD = t;
strcpy(t1, p->Id);
strcpy(p->Id, q->Id);
strcpy(q->Id, t1);
strcpy(t2, p->Name);
strcpy(p->Name, q->Name);
strcpy(q->Name, t2);
}
}
}
}
system("cls");
Pstudent temp = pHead->pNext;
while (temp != NULL) {
switch (subject) {
case 1:
printf("学号\t\t姓名\t模拟电子\n\n");
printf("%s\t\t%s\t%d\t\n",
temp->Id, temp->Name,
temp->AE);
break;
case 2:
printf("学号\t\t姓名\tC语言\n\n");
printf("%s\t%s\t%d\t\n",
temp->Id, temp->Name,
temp->C);
break;
case 3:
printf("学号\t\t姓名\t单片机\n\n");
printf("%s\t\t%s\t%d\t\n",
temp->Id, temp->Name,
temp->singlechip);
break;
case 4:
printf("学号\t\t姓名\t电子CAD\n\n");
printf("%s\t\t%s\t%d\t\n",
temp->Id, temp->Name,
temp->CAD);
break;
case 5:
printf("学号\t\t姓名\t开源硬件开发\n\n");
printf("%s\t\t%s\t%d\t\n",
temp->Id, temp->Name,
temp->OSHD);
break;
}
temp = temp->pNext;
}
system("pause");
return;
}
void inquireList(Pstudent pHead, char ID[]) {
system("cls");
Pstudent temp = pHead->pNext;
while (temp != NULL) {
if (strcmp(temp->Id, ID) == 0) {
printf("\n");
printf("学号\t\t姓名\t性别\t年龄\t模拟电子\tC语言\t单片机\t电子CAD\t开源硬件开发\n\n");
printf("%s\t%s\t%s\t%d\t%d\t\t%d\t%d\t%d\t%d\t\n",
temp->Id, temp->Name, temp->Gender, temp->Age,
temp->AE, temp->C, temp->singlechip, temp->CAD, temp->OSHD);
}
temp = temp->pNext;
}
system("pause");
}