高级语言程序设计课程第十次个人作业
这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C
这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2024C/homework/13314
学号:<102400227>
姓名:<谭培>
1:电脑上可以运行但是没有执行命令,原来是没有找到文件,添加文件路径或保存在相同路径就解决了
#include <stdio.h>
int main() {
int ch;
FILE *fp1, *fp2;
fp1 = fopen("in.txt","r");
fp2 = fopen("out.txt","w");
while((ch = fgetc(fp1)) != EOF){
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
2:fseek应当与末尾偏移量为-1,因为终止符占1位
#include <stdio.h>
int main() {
FILE *fp1;
fp1 = fopen("in.txt","r");
fseek( fp1, -1 , 2);
int len=ftell(fp1);
printf("%d",len);
fclose(fp1);
return 0;
}
3:把输出换成标准流就好啦
#include <stdio.h>
int main() {
int ch;
FILE *fp1, *fp2;
fp1 = fopen("in.txt","r");
while((ch = fgetc(fp1)) != EOF){
fputc(ch,stdout);
}
fclose(fp1);
return 0;
}
4:正常读入数据,没问题
#include <stdio.h>
int main() {
int ch;
FILE *fp1, *fp2;
fp1 = fopen("in.txt","r");
while((ch = fgetc(stdin)) != EOF){
fputc(ch,fp1);
}
fclose(fp1);
return 0;
}
5:了解到一行长度是有限的,所以设置一个阈值防止溢出
#include <stdio.h>
#define BUF_SIZE 256
char buffer[BUF_SIZE];//防止溢出
int main()
{
FILE* fp1, * fp2;
fp1 = fopen("Take Me Hand.txt", "r+");
fp2 = fopen("out", "w+");
if (!fp1)
exit(0);
const char* predicate = "In my dreams I feel your light\n";
while (fgets(buffer, sizeof(buffer), fp1))
{
if (!strcmp(buffer, predicate))
continue;
fputs(buffer, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
6:没问题,书上有
#include <stdio.h>
int main() {
FILE *fp1;
fp1 = fopen("in.txt","r");
fseek( fp1, -1 , 2);
int len=ftell(fp1);
printf("%d",len);
fclose(fp1);
return 0;
7:使用了结构体将数据分类更清晰,使用函数使得各部分任务有条理
#include <stdio.h>
#include <string.h>
#define CSIZE 5
typedef struct {
char id[11];
char first[11];
char last[11];
} full_name;
typedef struct {
full_name name;
double grade[3];
double average;
} student;
// 输入学生信息
void input_student_info(student list[]) {
char c;
for (int i = 0; i < CSIZE; i++) {
printf("Please enter student %d's id:\n", i + 1);
scanf("%10s", list[i].name.id);
printf("Please enter student %d's name:\n", i + 1);
if (scanf("%10s %10s", list[i].name.first, list[i].name.last) == 2) {
printf("Now,enter the 3 scores of this student:\n");
if (scanf("%lf%lf%lf", &list[i].grade[0], &list[i].grade[1], &list[i].grade[2]) == 3) {
printf("Well,continue.\n");
}
// 清空输入缓冲区
while ((c = getchar())!= '\n' && c!= EOF);
}
}
}
// 计算平均成绩
void calculate_average(student list[]) {
for (int i = 0; i < CSIZE; i++) {
double sum = 0;
for (int j = 0; j < 3; j++) {
sum += list[i].grade[j];
}
list[i].average = sum / 3;
}
}
// 将数据写入文件
void write_to_file(student list[]) {
FILE *fp = fopen("student.txt", "w+");
if (fp == NULL) {
perror("Error opening file");
return;
}
fprintf(fp, "%-10s %-10s %-10s%12s%20s\n", "ID", "First Name", "Last Name", "Grades", "Average");
for (int i = 0; i < CSIZE; i++) {
fprintf(fp, "%-10s%-10s%-10s %.2f %.2f %.2f %10.2f\n", list[i].name.id, list[i].name.first, list[i].name.last,
list[i].grade[0], list[i].grade[1], list[i].grade[2], list[i].average);
}
fclose(fp);
}
int main() {
student list[CSIZE] = {};
input_student_info(list);
calculate_average(list);
write_to_file(list);
return 0;
}