(作者联系方式见文末)
题目要求
源代码:
注意:本项目采用多文件编译(开发软件为VS2022),请先按照如下步骤进行设置:
1.项目->(项目名称xxx)属性->C/C++->预处理器->预处理器定义->编辑中添加:_CRT_SECURE_NO_WARNINGS (这一步是防止vs2022对于printf等基础函数不安全的报错.)
2.项目->(项目名称xxx)属性->链接器->命令行->其他选项中添加:/FORCE:MULTIPLE (防止link找到多个定义的报错)
3.最后成果如下
头文件
(下文可直接复制!!)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
struct subjects
{
int score;
float weight;
char sub_number[6];
char sub_name[15];
};
struct Students
{
char num[9];
char name[30];
struct subjects arr[3];/* = {
{-1,5,"00001","Math"},
{-1,4,"00002","C Language"},
{-1,3,"00003","English"},
};*/
struct Students * next;
}; struct Students * head, * n, * pt;
int menu();
int num_check(char[], struct Students* head);
void cleanup(struct Students* head);
struct Students* CheckIn(struct Students* head, struct Students* n, int stu);
struct Students* Showinfo(char[],struct Students* head);
struct Students* Delete(char[],struct Students* head);
struct Students* Class(char[],struct Students* head);
struct Students* Scores(char[],struct Students* head);
void main()
{
int choice = 0; //菜单用,返回选项
int stu = 0; //学生数
while (choice != 5)
{
choice = menu();
switch (choice)
{
case 1:
printf("输入要添加的学生数:\n");
scanf("%d", &stu);
if (stu <= 0)
{
printf("数字错误!正在退出..\n");
break;
}
head = CheckIn(head, n, stu);
break;
case 2:
printf("输入要查询的学号或姓名:");
char A[30];
scanf("%s", A);
Showinfo(A, head);
break;
case 3:
printf("输入要删除的学号或姓名:");
char B[30];
scanf("%s", B);
head = Delete(B, head);
break;
case 4:
printf("输入要查询的科目编号:");
char C[10];
scanf("%s", C);
head = Class(C, head);
break;
case 5:
cleanup(head);
break;
case 6:
printf("输入要添加成绩的学生学号或姓名:");
char D[30];
scanf("%s", D);
head = Scores(D,head);
break;
default:
printf("数字错误!重新输入\n");
scanf("%d",&choice);
break;
}
}
}
menu函数
( 在解决方案资源管理器->源文件 一栏中创建,以下函数同)
#include"head.h"
int menu(void)
{
int choice = 0;
printf("加载菜单中...\n");
printf("****************************\n");
printf("*添加学生:1 查找学生:2 *\n");
printf("*删除学生:3 班级成绩:4 *\n");
printf("*登入成绩:6 退出程序:5 *\n");
printf("****************************\n");
printf("请选择:\n");
scanf("%d",&choice);
return choice;
}
CheckIn函数
#include"head.h"
struct Students* CheckIn(struct Students* head, struct Students* n,int stu)
{
if (head == NULL)
{
struct Students* pt;
pt = (struct Students*)malloc(sizeof(Students));
pt->next = NULL;
n = pt;
for (int i = 1; i <= (stu - 1); i++)
{
pt = (struct Students*)malloc(sizeof(Students));
pt->next = n;
n = pt;
}
head = n;
//输入信息
int stus = 1;
for (; head->next != NULL; head = head->next, stus++)
{
printf("为第%d位学生添加信息:\n", stus);
printf("输入学号:\n");
char temp[9] = {"0"};//学号查重
scanf("%s", temp);
bool judge = 0;
while (judge ==0 )
{
judge = num_check(temp, n);
if (judge == 0)
{
scanf("%s", temp);
}
}
strcpy(head->num,temp);
printf("输入姓名:\n");
scanf("%s", head->name);
}
printf("现在为最后一位学生%d添加信息:\n", stus);
printf("输入学号:\n");
char temp[9] = { "0" };
scanf("%s", temp);
bool judge = 0;
while (judge == 0)
{
judge = num_check(temp, n);
if (judge == 0)
{
scanf("%s", temp);
}
}
strcpy(head->num, temp);
printf("输入姓名:\n");
scanf("%s", head->name);
return n;
}
else
{
struct Students* origin = head;
for (; head->next != NULL; head = head->next);//将head指向末尾表
pt = (struct Students*)malloc(sizeof(Students));
pt->next = NULL;
n = pt;
for (int i = 1; i <= (stu - 1); i++)
{
pt = (struct Students*)malloc(sizeof(Students));
pt->next = n;
n = pt;
}
head->next = n;
//输入信息
int stus = 1;
for (; n->next != NULL; n = n->next, stus++)
{
printf("为第%d位新学生添加信息::\n", stus);
printf("输入学号:\n");
char temp[9] = { "0" };
scanf("%s", temp);
bool judge = 0;
while (judge == 0)
{
judge = num_check(temp, origin);
if (judge == 0)
{
scanf("%s", temp);
}
}
strcpy(n->num, temp);
printf("Enter stu_name:\n");
scanf("%s", n->name);
}
printf("现在为最后一位新学生%d添加信息:\n", stus);
printf("输入学号:\n");
char temp[9] = { "0" };
scanf("%s", temp);
bool judge = 0;
while (judge == 0)
{
judge = num_check(temp, origin);
if (judge == 0)
{
scanf("%s", temp);
}
}
strcpy(n->num, temp);
printf("Enter stu_name:\n");
scanf("%s", n->name);
//最后一个学生
printf("操作完成,返回菜单...\n");
return origin;
}
}
num_check函数
#include"head.h"
int num_check(char *temp, struct Students* head)
{
for (; strcmp(head->num,temp)!=0; head = head->next)
{
if (head->next == NULL)
{
return 1;
}
}
if (strcmp(head->num, temp) == 0)
{
printf("学号重复!请重新输入!\n");
return 0;
}
}
Scores函数
此函数用来对学生进行成绩登记
#include"head.h"
struct Students* Scores(char A[], struct Students* head)
{
if (head == NULL)
{
printf("空链表!\n");
return head;
}
struct Students* head0 = head;
for (; strcmp((head->num), A) != 0 && strcmp((head->name), A) != 0; head = head->next)
{
if (head->next == NULL)
{
printf("查无此人!!!退出程序...\n");
return head0;
}
}
{
int sub = 0;
char B[15] = { NULL };
printf("输入要添加成绩的学科的项目编号:\n");
scanf ("%s",B);
if (strcmp("00001", B) == 0) { sub = 0; strcpy(head->arr[0].sub_number, B); strcpy(head->arr[0].sub_name, "数学");}//数学
else if (strcmp("00002", B) == 0) { sub = 1; strcpy(head->arr[1].sub_number, B); strcpy(head->arr[1].sub_name, "C语言");}//英语
else if (strcmp("00003", B) == 0) { sub = 2; strcpy(head->arr[2].sub_number, B); strcpy(head->arr[2].sub_name, "英语");}//C语言
else
{
printf("数据非法或科目不存在!返回菜单...\n");
return head0;
}
printf("该学科为:%s 请输入成绩:\n",head->arr[sub].sub_name);
int sco = 0,logic = 0;
scanf("%d", &sco);
while(logic==0)
{
if (sco <= 100 && sco >= 0) { head->arr[sub].score = sco; logic = 1; }
else
{
printf("数据非法,重新输入!\n");
scanf("%d", &sco);
}
}
}
printf("操作完成,返回菜单...\n");
return head0;
}
Class函数
用来展示全班成绩
#include"head.h"
struct Students* Class(char* A, struct Students* head)
{
struct Students* head0 = head;
//确定科目
if (head == NULL)
{
printf("空链表!\n");
return head;
}
int sub = 0;
if (strcmp((head->arr[0].sub_number), A) == 0) sub = 0; //数学
else if (strcmp((head->arr[1].sub_number), A) == 0) sub = 1; //英语
else if (strcmp((head->arr[2].sub_number), A) == 0) sub = 2; //C语言
int sum = 0, stu = 0, f_stu = 0;
float ave = 0,loser = 0;
int subsection[11] = { 0 };
for (; head!=NULL&&head->next != NULL; head = head->next,stu++)
{
if (head->arr[sub].score < 0)
{
printf("****该同学未参加考试!****\n");
head->arr[sub].score = -1;
}
sum += head->arr[sub].score;
if (head->arr[sub].score < 60) f_stu++;
int p = (head->arr[sub].score) / 10;
switch (p)
{
case 0:subsection[p]++; break;
case 1:subsection[p]++; break;
case 2:subsection[p]++; break;
case 3:subsection[p]++; break;
case 4:subsection[p]++; break;
case 5:subsection[p]++; break;
case 6:subsection[p]++; break;
case 7:subsection[p]++; break;
case 8:subsection[p]++; break;
case 9:subsection[p]++; break;
case 10:subsection[p]++; break;
}
printf("学号:%s 姓名:%s 本科目成绩:%d\n\033[0m",head->num,head->name,head->arr[sub].score);
}
if (head != NULL)
{
if (head->arr[sub].score < 0)
{
printf("****该同学未参加考试!****\n");
head->arr[sub].score = -1;
}
sum += head->arr[sub].score;
if (head->arr[sub].score < 60) f_stu++;
int p = (head->arr[sub].score) / 10;
switch (p)
{
case 0:subsection[p]++; break;
case 1:subsection[p]++; break;
case 2:subsection[p]++; break;
case 3:subsection[p]++; break;
case 4:subsection[p]++; break;
case 5:subsection[p]++; break;
case 6:subsection[p]++; break;
case 7:subsection[p]++; break;
case 8:subsection[p]++; break;
case 9:subsection[p]++; break;
case 10:subsection[p]++; break;
}
printf("学号:%s 姓名:%s 本科目成绩:%d\n\033[0m", head->num, head->name, head->arr[sub].score);
stu++;
}
//统计完毕
ave = (float)sum / stu;
loser = (float)f_stu / stu * 100;
printf("均分为:%.2f\n",ave);
printf("挂科率为:%.2f%%\033[0m\n",loser);
for (int i = 0; i <= 10; i++)
{
printf("%3d - %3d 分:",i*10,i*10+9);
printf("%d人 占比%.2f%%\033[0m\n", subsection[i], (float)subsection[i] / stu * 100);
}
return head0;
}
Delete函数
用来删除某位学生的信息
#include"head.h"
struct Students* Delete(char *A, struct Students* head)
{
if (head == NULL)
{
printf("空链表!\n");
return head;
}
struct Students* head0 = head;
struct Students* temp = head;
for (; strcmp(head->num, A) != 0 && strcmp(head->name, A) != 0; head = head->next)
{
temp = head;
if (head->next == NULL)
{
printf("查无此人!!! 操作失败,返回菜单。\n");
return head0;
}
}
if (strcmp((head->num), A) == 0 || strcmp(head->name, A) == 0)
{
if (head!=head0 && (head->next == temp->next))//只有一个学生的情况
{
free(head);
printf("操作完成,返回菜单...\n");
return NULL;
}
if (head == head0) //删除头标
{
struct Students* temp = head;
head = head->next;
free(temp);
printf("操作完成,返回菜单...\n");
return head;
}
temp->next = head->next;
free(head);
printf("操作完成,返回菜单...\n");
}
return head0;
}
Search函数
用来查找某一位学生的信息
#include"head.h"
struct Students* Showinfo(char *A , struct Students* head)
{
struct Students* head0 = head;
if (head == NULL) {
printf("空链表!\n"); return head;
}//空链表的情况
for (; strcmp((head->num), A)!=0&& strcmp((head->name), A) != 0; head = head->next)
{
if (head->next == NULL)
{
printf("查无此人!!!\n");
break;
}
}
if (strcmp((head->num), A) ==0 || strcmp(head->name,A)==0)
{
if (head->arr[0].score < 0)
{
head->arr[0].score = -1;
printf("****该生目前未参加数学考试!****\n");
}
if (head->arr[1].score < 0)
{
head->arr[1].score = -1;
printf("****该生目前未参加C语言考试!****\n");
}
if (head->arr[2].score < 0)
{
head->arr[2].score = -1;
printf("****该生目前未参加英语考试!****\n");
}
printf("学号:%s 姓名:%s\n数学成绩:%d \n英语成绩:%d\nC语言成绩:%d\n\033[0m", head->num, head->name, head->arr[0].score, head->arr[1].score, head->arr[2].score);
}
printf("操作完成,返回菜单...\n");
return head0;
}
cleanup函数
释放内存(有没有这个无所谓)
#include"head.h"
void cleanup(struct Students* head)
{
if (head == NULL)
{
return;
}
else
{
struct Students* temp;
while(head->next!=NULL)
{
temp = head;
head = head->next;
free(temp);
}
free(head);
}
}
//代码结束
有任何问题请联系QQ2695286486