文档详情

2011年9月份全国计算机等级考试二级C语言上机题库.doc

发布:2018-01-18约11.77万字共45页下载文档
文本预览下载声明
第1套 1. 程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2。 #include stdio.h #include stdlib.h #define N 5 typedef struct node { int data; struct node *next; } NODE; /**********found**********/ __1__ fun(NODE *h) { NODE *p, *q, *r; p = h; if (p == NULL) return NULL; q = p-next; p-next = NULL; /**********found**********/ while (__2__) { r = q-next; q-next = p; p = q; /**********found**********/ q = __3__ ; } return p; } NODE *creatlist(int a[]) { NODE *h,*p,*q; int i; h=NULL; for(i=0; iN; i++) { q=(NODE *)malloc(sizeof(NODE)); q-data=a[i]; q-next = NULL; if (h == NULL) h = p = q; else { p-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 *head; int a[N]={2,4,6,8,10}; head=creatlist(a); printf(\nThe original list:\n); outlist(head); head=fun(head); printf(\nThe list after inverting :\n); outlist(head); } 答案:(1)NODE* (2)q (3)r 2..给定程序MODI1.C中函数fun的功能是:将s所指字符串中位于奇数位置的字符或ASCII码为偶数的字符放入t所指数组中(规定第一个字符放在第0位中)。 #include stdio.h #include string.h #define N 80 void fun(char *s, char t[]) { int i, j=0; for(i=0; istrlen(s); i++) /***********found**********/ if(i%2 s[i]%2==0) t[j++]=s[i]; /***********found**********/ t[i]=\0; } main() { char s[N], t[N]; printf(\nPlease enter string s : ); gets(s); fun(s, t); printf(\nThe result is : %s\n,t); } 答案:(1) if(i%2||s[i]%2= = 0) (2) t[j]=’\0’; 3. 请编写函数fun,函数的功能是:将M行N列的二维数组中的数据,按列的顺序依次放到一维数组中。 #include stdio.h void fun(int (*s)[10], int *b, int *n, int mm, int nn) { } main() { int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}},i,j; int a[100]={0}, n=0; printf(The matrix:\n); for(i=0;
显示全部
相似文档