#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float elec, expe, requ, sum;
};
struct student stu[50];
void menu()/* 自定义函数实现功能*/
{
system("cls");
printf("\n\n\n\n\n");
printf("\t\t|-----------------STUDENT-----------------|\n");
printf("\t\t|\t 1. search record |\n");
printf("\t\t|\t 2. delete record |\n");
printf("\t\t|\t 3. modify record |\n");
printf("\t\t|\t 4. insert record |\n");
printf("\t\t|\t 5. total |\n");
printf("\t\t|\t 6. exit |\n");
printf("\t\t|-----------------------------------------|\n\n");
printf("\t\tchoose(1-6):");
}
void search(int snum)
{
FILE *fp;
int i = 0;
if ((fp = fopen("data", "rb")) == NULL)
{
printf("can not open\n");
return;
}
while (fread(&stu[i], LEN, 1, fp) == 1)
{
if (stu[i].num == snum)
{
printf("num:%d name:%s elective:%.2f experiment:%.2f require course:%.2f sum:%.2f\n", stu[i].num, stu[i].name, stu[i].elec, stu[i].expe, stu[i].requ, stu[i].sum);
break;
}
i++;
}
fclose(fp);
}
void del(int dnum)
{
FILE *fp;
int i = 0, j, m = 0;
if ((fp = fopen("data", "rb+")) == NULL)
{
printf("can not open\n");
return;
}
while (fread(&stu[i], LEN, 1, fp) == 1)
{
if (stu[i].num == dnum)
{
for (j = i; j < m - 1; j++)
stu[j] = stu[j + 1];
m--;
fseek(fp, 0L, 0);
for (j = 0; j < m; j++)
fwrite(&stu[j], LEN, 1, fp);
break;
}
i++;
}
fclose(fp);
}
void modify(int mnum)
{
FILE *fp;
int i = 0;
if ((fp = fopen("data", "rb+")) == NULL)
{
printf("can not open\n");
return;
}
while (fread(&stu[i], LEN, 1, fp) == 1)
{
if (stu[i].num == mnum)
{
printf("please input new elective:");
scanf("%f", &stu[i].elec);
printf("please input new experiment:");
scanf("%f", &stu[i].expe);
printf("please input new require course:");
scanf("%f", &stu[i].requ);
stu[i].sum = stu[i].elec + stu[i].expe + stu[i].requ;
fseek(fp, i * LEN, 0);
fwrite(&stu[i], LEN, 1, fp);
break;
}
i++;
}
fclose(fp);
}
void insert()
{
FILE *fp;
int i = 0, m = 0;
float Felec, Fexpe, Frequ;
if ((fp = fopen("data", "ab+")) == NULL)
{
printf("can not open\n");
return;
}
while (fread(&stu[m], LEN, 1, fp) == 1)
m++;
printf("student number:\n");
scanf("%d", &stu[m].num);
printf("please input per centum\n");
printf("elective:");
scanf("%f", &Felec);
printf("experiment:");
scanf("%f", &Fexpe);
printf("require course:");
scanf("%f", &Frequ);
printf("name:\n");
scanf("%s", stu[m].name);
printf("elective:");
scanf("%f", &stu[m].elec);
printf("experiment:");
scanf("%f", &stu[m].expe);
printf("require course:");
scanf("%f", &stu[m].requ);
stu[m].sum = stu[m].elec * Felec + stu[m].expe * Fexpe + stu[m].requ * Frequ;
fwrite(&stu[m], LEN, 1, fp);
fclose(fp);
}
void total()
{
FILE *fp;
int m = 0;
if ((fp = fopen("data", "rb")) == NULL)
{
printf("can not open\n");
return;
}
while (fread(&stu[m], LEN, 1, fp) == 1)
m++;
printf("the class has %d students!\n", m);
fclose(fp);
}
int main()
{
int choice, num;
while (1)
{
menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("please input the number you want to search:");
scanf("%d", &num);
search(num);
break;
case 2:
printf("please input the number you want to delete:");
scanf("%d", &num);
del(num);
break;
case 3:
printf("please input the number you want to modify:");
scanf("%d", &num);
modify(num);
break;
case 4:
insert();
break;
case 5:
total();
break;
case 6:
exit(0);
default:
printf("input error, please input again\n");
}
}
return 0;
}
C语言简单实现学生成绩管理系统
本文详细描述了一个用C语言实现的学生信息管理系统,包括搜索、删除、修改、插入记录以及查看总人数的功能,通过结构体和文件操作来管理数据。
摘要由CSDN通过智能技术生成