c语言版学生管理系统
作者:互联网
老样子,这个程序和上一个差不多,首先我们还是创建一个学生类,不过我们学生类中多了一个next指针,用来指向下一个学生,我们利用的是动态内存的方式保存数据成员。上一个c++的我们运用的是数组的方式。
#pragma once
#include<stdio.h>
#include<string>
using namespace std;
typedef struct Student {
public:
int id;//学号
char name[10] = "0";//姓名
int politicsScore=0;//思政成绩
int mathScore=0;//高数成绩
int englishScore=0;//英语成绩
int Score=0;//总成绩
Student*next;
}stu;
因为c语言中不能用类的方式,我们只好定义多个函数了。
其中的第三行代码是vs中解决scanf 不安全用的。
#include"Student.h"
#define FILENAME "empFile.txt"
#pragma warning(disable:4996)
using namespace std;
void add(Student *head) {
Student *q = (stu*)malloc(sizeof(stu));
q->next = NULL;
stu *p;
p = head;
char name[10] = {0};//姓名
int id;//学号
int mathScore = 5;//高数成绩
int politicsScore = 0;//思政成绩
int englishScore = 0;//英语成绩
int Score = 0;//总成绩
printf("请输入该学生的姓名:");
scanf_s("%s", name,10);
printf("请输入该学生的id:");
scanf_s("%d", &id);
printf("请输入该学生的高数成绩");
scanf_s("%d", &mathScore);
printf("请输入该学生的思政成绩");
scanf_s("%d", &politicsScore);
printf("请输入该学生的英语成绩");
scanf_s("%d", &englishScore);
Score = mathScore + politicsScore + englishScore;
q->id = id; q->englishScore = englishScore; q->Score = Score; q->politicsScore = politicsScore; q->mathScore = mathScore;
strcpy_s(q->name, strlen(q->name) - 1, name);
while (p->next != NULL) {
p = p->next;
}
p->next = q;
printf("添加成功!");
}
void Add(stu *head) { //添加
int n;
printf("输入想插入学生个数:\n");
scanf_s("%d", &n);
for (int x = 0; x < n; x++) {
add(head);
}
}
void Del(stu *head) { //删除
stu *p, *q;
p = head;
int m_id;
if (p->next == NULL) {
printf("此时无数据");
}
else {
printf("请输入你想删除的学生的id:");
scanf_s("%d", &m_id);
while (p->next != NULL) {
q = p->next;
if (p->next->id == m_id) {
p->next = q->next;
free(q);
printf("删除成功");
break;
}
p = p->next;
if (p->next == NULL) {
printf("查无此人");
}
}
}
}
void Print(Student *head)//显示学生信息
{
stu *p=NULL;
p = head->next;
if (p == NULL)printf("文件为空");
while (p != NULL)
{
printf("id为:%d 姓名:%s 高数成绩:%d 思政成绩:%d 英语成绩:%d 总成绩:%d\n",
p->id,p->name, p->mathScore, p->politicsScore, p->englishScore, p->Score);
p = p->next;
}
return;
}
void Modify(stu*head) {//修改学生信息
stu*p = head;
printf("请输入要修改人的id");
int id = 0;
char name[10] = "0";
int mathScore = 0;
int politicsScore=0;
int englishScore = 0;
int Score = 0;
scanf("%d", &id);
while (p) {
if (id == p->id) {
printf("请输入该学生的姓名:");
scanf("%s", name, 10);
printf("请输入该学生的id:");
scanf("%d", &id);
printf("请输入该学生的高数成绩");
scanf("%d", &mathScore);
printf("请输入该学生的思政成绩");
scanf("%d", &politicsScore);
printf("请输入该学生的英语成绩");
scanf("%d", &englishScore);
Score = mathScore + politicsScore + englishScore;
p->id = id; p->englishScore = englishScore; p->Score = Score; p->politicsScore = politicsScore; p->mathScore = mathScore;
strcpy(p->name, name);
printf("修改成功");
break;
}
p = p->next;
}
if (p == NULL)printf("查无此人");
}
void Search(stu *head) { //搜索
int k = 0;
char name3[10]="0";
printf("请输入要搜所的学生的名字\n");
scanf_s("%s", name3,10);
stu *p;
for (p = head->next; p != NULL; p = p->next) {
if (strcmp(name3, p->name) == 0) {
k = k + 1;
printf("id为:%d 姓名:%s 高数成绩:%d 思政成绩:%d 英语成绩:%d 总成绩:%d",
p->id, p->name, p->mathScore, p->politicsScore,p->englishScore, p->Score);
}
}
if (k == 0) {
printf("查无此人");
}
}
void Save(Student*head)//保存学生信息到文件
{
FILE *fp;
Student *p = head->next;
if ((fp = fopen("FILENAME", "w")) == NULL)// 以可写的方式打开当前目录下的.txt
{
printf("不能打开此文件,请按任意键退出\n");
exit(1); //异常退出
}
fp = fopen(FILENAME, "w");
while (p)
{
fprintf(fp, "%d %s %d %d %d %d\n", p->id, p->name, p->mathScore, p->politicsScore, p->englishScore, p->Score);
p = p->next;
}
printf("保存成功\n");
fclose(fp);
}
void Init(Student*head) {//初始化学生信息
FILE * fp;
Student*p = head;
if ((fp = fopen(FILENAME, "r")) == NULL) return;
//从文件读取信息
fp = fopen(FILENAME, "r");//以只读方式打开文件
int id = 0;//学号
char name[10] = "0";//姓名
int politicsScore = 0;//思政成绩
int mathScore = 0;//高数成绩
int englishScore = 0;//英语成绩
int Score = 0;//总成绩
while (fscanf(fp, "%d %s %d %d %d %d\n", &id, &name, &mathScore, &politicsScore, &englishScore, &Score) != EOF)//将文件信息输入到链表中
{
Student *q = (Student*)malloc(sizeof(Student));
q->next = NULL;
q->id = id;
strcpy_s(q->name, strlen(q->name) - 1, name);
q->englishScore = englishScore;
q->mathScore = mathScore;
q->politicsScore = politicsScore;
q->Score = Score;
p->next = q;
p = p->next;
if (p == NULL)break;
}
fclose(fp);//关闭文件
}
void freeStudent(Student* h)//释放内存
{
Student* p = h, *q = h;
while (p)
{
q = p->next;
free(p);
p = q;
}
}
int main() {
int i=0;
Student *head = (Student*)malloc(sizeof(Student));
head->next = NULL;
Init(head);
do {
printf("0、退出\n");
printf("1、新增学生信息\n");
printf("2、删除学生信息\n");
printf("3、学生信息搜索\n");
printf("4、显示学生信息\n");
printf("5、修改学生信息\n");
printf("6、学生信息保存\n");
printf("请输入要实现的功能\n");
scanf_s("%d", &i);
switch (i) {
case 0:
{printf("欢迎下次使用");
system("pause");
exit(0); }
case 1:
system("cls");
Add(head);
system("pause");
system("cls");
break;
case 2:
system("cls");
Del(head);
system("pause");
system("cls");
break;
case 3:
system("cls");
Search(head);
system("pause");
system("cls");
break;
case 4:
{system("cls");
Print(head);
system("pause");
system("cls");
break;
}
case 5:
system("cls");
Modify(head);
system("pause");
system("cls");
break;
case 6:
{system("cls");
Save(head);
system("pause");
system("cls");
break;
}
}
} while (i != 0);
freeStudent(head);
printf("运行结束");
return 0;
}
标签:head,name,管理系统,int,next,printf,学生,id,语言版 来源: https://blog.csdn.net/weixin_62657143/article/details/123591713