请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。
注意:源程序存放在考生文件夹下BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a)
{struct student b; int i;
/**********found**********/
b = __1__;
b.sno = 10002;
/**********found**********/
strcpy(__2__, "LiSi");
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",b.sno, b.name);
/**********found**********/
for (i=0; i<3; i++) printf("%6.2f ", b.__3__);
printf("\n");
}
main()
{struct student s={10001,"ZhangSan", 95, 80, 88};
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
fun(s);
}
解题答案:
/**********第一空**********/
b = a;
/**********第二空**********/
strcpy(b.name, "LiSi");
/**********第三空**********/
for (i=0; i<3; i++) printf("%6.2f ",
****************************************** [NextPage] 二、改错题:给定程序MODI1.C中函数fun的功能是:从s所指字符串中删除所有小写字母c。
请改正程序中的错误,使它能计算出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
void fun(char *s)
{int i,j;
for(i=j=0; s[i]!='\0'; i++)
if(s[i]!='c')
/************found************/
s[j]=s[i];
/************found************/
s[i]='\0';
}
main()
{char s[80];
printf("Enter a string: "); gets(s);
printf("The original string: "); puts(s);
fun(s);
printf("The string after deleted : "); puts(s); printf("\n\n");
}
解题答案:
/************found************/
s[j++]=s[i];
/************found************/
s[j]='\0';
[NextPage] 三、程序题:假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:将字符串中的前导*号全部移到字符串的尾部。函数fun中给出的语句仅供参考。
例如,字符串中的内容为:*******A*BC*DEF*G****,移动后,字符串中的内容应当是:A*BC*DEF*G***********。在编写函数时, 不得使用C语言提供的字符串函数。
注意: 部分源程序在文件PROG1.C文件中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include
void fun(char *a)
{
}
main()
{char s[81]; int n=0; void NONO ();
printf("Enter a string:\n");gets(s);
fun(s);
printf("The string after moveing:\n");puts(s);
NONO();
}
void NONO ()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *in, *out ;
int i ; char s[81] ;
in = fopen("in.dat","r");
out = fopen("out.dat","w");
for(i = 0 ; i < 10 ; i++) {
fscanf(in, "%s", s);
fun(s);
fprintf(out, "%s\n", s);
}
fclose(in);
fclose(out);
}
参考答案:
void fun( char *a )
{
/* 以下代码仅供参考 */
char *p,*q;
int n=0;
p=a;
while(*p=='*') /* 统计串头'*'个数n */
{n++; p++;}
q=a;
/* 向前复制字符串,请填写相应的语句完成其功能 */
while(*p) {
*q=*p;
p++;q++;
}
for(;n>0;n--) /* 在串尾补n个'*' */
*q++='*';
*q='\0';
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|