计算机二级C语言上机操作题及答案(41_50)
计算机二级C语言上机操作题及答案(41-50)
第41套
第一题:填空题
请补充main函数,该函数的功能是:从键盘输入一个长整数,如果这个数是负数,则取它的绝对值,并显示出来。
例如:输入:结果为仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
# include
# include
main()
{
long int n;
printf("Enter the data:\n");
scanf(___1___);
printf("****** the origial data ********\n");
if (n < 0)
___2___
printf("\n\n");
printf(___3___);
}
第二题:改错题
下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun的功能是将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并且作为函数值返回。
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
#include
typedef struct aa
{
int data;
struct aa *next;
} NODE;
int fun(NODE *h)
{
int sum = 0;
NODE *p;
p = h->next;
/********found********/
while (p->next)
{
if (p->data%2 == 0)
sum += p->data;
/********found********/
p = h->next;
}
return sum;
}
NODE *creatlink(int n)
{
NODE *h, *p, *s;
int i;
h = p = (NODE*)malloc(sizeof(NODE));
for (i=1; i
{
s = (NODE*)malloc(sizeof(NODE));
s->data = rand()%16;
s->next = p->next;
p->next = s;
p = p->next;
}
p->next = NULL;
return h;
}
outlink(NODE *h)
{
NODE *p;
p = h->next;
printf("\n\nTHE LIST :\n\n HEAD");
while (p)
{
printf("->%d ", p->data);
p = p->next;
}
printf("\n");
}
main()
{
NODE *head;
int sum;
head = creatlink(10);
outlink(head);
sum = fun(head);
printf("\nSUM=%d", sum);
}
第三题:编程题
请编写函数fun,该函数的功能是;将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。
例如,若二维数组中的数据为:
W W W W
S S S S
H H H H
则字符串中的内容应是WSHWSHWSH。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#define M 3
#define N 4
void fun(char (*s)[N], char *b)
{
}
main()
{
char a[100],w[M][N]={{'w','w', 'w','w'},{'S','S','S','S'},{'H','H','H','H'}};
int i,j;
FILE *out;
printf("The matrix:\n");
for(i=0; i
{
for(j=0;j
printf("%3c",w[i][j]);
printf("\n");
}
fun(w,a);
printf("The A string:\n");
puts(a);
printf("\n\n");
out=fopen ("out.dat", "w");
fprintf(out, "%s