主页面效果图如下:
代码如下:
#include<stdio.h> //注意整个操作系统代码是基于WIndows系统,若用其他系统,运行代码时观感引起的不适见谅。
#include<stdlib.h> //在Linux系统下可以将system(“cls”)改成system(“clear”),同样可以完成整段代码
#include<string.h>
void Print_Menu(); //输出表单栏
void InputStu_Infor(); //录入学生信息
void Cal_Course_V(); //每门课的平均分
void Cal_Stu_V(); //每个学生的平均分
void Return_Menu(); //返回菜单栏函数
void High_Short(); //从高到低排序并且输出
void Short_High(); //从低到高排序并且输出
void Stu_ID_Order(); //以学号排序
void Name_Dic(); //以字典顺序排序并且输出
void FindStu_ID(); //按照学号查找信息
void FindName(); //按照名字查找信息
void Print_Level(); //按照成绩划分优秀...
void Print_All(); //打印所有已知信息
void Write_File(); //写文件
void Read_File(); //读文件
#define Average 0
#define MAX_Student_Num 30
int Num_class,Num_course;
typedef struct InFormation
{
char name[25]; //名字
double Stu_ID[12]; //学号
struct grade //成绩
{
float NUM[6];
}Grade;
}InFor;
InFor student [MAX_Student_Num]; //最大班级人数
InFor people [31]; //班级人数
int main(){ //主要完成选择任务栏
int Math;
Print_Menu();
while(1)
{
scanf("%d",&Math);
switch (Math)
{
case 1:
system("cls");
InputStu_Infor();
system("cls");
Print_Menu();
break;
case 2:
system("cls");
Cal_Course_V();
Return_Menu();
system("cls");
Print_Menu();
break;
case 3:
system("cls");
Cal_Stu_V();
Return_Menu();
system("cls");
Print_Menu();
break;
case 4:
system("cls");
High_Short();
system("cls");
Print_Menu();
break;
case 5:
system("cls");
Short_High();
system("cls");
Print_Menu();
break;
case 6:
system("cls");
Stu_ID_Order();
system("cls");
Print_Menu();
break;
case 7:
system("cls");
Name_Dic();
system("cls");
Print_Menu();
break;
case 8:
system("cls");
FindStu_ID();
system("cls");
Print_Menu();
break;
case 9:
system("cls");
FindName();
system("cls");
Print_Menu();
break;
case 10:
system("cls");
Print_Level();
Return_Menu();
system("cls");
Print_Menu();
break;
case 11:
system("cls");
Print_All();
system("cls");
Print_Menu();
break;
case 12:
system("cls");
Write_File();
system("cls");
Print_Menu();
break;
case 13:
system("cls");
break;
case 0:
system("cls");
printf("\n\n\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tEnd of program!\n");
printf("\t\t\t******************************************************************************\n");
return 0;
default: //输入有误提醒
system("cls");
printf("\n\n\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\tWarning Error !!!\n");
printf("\t\t\tPlease input according to the serial numbers from 1 to 13 above!!!\n");
printf("\t\t\t******************************************************************************\n");
}
}
}
void Print_Menu() //打印菜单栏
{
printf("\n");
printf("\t\t\t******************************************************************************\n");
printf("\t\t\t* Management for Students' scores *\n");
printf("\t\t\t* 1. Input record *\n");
printf("\t\t\t* 2. Caculate total and average score of every course *\n");
printf("\t\t\t* 3. Caculate total and average score of every student *\n");
printf("\t\t\t* 4. Sort in descending order by score *\n");
printf("\t\t\t* 5. Sort in ascending order by score *\n");
printf("\t\t\t* 6. Sort in ascending order by number *\n");
printf("\t\t\t* 7. Sort in dictionary order by name *\n");
printf("\t\t\t* 8. Search by number *\n");
printf("\t\t\t* 9. Search by name *\n");
printf("\t\t\t* 10. Statistic analysis *\n");
printf("\t\t\t* 11. List record *\n");
printf("\t\t\t* 12. Write to a file *\n");
printf("\t\t\t* 13. Read from a file *\n");
printf("\t\t\t* 0. Exit *\n");
printf("\t\t\t******************************************************************************\n");
printf("\n");
printf("\t\t\tPlease input the number for your choice:");
}
void Return_Menu() //返回菜单栏函数
{
char input;
New:
printf("If you want to quit,Please input ' r' or ' R':"); //如果输入无反应可以尝试在r前加一个空格
scanf(" %c",&input);
while(1){
if(input == 'r' || input == 'R'){
break;
}
else
printf("Error!!!\n"); //完备性检查,以防他人输错退出指令
goto New;
}
}
void InputStu_Infor() //输入学生信息
{
InFor* p_ID = 0;
InFor* p_Gra = 0;
InFor* p_name = 0;
p_name = people;
p_ID = people;
p_Gra = people;
printf("Please enter the number of people in the class:");
scanf("%d",&Num_class);
printf("Please enter the total number of subjects for the exam:");
scanf("%d",&Num_course);
for (int i = 0; i < Num_class; i++)
{
printf("Please input Name:\n");
scanf("%s",people[i].name);
printf("Please intput Student ID(The Max Number is 12):\n");
scanf("%lf",people[i].Stu_ID);
printf("Please intput Student Grade:\n");
for(int k = 0;k < Num_course;k++){
scanf("%f",&people[i].Grade.NUM[k]);
}
}
printf("Name StudentID Grade\n");
for (int i = 0; i < Num_class; i++)
{
printf("%s %.0lf ",(p_name -> name),*(p_ID -> Stu_ID));
for (int k = 0; k < Num_course; k++)
{
printf("%.1f",people[i].Grade.NUM[k]);
printf("\t");
}
p_name++;
p_ID++;
p_Gra++;
printf("\n");
}
while(getchar() != '\n');
Return_Menu();
}
void Cal_Course_V() //计算每门课的总分和平均分
{
float sum[6] = {0};
for(int i = 0; i < Num_course; i++){
for (int j = 0; j < Num_class; j++)
{
sum[i] += people[j].Grade.NUM[i];
}
}
for (int k = 0; k < Num_course; k++)
{
printf("The Class Sum Grade Is:%.2f\n",sum[k]);
printf("The Class Average Grade Of %d Is:%.2f\n",k,sum[k]/Num_class);
}
}
void Cal_Stu_V() //计算每个人的总分和平均分
{
float sum[31] = {0};
for(int i = 0; i < Num_class; i++){
for (int j = 0; j < Num_course; j++)
{
sum[i] += people[i].Grade.NUM[j];
}
}
for (int i = 0; i < Num_class; i++)
{
printf("The Grade Of Student(%s) All Grade Is:%.2f\n",people[i].name,sum[i]);
printf("The Grade Of Every Student(%s) Is:%.2f\n",people[i].name,sum[i]/Num_course);
printf("\n");
}
}
void High_Short() //使用冒泡排序实现(高到低排序并且输出)
{
float Sum[31]={0};
for (int i = 0; i < Num_class; i++)
{
for (int j = 0; j < Num_course; j++)
{
Sum [i] += people[i].Grade.NUM[j];
}
}
for (int i = 0; i < Num_class - 1; i++)
{
for (int j = 0; j < Num_class - i - 1; j++)
{
if(Sum[j] < Sum[j+1])
{
InFor people_t = people[j];
people[j] = people[j+1];
people[j+1] = people_t;
float temp = Sum[j];
Sum[j] = Sum[j + 1];
Sum[j + 1] = temp;
}
}
}
printf("\n\n\n");
printf("The following are the results sorted from highest to lowest based on the total score:\n");
printf(" Name StudentID Grade\n");
for (int i = 0; i < Num_class; i++)
{
printf("%d %s %.0lf ",i+1,people[i].name,*(people[i].Stu_ID));
for (int k = 0; k < Num_course; k++)
{
printf("%.1lf",people[i].Grade.NUM[k]);
printf("\t");
}
printf("\n");
}
while(getchar() != '\n');
Return_Menu();
}
void Short_High() //冒泡排序实现(低到高排序并且输出)
{
float Sum[31]={0};
for (int i = 0; i < Num_class; i++)
{
for (int j = 0; j < Num_course; j++)
{
Sum [i] += people[i].Grade.NUM[j];
}
}
for (int i = 0; i < Num_class - 1; i++)
{
for (int j = 0; j < Num_class - i - 1; j++)
{
if(Sum[j] > Sum[j+1])
{
InFor people_t = people[j];
people[j] = people[j+1];
people[j+1] = people_t;
float temp = Sum[j];
Sum[j] = Sum[j + 1];
Sum[j + 1] = temp;
}
}
}
printf("\n\n\n");
printf("The following are the results sorted from shortest to highest based on the total score:\n");
printf(" Name StudentID Grade\n");
for (int i = 0; i < Num_class; i++)
{
printf("%d %s %.0lf ",i+1,people[i].name,*(people[i].Stu_ID));
for (int k = 0; k < Num_course; k++)
{
printf("%.1lf",people[i].Grade.NUM[k]);
printf("\t");
}
printf("\n");
}
while(getchar() != '\n');
Return_Menu();
}
void Stu_ID_Order() //以学号的大小进行排序(由小到大) //注:学号在这里默认是相同位数,故没考虑位数不同的情况
{
for (int i = 0; i < Num_class - 1; i++)
{
for (int k = 0; k < Num_class - 1 - i; k++)
{
for (int j = 0; j < 12; j++)
{
if (people[k].Stu_ID[j] > people[k+1].Stu_ID[j])
{
InFor Temp = people[k];
people[k] = people[k+1];
people[k+1] = Temp;
}
}
}
}
printf("\n\n\n");
printf("The following are the results sorted from shortest to highest based on the student ID:\n");
printf(" Name StudentID Grade\n");
for (int i = 0; i < Num_class; i++)
{
printf("%d %s %.0lf ",i+1,people[i].name,*(people[i].Stu_ID));
for (int k = 0; k < Num_course; k++)
{
printf("%.1lf",people[i].Grade.NUM[k]);
printf("\t");
}
printf("\n");
}
while(getchar() != '\n');
Return_Menu();
}
void Name_Dic() //完成以字典顺序排序 //strcmp函数的应用!!!
{
for (int i = 0; i < Num_class - 1; i++)
{
for (int j = 0; j < Num_class - 1 - i; j++)
{
int min_length = strlen(people[j].name) < strlen(people[j+1].name) ? strlen(people[j].name) : strlen(people[j+1].name);
for (int k = 0; k < min_length; k++)
{
if(strcmp(people[j].name, people[j+1].name) > 0)
{
InFor temp = people[j];
people[j] = people[j+1];
people[j+1] = temp;
}
}
}
}
printf("\n\n\n");
printf("The following are the results sorted from shortest to highest based on the dictionary order:\n");
printf(" Name StudentID Grade\n");
for (int i = 0; i < Num_class; i++)
{
printf("%d %s %.0lf ",i+1,people[i].name,*(people[i].Stu_ID));
for (int k = 0; k < Num_course; k++)
{
printf("%.1lf",people[i].Grade.NUM[k]);
printf("\t");
}
printf("\n");
}
while(getchar() != '\n');
Return_Menu();
}
void FindStu_ID() //按照学号查找信息
{
double Id[12] = {0};
int n = 0;
printf("Please input the student ID to find the information of student:");
scanf("%lf",Id);
for (int i = 0; i < Num_class; i++)
{
if(*Id == *(people[i].Stu_ID))
{
n = i;
}
}
printf("%d %s %.0lf",n+1,people[n].name,*(people[n].Stu_ID));
for (int i = 0; i < Num_course; i++)
{
printf(" %.1lf",people[n].Grade.NUM[i]);
printf("\t");
}
printf("\n");
while(getchar() != '\n');
Return_Menu();
}
void FindName() //按照名字查找信息
{
char name[25];
int n = 0;
printf("Please input the student name:");
scanf("%s",name);
for (int i = 0; i < Num_class; i++)
{
if(strcmp(name,people[i].name) == 0)
{
n = i;
}
}
printf("%d %s %.0lf",n+1,people[n].name,*(people[n].Stu_ID));
for (int i = 0; i < Num_course; i++)
{
printf(" %.1lf",people[n].Grade.NUM[i]);
printf("\t");
}
printf("\n");
while(getchar() != '\n');
Return_Menu();
}
void Print_Level() //按照成绩划分优秀...
{
float sum[31] = {0},aver[31] = {0};
int a = 0,b = 0,c = 0,d = 0,e = 0; //代表五个程度
for(int i = 0; i < Num_class; i++){
for (int j = 0; j < Num_course; j++)
{
sum[i] += people[i].Grade.NUM[j];
aver[i] = sum[i]/Num_course; //计算每个人的平均分数
}
}
for (int i = 0; i < Num_class; i++)
{
if (aver[i] <= 100 && aver[i] >= 90)
{
a++;
}
else if(aver[i] <= 89 && aver[i] >= 80)
b++;
else if(aver[i] <= 79 && aver[i] >= 70)
c++;
else if(aver[i] <= 69 && aver[i] >= 60)
d++;
else
e++;
}
printf("优秀: %d %.4f\n",a,(float)a/Num_class);
printf("良好: %d %.4f\n",b,(float)b/Num_class);
printf("中等: %d %.4f\n",c,(float)c/Num_class);
printf("及格: %d %.4f\n",d,(float)d/Num_class);
printf("不及格: %d %.4f\n",e,(float)e/Num_class);
}
void Print_All() //打印所有已知信息
{
float sum[31] = {0};
for(int i = 0; i < Num_class; i++){
for (int j = 0; j < Num_course; j++)
{
sum[i] += people[i].Grade.NUM[j];
}
} //计算每个人的总成绩、平均分数
float Sum[6] = {0};
for(int i = 0; i < Num_course; i++){
for (int j = 0; j < Num_class; j++)
{
Sum[i] += people[j].Grade.NUM[i];
}
}
printf(" Name StudentID Grade Sum Average\n");
for (int i = 0; i < Num_class; i++)
{
printf("%d %s %.0lf ",i+1,people[i].name,*(people[i].Stu_ID));
for (int k = 0; k < Num_course; k++)
{
printf("%.1lf ",people[i].Grade.NUM[k]);
printf("\t");
}
printf("%.2f %.2f",sum[i],sum[i]/Num_course);
printf("\n");
}
for (int k = 0; k < Num_course; k++)
{
printf("The Class Sum Grade Is:%.2f\n",Sum[k]);
printf("The Class Average Grade Of %d Is:%.2f\n",k,Sum[k]/Num_class);
}
while(getchar() != '\n');
Return_Menu();
}
void Write_File() //将已知信息写入文件中
{
float sum[31] = {0}; //信息读取
for(int i = 0; i < Num_class; i++){
for (int j = 0; j < Num_course; j++)
{
sum[i] += people[i].Grade.NUM[j];
}
}
float Sum[6] = {0};
for(int i = 0; i < Num_course; i++){
for (int j = 0; j < Num_class; j++)
{
Sum[i] += people[j].Grade.NUM[i];
}
} //读取完毕
FILE* fp = fopen("students.txt", "w"); // 打开文件
if (fp == NULL) {
fprintf(fp,"Failed to open file.\n");
}
fprintf(fp," Name StudentID Grade Sum Average\n");
for (int i = 0; i < Num_class; i++)
{
fprintf(fp,"%d %s %.0lf ",i+1,people[i].name,*(people[i].Stu_ID));
for (int k = 0; k < Num_course; k++)
{
fprintf(fp,"%.1lf ",people[i].Grade.NUM[k]);
fprintf(fp,"\t");
}
fprintf(fp,"%.2f %.2f",sum[i],sum[i]/Num_course);
fprintf(fp,"\n");
}
for (int k = 0; k < Num_course; k++)
{
printf("The Class Sum Grade Is:%.2f\n",Sum[k]);
printf("The Class Average Grade Of %d Is:%.2f\n",k,Sum[k]/Num_class);
}
fclose(fp); // 关闭文件
}
void Read_File() //读取文件夹中的数据
{
char filename[256];
printf("Enter filename: ");
scanf("%s", filename);
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file: %s\n", filename);
exit(1);
}
char line[256];
while (fgets(line, 256, fp) != NULL) {
int id;
char name[256];
double score;
int ret = sscanf(line, "%d\t%s\t%lf", &id, name, &score); // 使用sscanf函数从字符串中读取学生记录信息
if (ret != 3) {
printf("Failed to parse line: %s", line);
}
else {
// 显示学生记录信息
printf("Name: %s\n", name);
printf("Student ID: %d\n", id);
printf("Score: %.2lf\n", score);
printf("\n");
}
}
fclose(fp);
printf("Done.\n");
}