C语言2. 键盘输入6名学生信息(学号、姓名、专业、性别、班级、出生年月日、两门课程的成绩),计算每名学生总分,将原有数据和平均分存放在磁盘文件JXXYstu.txt中。
时间: 2023-10-20 17:25:52 浏览: 98
以下是一个简单的实现,你可以根据需求进行修改:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENT 6
#define MAX_NAME_LENGTH 20
#define MAX_MAJOR_LENGTH 10
#define MAX_CLASS_LENGTH 10
// 学生信息结构体
typedef struct {
int id;
char name[MAX_NAME_LENGTH];
char major[MAX_MAJOR_LENGTH];
char gender;
char class[MAX_CLASS_LENGTH];
char birth[11];
int score1;
int score2;
int total_score;
} Student;
int main() {
Student students[MAX_STUDENT];
int i, total_score = 0;
FILE *fp;
// 输入学生信息
for (i = 0; i < MAX_STUDENT; i++) {
printf("请输入第%d名学生的信息:\n", i+1);
printf("学号:");
scanf("%d", &students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("专业:");
scanf("%s", students[i].major);
printf("性别:");
scanf(" %c", &students[i].gender);
printf("班级:");
scanf("%s", students[i].class);
printf("出生年月日(YYYY-MM-DD):");
scanf("%s", students[i].birth);
printf("第一门课的成绩:");
scanf("%d", &students[i].score1);
printf("第二门课的成绩:");
scanf("%d", &students[i].score2);
// 计算总分
students[i].total_score = students[i].score1 + students[i].score2;
// 累加总分
total_score += students[i].total_score;
}
// 计算平均分
float average_score = (float)total_score / MAX_STUDENT;
// 写入文件
fp = fopen("JXXYstu.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
exit(1);
}
fprintf(fp, "学号\t姓名\t专业\t性别\t班级\t出生年月日\t第一门课成绩\t第二门课成绩\t总分\n");
for (i = 0; i < MAX_STUDENT; i++) {
fprintf(fp, "%d\t%s\t%s\t%c\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name, students[i].major, students[i].gender, students[i].class, students[i].birth, students[i].score1, students[i].score2, students[i].total_score);
}
fprintf(fp, "平均分:%0.2f\n", average_score);
fclose(fp);
printf("学生信息已保存到文件JXXYstu.txt中!\n");
return 0;
}
```
注意,该程序仅作为示例,还有很多细节需要根据实际情况进行完善。
阅读全文