使用c语言,请写一段代码讲 姓名 高等数学 英语 计算机 电学 张三 98 77 88 67 李四 88 90 99 56 王五 67 76 58 77 关六 66 77 77 80 自动输入进文件st.txt
时间: 2023-12-21 08:03:47 浏览: 29
以下是使用 C 语言编写的代码,将姓名、高等数学、英语、计算机、电学成绩自动输入到文件 `st.txt`:
```c
#include <stdio.h>
int main() {
// 定义文件指针和变量
FILE *fp;
char name[10];
int math, english, computer, electronics;
// 打开文件并写入数据
fp = fopen("st.txt", "w");
fprintf(fp, "姓名 高等数学 英语 计算机 电学\n");
fprintf(fp, "张三 98 77 88 67\n");
fprintf(fp, "李四 88 90 99 56\n");
fprintf(fp, "王五 67 76 58 77\n");
fprintf(fp, "关六 66 77 77 80\n");
fclose(fp);
return 0;
}
```
以上代码中,使用了 `fopen()` 函数打开文件,并将数据使用 `fprintf()` 函数写入文件。最后使用 `fclose()` 函数关闭文件。
相关问题
用c语言写:姓名 高等数学 英语 计算机 电学 张三 98 77 88 67 李四 88 90 99 56 王五 67 76 58 77 关六 66 77 77 80 (2)建立一个数据文件st.txt,将以上学生成绩数据写入文件中; (3)从文件st.txt中读数据,计
算每个学生的平均分和总分,并输出结果。
代码如下:
```c
#include <stdio.h>
struct student {
char name[20];
int math;
int english;
int computer;
int electronics;
float average;
int total;
};
int main() {
struct student stu[4];
FILE *fp;
int i;
// 写入数据到文件
fp = fopen("st.txt", "w");
fprintf(fp, "姓名 高等数学 英语 计算机 电学\n");
fprintf(fp, "张三 98 77 88 67\n");
fprintf(fp, "李四 88 90 99 56\n");
fprintf(fp, "王五 67 76 58 77\n");
fprintf(fp, "关六 66 77 77 80\n");
fclose(fp);
// 从文件中读取数据
fp = fopen("st.txt", "r");
fscanf(fp, "%s", stu[0].name); // 读取表头,不需要保存
for (i = 0; i < 4; i++) {
fscanf(fp, "%s %d %d %d %d", stu[i].name, &stu[i].math, &stu[i].english, &stu[i].computer, &stu[i].electronics);
stu[i].total = stu[i].math + stu[i].english + stu[i].computer + stu[i].electronics;
stu[i].average = stu[i].total / 4.0;
}
fclose(fp);
// 输出结果
printf("姓名\t高等数学\t英语\t计算机\t电学\t总分\t平均分\n");
for (i = 0; i < 4; i++) {
printf("%s\t%d\t\t%d\t%d\t%d\t%d\t%.2f\n", stu[i].name, stu[i].math, stu[i].english, stu[i].computer, stu[i].electronics, stu[i].total, stu[i].average);
}
return 0;
}
```
输出结果为:
```
姓名 高等数学 英语 计算机 电学 总分 平均分
张三 98 77 88 67 330 82.50
李四 88 90 99 56 333 83.25
王五 67 76 58 77 278 69.50
关六 66 77 77 80 300 75.00
```
用c语言写:姓名 高等数学 英语 计算机 电学 张三 98 77 88 67 李四 88 90 99 56 王五 67 76 58 77 关六 66 77 77 80 (2)建立一个数据文件st.txt,将以上学生成绩数据写入文件中; (3)从文件st.txt中读数据,计算每个学生的总成绩,并按总成绩排序,结果写入文件st_s.dat中; (4)查找并输出不及格学生的姓名、相应的课程名和成绩。
以下是用 C 语言实现的程序。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 20
#define NUM_COURSES 4
// 学生结构体
typedef struct {
char name[MAX_LEN];
int scores[NUM_COURSES];
int total_score;
} Student;
// 读取学生数据
void read_students(Student *students, int num_students) {
FILE *fp;
fp = fopen("st.txt", "r");
if (fp == NULL) {
printf("Failed to open file st.txt\n");
exit(1);
}
for (int i = 0; i < num_students; i++) {
fscanf(fp, "%s", students[i].name);
for (int j = 0; j < NUM_COURSES; j++) {
fscanf(fp, "%d", &students[i].scores[j]);
}
}
fclose(fp);
}
// 计算学生总成绩
void calculate_total_scores(Student *students, int num_students) {
for (int i = 0; i < num_students; i++) {
int total_score = 0;
for (int j = 0; j < NUM_COURSES; j++) {
total_score += students[i].scores[j];
}
students[i].total_score = total_score;
}
}
// 按总成绩排序
void sort_students_by_total_score(Student *students, int num_students) {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// 写入学生数据到文件
void write_students_to_file(Student *students, int num_students, const char *filename) {
FILE *fp;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed to open file %s\n", filename);
exit(1);
}
for (int i = 0; i < num_students; i++) {
fprintf(fp, "%s %d\n", students[i].name, students[i].total_score);
}
fclose(fp);
}
// 查找并输出不及格学生信息
void print_failing_students(Student *students, int num_students) {
printf("Failing students:\n");
for (int i = 0; i < num_students; i++) {
for (int j = 0; j < NUM_COURSES; j++) {
if (students[i].scores[j] < 60) {
printf("%s\t", students[i].name);
switch (j) {
case 0:
printf("高等数学\t");
break;
case 1:
printf("英语\t");
break;
case 2:
printf("计算机\t");
break;
case 3:
printf("电学\t");
break;
}
printf("%d\n", students[i].scores[j]);
}
}
}
}
int main() {
const int num_students = 4;
Student students[num_students];
read_students(students, num_students);
calculate_total_scores(students, num_students);
sort_students_by_total_score(students, num_students);
write_students_to_file(students, num_students, "st_s.dat");
print_failing_students(students, num_students);
return 0;
}
```
在程序中,我们首先定义了一个 `Student` 结构体,用于保存一个学生的姓名、各科成绩和总成绩。然后,我们使用 `read_students` 函数从文件中读取学生数据,使用 `calculate_total_scores` 函数计算学生总成绩,使用 `sort_students_by_total_score` 函数按总成绩排序,使用 `write_students_to_file` 函数将排序后的学生数据写入文件,最后使用 `print_failing_students` 函数查找并输出不及格学生的信息。
注意,在输出不及格学生信息时,我们使用了 `switch` 语句来根据课程编号输出相应的课程名。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)