赞
踩
语言:
C
平台:
VS2017
程序打包下载:
https://download.csdn.net/download/qq_41027556/15647720
功能:
学生信息的读取
姓名/学号/成绩的排序
学生姓名或学号进行查询
学生增删
数量固定
代码:
# include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define maxsize 50
char names[50][maxsize];
//《苏-C程序设计》 P254 字符串数组
int num[maxsize];
int mandarin[maxsize];
int math[maxsize];
int english[maxsize];
int sum_score[maxsize];
int i, n,j,m;
//int sum[maxsize];
char temp_name[50][maxsize];
int temp_num[maxsize];
int temp_mandarin[maxsize];
int temp_math[maxsize];
int temp_eng[maxsize];
int temp_sum[maxsize];
int sequence[maxsize];
int students_show_menu();
void students_input();
void students_show();
int students_numfind();
int student_namefind();
void num_seq();
void name_seq();
void students_add();
int students_del();
int main()
{
int choose;
while (1)
{
students_show_menu();
printf("请输入您的选择:");
scanf_s("%d", &choose);
switch (choose)
{
case(1)://输入学生信息
students_input();
break;
case(2)://输出全部信息
students_show();
break;
case(3)://学号查找
students_numfind();
break;
case(4)://姓名查找
student_namefind();
break;
case(5)://学号排序
num_seq();
break;
case(6)://姓名排序
name_seq();
break;
case(7)://添加
students_add();
break;
case(8)://删除
students_del();
break;
case (0)://退出
printf("\n退出系统成功\n");
printf("欢迎再次使用【学生信息管理系统v1.0】\n\n");
exit(0);
//正常退出(如果main()在一个递归程序中,exit()仍然会终止程序;但return将控制权移交给递归的前一级)
default:
printf("\n您输入的操作有误。请重新输入!\n");
break;
}
}
}
int students_show_menu() //显示菜单
{
printf("\n");
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
printf("欢迎使用学生信息管理系统v1.0\n\n");
printf("1.输入全部学生信息\n");
printf("2.读取全部学生信息\n");
printf("3.按学号查询学生\n");
printf("4.按姓名查询学生\n");
printf("5.按学号排序学生\n");
printf("6.按成绩排序学生\n");
printf("7.添加学生信息\n");
printf("8.删除学生信息\n");
printf("0.退出系统\n\n");
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
return 0;
}
void students_input() //输入所有学生信息
{
printf("\n【1】输入全部学生信息\n");
printf("输入学生个数:");
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
printf("\n请输入第%d个学生的信息:\n", i + 1);
printf("请输入学生姓名:");
scanf_s("%s", names[i],50);
printf("请输入学生学号:");
scanf_s("%d", &num[i]);
printf("请输入学生语文成绩:");
scanf_s("%d",&mandarin[i]);
printf("请输入学生数学成绩:");
scanf_s("%d", &math[i]);
printf("请输入学生英语成绩:");
scanf_s("%d", &english[i]);
sum_score[i] = mandarin[i] + math[i] + english[i];
}
printf("\n添加学生信息成功!\n");
}
void students_show() //学生信息的读取
{
printf("\n【2】读取全部学生信息\n");
int len = strlen(*names);
if (len == 0)
{
printf("\n当前没有任何学生信息记录! 请输入学生信息。\n");
return;
}
printf("\n姓名\t\t学号\t\t语文成绩\t数学成绩\t英语成绩\t总成绩\t\t\n");
for ( i = 1; i < 100; i++)
printf("-");
printf("\n");
for (j = 0; j< n+m; j++)
{
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", names[j], num[j], mandarin[j],
math[j],english[j], sum_score[j]);
}
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
}
int students_numfind() //按学号查找
{
printf("【3】按学号查询学生\n");
int nums;
int find_num = -1;
printf("\n请输入待查找学生学号:");
scanf_s("%d", &nums);
for (i = 0; i < n+m; i++)
{
if (nums == num[i])
{
find_num = i;
break;
}
}
if (find_num == -1)
{
printf("\n抱歉,没有找到该学生的信息! \n");
return 0;
}
else
{
printf("\n查找学生信息成功!\n");
printf("\n姓名\t\t学号\t\t语文成绩\t数学成绩\t英语成绩\t总成绩\t\t\n");
for (i = 1; i < 100; i++)
printf("-");
printf("\n%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", names[find_num], num[find_num], mandarin[find_num],
math[find_num], english[find_num], sum_score[find_num]);
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
}
return 1;
}
int student_namefind() //按姓名查找
{
printf("【4】按姓名查询学生\n");
char name_str[50];
int find_name = -1;
printf("\n请输入待查找学生姓名:");
scanf_s("%s", name_str,50); //此值应小于定义(50)
//输入参数是已经定义好的“字符数组名”, 不用加&
//在C语言中数组名就代表该数组的起始地址
for (i = 0; i < n+m; i++)
{
if (strcmp(name_str, names[i]) == 0)
/*strcmp函数是string compare(字符串比较)的缩写,用于比较两个字符串并根据比较结果返回整数。
基本形式为strcmp(str1,str2),若str1=str2,则返回零;若str1str2,则返回正数。*/
{
find_name = i;
break;
}
}
if (find_name == -1)
{
printf("\n抱歉,没有找到该学生的信息! \n");
return 0;
}
else
{
printf("\n查找学生信息成功!\n");
printf("\n姓名\t\t学号\t\t语文成绩\t数学成绩\t英语成绩\t总成绩\t\t\n");
for (i = 1; i < 100; i++)
printf("-");
printf("\n%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", names[find_name], num[find_name], mandarin[find_name],
math[find_name], english[find_name], sum_score[find_name]);
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
}
return 1;
}
void num_seq() //按学号排序
{
printf("\n【5】按学号排序学生\n");
int max, min;
for (i = 0; i < n+m; i++)
{
temp_num[i] = num[i];
}
max = 0;
for (j = 1; j < n+m; j++)
{
if (temp_num[max] < temp_num[j])
{
max = j;
}
}
sequence[n +m- 1] = max;
for (i = 0; i < n+m-1; i++)
{
min = i;
for (j = 0; j < n+m; j++)
{
if (temp_num[min] > temp_num[j])
{
min = j;
}
}
sequence[i] = min;
temp_num[min] = temp_num[max];
//利用临时数组将查找过的学生信息的学号设为最大值,排除查找的干扰
}
for (i = 0; i < n+m; i++)
{
//strcpy(temp_name[i], names[i]); //strcpy(a,b); 将b中的COPY复制到a中
strcpy_s(temp_name[i], 50 * sizeof(char),names[i]);/*若新建项目时不选选择安全开发生命周期(SDL)检查
运行上一句没有问题,同一语句在 Devc++ 中可以运行 */
//*temp_name[i] = names[i];
//memcpy(temp_name, names,sizeof(names) );
temp_num[i] = num[i];
temp_mandarin[i] = mandarin[i];
temp_math[i] = math[i];
temp_eng[i] = english[i];
temp_sum[i] = sum_score[i];
}
for(i=0; i<n+m; i++)
{
//strcpy(names[i], temp_name[sequence[i]]);
strcpy_s(names[i], 50 * sizeof(char),temp_name[sequence[i]]);
//*names[i] = temp_name[sequence[i]];
//memcpy(names, temp_name,sizeof(temp_name) );
num[i] = temp_num[sequence[i]];
mandarin[i] = temp_mandarin[sequence[i]];
math[i] = temp_math[sequence[i]];
english[i] = temp_eng[sequence[i]];
sum_score[i] = temp_sum[sequence[i]];
}
printf("\n排序完毕,结果如下:\n");
printf("\n姓名\t\t学号\t\t语文成绩\t数学成绩\t英语成绩\t总成绩\t\t\n");
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
for (j = 0; j< n+m; j++)
{
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", names[j], num[j], mandarin[j],
math[j], english[j], sum_score[j]);
}
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
return;
}
void name_seq() //按成绩排序
{
printf("\n【6】按成绩排序学生\n");
int max,min;
for (i = 0; i < n+m ; i++)
{
temp_sum[i] = sum_score[i];
}
max = 0;
for (j = 0; j < n+m; j++)
{
if (temp_sum[max] < temp_sum[j])
max = j;
}
sequence[n+m - 1] = max;
for (i = 0; i < n+m - 1; i++)
{
min = i;
for (j = 0; j < n+m; j++)
{
if (temp_sum[min] > temp_sum[j])
min = j;
}
sequence[i] = min;
temp_sum[min] = temp_sum[max];
}
for (i = 0; i < n+m; i++)
{
strcpy_s(temp_name[i], 50 * sizeof(char), names[i]);
temp_num[i] = num[i];
temp_mandarin[i] = mandarin[i];
temp_math[i] = math[i];
temp_eng[i] = english[i];
temp_sum[i] = sum_score[i];
}
for (i = 0; i < n+m; i++)
{
strcpy_s(names[i], 50 * sizeof(char), temp_name[sequence[i]]);
num[i] = temp_num[sequence[i]];
mandarin[i] = temp_mandarin[sequence[i]];
math[i] = temp_math[sequence[i]];
english[i] = temp_eng[sequence[i]];
sum_score[i] = temp_sum[sequence[i]];
}
printf("\n排序完毕,结果如下:\n");
printf("\n姓名\t\t学号\t\t语文成绩\t数学成绩\t英语成绩\t总成绩\t\t\n");
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
for (j = 0; j< n+m; j++)
{
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n", names[j], num[j], mandarin[j],
math[j], english[j], sum_score[j]);
}
for (i = 1; i < 100; i++)
printf("-");
printf("\n");
return;
}
void students_add() //新增学生信息
{
printf("\n【7】添加学生信息\n");
printf("输入增加学生个数:");
scanf_s("%d", &m);
for (i = n; i < n+m; i++)
{
printf("\n请输入第%d个(除去已有学生,则为第%d个)学生的信息:\n", i + 1 , i-n+1);
printf("请输入学生姓名:");
scanf_s("%s", names[i], 50);
printf("请输入学生学号:");
scanf_s("%d", &num[i]);
printf("请输入学生语文成绩:");
scanf_s("%d", &mandarin[i]);
printf("请输入学生数学成绩:");
scanf_s("%d", &math[i]);
printf("请输入学生英语成绩:");
scanf_s("%d", &english[i]);
sum_score[i] = mandarin[i] + math[i] + english[i];
}
printf("\n添加学生信息成功!\n");
}
int students_del() //删除学生信息
{
printf("\n【8】删除学生信息\n");
char name_f[maxsize];
int findstu=-1;
printf("请输入需要删除的学生信息:");
scanf_s("%s", name_f,50);
for (i = 0; i < n + m; i++)
{
if (strcmp(name_f, names[i]) == 0)
{
findstu = i;
break;
}
}
if (findstu == -1)
{
printf("\n没有该学生的信息! \n");
return 0;
}
else
{
for (i = findstu; i < n + m - 1; i++)
{
strcpy_s(names[i], 50 * sizeof(char), names[i + 1]);
num[i] = num[i + 1];
mandarin[i] = mandarin[i + 1];
math[i] = math[i + 1];
english[i] = english[i + 1];
sum_score[i] = sum_score[i + 1];
}
n--;
}
printf("删除成功!\n");
return 1;
}
功能:
同上
信息读取
排序
查询
无限增删
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxsize 50
typedef struct
{
char names[maxsize];
int num;
int math;
int english;
int sum_score;
}student;
student f;
student f2;
student in;
student dl;
typedef struct LNode
{
student data;
struct LNode *next;
}LNode;
int i, j, n, m;
int student_show_menu();
void student_show(LNode *head); //读取全部学生信息
void student_input(LNode *head); //输入学生信息
int name_find(LNode *head, student f); //按姓名查询学生
int num_find(LNode *head, student f2); //按学号查找学生
void num_seq(LNode *head); //按学号排序
void sum_seq(LNode *head); //按总成绩排序
void stu_insert(LNode *head, student in); //增加学生信息
void stu_del(LNode *head, student *dl); //删除学生信息
int main()
{
LNode *head = (LNode*)malloc(sizeof(LNode));
head->next = NULL;
int choose;
while (1)
{
student_show_menu();
printf("请输入您的选择:");
scanf_s("%d", &choose);
switch (choose)
{
case(1):
student_input(head);//输入信息
break;
case(2):
student_show(head); //显示信息
break;
case(3):
name_find(head, f); //按姓名查找
break;
case(4):
num_find(head, f2); //按学号查找
break;
case(5):
num_seq(head); //按学号排序
break;
case(6):
sum_seq(head); //按总成绩排序
break;
case(7):
stu_insert(head,in); //增加学生
break;
case(8):
stu_del(head, &dl); //删除学生 (对比*、&) 声明函数的时候指针用*代表指针类型,&获取目标地址传递给指针
//在声明中使用*代表取目标地址存储的值。*是指针引用,&是地址引用
break;
case (0)://退出系统
printf("\n退出系统成功\n");
printf("欢迎再次使用【学生信息管理系统v2.0】\n\n");
exit(0);
default:
printf("\n您输入的操作有误。请重新输入!\n");
break;
}
}
return 0;
}
int student_show_menu() //显示菜单
{
printf("\n");
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
printf("欢迎使用学生信息管理系统v2.0\n\n");
printf("1.输入全部学生信息\n");
printf("2.读取全部学生信息\n");
printf("3.按姓名查询学生\n");
printf("4.按学号查询学生\n");
printf("5.按学号排序学生\n");
printf("6.按总成绩排序学生\n");
printf("7.添加学生信息\n");
printf("8.删除学生信息\n");
printf("0.退出系统\n\n");
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
return 0;
}
void student_input(LNode *head) //输入学生信息
{
printf("\n【1】输入全部学生信息\n");
printf("输入学生个数:");
scanf_s("%d", &n);
LNode *p;
LNode *r = head;
printf("请输入学生信息:\n");
for (i = 0; i < n; i++)
{
p = (LNode*)malloc(sizeof(LNode));
p->next = NULL;
printf("请输入学生姓名:");
scanf_s("%s", p->data.names, maxsize);
printf("请输入学号:");
scanf_s("%d", &p->data.num);
printf("请输入学生数学成绩:");
scanf_s("%d", &p->data.math);
printf("请输入学生英语成绩:");
scanf_s("%d", &p->data.english);
//int *sum = p->data.sum_score;
p->data.sum_score = p->data.math + p->data.english;
r->next = p;
r = r->next;
//free(p); //若使用了此句,后续输出列表会出错。因为每个空间的数据都是有用的,所以不用free。
printf("\n");
}
printf("\n添加学生信息成功!\n");
//return head;
}
void student_show(LNode *head) //读取全部学生信息
{
printf("\n【2】读取全部学生信息\n");
LNode *p;
p = head;
if (p->next == NULL)
{
printf("\n当前没有任何学生信息记录! 请输入学生信息。\n");
return;
}
printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n");
while (p->next != NULL)
{
p = p->next;
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",
p->data.names, p->data.num,p->data.math, p->data.english, p->data.sum_score);
}
}
int name_find(LNode *head,student f) //按姓名查询学生
{
printf("【3】按姓名查询学生\n");
LNode *p;
p = head;
printf("请输入查找学生姓名:");
scanf_s("%s", f.names,maxsize);
while ((p->next != NULL)&&strcmp(f.names,p->data.names)) //str1=str2,则返回零;若str1!=str2,则返回正数
{
p = p->next;
}
if ((p->next == NULL)&& strcmp(f.names, p->data.names))
{
printf("\n抱歉,没有找到该学生的信息! \n");
return 0;
}
else
{
printf("\n查找学生信息成功!\n");
printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n");
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",
p->data.names, p->data.num, p->data.math, p->data.english, p->data.sum_score);
return 1;
}
}
int num_find(LNode *head, student f2) //按学号查找学生
{
printf("【4】按学号查询学生\n");
LNode *p;
p = head;
printf("请输入查找学生学号:");
scanf_s("%d", &f2.num);
while ((p->next != NULL) && (p->data.num != f2.num))
{
p = p->next;
}
if ((p->next == NULL) && (p->data.num != f2.num))
{
printf("\n抱歉,没有找到该学生的信息! \n");
return 0;
}
else
{
printf("\n查找学生信息成功!\n");
printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n");
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",
p->data.names, p->data.num, p->data.math, p->data.english, p->data.sum_score);
return 1;
}
}
void num_seq(LNode *head) //按学号排序
{
printf("\n【5】按学号排序学生\n");
LNode *newhead, *rear;
newhead = head->next;
rear = NULL;
if (newhead == NULL || newhead->next == NULL)
{
printf("\n当前没有任何学生信息记录! 请输入学生信息。\n");
return;
}
while (newhead!=rear)
{
while (newhead->next!=rear)
{
if (newhead->data.num > newhead->next->data.num)
{
int temp = newhead->data.num;
newhead->data.num = newhead->next->data.num;
newhead->next->data.num = temp;
int temp1 = newhead->data.math;
newhead->data.math = newhead->next->data.math;
newhead->next->data.math = temp1;
int temp2 = newhead->data.english;
newhead->data.english = newhead->next->data.english;
newhead->next->data.english = temp2;
int temp3 = newhead->data.sum_score;
newhead->data.sum_score = newhead->next->data.sum_score;
newhead->next->data.sum_score = temp3;
//char temp4_str = gets(newhead->data.names, sizeof(char));
//char *temp4 = newhead->data.names; //先替换了此值,故第二个strcpy使用*temp4已经是新值了。
student str;
strcpy_s(str.names, maxsize, newhead->data.names);
//gets_s(newhead->data.names,maxsize);
//newhead->data.names = newhead->next->data.names;
strcpy_s(newhead->data.names, maxsize, newhead->next->data.names);
//newhead->next->data.names = temp4;
strcpy_s(newhead->next->data.names, maxsize, str.names);
}
newhead = newhead->next;
}
rear = newhead;
newhead = head->next;
}
printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n");
while (head->next != NULL)
{
head = head->next;
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",
head->data.names, head->data.num, head->data.math, head->data.english, head->data.sum_score);
}
}
void sum_seq(LNode *head) //按总成绩排序
{
printf("\n【6】按总成绩排序学生\n");
LNode *newhead, *rear;
newhead = head->next;
rear = NULL;
if (newhead == NULL || newhead->next == NULL)
{
printf("\n当前没有任何学生信息记录! 请输入学生信息。\n");
return;
}
while (newhead != rear)
{
while (newhead->next != rear)
{
if (newhead->data.sum_score < newhead->next->data.sum_score)
{
int temp = newhead->data.num;
newhead->data.num = newhead->next->data.num;
newhead->next->data.num = temp;
int temp1 = newhead->data.math;
newhead->data.math = newhead->next->data.math;
newhead->next->data.math = temp1;
int temp2 = newhead->data.english;
newhead->data.english = newhead->next->data.english;
newhead->next->data.english = temp2;
int temp3 = newhead->data.sum_score;
newhead->data.sum_score = newhead->next->data.sum_score;
newhead->next->data.sum_score = temp3;
student str;
strcpy_s(str.names, strlen(newhead->data.names)+1 , newhead->data.names);
strcpy_s(newhead->data.names, strlen(newhead->next->data.names) + 1, newhead->next->data.names); //注意缓冲区长度,strlen()函数计算的字符串长度是不包括'\0’
strcpy_s(newhead->next->data.names, strlen(str.names) + 1, str.names);
}
newhead = newhead->next;
}
rear = newhead;
newhead = head->next;
}
printf("\n姓名\t\t学号\t\t数学成绩\t英语成绩\t总成绩\t\t\n");
while (head->next != NULL)
{
head = head->next;
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t\n",
head->data.names, head->data.num, head->data.math, head->data.english, head->data.sum_score);
}
}
void stu_insert(LNode *head,student in) //增加学生信息
{
printf("\n【7】添加学生信息\n");
printf("请输入学生信息:\n");
printf("请输入学生姓名:");
scanf_s("%s", in.names, maxsize);
printf("请输入学号:");
scanf_s("%d", &in.num);
printf("请输入学生数学成绩:");
scanf_s("%d", &in.math);
printf("请输入学生英语成绩:");
scanf_s("%d", &in.english);
in.sum_score = in.math + in.english ;
LNode *stu, *r;
r = head;
int d,dp=0;
printf("请输入要插入的位置:");
scanf_s("%d", &d);
while ((r != NULL) && (dp < d-1))
{
r = r->next;
dp = dp + 1;
}
if (r == NULL)
{
printf("\n插入错误,请重新输入。\n");
return;
}
stu = (LNode*)malloc(sizeof(LNode));
stu->data = in;
stu->next = r->next;
r->next = stu;
printf("\n插入信息成功!\n");
}
void stu_del(LNode *head, student *dl) //删除学生信息
{
printf("\n【8】删除学生信息\n");
LNode *stu, *r;
r = head;
int d, dp = 0;
printf("请输入要删除的位置:");
scanf_s("%d", &d);
while ((r->next != NULL) && (dp < d-1 ))
{
r = r->next;
dp++;
}
if (r ->next== NULL)
{
printf("\n删除错误,请重新输入。\n");
return;
}
stu = r->next;
r->next = stu->next;
*dl = stu->data; //此时应为*dl ,下面printf才能顺利输出
free(stu);
printf("删除%s成功!\n",dl->names);
//return;
}
功能:
同上
信息读取
排序
查询
无限增删
学生增删
将个人学习与科目成绩分为两单链表
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxsize 20
//成绩链表
typedef struct LNode1
{
char subject[maxsize];
int chengji;
struct LNode1 *next;
}O;
//学生链表
typedef struct LNode2
{
int num;
char name[maxsize];
int count;//成绩数目
O* p;
O* head1; //成绩头结点
struct LNode2 *next;
}S;
int count = 0;
S* addstudent(S *head2); //插入学生
void addscore(S *p); //插入成绩(头)
void insertscore(O *head1, char str[], int sco); //插入成绩
void stuprint(S *head2); //输出学生
void scoreprint(O* head1); //输出成绩
void addso(S* head2); //给某人增加成绩
void show(S* head2, int cas, char str[], int xuehao); //查找
void search(S* head2); //操作【4】
int searchscore(O* head1, char str[], S* a); //判断科目
void sortbynum(S* head2); //按学号排序
void sortbysub(S* head2); //按科目排序
int stusort(S* head2);//按个人成绩
O* mergeSort(O* head1);//接上排序
S* delstu(S* head2);//删除学生
int main()
{
S *head2 = (S*)malloc(sizeof(S)); //学生根结点
head2->head1 = (O*)malloc(sizeof(O)); //※这里不写清楚,search中无法读取head2->head1(在都是头结点的情况!)
head2->next = NULL;
head2->head1->next = NULL;
int choose;
printf("\n");
while (1)
{
printf("\n");
for (int i = 0; i < 50; i++)
printf("*");
printf("\n");
printf("欢迎使用学生信息管理系统V3.0\n\n");
printf("【1】. 添加学生\n");
printf("【2】. 输出学生信息\n");
printf("【3】. 添加单个人成绩\n");
printf("【4】. 查找学生\n");
printf("【5】. 按学号排序\n");
printf("【6】. 按科目成绩排序\n");
printf("【7】. 学生个人成绩排序\n");
printf("【8】. 删除学生\n");
printf("【0】. 退出系统\n\n");
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
printf("请输入您的选择:");
scanf("%d", &choose);
switch (choose)
{
case 1:
head2 = addstudent(head2); //添加学生
break;
case 2:
stuprint(head2); //输出学生信息
break;
case 3:
addso(head2); //给单人添加成绩
break;
case 4:
search(head2); // 查找学生
break;
case 5:
sortbynum(head2); //按学号排序
break;
case 6:
sortbysub(head2);//按科目排序
break;
case 7:
stusort(head2); //按个人成绩排序
break;
case 8:
delstu(head2);//删除学生
break;
case 0:
printf("\n退出系统成功\n");
printf("欢迎再次使用【学生信息管理系统v3.0】\n\n");
exit(0);
default:
printf("\n您输入的操作有误。请重新输入!\n");
break;
}
}
}
S* addstudent(S *head2) //添加学生
{
int cse, no;
char str[maxsize];
while (1)
{
for (int i = 1; i < 50; i++)
printf("-");
printf("\n");
printf("输入学生学号:");
scanf("%d", &no);
printf("输入学生姓名:");
scanf("%s", str);
S *p = (S*)malloc(sizeof(S));
strcpy(p->name, str);
p->num=no;
p->next = head2->next;
head2->next = p;
p->count = 0;
printf("是否为学生添加成绩(1.是 2.否):");
scanf("%d", &cse);
if (cse == 1)
{
addscore(p);
}
printf("添加学生信息成功!");
printf("是否继续添加学生(1.是 2.否):");
scanf("%d", &cse);
if (cse != 1)
{
break;
}
}
return head2;
}
void addscore(S *p) //添加成绩
{
int cas, sco;
char str[maxsize];
while (1)
{
printf("输入科目:");
scanf("%s", str);
printf("输入成绩:");
scanf("%d", &sco);
if (p->count == 0)
{
p->head1 = (O*)malloc(sizeof(O));
p->head1->next = NULL;
O *p1 = (O *)malloc(sizeof(O));
strcpy(p1->subject, str);
p1->chengji=sco;
p1->next = p->head1->next;
p->head1->next = p1;
p->count++;
}
else
{
insertscore(p->head1, str, sco);
p->count++;
}
printf("添加成绩成功!是否继续添加成绩(1.是 2.否):");
scanf("%d", &cas);
if (cas != 1)
{
break;
}
}
}
void insertscore(O *head1, char str[], int sco) //成绩链表元素插入
{
//int cas;
if (head1 == NULL)
{
printf("新建错误。\n");
}
O *p1 = (O *)malloc(sizeof(O));
strcpy(p1->subject, str);
p1->chengji= sco;
p1->next = head1->next;
head1->next = p1;
}
void stuprint(S *head2) //输出学生信息
{
S *q;
q = head2;
if (q->next == NULL)
{
printf("\n当前没有任何学生信息记录! 请输入学生信息。\n");
return;
}
printf("\n学号\t\t姓名\t\t\t\t\t成绩\n");
while (q->next != NULL)
{
q = q->next;
printf("%d\t\t%s\t\t\t",q->num,q->name);
scoreprint(q->head1);
}
}
void scoreprint(O* head1) //输出成绩信息
{
O *q1 = head1->next;
if (q1 == NULL)
return;
while (q1 != NULL)
{
printf("\t%s:%d", q1->subject, q1->chengji);
q1 = q1->next;
}
printf("\n");
}
void addso(S* head2) //给单个人增加成绩
{
int xuehao = -1;
char str[maxsize];
printf("请输入学生姓名:");
scanf("%s", str);
show(head2, 5, str, xuehao);
}
void show(S* head2, int cas, char str[],int xuehao) //查找显示学生信息
{
if (head2 == NULL)
{
return;
}
if (cas == 5 && strcmp(head2->name, str) == 0 )
addscore(head2);
if (cas == 1 && (head2->num==xuehao) ) //按学号查找
{
printf("\n学号\t\t姓名\t\t\t\t\t成绩\n");
printf("%d\t\t%s\t\t\t", head2->num, head2->name);
scoreprint(head2->head1);
}
if (cas == 2 && strcmp(head2->name, str) == 0) //按姓名查找
{
printf("\n学号\t\t姓名\t\t\t\t\t成绩\n");
printf("%d\t\t%s\t\t\t", head2->num, head2->name);
scoreprint(head2->head1);
}
if (cas == 3 )
{
//if (head2 == NULL) {head2 = head2->next;}
if (searchscore(head2->head1, str, head2)) //该学生有本科目的成绩
{
printf("学号:%d\t\t姓名:%s\t\t科目:%s\t\t成绩:%d", head2->num, head2->name,head2->p->subject,head2->p->chengji);
printf("\n");
}
}
show(head2->next, cas, str,xuehao);
}
void search(S* head2) //操作【4】
{
int cas,xuehao=-1;
//int no;
char str[maxsize];
//S* p;
for (int i = 1; i < 50; i++)
printf("+");
printf("\n");
printf("[1].按学号查找\n");
printf("[2].按姓名查找\n");
printf("[3].按科目查找\n");
for (int i = 1; i < 50; i++)
printf("+");
printf("\n");
printf("请输入操作:");
scanf("%d", &cas);
if (cas == 1)
{
printf("请输入学生学号:");
scanf("%d", &xuehao);
}
if (cas == 2)
{
printf("请输入学生姓名:");
scanf("%s", str);
}
if (cas == 3)
{
printf("请输入学生科目:");
scanf("%s", str);
}
show(head2, cas, str,xuehao);
}
int searchscore(O* head1, char str[],S* p) //查找科目是否存在
{
if (head1 == NULL)
return 0;
else if (strcmp(head1->subject, str) == 0)
{
p->p = head1;
return 1;
}
else
return searchscore(head1->next, str, p) ;
}
void sortbynum(S* head2) //按学号排序
{
int len=0;
S* ptr = head2->next;
while (ptr != NULL)
{
len++;
ptr = ptr->next;
}
S* shead, *p, *q, *tmp;
int i, j;
for (i = 0; i < len - 1; i++)
{
shead = head2;
p = head2->next;
q = p->next;
for (j = 0; j < len - 1 - i; j++)
{
if (p->num > q->num)
{
shead->next = p->next;
p->next = q->next;
q->next = p;
tmp = p;
p = q;
q = tmp;
}
shead = shead->next;
p = p->next;
q = q->next;
}
}
stuprint(head2);
}
void sortbysub(S* head2) //按科目排序
{
char str6[maxsize];
S* newhead = (S*)malloc(sizeof(S));
newhead->head1 = (O*)malloc(sizeof(O));
newhead->next = NULL;
newhead->head1->next = NULL;
S* p6 = head2;
S* p6_2 = p6->next;
S *q6;
q6 = newhead;
printf("请输入学生科目:");
scanf("%s", str6);
while (p6 != NULL)
{
if (searchscore(p6->head1, str6, p6)) //该学生有本科目的成绩
{
S* q6_2 = (S*)malloc(sizeof(S));
q6_2->num = p6->num; strcpy(q6_2->name, p6->name); q6_2->head1 = p6->head1; q6_2->p = p6->p; q6_2->next = NULL;
q6->next = q6_2;
q6 = q6->next;
}
p6 = p6->next;
}
S *nq;
nq = newhead;
if (nq->next == NULL)
{
printf("\n当前没有任何学生信息记录! \n");
return;
}
printf("\n有此科目者:");
printf("\n学号\t\t姓名\t\t\t科目\t\t成绩\n");
while (nq->next != NULL)
{
nq = nq->next;
printf("%d\t\t%s\t\t\t%s\t\t%d\n", nq->num, nq->name,nq->p->subject,nq->p->chengji);
}
int len = 0;
S* ptrk = newhead->next;
while (ptrk != NULL)
{
len++;
ptrk = ptrk->next;
}
S* sheadk, *pk, *qk, *tmpk;
int i, j;
for (i = 0; i < len - 1; i++)
{
sheadk = newhead;
pk = newhead->next;
qk = pk->next;
for (j = 0; j < len - 1 - i; j++)
{
if (pk->p->chengji < qk->p->chengji)
{
sheadk->next = pk->next;
pk->next = qk->next;
qk->next = pk;
tmpk = pk;
pk = qk;
qk = tmpk;
}
sheadk = sheadk->next;
pk = pk->next;
qk = qk->next;
}
}
S *nqq = newhead;
if (nqq->next == NULL)
{
printf("\n当前没有任何学生信息记录! \n");
return;
}
printf("\n按科目成绩排序结果如下:");
printf("\n学号\t\t姓名\t\t\t科目\t\t成绩\n");
while (nqq->next != NULL)
{
nqq = nqq->next;
printf("%d\t\t%s\t\t\t%s\t\t%d\n", nqq->num, nqq->name, nqq->p->subject, nqq->p->chengji);
}
}
int stusort(S* head2) //学生个人成绩排序(小-->大)
{
char str7[maxsize];
printf("请输入学生姓名:");
scanf("%s", str7);
S *p7 = head2;
while ((p7->next != NULL) && strcmp(str7, p7->name))
{
p7 = p7->next;
}
if ((p7->next == NULL) && strcmp(str7, p7->name))
{
printf("\n查无此人! \n");
return 0;
}
else
{
O* p07 = mergeSort(p7->head1);
O* p07_n = p07->next;
printf("结果如下:");
printf("\n学号\t\t姓名\t\t\t\t\t成绩\n");
printf("%d\t\t%s\t\t\t", p7->num, p7->name);
while (p07_n != NULL)
{
printf("\t%s:%d", p07_n->subject, p07_n->chengji);
p07_n = p07_n->next;
}
printf("\n");
return 1;
}
}
O* mergeSort(O* head1) //归并
{
if (head1 != NULL && head1->next != NULL)
{
O *fast = head1->next;
O *slow = head1;
while (fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
O *p1 = mergeSort(slow->next);
slow->next = NULL;
O *p2 = mergeSort(head1);
O *p0 = (O*)malloc(sizeof(O));
O *p = p0;
while (p1 != NULL && p2 != NULL)
{
if (p1->chengji < p2->chengji)
{
p->next = p1;
p1 = p1->next;
}
else
{
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
if (p1 != NULL)
{
p->next = p1;
}
if (p2 != NULL)
{
p->next = p2;
}
p = p0->next;
free(p0);
return p; //此时p对应head1,ASCII码中\0为0。
}
return head1;
}
S* delstu(S* head2)
{
char str8[maxsize];
printf("请输入学生姓名:");
scanf("%s", str8);
S *p8 = head2;
while ((p8->next != NULL) && strcmp(str8, p8->name))
{
p8 = p8->next;
}
if ((p8->next == NULL) && strcmp(str8, p8->name))
{
printf("\n查无此人! \n");
return 0;
}
else
{
S **pp8 = &head2;
for (; *pp8; pp8 = &((*pp8)->next))
{
if (strcmp(str8, ((*pp8)->name)) == 0)
{
*pp8 = (*pp8)->next;
break;
}
}
printf("删除成功!\n");
/*
这种二级指针删除的方法参考自据说Linux大神的
http://wordaligned.org/articles/two-star-programming
https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/solution/linusda-lao-de-jie-jue-fang-an-by-adjwang/
*/
return head2;
}
}
功能:
信息读取
排序
查询
无限增删
信息存储采用二叉树形式
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 20
//成绩二叉树
typedef struct score
{
char subject[maxsize];
int chengji;
struct score* left;
struct score* right;
}O;
//学生二叉树
typedef struct student
{
char num[maxsize];
char name[maxsize];
int count; //成绩数目
O* p;
O* head; //成绩头结点
struct student* left;
struct student* right;
}S;
int count = 0;
S* addstudent(S *root); //增加学生
void addscore(S *root); //增加成绩
void insertscore(O *head, char str[], int sco); //成绩树插入
void insertstu(S *root, char str[], char no[]); //学生树插入
void scoreprint(O *head); //输出成绩信息
void stuprint(S* root); //输出学生信息
void addso(S* root);//给个人加成绩
void show(S *root, int cas, char str[]); //查找学生信息,汇集一些遍历的操作(姓名/学号查找之类的)
void search(S* root); //查找函数
void stusort(S* root); //按个人成绩排序
void sortbyname(S* root); //按姓名排序 ※
void sortbysub(S* root); //按科目排序
void allshow(S* root); //显示所有学生信息
int main()
{
S *root = NULL; //学生根节点
int choose;
printf("\n");
while (1) {
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
printf("欢迎使用学生信息管理系统v3.0\n\n");
printf("【1】. 添加学生\n");
printf("【2】. 添加成绩\n");
printf("【3】. 查找学生\n");
printf("【4】. 按学号排序\n");
printf("【5】. 按姓名排序\n");
printf("【6】. 按单科成绩排序\n");
printf("【7】. 学生个人成绩排序\n");
printf("【0】. 退出系统\n\n");
for (int i = 1; i < 50; i++)
printf("*");
printf("\n");
printf("请输入您的选择:");
scanf("%d", &choose);
switch (choose)
{
case 1:
root = addstudent(root);
break; //添加学生
case 2:
addso(root);
break; //添加个人成绩
case 3:
search(root);
break; //查找(学号、姓名、科目)
case 4:
allshow(root);
break; //按学号排序(输出全部信息)
case 5:
sortbyname(root);
break; //按姓名排序
case 6:
sortbysub(root);
break; //按科目排序
case 7:
stusort(root);
break; //个人的成绩排序
case 0: //退出系统
printf("\n退出系统成功\n");
printf("欢迎再次使用【学生信息管理系统v3.0】\n\n");
exit(0);
default:
printf("\n您输入的操作有误。请重新输入!\n");
break;
}
}
}
//增加学生
S* addstudent(S *root)
{
int cse;
char str[maxsize], no[maxsize];
while (1)
{
for (int i = 1; i < 50; i++)
printf("-");
printf("\n");
printf("输入学生学号:");
scanf("%s", no);
printf("输入学生姓名:");
scanf("%s", str);
if (count == 0)
{
root = (S*)malloc(sizeof(S*));
strcpy(root->name, str);
strcpy(root->num, no);
root->left = NULL;
root->right = NULL;
root->head = NULL;
root->count = 0;
printf("是否为学生添加成绩(1.是 2.否):");
scanf("%d", &cse);
if (cse == 1)
{
addscore(root);
}
count++;
}
else
{
insertstu(root, str, no);
}
printf("添加学生信息成功!");
printf("是否继续添加学生(1.是 2.否):");
scanf("%d", &cse);
if (cse != 1)
{
break;
}
}
return root;
}
//新增成绩
void addscore(S *root)
{
int sco, cas;
char str[20];
while (1)
{
printf("输入科目:");
scanf("%s", str);
printf("输入成绩:");
scanf("%d", &sco);
if (root->count == 0)
{
root->head = (O *)malloc(sizeof(O)); //根节点创建
strcpy(root->head->subject, str);
root->head->chengji = sco;
root->head->left = NULL;
root->head->right = NULL;
root->count++;
}
else
{
insertscore(root->head, str, sco);
}
printf("添加成绩成功!是否继续添加成绩(1.是 2.否):");
scanf("%d", &cas);
if (cas != 1)
{
break;
}
}
}
//成绩树插入
void insertscore(O *head, char str[], int sco)
{
int cas;
if (head == NULL)
{
printf("新建错误。\n");
}
else if (sco >= head->chengji)
{
if (head->left == NULL)
{
O *p = (O *)malloc(sizeof(O)); //根节点创建
strcpy(p->subject, str);
p->chengji = sco;
p->left = NULL;
p->right = NULL;
head->left = p;
}
else
{
insertscore(head->left, str, sco);
}
}
else
{
if (head->right == NULL) {
O *p = (O *)malloc(sizeof(O)); //根节点创建
strcpy(p->subject, str);
p->chengji = sco;
p->left = NULL;
p->right = NULL;
head->right = p;
}
else
{
insertscore(head->right, str, sco);
}
}
}
//学生树插入
void insertstu(S *root, char str[], char no[])
{
int cas;
//strcmp(str1, str2),若str1 = str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数。
if (strcmp(root->num, no) > 0)
{
if (root->left == NULL)
{
S *p = (S *)malloc(sizeof(S)); //节点创建
strcpy(p->name, str);
strcpy(p->num, no);
p->left = NULL;
p->right = NULL;
p->head = NULL;
p->count = 0;
root->left = p;
printf("是否为学生添加成绩(1.是 2.否):");
scanf("%d", &cas);
if (cas == 1)
{
addscore(p);
}
}
else
{
insertstu(root->left, str, no);
}
}
else
{
if (root->right == NULL)
{
S *p = (S *)malloc(sizeof(S)); //节点创建
strcpy(p->name, str);
strcpy(p->num, no);
p->left = NULL;
p->right = NULL;
p->head = NULL;
p->count = 0;
root->right = p;
printf("是否为学生添加成绩(1.是 2.否):");
scanf("%d", &cas);
if (cas == 1)
{
addscore(p);
}
}
else
{
insertstu(root->right, str, no);
}
}
}
void scoreprint(O *head) //输出成绩信息
{
if (head == NULL)
return;
scoreprint(head->left);
printf("%s:%d\t\t", head->subject, head->chengji);
scoreprint(head->right);
}
void stuprint(S* root) //输出学生信息
{
printf("学号:%s\t\t姓名:%s\t\t", root->num, root->name);
scoreprint(root->head);
printf("\n");
}
int searchscore(O* head, char str[], S* p) //查找科目是否存在
{
if (head == NULL)
return 0;
else if (strcmp(head->subject, str) == 0)
{
p->p = head;
return 1;
}
else
return searchscore(head->left, str, p) || searchscore(head->right, str, p);//否则返回子节点的结果
}
void show(S* root, int cas, char str[]) //查找学生信息
{
if (root == NULL)
return;
show(root->left, cas, str);
if (cas == 1 && strcmp(root->num, str) == 0) //按学号查找
stuprint(root);
if (cas == 2 && strcmp(root->name, str) == 0) //按姓名查找
stuprint(root);
if (cas == 3)
{
O *temp;
if (searchscore(root->head, str, root)) //该学生有本科目的成绩
{
stuprint(root);
}
}
if (cas == 5 && strcmp(root->num, str) == 0)
addscore(root);
show(root->right, cas, str);
}
void addso(S *root) //给单人加成绩
{
char str[20];
printf("请输入学生学号:");
scanf("%s", str);
show(root, 5, str);
}
void search(S* root) //查找函数
{
int cas;
int no;
char str[20];
S* p;
for (int i = 1; i < 50; i++)
printf("-");
printf("\n");
printf("[1].按学号查找\n");
printf("[2].按姓名查找\n");
printf("[3].按科目查找\n");
//printf("[4].显示所有信息\n");
for (int i = 1; i < 50; i++)
printf("-");
printf("\n");
printf("请输入操作:");
scanf("%d", &cas);
if (cas == 1)
printf("请输入学生学号:");
if (cas == 2)
printf("请输入学生姓名:");
if (cas == 3)
printf("请输入学生科目:");
scanf("%s", str);
show(root, cas, str);
}
void allshow(S* root) //显示所有学生信息/学号排序
{
if (root == NULL)
{
return;
}
allshow(root->left);
stuprint(root);
allshow(root->right);
}
void stusort(S* root) //按个人成绩排序
{
char str[20];
printf("请输入学生学号:");
scanf("%s", str);
show(root, 1, str);
}
void sortbyname(S* root) //按姓名排序 ※
{
char str[20];
S* a[100], *temp; //
int i = 0, j = 0, k;
if (root != NULL)
a[i++] = root;
else
{
printf("排序错误,请输入学生。\n");
return;
}
while (j<i)
{
if (a[j]->left != NULL)
a[i++] = a[j]->left;
if (a[j]->right != NULL)
a[i++] = a[j]->right;
j++;
}
for (j = 0; j < i; j++)
{
for (k = 0; k < i - j - 1; k++)
{
if (strcmp(a[k]->name, a[k + 1]->name) > 0)
{
temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
}
for (i = 0; i < j; i++)
{
stuprint(a[i]);
}
}
void sortbysub(S* root) //按科目排序
{
char str[20];
S* a[100], *temp;
int i = 0, j = 0, k;
printf("请输入科目:");
scanf("%s", str);
if (root != NULL)
{
a[i++] = root;
}
else
{
printf("无内容,请重新输入!\n");
return;
}
while (j < i)
{
if (a[j]->left != NULL)
a[i++] = a[j]->left;
if (a[j]->right != NULL)
a[i++] = a[j]->right;
if (searchscore(a[j]->head, str, a[j])) // ※
j++;
else // ※
{
i--;
for (k = j; k < i; k++)
{
a[k] = a[k + 1];
}
}
}
for (j = 0; j < i; j++)
{
for (k = 0; k < i - j - 1; k++)
{
if (a[k]->p->chengji < a[k + 1]->p->chengji)
{
temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
}
for (j = 0; j < i; j++)
{
printf("学号:%s\t\t姓名:%s\t\t", a[j]->num, a[j]->name);
printf("%s\t\t%d\n", a[j]->p->subject, a[j]->p->chengji);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。