100套C语言改错题答案
- 格式:doc
- 大小:1.30 MB
- 文档页数:7
改错题(一)1.Cmody.011.C,功能是:从字符串数组str1中取出ASCII码值为偶数且下标为偶数的字符依次存放到字符串t中。
如:若str1所指的字符串为:4Az18c?Ge9a0z! 则t所指的字符串为:4z8z注意:数组下标从0开始#include <math.h>#include <stdio.h>#include <string.h>#include <conio.h>void main(){char str1[100],t[200];int i,j;/**/ i=0;j=o;/**/strcpy(str1,"4Az18c?Ge9a0z!");for(i=0;i<strlen(str1);i++){/**/ if((str1[i]%2==0)&&(i%2!==0)) /**/{t[j]=str1[i];j++;}}t[j]='\0';printf("\nOriginal string :%s\n",str1);printf("\nResult string :%s\n",t);}2.Cmody012.C,fun()功能:根据n,计算大于10的最小n个能被3整除的正整数的倒数之和。
如:fun(8)=1/12+1/15+1/18+1/21+1/24+1/27+1/30+1/33=0.396#include <math.h>#include <stdio.h>#include <string.h>#include <conio.h>#define M 50double fun(int n){double y=0.0;int i,j;j=0;for(i=1;;i++){/**/ if(i<>10)&&(i%3==0)) /**/{ /**/ y+=1 1.0/i; /**/j++;}if(j==n) break;}return y;}void main(){printf("fun(8)=%8.3lf\n",fun(8));}(二)1.Cmody021.C,输出:*************************#include <stdio.h>void main(){/**/int i;,j; /**/for(i=1;i<=5;i++){for(j=1;j<=10-2*i;j++) printf(" ");/**/ for(j=1;j<=52*i-1;j++) /**/printf("*");printf("\n");}}2.Cmody022.C的功能是求解百元买百鸡问题:设一只公鸡2元,一只母鸡1元,一只小鸡0.5元。
c语言改错题及答案【篇一: c 语言二级上机(t 填空 ,改错 ,编程 )题库及答案】100套题目(每套题目包含 3 道题:一道程序填空题、一道程序修改题、一道程序设计题),真实考试的时候,考生输入准考证后计算机随机为你抽取一套考试,每个考生考试时只需考一套题目(包含三道题)二级 c 语言上机试题汇编※※※※※※※※※※※※※※※※※※※※※※※第01套:给定程序中,函数 fun 的功能是:将形参 n 所指变量中,各位上为偶数的数去除,剩余的数按原来从高位到低位的顺序组成一个新的数,并通过形参指针 n 传回所指变量。
例如,输入一个数:27638496 ,新的数:为739 。
请在程序的下划线处填入正确的内容并把下划线删除 , 使程序得出正确的结果。
注意:源程序存放在考生文件夹下的blank1.c中。
不得增行或删行,也不得更改程序的结构!给定源程序:#include stdio.hvoid fun(unsigned long *n){ unsigned long x=0, i; int t;i=1;while(*n)/**********found**********/{ t=*n % __1__;/**********found**********/if(t%2!= __2__){ x=x+t*i; i=i*10; }*n =*n /10;}/**********found**********/*n=__3__;}main(){ unsigned long n=-1;while(n99999999||n0){ printf(please input(0n100000000): ); scanf(%ld,n); }fun(n);printf(\nthe result is: %ld\n,n);}【参考答案】(1)10 (2) 0 (3) x解题思路:第一处: t 是通过取模的方式来得到*n 的个位数字,所以应填:10 。
下列给定程序中函数fun()的功能是计算1/n!的值。
例如:给n输入5,则输出0.008333。
请改正程序中的错误,使它能得到正确结果。
(1)错误:int fun(int n)正确:double fun(int n)(2)错误:result *=n++ ;正确:result *=n-- ; 2. 改错题下列给定程序中函数fun()的功能是计算正整数num的各位上的数字之平方和。
(1)错误:long k=1;正确:long k=0;(2)错误:while(num)正确:while(num);下列给定程序中,函数fun()的功能是将字符串s中位于偶数位置的字符或ASCII码为奇数的字符放入字符串t中(规定第一个字符放在第0位中)。
(1)错误: void fun(char s, char t[ ])正确:void fun(char *s, char t[ ])(2)错误: if(i%2=0||s[i]%2!=0)正确:if(i%2==0||s[i]%2!=0)下列给定程序中,函数fun()的功能是找出100~n(不大于1000)之间百位数字加十位数字等于个位数字的所有整数,把这些整数放在s所指的数组中,个数作为函数值返回。
(1)错误:k=n; 正确:k=i;(2)错误:s[j]=i;正确:s[j++]=i;下列给定程序中,函数fun()的功能是求出数组中最小数和次最小数,并把最小数和a[0]中的数对调,次最小数和a[1]中的数对调.1)错误: for(i=0; i<n; i++)正确:for(i=0; i<2; i++)(2)错误:k=m;正确:m=k;下列给定程序中,函数fun()的功能是计算并输出high以内的素数之和。
high由主函数传给fun()函数。
若high的值为100,则函数的值为1060。
(1)错误:ifhigh%j==0正确:if(high%j==0)(2)错误:if(yes==0)正确:if(yes)下列给定程序中,函数fun()的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码降序排序后输出。
改错参考答案:(3)将字符串yy在屏幕上输出#include<stdio.h>void main(){/*********Found************/ char yy[100] = "ok??\n";/*********Found************/f0r (; *yy; yy++){putchar(*yy);}}#include<stdio.h>void main(){/*********Found************/ char*yy="ok??\n";/*********Found************/for (; *yy; yy++){putchar(*yy);}}(4)计算半径为2+3的圆的面积#include<stdio.h>#define PI3."14/*********Found************/#define S(r) PI*r*rvoid main(){/*********Found************/ int mianJi;mianJi = S(2+3);printf("mian ji=%5."2f\n", mianJi);}#include<stdio.h>#define PI3."14/*********Found************/#define S(r) PI*(r)*(r)void main(){/*********Found************/1floatmianJi; mianJi = S(2+3);printf("mian ji=%5."2f\n", mianJi);}(8)打开文件d:\te.c用于读并判断打开是否成功#include<stdio.h>void main(){FILE *fp;/*********Found************/char fileName[] = "d:\te.c";/*********Found************/fp = fopen(fileName, "w");/*********Found************/if (fp == EOF){puts("File Open Error!");exit(1);}putchar(fgetc(fp));fclose(fp);}#include<stdio.h>void main(){FILE *fp;/*********Found************/char fileName[] = "d:\\te.c";/*********Found************/fp = fopen(fileName, "r");/*********Found************/if (fp ==NULL){puts("File Open Error!");exit(1);}putchar(fgetc(fp));fclose(fp);}(9)申请100个字节的内存空间,显示其首地址,然后释放申请到的内存空间#include<stdio.h>#include<alloc.h>void main(){/*********Found************/char p[100];2/*********Found************/if (p = (char *)malloc(100) == NULL){printf("malloc memory fail!\n");return ;}printf("%p\n", p);/*********Found************/fclose(p);}#include<stdio.h>#include<alloc.h>void main(){/*********Found************/ char*p;/*********Found************/if ((p = (char *)malloc(100))== NULL){printf("malloc memory fail!\n"); exit(1);}printf("%p\n", p);/*********Found************/free(p);}(10)将字符串p显示在屏幕上#include<stdio.h>/*********Found************/#define BEGIN/*********Found************/#define ENDvoid main(){char *p = "";const int i = 0;for (printf("\n"); p[i]; )BEGINputchar(p[i]);i++;END}#include<stdio.h>/*********Found************/#define BEGIN{/*********Found************/#define END}3void main(){char *p = "";const int i = 0;for (printf("\n"); p[i]; )BEGINputchar(p[i]);/*********Found************/p++;END}(14)调用函数swap,将a和b的值交换,最后在屏幕上显示交换后的a,b之值#include<stdio.h>/*********Found************/void swap(int x, int y){int tmp;/*********Found************/______*x = *y;/*********Found************/y = x;}void main(){int a = 3, b = 4;swap(a, b);printf("a=%d b=%d\n", a, b);}#include<stdio.h>/*********Found************/void swap(int*x, int*y){int tmp;/*********Found************/tmp = *x;*x = *y;/*********Found************/*y = tmp;}void main(){int a = 3, b = 4;/*********Found************/swap(&a,&b);printf("a=%d b=%d\n", a, b);}4(15)调用函数swap,将a和b的值交换,最后在屏幕上显示交换后的a,b之值#include<stdio.h>void swap(int *x, int *y){int *tmp, xy;/*********Found************/*tmp = x;*x = *y;/*********Found************/y = *tmp;}void main(){int a = 3, b = 4;/*********Found************/swap(*a, *b);/*********Found************/printf("a=%d b=%d\n", &a, &b);}#include<stdio.h>void swap(int *x, int *y){int *tmp, xy;/*********Found************/xy=*x;*x = *y;/*********Found************/*y =xy;}void main(){int a = 3, b = 4;/*********Found************/swap(&a,&b);/*********Found************/printf("a=%d b=%d\n",a,b);}(16)worker的信息使用结构体存储,从键盘读入其各项信息并显示#include<stdio.h>void main(){struct WKER{long ID;long int num;char name[20];5char sex;/*********Found************/} ;worker.ID = 1L;/*********Found************/scanf("%d %s %s", &worker.num, , &worker.sex);/*********Found************/printf("worker's info:num=%d name=%s sex=%s\n",worker.num, , worker.sex);}#include<stdio.h>void main(){struct WKER{long ID;long int num;char name[20];char sex;/*********Found************/}worker;worker.ID = 1L;/*********Found************/scanf("%ld %s %c", &worker.num, ,&worker.sex);/*********Found************/printf("worker's info:num=%ld name=%s sex=%c\n",worker.num, , worker.sex);}(17)函数userLogin的功能主要是统计并返回登录的用户数,用户名最长30字节#include <stdio.h>long userLogin(char *userName){/*********Found************/long userCount = 0;userCount++;/*********Found************/return ;}void main( ){/*********Found************/char *userName;int i;while(1){printf("userName:");scanf("%s", userName);i = userLogin(userName);6}}#include <stdio.h>long userLogin(char *userName){/*********Found************/ staticlong userCount = 0;userCount++;/*********Found************/returnuserCount;}void main( ){/*********Found************/ charuserName[31];int i;while(1){printf("userName:");scanf("%s", userName);i = userLogin(userName);}}(19)将inBuf中字符串拆分成一个个的单词/*单词之间的分隔符由串divChar,程序中定义为“;?!,.>/\”。
第1题【程序改错】功能:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。
例如:当s中的字符串为:“ABCDE”时,则t中的字符串应为:“EDCBAABCDE”。
------------------------------------------------------*/#include <conio.h>#include <stdio.h>#include <string.h>void fun (char *s, char *t){/**********FOUND**********/int i;sl = strlen(s);for (i=0; i<sl; i++)/**********FOUND**********/t[i] = s[sl-i];for (i=0; i<sl; i++)t[sl+i] = s[i];/**********FOUND**********/t[2*sl] = "0";}main(){char s[100], t[100];printf("\nPlease enter string s:"); scanf("%s", s);fun(s, t);printf("The result is: %s\n", t);}答案:1). int i,sl;2). t[i] = s[sl-i-1];3). t[2*sl] = '\0'; 或 t[2*sl] = 0;第2题【程序改错】功能:求出以下分数序列的前n项之和。
和值通过函数值返回main 函数。
2/1+3/2+5/3+8/5+13/8+21/13 ……例如:若n = 5,则应输出:8.391667。
------------------------------------------------------*/#include <conio.h>#include <stdio.h>/**********FOUND**********/fun ( int n ){int a, b, c, k; double s;s = 0.0; a = 2; b = 1;for ( k = 1; k <= n; k++ ){/**********FOUND**********/s = (double)a / b;c = a;a = a + b;b = c;}/**********FOUND**********/return c;}main( ){int n = 5;printf( "\nThe value of function is: %lf\n", fun ( n ) );}答案:1). double fun(int n)2). s = s + (double)a / b; 或 s += (double)a / b; 或 s += a /(double)b; 或s=s+a/(double)b;3). return s;第3题【程序改错】功能:读入一个整数m( 5≤m≤20 ),函数getarr调用函数rnd获得m个随机整数,函数sortpb将这m个随机整数从小到大排序。