当前位置: 首页 > news >正文

C语言:学生成绩管理系统(含源代码)

一.功能

二.源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 100
typedef struct
{char no[30];char name[10];char sex[10];char phone[20];float cyuyan;float computer;float datastruct;
} *student, student1;typedef struct
{student stu[MAX_NUM];int number;
} *studentDB;void read(studentDB temp)
{FILE *infile;infile = fopen("./数据.txt", "r");if (!infile){printf("文件打开失败!");exit(0);}while (!feof(infile)){temp->stu[temp->number] = malloc(sizeof(student1));fscanf(infile, "%s %s %s %s %f %f %f", temp->stu[temp->number]->no, temp->stu[temp->number]->name, temp->stu[temp->number]->sex, temp->stu[temp->number]->phone, &(temp->stu[temp->number]->cyuyan), &(temp->stu[temp->number]->computer), &(temp->stu[temp->number]->datastruct));temp->number++;}fclose(infile);
}void write(studentDB temp)
{FILE *outfile;outfile = fopen("./数据.txt", "w");if (!outfile){printf("文件打开失败!");exit(1);}if (temp && temp->number > 0){int i;for (i = 0; i < temp->number; i++){if (i == temp->number - 1){fprintf(outfile, "%s %s %s %s %.2f %.2f %.2f", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);}else{fprintf(outfile, "%s %s %s %s %.2f %.2f %.2f\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);}}}fclose(outfile);printf("保存成功");read(temp);
}void display(studentDB temp)
{int i;printf("|   学号   |姓名|性别|  手机号  |c语言|英语|高等数学|\n");for (i = 0; i < temp->number; i++){printf("%s %s %s %s %.2f %.2f %.2f\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);}
}void menu()
{printf("\n\n\t****************************简单学生信息管理系统*****************************\n");printf("\t*                              1.显示学生信息                             *|\n");printf("\t*                              2.增加学生信息                             *|\n");printf("\t*                              3.删除学生信息                             *|\n");printf("\t*                              4.修改学生信息                             *|\n");printf("\t*                              5.查询学生信息                             *|\n");printf("\t*                              6.排序学生成绩                             *|\n");printf("\t*                              7.计算学生平均成绩                         *|\n");printf("\t*                              8.保存学生信息                             *|\n");printf("\t*                              9.统计全部课程及格人数                     *|\n");printf("\t*                              10.输出总成绩最高的学生信息                *|\n");printf("\t*                              0.退出系统                                 *|\n");printf("\t***************************************************************************\n");printf("请选择你的操作并将序号输入:");
}int countDigits(long long n)
{int count = 0;while (n > 0){n /= 10;count++;}return count;
}void insert(studentDB temp)
{char no[30];printf("请输入要添加学生的学号:");scanf("%s", no);int n;long long num;sscanf(no, "%lld", &num);n = countDigits(num);if (n != 11){printf("输入的学号位数有误,请重新输入!");return;}else{student1 stu;FILE *outfile;outfile = fopen("./数据.txt", "a");if (!outfile){printf("文件打开失败!");exit(0);}strcpy(stu.no, no);printf("请输入姓名:");scanf("%s", stu.name);printf("请输入性别:");scanf("%s", stu.sex);printf("请输入手机号:");scanf("%s", stu.phone);printf("请输入c语言成绩(带小数点):");scanf("%f", &stu.cyuyan);printf("请输入英语成绩(带小数点):");scanf("%f", &stu.computer);printf("请输入高等数学成绩(带小数点):");scanf("%f", &stu.datastruct);n = fprintf(outfile, "\n%s %s %s %s %f %f %f", stu.no, stu.name, stu.sex, stu.phone, stu.cyuyan, stu.computer, stu.datastruct);printf("%s %s %s %s %.2f %.2f %.2f", stu.no, stu.name, stu.sex, stu.phone, stu.cyuyan, stu.computer, stu.datastruct);printf("学生信息已成功插入!信息长度:%d\n", n);fclose(outfile);read(temp);return;}
}void delete(studentDB temp)
{printf("请输入要删除的学生信息学号:");char no[15];int i, k;scanf("%s", no);printf("%s", no);for (i = 0; i < temp->number; i++){if (strcmp(temp->stu[i]->no, no) == 0){for (k = i; k < temp->number; k++){temp->stu[k] = temp->stu[k + 1];}free(temp->stu[temp->number]);temp->number--;printf("学生信息已成功删除!\n");return;}}printf("学生学号输入错误!");return;
}void modify(studentDB temp)
{printf("请输入要修改的学生信息学号:");char no[15];int i, flag = 0;long num;scanf("%s", no);sscanf(no, "%ld", &num);for (i = 0; i < temp->number; i++){if (strcmp(no, temp->stu[i]->no) == 0){printf("\n学号:%s  姓名:%s  性别:%s  手机号:%s  c语言:%.2f  英语:%.2f  高等数学:%.2f\n\n\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);printf("|1.学号|2.姓名|3.性别|4.手机号|5.c语言成绩|6.英语成绩|7.高等数学成绩|8.不改动并返回菜单|9.依次修改全部数据\n\n\n请输入要改动的数据项:");scanf("%d", &flag);switch (flag){case 1:printf("请输入要修改的学号:");scanf("%s", temp->stu[i]->no);break;case 2:printf("请输入姓名:");scanf("%s", temp->stu[i]->name);break;case 3:printf("请输入性别:");scanf("%s", temp->stu[i]->sex);break;case 4:printf("请输入手机号:");scanf("%s", temp->stu[i]->phone);break;case 5:printf("请输入c语言成绩(带小数点):");scanf("%f", &(temp->stu[i]->cyuyan));break;case 6:printf("请输入英语成绩(带小数点):");scanf("%f", &(temp->stu[i]->computer));break;case 7:printf("请输入高等数学成绩(带小数点):");scanf("%f", &(temp->stu[i]->datastruct));break;case 8:return;case 9:printf("请输入姓名:");scanf("%s", temp->stu[i]->name);printf("请输入性别:");scanf("%s", temp->stu[i]->sex);printf("请输入手机号:");scanf("%s", temp->stu[i]->phone);printf("请输入c语言成绩(带小数点):");scanf("%f", &(temp->stu[i]->cyuyan));printf("请输入英语成绩(带小数点):");scanf("%f", &(temp->stu[i]->computer));printf("请输入高等数学成绩(带小数点):");scanf("%f", &(temp->stu[i]->datastruct));break;default:printf("请重新选择!");break;}return;}}
}void search(studentDB temp)
{printf("请输入要查询的学生信息学号:");char no[30];int i, n;long long num;scanf("%s", no);sscanf(no, "%lld", &num);n = countDigits(num);if (n != 11){printf("输入的学号位数有误,请重新输入!");return;}else{for (i = 0; i < temp->number; i++){if (strcmp(temp->stu[i]->no, no) == 0){printf("\n学号:%s  姓名:%s  性别:%s  手机号:%s  c语言:%.2f  英语:%.2f  高等数学:%.2f\n\n\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);return;}}printf("学生学号输入错误!");return;}
}void sort(studentDB temp)
{int i, j, n;float t;for (i = 0; i < temp->number; i++){for (j = i + 1; j < temp->number; j++){if ((temp->stu[i]->cyuyan + temp->stu[i]->computer + temp->stu[i]->datastruct) < (temp->stu[j]->cyuyan + temp->stu[j]->computer + temp->stu[j]->datastruct)){student1 *tempstu = temp->stu[i];temp->stu[i] = temp->stu[j];temp->stu[j] = tempstu;}}}printf("已按成绩排序完成!\n");
}void average(studentDB temp)
{int i;float ctotal = 0, comtotal = 0, dstotal = 0;for (i = 0; i < temp->number; i++){ctotal += temp->stu[i]->cyuyan;comtotal += temp->stu[i]->computer;dstotal += temp->stu[i]->datastruct;}printf("C语言平均成绩:%.2f 英语平均成绩:%.2f 高等数学平均成绩:%.2f\n", ctotal / temp->number, comtotal / temp->number, dstotal / temp->number);
}void passing(studentDB temp)
{int i, cpass = 0, compass = 0, dpass = 0;for (i = 0; i < temp->number; i++){if (temp->stu[i]->cyuyan >= 60){cpass++;}if (temp->stu[i]->computer >= 60){compass++;}if (temp->stu[i]->datastruct >= 60){dpass++;}}printf("C语言及格人数:%d 英语及格人数:%d 高等数学及格人数:%d\n", cpass, compass, dpass);
}void max(studentDB temp)
{int i, index = 0;float maxScore = 0;for (i = 0; i < temp->number; i++){float totalScore = temp->stu[i]->cyuyan + temp->stu[i]->computer + temp->stu[i]->datastruct;if (totalScore > maxScore){maxScore = totalScore;index = i;}}printf("学号:%s  姓名:%s  性别:%s  手机号:%s  c语言:%.2f  英语:%.2f  高等数学:%.2f 总成绩:%.2f\n", temp->stu[index]->no, temp->stu[index]->name, temp->stu[index]->sex, temp->stu[index]->phone, temp->stu[index]->cyuyan, temp->stu[index]->computer, temp->stu[index]->datastruct, maxScore);
}int main()
{int select;studentDB stu = malloc(sizeof(studentDB));stu->number = 0;read(stu);do{menu();scanf("%d", &select);switch (select){case 1:display(stu);break;case 2:insert(stu);break;case 3:delete (stu);break;case 4:modify(stu);break;case 5:search(stu);break;case 6:sort(stu);break;case 7:average(stu);break;case 8:write(stu);break;case 9:passing(stu);break;case 10:max(stu);break;case 0:exit(1);}} while (1);return 0;
}

三.注意事项

(1)事先准备: 数据.txt 文件(可以含有学生信息,可以空白,但是必须有)

(2)学号为11

(3)该代码不含有C99或者C11

http://www.lryc.cn/news/361226.html

相关文章:

  • MySQL 导出导入的101个坑
  • OpenCv之简单的人脸识别项目(人脸提取页面)
  • linux 内核映像差异介绍:vmlinux、zImage、zbImage、image、uImage等
  • 【Linux-INPUT输入的子系统】
  • 密码加密及验证
  • 找出字符串中出现最多次数的字符以及出现的次数
  • 如何看待央行买卖长期国债?
  • MATLAB算法实战应用案例精讲-【数模应用】Turf组合模型(附MATLAB、python和R语言代码实现)
  • android源码下载编译模拟器运行
  • Golang:Sirupsen/logrus是一个日志库
  • Android Studio插件开发 - Dora SDK的IDE插件
  • 【mybatis】缓存
  • 自定义类型:结构体类型
  • C++对象移动
  • “华为杯”第十三届中国研究生 数学建模竞赛-E题:粮食最低收购价政策问题研究(续)
  • (一)django目录介绍
  • leetcode5 最长回文子串
  • 《论文阅读》通过顺序不敏感的表示正则化实现稳健的个性化对话生成 ACL 2023
  • python采集汽车价格数据
  • 德克萨斯大学奥斯汀分校自然语言处理硕士课程汉化版(第四周) - 语言建模
  • Jitsi meet 退出房间后,用户还在房间内
  • Java 18 新特性
  • c++基础创建对象
  • WHAT - 容器化系列(二)- docker
  • 力扣 19题 删除链表的倒数第 N 个结点 记录
  • 渗透测试之Web安全系列教程(二)
  • 【算法】在?复习一下快速排序?
  • matlab安装及破解
  • Tree——输出项目的文件结构(Linux)
  • UE5 读取本地图片并转换为base64字符串