2013计算机等级考试二级C语言试题及答案13
第一题、请补充main函数,该函数的功能是求方程ax2+bx+c=0的两个实数根。方程的系数a、b、c从键盘输入,如果判别式(disc=b*b-4*a*c)小于0,则要求从新输入a、b、c的值。一无二次方程的求根公式为:
例如,当a=1,b=2,c=1时,方程的两个根分别是:x1=-1.00,x2=-1.00.
注意:部分源程序给出如下。
仅在横线上填入所编写的若干表达式式语句,勿改动函数中的其他任何内容。
#include
#include
main()
{
double a, b, c, disc, x1, x2;
do
{
printf("Input a, b, c: ");
scanf("%lf,%lf,%lf", &a, &b, &c);
disc = b*b - 4*a*c;
if (disc < 0)
printf("disc=%lf \n Input again!\n", disc);
} while (__1___);
printf("*******the result*******\n");
x1 = (-b+___2 __(disc))/(2*a);
x2 = (-b-__3__(disc))/(2*a);
printf("\nx1=%6.2lf\nx2=%6.2lf\n", x1, x2);
}
第二题、下列给定程序中,函数fun的功能是:对N名学生的学习成绩,按从高到低顺序找出前m(m<=10)名觉得来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增得或删行,也不得更改程序的结构!
#include
#include
#include
#define N 10
typedef struct ss
{
char num[10];
int s;
} STU;
STU *fun(STU a[], int m)
{
STU b[N], *t;
int i, j, k;
/********found********/
*t = malloc(sizeof(STU));
for (i=0; i
for (k=0; k
for (i=j=0; i
j = i;
/********found********/
t[k].num = b[j].num;
t[k].s = b[j].s;
b[j].s = 0;
}
return t;
}
outresult(STU a[], FILE *pf)
{
int i;
for (i=0; i
fprintf(pf, "\n\n");
}
main()
{
STU a[N] =
{
{"A01", 81}, {"A02", 89}, {"A03", 66}, {"A04", 87}, {"A05", 77},
{"A06", 90}, {"A07", 79}, {"A08", 61}, {"A09", 80}, {"A10", 71}
};
STU *pOrder;
int i, m;
printf("***** The Original data *****\n");
outresult(a, stdout);
printf("\nGive the number of the students who have better score: ");
scanf("%d", &m);
while (m > 10)
{
printf("\nGive the number of the students who have better score: ");
scanf("%d", &m);
}
pOrder = fun(a, m);
printf("***** THE RESULT *****\n");
printf("The top :\n");
for (i=0; i
free(pOrder);
}
第三题、请编写函数fun,其功能是:次s所指字符串下标为偶数同时ASCII值为奇数的字符删除,s中剩余的字符形成的新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFG12345,其中字符C的ACSII码值为奇数,在数组的下标为偶数,因些必须删;而字符1的ASCII码值为奇数,在数组中的下标也为奇数,因此不应当删除,其他依此类推。最后t所指的数组中的内容就是BDF12345。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#include
void fun(char *s, char t[])
{
}
main()
{
char s[100], t[100];
FILE *out;
printf("\nPlease enter string S:");
scanf("%s", s);
fun(s, t);
printf("\nThe result is : %s\n", t);
out=fopen ("out.dat", "w");
strcpy(s, "Please enter string S:");
fun(s, t);
fprintf(out, "%s", t);
fclose (out );
}