1、国家二级 C语言机试(操作题)模拟试卷 450及答案解析(总分:6.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:2.00)1.给定程序中,函数 fun的功能是:将带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数据域从头至尾的数据为 10、4、2、8、6,排序后链表结点数据域从头至尾的数据为2、4、6、8、10。 请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的结果。 注意:部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #include stdioh #include stdlibh #define N 6 type
2、def struct node ; int data; struct node*next; NODE; void fun(NODE,*h) NODE*p,*q;int t; p=h; while(p) /*found*/ q= 【1】 ; /*found*/ while( 【2】 ) if(p 一 data q 一 data) t=p 一 data; p 一 data=q 一 data; q 一 data=t;) q=q 一 next; /*found*/ p= 【3】 ; NODE*creatlist(int a) NODE*h,*p,*q;int i; h=NULL; for(i=0;i
3、N;i+) q=(NODE*)malloc(sizeof (NODE); q 一 data=ai, q 一 next=NULL; if(h=NULL)h=p=q; elsep 一 next=q;p=q; return h; void outlist(NODE*h) NODE*p; p=h; if(p=NULL) printf(“The list is NULL!/n“); else printf(“/nHead“); do printf(“一 d“,p 一 data); P=p 一 next;) while(p!=NULL); printf(“一 End/n“); main() NODE*he
4、ad; int aN=0,10,4,2,8,6; head=creatlist(a); printf(“/nThe original list:/n“); outlist(head); fun(head); printf f“/nThe list after inverting:/n“); outlist(head); (分数:2.00)_二、程序修改题(总题数:1,分数:2.00)2.下列给定程序中,函数 fun的功能是:将 s所指字符串中的字母转换为按字母序列的后续字母(如“Z”转化为“A”,“z”转化为“a”),其他字符不变。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动
5、main函数,不得增行或删行,也不得更改程序的结构! 试题程序:include stcllibh #include stctioh #include ctypeh #include conioh void fun(char*s)/*found*/ while(=*s!=) if(*s =A&*s =Z|*s =a&*s =z) if(*s=z)*s=A; else if(*s=z)*s=a; else *s+=1; /*found*/ (*s)+; void main() char s80; system(“CLS“); printf(“/n Enter a string withlength
6、 80:/n/n“); gets(s); printf(“/n The string:/n/n“); putS(s);fun(s); printf(“/n/n The Cords:/n/n“); puts(s);(分数:2.00)_三、程序设计题(总题数:1,分数:2.00)3.请编写函数 fun,其功能是:移动一维数组中的内容,若数组中有 n个整数,要求把下标从 0p(含p,p 小于等于 n一 1)的数组元素平移到数组的最后。例如,一维数组中的原始内容为:1、2、3、4、5、6、7、8、9、10;p 的值为 3。移动后,一维数组中的内容应为:5、6、7、8、9、10、1、2、3、4。注意:部
7、分源程序给出如下。请勿改动主函数 maln和其他函数中的内容,仅在函数 fun的花括号中填入你编写的若干语句。试题程序:#include stdioh #define N 80void fun (int *w, int p int n)main ()8 , 9,10 ,11,12 ,13 ,14 ,15; int i,p,n =15; printf ( “ The original da一 ta:/n“); for(i=0;i n;i+) printf (“3 d“,ai); printf(“/n/nEnter p:“); scanf(“d“,&p); fun(a,p,n); printf(“
8、/nThe data after moving:/n“); for(i=0;i n;i+) printf(“3 d“,ai); printf(“/n/n“);(分数:2.00)_国家二级 C语言机试(操作题)模拟试卷 450答案解析(总分:6.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:2.00)1.给定程序中,函数 fun的功能是:将带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数据域从头至尾的数据为 10、4、2、8、6,排序后链表结点数据域从头至尾的数据为2、4、6、8、10。 请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的结果。 注
9、意:部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #include stdioh #include stdlibh #define N 6 typedef struct node ; int data; struct node*next; NODE; void fun(NODE,*h) NODE*p,*q;int t; p=h; while(p) /*found*/ q= 【1】 ; /*found*/ while( 【2】 ) if(p 一 data q 一 data) t=p 一 data; p 一 data=q 一 data; q 一 data=t;) q=q
10、 一 next; /*found*/ p= 【3】 ; NODE*creatlist(int a) NODE*h,*p,*q;int i; h=NULL; for(i=0;i N;i+) q=(NODE*)malloc(sizeof (NODE); q 一 data=ai, q 一 next=NULL; if(h=NULL)h=p=q; elsep 一 next=q;p=q; return h; void outlist(NODE*h) NODE*p; p=h; if(p=NULL) printf(“The list is NULL!/n“); else printf(“/nHead“); d
11、o printf(“一 d“,p 一 data); P=p 一 next;) while(p!=NULL); printf(“一 End/n“); main() NODE*head; int aN=0,10,4,2,8,6; head=creatlist(a); printf(“/nThe original list:/n“); outlist(head); fun(head); printf f“/nThe list after inverting:/n“); outlist(head); (分数:2.00)_正确答案:(正确答案:(1)p 一next (2)q (3)p 一next)解析:
12、解析:填空 1:从第 2个 while循环可知,q 的初值应该为 p的 next,故此空应该填写 p一next。 填空 2:第 2个 while循环表示的是每次从链表剩下的树中找出最小的数,因此此空应该以 q是否为空来判断循环是否结束,所以此空应该填写 q。 填空 3:当找到一个最小的数时 p应该向后移,因此此空应该填写 p一next。二、程序修改题(总题数:1,分数:2.00)2.下列给定程序中,函数 fun的功能是:将 s所指字符串中的字母转换为按字母序列的后续字母(如“Z”转化为“A”,“z”转化为“a”),其他字符不变。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 ma
13、in函数,不得增行或删行,也不得更改程序的结构! 试题程序:include stcllibh #include stctioh #include ctypeh #include conioh void fun(char*s)/*found*/ while(=*s!=) if(*s =A&*s =Z|*s =a&*s =z) if(*s=z)*s=A; else if(*s=z)*s=a; else *s+=1; /*found*/ (*s)+; void main() char s80; system(“CLS“); printf(“/n Enter a string withlength 8
14、0:/n/n“); gets(s); printf(“/n The string:/n/n“); putS(s);fun(s); printf(“/n/n The Cords:/n/n“); puts(s);(分数:2.00)_正确答案:(正确答案:(1) while(*s)或 while(*s!=/0) (2)s+;)解析:解析:(1)通过 while语句可对字符串所有字符进行遍历,循环条件是对当前字符进行判断,若当前字符不是字符串结尾,则对其进行其他操作。 (2)因为该循环通过指针 s的移动遍历字符串,所以每循环一次要使指针向后移动一个位置,而不是将指针所指的元素加 1。三、程序设计题(总
15、题数:1,分数:2.00)3.请编写函数 fun,其功能是:移动一维数组中的内容,若数组中有 n个整数,要求把下标从 0p(含p,p 小于等于 n一 1)的数组元素平移到数组的最后。例如,一维数组中的原始内容为:1、2、3、4、5、6、7、8、9、10;p 的值为 3。移动后,一维数组中的内容应为:5、6、7、8、9、10、1、2、3、4。注意:部分源程序给出如下。请勿改动主函数 maln和其他函数中的内容,仅在函数 fun的花括号中填入你编写的若干语句。试题程序:#include stdioh #define N 80void fun (int *w, int p int n)main ()
16、8 , 9,10 ,11,12 ,13 ,14 ,15; int i,p,n =15; printf ( “ The original da一 ta:/n“); for(i=0;i n;i+) printf (“3 d“,ai); printf(“/n/nEnter p:“); scanf(“d“,&p); fun(a,p,n); printf(“/nThe data after moving:/n“); for(i=0;i n;i+) printf(“3 d“,ai); printf(“/n/n“);(分数:2.00)_正确答案:(正确答案:void fun (int *w, int p,
17、int n) int x,j,ch; for(x=0;x =p;x+) ch=w0; for(j=1;j n;j+)/*通过 for循环语句,将 p+1到 n一 1(含 n一 1)之间的数组元素依次向前移动 p+1个存储单元*/ wj 一 1=wj; wn 一 1=ch;/*将 0到 p个数组元素逐一赋给数组 wn1*/ )解析:解析:本题要求把下标从 0一 p(含 p,p 小于等于 n一 1)的数组元素平移到数组的最后,可以根据输入的 p值,通过 for循环语句,将 p+1n一 1(含 n一 1)之间的数组元素依次向前移动 p+1个存储单元,即 w j一 1 =wj;,同时将 0一 p个数组元素逐一赋给数组 wn 一 1也就是通过语句 wn 一1=ch;来实现此操作的。