请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。
注意:源程序存放在考生文件夹下BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
#define N 5
#define M 10
/**********found**********/
void fun(char (*ss) __1__, int k)
{int i=0 ;
/**********found**********/
while(i< __2__) {
/**********found**********/
ss[i][k]=__3__; i++;}
}
main()
{char x[N][M]={"Create","Modify","Sort","skip", "Delete"};
int i;
printf("\nThe original string\n\n");
for(i=0;i fun(x,4); printf("\nThe string after deleted :\n\n"); for(i=0; i } 解题答案: 本题是根据给定的字符串数组中删除串长大于某个值的右边字符串。 /**********第一空**********/ void fun(char (*ss) [M], int k) /**********第二空**********/ while(i< N) { /**********第三空**********/ ss[i][k]=0; i++;} ****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是:根据以下公式求π值,并作为函数值返回。 例如,给指定精度的变量eps输入0.0005时,应当输出Pi=3.140578。 π 1 1 2 1 2 3 1 2 3 4 ─=1+ ─ + ─×─ + ─×─×─ + ─×─×─×─+…… 2 3 3 5 3 5 7 3 5 7 9 请改正程序中的错误,使它能得出正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 给定源程序: #include #include double fun(double eps) {double s,t; int n=1; s=0.0; /************found************/ t=0; while(t>eps) {s+=t; t=t * n/(2*n+1); n++; } /************found************/ return(s); } main() {double x; printf("\nPlease enter a precision: "); scanf("%lf",&x); printf("\neps=%lf, Pi=%lf\n\n",x,fun(x)); } 解题答案: /************found************/ t=1; /************found************/ return(2*s); ****************************************** [NextPage] 三、程序题:假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:使字符串的前导*号不得多于n个;若多于n个,则删除多余的*号; 若少于或等于n个, 则什么也不做,字符串中间和尾部的*号不删除。函数fun中给出的语句仅供参考。 例如,字符串中的内容为:*******A*BC*DEF*G****,若n的值为4,删除后, 字符串中的内容应当是:****A*BC*DEF*G****;若n的值为8,则字符串中的内容仍为:*******A*BC*DEF*G****。n的值在主函数中输入。 在编写函数时,不得使用C语言提供的字符串函数。 注意: 部分源程序在文件PROG1.C文件中。 请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 给定源程序: #include void fun(char *a, int n) { } main() {char s[81]; int n;void NONO (); printf("Enter a string:\n");gets(s); printf("Enter n : ");scanf("%d",&n); fun(s,n); printf("The string after deleted:\n");puts(s); NONO(); } void NONO () {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *in, *out ; int i, n ; char s[81] ; in = fopen("in.dat","r"); out = fopen("out.dat","w"); for(i = 0 ; i < 10 ; i++) { fscanf(in, "%s", s); fscanf(in, "%d", &n); fun(s,n); fprintf(out, "%s\n", s); } fclose(in); fclose(out); } 参考答案: void fun( char *a, int n ) { /* 以下代码仅供参考 */ int i=0,j,k=0; while(a[k]=='*') k++; /* k为统计*字符个数 */ if(k>n) { i=n;j=k; /* 以下完成将下标为k至串尾的字符前移k-n个位置 */ for(; a[j] !=0 ; j++) a[i++]=a[j]; a[i] = 0; } }
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|