咳咳,上一次写的链接 点击可至"乞丐版"
增强版仅针对自己,之前写过一个“乞丐版”的学生信息管理系统,由于对函数模块化思想不够了解,写的比较拉跨,这次来做一个提高吧!
这次就来简单说一说函数吧!
void InputStuData()
{
int n;
printf("请输入录入学生的人数:\n");
while (scanf("%d", &n) != 1)
{
printf("输入有误!请重新输入:\n");
fflush(stdin);
}
printf("人数录入完毕,请录入%d位学生的信息:\n", n);
printf("以学号、姓名、专业和语、数、外三门课成绩为顺序\n");
printf("例如:E02135292 张三 计科 85 79 94\n");
while (n --)
{
Stu * temp = (Stu *)malloc(sizeof(Stu));
if (temp == NULL)
{
printf("内存分配出错!\n");
exit(0);
}
else
{
scanf("%s %s %s", &temp->nums, &temp->name, &temp->major);
scanf("%f %f %f", &temp->score[0], &temp->score[1], &temp->score[2]);
temp->next = NULL;
if (head == NULL)//刚开始头节点为空
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
return;
}
Stu * SearchStudent()
{
char str[20], ch;
Repeat:
printf("请输入要寻找(正确的)学号或者姓名按'#'退出\n");
scanf("%s", str);
getchar();
if (str[0] == '#') return NULL;
Stu * temp = head;
if (head == NULL)
{
printf("未录入信息!\n");
return NULL;
}
else
{
while (temp)
{
if (!strcmp(temp->nums, str) || !strcmp(temp->name, str))
break;
temp = temp->next;
}
}
if (temp == NULL)
{
printf("未找到相关信息!\n");
goto Repeat;
}
else
{
printf("查找成功,该同学信息如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
}
printf("是否确认查询无误?(y or no)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y')
goto Repeat;
printf("已确认该名同学的信息!\n");
return temp;
}
该函数返回一个结构体指针,即为查找位置的指针
void ModifyStuData()
{
char str[20], ch;
Stu * temp = SearchStudent();
if (temp == NULL) printf("未指定修改内容位置!\n");
else
{
int num;
Repeat:
printf("请选择需要修改的信息:\n");
printf("1. 学号 2. 姓名 3. 专业\n");
printf("4. 语文成绩 5. 数学成绩 6. 英语成绩\n");
while (scanf("%d", &num) != 1 || num > 6 || num < 1)
{
printf("选择无效,请重新输入范围内的数字:\n");
fflush(stdin);
}
switch (num)
{
case 1:
printf("请输入正确的学号:\n");
scanf("%s", temp->nums); break;
case 2:
printf("请输入正确的名字:\n");
scanf("%s", temp->name); break;
case 3:
printf("请输入正确的专业:\n");
scanf("%s", temp->major); break;
case 4:
printf("请输入正确的语文成绩:\n");
scanf("%f", &temp->score[0]); break;
case 5:
printf("请输入正确的数学成绩:\n");
scanf("%f", &temp->score[1]); break;
case 6:
printf("请输入正确的英语成绩:\n");
scanf("%f", &temp->score[2]); break;
}
getchar();
printf("信息更改后如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
printf("是否确认无误?(y or n)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y') goto Repeat;
printf("您已确认,更改完成!\n");
}
return;
}
修改信息需要用到上面查找函数,找到定位后,根据选项进行更改
void RankingList()
{
int i;
printf("请选择学科进行成绩排名:\n");
printf("1. 语文 2. 数学 3. 英语\n");
while (scanf("%d", &i) && i > 3 && i < 1)
printf("请输入范围内的学科!\n");
fflush(stdin);
Stu * pre, * p, * end = NULL;
pre = p = head;
while (p->next != end)
{
p = head->next;
while (p != end)
{
if (pre->score[i-1] > p->score[i-1])
{
SwapStuNode(pre, p);
Stu * temp = pre;
pre = p;
p = temp;
}
pre = pre->next;
p = p->next;
}
end = pre;
pre = p = head;
}
return;
}
void SwapStuNode(Stu * p, Stu * q)
{
if (p == q || p == NULL || q == NULL)
return;
if (p == head)
{
if (q == tail)
{
tail->next = p;
p->next = NULL;
head = tail;
tail = head->next;
}
else
{
p->next = q->next;
q->next = p;
head = q;
}
}
else
{
if (q == tail)
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
q->next = p;
p->next = NULL;
tail = p;
}
else
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
p->next = q->next;
q->next = p;
}
}
return;
}
这里的排名实际上用到是冒泡排序,这里为了好理解,用了最笨的方法,实际上链表排序方法多样,本人曾经总结过单链表冒泡、插入排序的实现和优化,给出链接单链表冒泡、插入排序的实现和优化
void PrintStuList()
{
Stu * phead = head;
if (phead == NULL) printf("暂无学生信息!\n");
while (phead)
{
printf("%10s %10s %10s", phead->nums, phead->name, phead->major);
printf("%10.2f %10.2f %10.2f\n",phead->score[0],phead->score[1],phead->score[2]);
phead = phead -> next;
}
return;
}
从头开始,一直到尾部,尾部指针域为空
#include
#include
#include
#include
typedef struct Stu
{
char nums[10];
char name[20], major[20];
float score[3];
struct Stu * next;
}Stu;
Stu * head = NULL;
Stu * tail = NULL;
void MainMenuSelect();//主选择菜单
void InputStuData();//录入信息
Stu * SearchStudent();//学号查询、姓名查询
void ModifyStuData();//修改信息
void DeleteStuData();//删除信息
void RankingList();//成绩排名
void PrintStuList();//打印链表
void SwapStuNode(Stu * p, Stu * q);//交换节点
int main()
{
int num; char ch;
do
{
MainMenuSelect();
printf("请选择一个操作:\n");
while (scanf("%d", &num) != 1 || num < 0 || num > 6 )
{
printf("您的输入不正确,请重新输入:\n");
fflush(stdin);
}
switch (num)
{
case 0:
printf("谢谢使用,再见!\n");
return 0;
case 1:
InputStuData(); break;
case 2:
SearchStudent(); break;
case 3:
ModifyStuData(); break;
case 4:
DeleteStuData(); break;
case 5:
RankingList(); break;
case 6:
PrintStuList(); break;
}
printf("本次任务结束,回主菜单或退出程序:(y or n)\n");
scanf(" %c", &ch);
} while (ch == 'Y' || ch == 'y');
printf("谢谢使用!再见\n");
return 0;
}
void MainMenuSelect()
{
printf("---------------主菜单-------------\n");
printf("1. 录入学生信息 2. 查询学生信息\n");
printf("3. 修改学生信息 4. 删除学生信息\n");
printf("5. 学生成绩排名 6. 打印学生信息\n");
printf("-----------0. 退出系统------------\n");
return;
}
void InputStuData()
{
int n;
printf("请输入录入学生的人数:\n");
while (scanf("%d", &n) != 1)
{
printf("输入有误!请重新输入:\n");
fflush(stdin);
}
printf("人数录入完毕,请录入%d位学生的信息:\n", n);
printf("以学号、姓名、专业和语、数、外三门课成绩为顺序\n");
printf("例如:E02135292 张三 计科 85 79 94\n");
while (n --)
{
Stu * temp = (Stu *)malloc(sizeof(Stu));
if (temp == NULL)
{
printf("内存分配出错!\n");
exit(0);
}
else
{
scanf("%s %s %s", &temp->nums, &temp->name, &temp->major);
scanf("%f %f %f", &temp->score[0], &temp->score[1], &temp->score[2]);
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
return;
}
Stu * SearchStudent()
{
char str[20], ch;
Repeat:
printf("请输入要寻找(正确的)学号或者姓名按'#'退出\n");
scanf("%s", str);
getchar();
if (str[0] == '#') return NULL;
Stu * temp = head;
if (head == NULL)
{
printf("未录入信息!\n");
return NULL;
}
else
{
while (temp)
{
if (!strcmp(temp->nums, str) || !strcmp(temp->name, str))
break;
temp = temp->next;
}
}
if (temp == NULL)
{
printf("未找到相关信息!\n");
goto Repeat;
}
else
{
printf("查找成功,该同学信息如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
}
printf("是否确认查询无误?(y or no)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y')
goto Repeat;
printf("已确认该名同学的信息!\n");
return temp;
}
void ModifyStuData()
{
char str[20], ch;
Stu * temp = SearchStudent();
if (temp == NULL) printf("未指定修改内容位置!\n");
else
{
int num;
Repeat:
printf("请选择需要修改的信息:\n");
printf("1. 学号 2. 姓名 3. 专业\n");
printf("4. 语文成绩 5. 数学成绩 6. 英语成绩\n");
while (scanf("%d", &num) != 1 || num > 6 || num < 1)
{
printf("选择无效,请重新输入范围内的数字:\n");
fflush(stdin);
}
switch (num)
{
case 1:
printf("请输入正确的学号:\n");
scanf("%s", temp->nums); break;
case 2:
printf("请输入正确的名字:\n");
scanf("%s", temp->name); break;
case 3:
printf("请输入正确的专业:\n");
scanf("%s", temp->major); break;
case 4:
printf("请输入正确的语文成绩:\n");
scanf("%f", &temp->score[0]); break;
case 5:
printf("请输入正确的数学成绩:\n");
scanf("%f", &temp->score[1]); break;
case 6:
printf("请输入正确的英语成绩:\n");
scanf("%f", &temp->score[2]); break;
}
getchar();
printf("信息更改后如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
printf("是否确认无误?(y or n)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y') goto Repeat;
printf("您已确认,更改完成!\n");
}
return;
}
void DeleteStuData()
{
if (head == NULL)
{
printf("还未录入信息!\n");
return;
}
Stu * temp = SearchStudent();
if (temp == NULL)
printf("未找到相关信息!\n");
else if (head == tail)
{
free(head);
head = NULL;
tail = NULL;
printf("删除后已没有任何学生信息!\n");
}
else if (temp == head)
{
head = head->next;
free(temp);
printf("已完成删除操作!\n ");
}
else if (temp == tail)
{
Stu * pt = head;
while (pt->next->next) pt = pt->next;
free(tail);
tail = pt;
tail -> next = NULL;
printf("已完成删除操作!\n ");
}
else
{
Stu * pt = head;
while (pt->next != temp) pt = pt->next;
pt->next = temp->next;
free(temp);
printf("已完成删除操作!\n ");
}
return;
}
void RankingList()
{
int i;
printf("请选择学科进行成绩排名:\n");
printf("1. 语文 2. 数学 3. 英语\n");
while (scanf("%d", &i) && i > 3 && i < 1)
printf("请输入范围内的学科!\n");
fflush(stdin);
Stu * pre, * p, * end = NULL;
pre = p = head;
while (p->next != end)
{
p = head->next;
while (p != end)
{
if (pre->score[i-1] > p->score[i-1])
{
SwapStuNode(pre, p);
Stu * temp = pre;
pre = p;
p = temp;
}
pre = pre->next;
p = p->next;
}
end = pre;
pre = p = head;
}
return;
}
void SwapStuNode(Stu * p, Stu * q)
{
if (p == q || p == NULL || q == NULL)
return;
if (p == head)
{
if (q == tail)
{
tail->next = p;
p->next = NULL;
head = tail;
tail = head->next;
}
else
{
p->next = q->next;
q->next = p;
head = q;
}
}
else
{
if (q == tail)
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
q->next = p;
p->next = NULL;
tail = p;
}
else
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
p->next = q->next;
q->next = p;
}
}
return;
}
void PrintStuList()
{
Stu * phead = head;
if (phead == NULL) printf("暂无学生信息!\n");
while (phead)
{
printf("%10s %10s %10s", phead->nums, phead->name, phead->major);
printf("%10.2f %10.2f %10.2f\n",phead->score[0],phead->score[1],phead->score[2]);
phead = phead -> next;
}
return;
}
谢谢观看,如有错误还请指正!