创建一个c语言程序学生成绩管理系统包括添加学生成绩,查询学生成绩,更新学生成绩,统计学生成绩,输出学生成绩,退出系统
时间: 2024-01-07 15:21:00 浏览: 77
好的,以下是一个简单的 C 语言学生成绩管理系统的代码示例,包括添加学生成绩,查询学生成绩,更新学生成绩,统计学生成绩,输出学生成绩,退出系统等功能。
```
#include <stdio.h>
#include <stdlib.h>
struct student {
char name[50];
int roll;
float marks;
} s;
void addRecord() {
FILE *fptr;
fptr = fopen("database.txt", "a");
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
fprintf(fptr, "%s %d %.2f\n", s.name, s.roll, s.marks);
fclose(fptr);
}
void searchRecord() {
FILE *fptr;
int r, flag = 0;
fptr = fopen("database.txt", "r");
printf("Enter roll number to search: ");
scanf("%d", &r);
while (fscanf(fptr, "%s %d %f\n", s.name, &s.roll, &s.marks) != EOF) {
if (s.roll == r) {
printf("Name: %s\nRoll number: %d\nMarks: %.2f\n", s.name, s.roll, s.marks);
flag = 1;
}
}
fclose(fptr);
if (flag == 0) {
printf("Record not found!\n");
}
}
void updateRecord() {
FILE *fptr, *fptr2;
int r, flag = 0;
fptr = fopen("database.txt", "r");
fptr2 = fopen("temp.txt", "w");
printf("Enter roll number to update: ");
scanf("%d", &r);
while (fscanf(fptr, "%s %d %f\n", s.name, &s.roll, &s.marks) != EOF) {
if (s.roll == r) {
printf("Enter new name: ");
scanf("%s", s.name);
printf("Enter new marks: ");
scanf("%f", &s.marks);
fprintf(fptr2, "%s %d %.2f\n", s.name, s.roll, s.marks);
flag = 1;
}
else {
fprintf(fptr2, "%s %d %.2f\n", s.name, s.roll, s.marks);
}
}
fclose(fptr);
fclose(fptr2);
remove("database.txt");
rename("temp.txt", "database.txt");
if (flag == 0) {
printf("Record not found!\n");
}
}
void countRecord() {
FILE *fptr;
int count = 0;
float total = 0, avg;
fptr = fopen("database.txt", "r");
while (fscanf(fptr, "%s %d %f\n", s.name, &s.roll, &s.marks) != EOF) {
count++;
total += s.marks;
}
fclose(fptr);
avg = total / count;
printf("Total students: %d\n", count);
printf("Average marks: %.2f\n", avg);
}
void displayRecord() {
FILE *fptr;
fptr = fopen("database.txt", "r");
printf("Name\tRoll\tMarks\n");
while (fscanf(fptr, "%s %d %f\n", s.name, &s.roll, &s.marks) != EOF) {
printf("%s\t%d\t%.2f\n", s.name, s.roll, s.marks);
}
fclose(fptr);
}
int main() {
int ch;
while (1) {
printf("\n1. Add record\n");
printf("2. Search record\n");
printf("3. Update record\n");
printf("4. Count record\n");
printf("5. Display record\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
addRecord();
break;
case 2:
searchRecord();
break;
case 3:
updateRecord();
break;
case 4:
countRecord();
break;
case 5:
displayRecord();
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
注意:该程序只是一个简单的示例,实际应用中还需要进行错误处理、数据验证等工作。
阅读全文