成信大2021自动化专业-平时自主学习-C语言改错题解题参考-整理第07页

本文通过具体案例详细解析了C语言编程中常见的错误,并提供了正确的修改方案,涉及文件操作、结构体、指针等核心概念。

第07页

题面如下:

在这里插入图片描述

题解如下:

D1043.c

原文件

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   
   
	FILE *fp;
	char ch;

	/*********Found************/
	if ((fp = fopen("Exam.txt", "r")) == NULL)  
	{
   
   
		printf("can not open this file\n");
		exit(0);
	}

	/*********Found************/
	for( ; ch=getchar() == '@'; )
	{
   
   
		fputc(ch, fp);
	}

	fclose(fp);
	return 0;
}

改后文件

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   
   
	FILE *fp;
	char ch;

	/*********Found************/
	if ((fp = fopen("Exam.txt", "w")) == NULL)  
	{
   
   
		printf("can not open this file\n");
		exit(0);
	}

	/*********Found************/
	for( ; (ch=getchar()) != '@'; )
	{
   
   
		fputc(ch, fp);
	}

	fclose(fp);
	return 0;
}

考查要点:

  1. 打开文件的模式要与功能需求一致,读用r,写用w
  2. ch=getchar()赋值完成以后再判断,注意运算的优先级

D1045.c

原文件

#include <stdio.h>

struct Student
{
   
   
	char No[11];
	int Score;
};

int FindMaxScore(struct Student stu[], int n);

int main(void)
{
   
   
	struct Student stus[3] = {
   
   {
   
   "2008030201", 89}, {
   
   "2008030202", 92}, {
   
   "2008030203", 78}};
	int k;
	
	k = FindMaxScore(stus, 3);
	printf("成绩最高的学生信息是:\n");
	printf("学号\t\t成绩\n");
	printf("%s\t%d\n", stus[k].No, stus[k].Score);
	
	return 0;
}

/*********Found************/
int FindMaxScore(______________________)
{
   
   
	int i, max, k=0;
	
	max = stu[k].Score;	
	for (i=1; i<n; i++)
	{
   
   
		/*********Found************/
		if (__________________) 
		{
   
   
			k = i;
			max = stu[k].Score;
		}
	}

	return k;
}

改后文件

#include <stdio.h>

struct Student
{
   
   
	char No[11];
	int Score;
};

int FindMaxScore(struct Student stu[], int n);

int main(void)
{
   
   
	struct Student stus[3] = {
   
   {
   
   "2008030201", 89}, {
   
   "2008030202", 92}, {
   
   "2008030203", 78}};
	int k;
	
	k = FindMaxScore(stus, 3);
	printf("成绩最高的学生信息是:\n");
	printf("学号\t\t成绩\n");
	printf("%s\t%d\n", stus[k].No, stus[k].Score);
	
	return 0;
}

/*********Found************/
int FindMaxScore(struct Student stu[], int n)
{
   
   
	int i, max, k=0;
	
	max = stu[k].Score;	
	for (i=1; i<n; i++)
	{
   
   
		/*********Found************/
		if (max < stu[i].Score) 
		{
   
   
			k = i;
			max = stu[k].Score;
		}
	}

	return k;
}

考查要点:

  1. 函数的参数:有数组时,一般都要将数组元素的个数也传入,方便对数据直接进行操作
  2. 函数的声明,定义实现,调用,头部分保持一致
  3. 这里使用的是打擂算法,要将数组里面的结构的对应成员值取出来比较【选取数组元素,取出来是一个结构,再打点取该元素对应的成员】

D1046.c

原文件

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   
   
	FILE *fp;
	int ch;

	/*********Found************/
	fp = fopen("jsgc01.txt", "w");
	if (NULL == fp)
	{
   
     
		printf( "Cannot open file!\n") ;
		exit(1) ;     
	}

	ch = fgetc(fp);
	/*********Found************/
	while (ch)
	{
   
   
		putchar(ch);
		ch = fgetc(fp);
	}
	printf("\n");

	fclose(fp);
	return 0;
}

改后文件

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   
   
	FILE *fp;
	int ch;

	/*********Found************/
	fp = fopen("jsgc01.txt", "r");
	if (NULL == fp)
	{
   
     
		
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值