2015年计算机二级C语言上机操作题及答案(89)
发布时间:2011/7/28 15:40:02 来源:城市学习网 编辑:ziteng
一、从键盘输入一组小写字母,保存在字符数组str中,请补充fun函数,该函数的功能是:把字符数组str中字符下标为奇数的小写字母转换成对应的大写字母,结果仍保存在原数组中。
例如,输入:“abcdefg”,输出:“aBcDeFg“。
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
#include
#define N 80
void fun(char s[])
{
int i = 0;
while (___1___)
{
if (i%2 != 0)
s[i] -= ___2___;
___3___;
}
}
main()
{
char str[N];
printf("\n Input a string: \n");
gets(str);
printf("\n******** original string ********\n");
puts(str);
fun(str);
printf("\n******** new string ********\n");
puts(str);
} [NextPage] 二、下列给定程序中,函数fun的功能是:求整数x的y次方的低3位值。例如,整数5的6次方为15625,此数的低3位值为625。
请改正函数中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
long fun(int x, int y, long *p)
{
int i;
long t = 1;
/********found********/
for (i=1; i t = t*x;
*p = t;
/********found********/
t = t/1000;
return t;
}
main()
{
long t, r;
int x, y;
printf("\nInput x and y: ");
scanf("%ld%ld", &x, &y);
t = fun(x, y, &r);
printf("\n\nx=%d, y=%d, r=%ld, last=%ld\n\n", x, y, r, t);
} [NextPage] 三、学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把高于等于平均分的学生数据放在b所指的数组总,高于等于平均分的学生人数通过形参n传回,平均分通过函数值返回。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#define N 12
typedef struct
{
char num [10];
double s;
}STREC;
double fun ( STREC *a, STREC *b, int *n )
{
}
main ()
{
STREC s[N]={{"GA05",85}, {"GA03",76}, {"GA02",69}, {"GA04",85},
{"GA01",91}, {"GA07",72}, {"GA08",64}, {"GA06",87},
{"GA09",60}, {"GA11",79}, {"GA12",73}, {"GA10",90}};
STREC h [N], t;
FILE *out ;
int i,j,n;
double ave;
ave=fun ( s, h, &n );
printf ( "The %d student data which is higher than %7.3f:\n", n, ave);
for ( i=0; i printf ("%s %4.1f\n", h[i]. num, h[i]. s);
printf ("\n");
out=fopen("out.dat","w");
fprintf(out,"%d\n%7.3f\n", n, ave);
for(i=0;i for(j=i+1;j if(h[i].s {
t=h[i] ;
h[i]=h[j];
h[j]=t;
}
for(i=0;i fprintf(out, "%4.1f\n",h[i].s);
fclose(out);
}
答案:
一:1、s[i]!=’\0’或s[i]!=0 2、32或’a’-‘A’ 3、i++或++i或i+=1或i=i+1
二:1、应改为for(i=1:i<=y;i++) 2、应改为:t=t000