编程题:
46.学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:按分数的高低排列学生的记录,高分在前。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <stdio.h>
#define N 16
typedef struct
{ char num[10];
int s;
} STREC;
int fun ( STREC a[ ] )
{
}
main ()
{ STREC s[N]= {{“GA005”,85}, {“GA003”,76}, {“GA002”,69}, {“GA004”,85},
{“GA001”,91}, {“GA007”,72}, {“GA008”,64}, {“GA006”, 87},
{“GA015”,85}, {“GA013”,91}, {“GA012”,64}, {“GA014”,91},
{“GA011”,66}, {“GA017”,64}, {“GA018”,64}, {“GA016”,72}};
int i; FILE *out;
fun ( s );
printf (“The data after sorted :n”);
for (i=0; i<N; i++)
{ if ( (i)%4==0 ) printf (“n”);
printf (“%s %4d “, s[i].num, s[i].s);
}
printf (“n”);
out=fopen (“out16.dat”, “w”);
for (i=0; i<N; i++)
{ if ( (i)%4==0 && i ) fprintf (out, “n” );
fprintf (out, “%4d”, s[i].s);
}
fprintf ( out, “n” );
fclose (out );
}
47.请编写一个函数void fun(char*ss),其功能是:将字符串ss中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。
例如,若输入abc4EFgh,则应输出aBc4EFgH。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <conio.h>
#include <stdio.h>
void fun(char *ss)
{
}
void main( void )
{
char tt[51];
clrscr();
printf(“nPlease enter an character string within 50 characters:n” );
gets( tt );
printf(“nnAfter changing, the stringn %s”, tt );
fun(tt) ;
printf( “nbecomesn %s”, tt );
}
改错题:
14.下列给定程序中,函数fun和funx的功能是:用二分法求方程2x3-4x2+3x-6=0的根,并要求绝对误差不超过0.001。例如,若给m输入-100,给n输入90,则函数求得的一个根值为2.000。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include “stdio.h”
#include “math.h”
double funx(double x)
{
return (2*x*x*x-4*x*x+3*x-6);
}
double fun(double m,double n)
{
int r;
r=(m+n)/2;
while(fabs(n-m)<0.001)
{
if(funx(r)*funx(n)<0)
m=r;
else
n=r;
r=(m+n)/2;
}
return r;
}
main()
{
double m,n,root;
printf(“Enter m n : n”);
scanf(“%lf%lf”,&m,&n);
root=fun(m,n);
printf(“root=%6.3fn”,root);
}
15.下列给定程序中,函数fun的功能是:判断字符ch是否与str所指串中的某个字
符相同;若相同,则什么也不做,若不同,则将其插在串的最后。
请改正程序中的错误,使它能进行正确的操作。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<conio.h>
#include<stdio.h>
#include<string.h>
void fun(char str,char ch)
{
while ( *str && *str !=ch )
str++;
if(*str==ch)
{
str[0]=ch;
str[1]=’0′;
}
}
main()
{
char s[81],c;
clrscr();
printf(“n Please enter a string:n”);
gets(s);
printf(“n Please enter the character to search:”);
c=getchar();
fun(s,c);
printf(“nThe result is %sn”,s);
}



