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

第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)
	{  
		printf( "Cannot open file!\n") ;
		exit(1) ;     
	}

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

	fclose(fp);
	return 0;
}

考查要点:

  1. 文件打开模式按功能要求来做,读用r,写用w
  2. 判断有没有读到文件末尾,需要使用EOF来判断读出来的字符数据

D1047.c

原文件

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

struct aa
{
	int a;
	double b;
};

/*********Found************/
void Input(struct aa x);

int main(void)
{
	struct aa y;

	/*********Found************/
	Input(y);
	printf("%d %f\n", y.a, y.b);
	return 0;
}

/*********Found************/
void Input(struct aa x)
{
	char tmp[10];

	printf("请输入整型数a:");
	gets(tmp);
	x->a = atoi(tmp);
	printf("请输入单精度数b:");
	gets(tmp);
	x->b = atof(tmp);
}

改后文件

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

struct aa
{
	int a;
	double b;
};

/*********Found************/
void Input(struct aa *x);

int main(void)
{
	struct aa y;

	/*********Found************/
	Input(&y);
	printf("%d %f\n", y.a, y.b);
	return 0;
}

/*********Found************/
void Input(struct aa *x)
{
	char tmp[10];

	printf("请输入整型数a:");
	gets(tmp);
	x->a = atoi(tmp);
	printf("请输入单精度数b:");
	gets(tmp);
	x->b = atof(tmp);
}

考查要点:

  1. Input是输入功能的实现,写到内存,肯定要使用传地址的方式
  2. 定义时,入口参数为指针,调用时,要取地址

D1048.c

原文件

#include <stdio.h>
#include <string.h>

void func(char *str[]);

int main(void)
{
	int i;
	char s[6][1024], *pStr[6];

	printf("请输入6个字符串:\n");
	for (i=0; i<6; i++)
	{
		gets(s[i]);
		pStr[i] = s[i];
	}

	/*********Found************/
	func(_________);
	printf("\n排序后:\n");
	for (i=0; i<6; i++)
	{
		/*********Found************/
		puts(_________);
	}
	
	return 0;
}

void func(char *str[])
{
	int i, j;
	char *temp;

	for (i=0; i<5; i++)
	{
		for (j=i+1; j<6; j++)
		{
			/*********Found************/
			if (strcmp(________________) > 0)
			{
				temp = str[j];
				str[j] = str[i];
				str[i] = temp;
			}
		}
	}
}

改后文件

#include <stdio.h>
#include <string.h>

void func(char *str[]);

int main(void)
{
	int i;
	char s[6][1024], *pStr[6];

	printf("请输入6个字符串:\n");
	for (i=0; i<6; i++)
	{
		gets(s[i]);
		pStr[i] = s[i];
	}

	/*********Found************/
	func(pStr);
	printf("\n排序后:\n");
	for (i=0; i<6; i++)
	{
		/*********Found************/
		puts(pStr[i]);
	}
	
	return 0;
}

void func(char *str[])
{
	int i, j;
	char *temp;

	for (i=0; i<5; i++)
	{
		for (j=i+1; j<6; j++)
		{
			/*********Found************/
			if (strcmp(str[i],str[j]) > 0)
			{
				temp = str[j];
				str[j] = str[i];
				str[i] = temp;
			}
		}
	}
}

考查要点:

  1. 指针数组,对应入参传入指针数组名
  2. 一个指针刚好指向一个串,使用数组下标形式,输出串
  3. 选择排序算法实现时,交换的是指向,但判断的是串本身,串也刚好是用指针来指向的

D1049.c

原文件

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

struct STU
{
	char number[20];
	char name[20];
	int sex; //0表示女 1表示男
	int age;
	struct STU *next_stu;
};

struct STU *creat_stu();
struct STU *delete_stu(struct STU *head, struct STU *before, struct STU *temp);

int main(void)
{
	char str[20];
	struct STU *head, *temp, *before;

	head = creat_stu();
	printf("需要查找的学号为:");
	scanf("%s", str);
	before = temp = head;
	while (temp != NULL)
	{
		if (strcmp(temp->number, str) == 0)
		{
			printf("找到该学生,学号:%s、姓名:%s、性别:%s、年龄:%d\n",
								temp->number, temp->name, temp->sex==0?"女":"男", temp->age);
			head = delete_stu(head, before, temp);
			printf("删除该学生完成\n");
			break;
		}
		before = temp;
		/*********Found************/
		_____________________________;
	}

	if(NULL == temp)
	{
		printf("没有找到该学生\n");
	}

	return 0;
}

struct STU *creat_stu()
{
	struct STU *p, *before, *head;
	int temp;

	p = before = head = NULL;
	printf("请输入一个学生,以回车分开\n");
	while(1)
	{
		p = (struct STU *)malloc(sizeof(struct STU));
		printf("学号:");
		scanf("%s", p->number);
		printf("姓名:");
		scanf("%s", p->name);
		printf("性别(女填0,男填1):");
		scanf("%d", &p->sex);
		printf("年龄:");
		scanf("%d", &p->age);

		if (before != NULL)
		{
			before->next_stu = p;
		}
		else
		{
			head = p;
		}

		printf("是否继续输入?(是的话输入1,不是的话输入0):");
		scanf("%d", &temp);
		if (0 == temp)
		{
			/*********Found************/
			_____________________________;
			break;
		}
		before = p;
	}

	return head;
}

struct STU *delete_stu(struct STU *head, struct STU *before, struct STU *temp)
{
	if (temp == head)
	{
		/*********Found************/
		_____________________________;
	}
	else
	{
		/*********Found************/
		_____________________________;
	}

	return head;
}

改后文件

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

struct STU
{
	char number[20];
	char name[20];
	int sex; //0表示女 1表示男
	int age;
	struct STU *next_stu;
};

struct STU *creat_stu();
struct STU *delete_stu(struct STU *head, struct STU *before, struct STU *temp);

int main(void)
{
	char str[20];
	struct STU *head, *temp, *before;

	head = creat_stu();
	printf("需要查找的学号为:");
	scanf("%s", str);
	before = temp = head;
	while (temp != NULL)
	{
		if (strcmp(temp->number, str) == 0)
		{
			printf("找到该学生,学号:%s、姓名:%s、性别:%s、年龄:%d\n",
								temp->number, temp->name, temp->sex==0?"女":"男", temp->age);
			head = delete_stu(head, before, temp);
			printf("删除该学生完成\n");
			break;
		}
		before = temp;
		/*********Found************/
		temp = temp->next_stu;
	}

	if(NULL == temp)
	{
		printf("没有找到该学生\n");
	}

	return 0;
}

struct STU *creat_stu()
{
	struct STU *p, *before, *head;
	int temp;

	p = before = head = NULL;
	printf("请输入一个学生,以回车分开\n");
	while(1)
	{
		p = (struct STU *)malloc(sizeof(struct STU));
		printf("学号:");
		scanf("%s", p->number);
		printf("姓名:");
		scanf("%s", p->name);
		printf("性别(女填0,男填1):");
		scanf("%d", &p->sex);
		printf("年龄:");
		scanf("%d", &p->age);

		if (before != NULL)
		{
			before->next_stu = p;
		}
		else
		{
			head = p;
		}

		printf("是否继续输入?(是的话输入1,不是的话输入0):");
		scanf("%d", &temp);
		if (0 == temp)
		{
			/*********Found************/
			p->next_stu = NULL;
			break;
		}
		before = p;
	}

	return head;
}

struct STU *delete_stu(struct STU *head, struct STU *before, struct STU *temp)
{
	if (temp == head)
	{
		/*********Found************/
		head = temp->next_stu;
	}
	else
	{
		/*********Found************/
		before->next_stu = temp->next_stu;
	}

	return head;
}

考查要点:

  1. p用于开新节点,但判断它是最后一个节点,指针域要封闭
  2. 删除时,第一个节点,直接删除,头指针后移即可
  3. 非头节点,则before节点的指针域指向删除节点的后一个节点

D1050.c

原文件

#include <stdio.h>

int main(void)
{
	int JC[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
	int i, sum, x;

	for (i=100; i<1000; i++)
	{
		sum = 0;
		x = i;
		/*********Found************/
		while (______________)
		{
			sum += JC[x%10];
			x /= 10;
		}
		if (i == sum)
		{
			printf("%d\n",i);
		}
	}

	return 0;
}

改后文件

#include <stdio.h>

int main(void)
{
	int JC[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
	int i, sum, x;

	for (i=100; i<1000; i++)
	{
		sum = 0;
		x = i;
		/*********Found************/
		while (x)
		{
			sum += JC[x%10];
			x /= 10;
		}
		if (i == sum)
		{
			printf("%d\n",i);
		}
	}

	return 0;
}

考查要点:

  1. 分离位的算法实现,没有位了,就结束循环

D1051.c

原文件

#include <stdio.h>

void fun(double dTestNo, int iBase);

int main(void)
{	 
	double dTestNo = 0.795;
	int iBase;
	
	for (iBase=2; iBase<=9; iBase++)
	{
		fun(dTestNo, iBase);
	}
	printf("\n");
	
	return 0;
}

void fun(double dTestNo, int iBase)
{
	int iT[8], iNo;
	
	printf("十进制正小数 %f 转换成 %d 进制数为:", dTestNo, iBase);
	for (iNo=0; iNo<8; iNo++)
	{
		dTestNo *= iBase;
		/*********Found************/
		iT[iNo] = ______________;
		/*********Found************/
		dTestNo -= ______________;
	}
	
	printf("0.");
	for (iNo=0; iNo<8; iNo++)
	{
		printf("%d", iT[iNo]);
	}
	printf("\n");
}

改后文件

#include <stdio.h>

void fun(double dTestNo, int iBase);

int main(void)
{	 
	double dTestNo = 0.795;
	int iBase;
	
	for (iBase=2; iBase<=9; iBase++)
	{
		fun(dTestNo, iBase);
	}
	printf("\n");
	
	return 0;
}

void fun(double dTestNo, int iBase)
{
	int iT[8], iNo;
	
	printf("十进制正小数 %f 转换成 %d 进制数为:", dTestNo, iBase);
	for (iNo=0; iNo<8; iNo++)
	{
		dTestNo *= iBase;
		/*********Found************/
		iT[iNo] = (int)dTestNo;
		/*********Found************/
		dTestNo -= iT[iNo];
	}
	
	printf("0.");
	for (iNo=0; iNo<8; iNo++)
	{
		printf("%d", iT[iNo]);
	}
	printf("\n");
}

考查要点:

  1. 进制转换,小数部分:乘基取整

D1052.c

原文件

#include <stdio.h>

/*  m: 袋中红球的数目  n: 袋中白球的数目  x: 需要取出的数目  y: 红球至少出现的次数  */
double pro(int m, int n, int x, int y);

int main(void)
{
	int m, n, x;
	
	printf("请依次输入红球个数、白球个数及要取的球的个数:");
	scanf("%d%d%d", &m, &n, &x);
	printf("红球数目多于白球的概率 = %lf\n",  pro(m, n, x, x/2+1));
	
	return 0;
}

double pro(int m, int n, int x, int y)
{
	double p1, p2;

	if (y > x)
	{
		return 0;
	}
	if (0 == y)
	{
		return 1;
	}
	if (y > m)
	{
		return 0;
	}
	if (x-n > y)
	{
		return 1;
	}

	/*********Found************/
	p1 = ______________; //取红球
	/*********Found************/
	p2 = ______________; //取白球
	return (double)m/(m+n) * p1 + (double)n/(m+n) * p2;
}

改后文件

#include <stdio.h>

/*  m: 袋中红球的数目  n: 袋中白球的数目  x: 需要取出的数目  y: 红球至少出现的次数  */
double pro(int m, int n, int x, int y);

int main(void)
{
	int m, n, x;
	
	printf("请依次输入红球个数、白球个数及要取的球的个数:");
	scanf("%d%d%d", &m, &n, &x);
	printf("红球数目多于白球的概率 = %lf\n",  pro(m, n, x, x/2+1));
	
	return 0;
}

double pro(int m, int n, int x, int y)
{
	double p1, p2;

	if (y > x)
	{
		return 0;
	}
	if (0 == y)
	{
		return 1;
	}
	if (y > m)
	{
		return 0;
	}
	if (x-n > y)
	{
		return 1;
	}

	/*********Found************/
	p1=pro(m-1,n,x-1,y-1);
	/*********Found************/
	p2=pro(m,n-1,x-1,y);
	return (double)m/(m+n) * p1 + (double)n/(m+n) * p2;
}

考查要点:

# 没有死在编程上,但死在了概率论学科【数学基础差】上

# 求解利用了递归的思想
	假设第一次我摸到1个红球,那么以后我至少需要摸出x-1个球(红球至少出现y-1次),而白球依然不变是n个。
	但是如果我这次摸到的是白球,那么以后我至少还需摸出y个红球才能保证最终红球多于白球,其他同上。
	下面就是终止条件:
		if(y>x) return 0;应该没问题吧,如果要求红球比摸出的所有球都多,怎么可能!所以没有这种情况,返回0;
		if(y==0) return 1;判断还需摸出0个红球,满足条件,返回1;
		if(y>m) return 0;这也没问题!
		if(x-n>y) return 1;即使所有白球都被摸到,但还需摸出x-n个球(>y),只能全是红球。满足。
		最难懂的就是结尾:return (double)m/(m+n) * p1 + (double)n/(m+n) * p2;
# 这是函数返回给调用函数的值,利用了分步相乘的思想:
	左边表示此次抽到红球的概率*以后满足条件的概率;
	右边表示P(此次抽到白球)*P(以后满足条件);
# 这种递归直到算到最后直接用到判断条件为止。

D1053.c

原文件

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

int main(int argc, char *argv[])
{
	FILE *fp;
	char ch;

	/*********Found************/
	if (argc != ______________)
	{
		printf("the number of arguments not correct\n\n");
		printf("Usage: 可执行文件  filename\n");
		exit(1);
	}

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

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

	fclose(fp);
	return 0;
}

改后文件

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

int main(int argc, char *argv[])
{
	FILE *fp;
	char ch;

	/*********Found************/
	if (argc != 2)
	{
		printf("the number of arguments not correct\n\n");
		printf("Usage: 可执行文件  filename\n");
		exit(1);
	}

	/*********Found************/
	if ((fp = fopen(argv[1], "w")) == NULL)
	{
		printf("can not open this file\n");
		exit(2);
	}

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

	fclose(fp);
	return 0;
}

考查要点:

  1. main带参数时,参数是以串的形式传入的
  2. 串可以直接用作文件名
  3. 逐个字符的写文件
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值