当前位置:首页 > 江苏省计算机等级考试(二级C语言)考点分析
{ int d;
struct node *next; }NODE;
NODE *insert(NODE *head, int x, int key) {NODE *s, *p, *q;
s=(NODE *)malloc(sizeof(NODE)); s->d=key; s->next=NULL;
if(head==NULL) {head=s; return head; }
if(head->d==x) {s->next=head; head=s; return head; } else
{ q=head; p=q->next;
while((p->d!=x)&&(p->next!=NULL)) {q=p; p=p->next; } if(p->d==x) {s->next=p; q->next=s; } else { s->next=NULL; p->next=s; } return head; } }
void print(NODE *head); { if(head==NULL) return; while(head->next!=NULL)
{ printf(“%d,”, head->d); head=head->next; } printf(“%d\\n”,head->d); } main()
{ NODE *head=NULL;
head=insert(head,0,3); print(head); head=insert(head,3,1); print(head); head=insert(head,4,5); print(head); }
答案:3 1,3 1,3,5
(4) 2005年春填空题第13题(P66-13)
以下程序运行时输出结果的第一行为________,第二行为_________,第三行为__________。 #include
struct node *next; }
struct node *create(void)
{ struct node *head=NULL,*p, *q=NULL; int i;
for(i=2; i<=9; i++)
{ p=(struct node *)malloc(sizeof(struct node)); p->d=i; p->next=NULL; if(head==NULL) head=p; else q->next=p; q=p; }
return head; }
void print(struct node *head); { if(head==NULL) return; while(head->next!=NULL)
{ printf(“%d,”, head->d); head=head->next; } printf(“%d\\n”,head->d); }
struct node *delst(struct node *head, int *n) { int count=0; struct node *p, *q, *r; p=r=head; while(p!=NULL)
{ q=p->next; while(q!=NULL)
{ if((q->d)%(p->d)==0)
{r->next=q->next; free(q); count++; q=r->next; } else {r=q; q=q->next; } }
p=p->next; }
*n=count; return head; }
void main() { int y;
struct node *head; head=creat(); print(head);
head=delst(head,&y); print(head); printf(“%d”,y); }
答案:2,3,4,5,6,7,8,9 2,3,5,7 4 五、字符串处理
(1) 2003年春填空第10题(P16-10)
以下程序运行时输出第一行是_______,第二行是________。 #include
int convert(char s1[],char s2[]) { int i=0,j,s;
char
tab[8][4]={“000”,”001”,”010”,”011”,”100”,”101”,”110”,”111”};
for(i=0,j=0; s1[i]!=’\\0’; i++,j=j+3) strcpy(&s2[j],tab[s1[i]-‘0’]);
for(i=0,s=0;i { char ss1[]=”15”,ss2[80]; int y; y=convert(ss1,ss2); printf(“%d\\n%s”,y,ss2); } 分析:本程序将八进制数字组成的字符串ss1转换成二进制字符串和十进制数。 答案:13 001101 (2) 2003年春填空第12题(P17-12) 函数loop(s,m,n,str)的功能是:对字符串str中,从下标为s的字符开始的所有间隔为m的字符进行循环左移,即:str[s]←s[s+m], str[s+m]←s[s+3m], str[s+2m]←s[s+3m],?, str[s+(k-1)m]←s[s+km], str[s+km]←s[s](k为整数,下标s+km不越界),共做n次。 例如,调用loop(1,2,1,str)前后str中数据的变化情况如下: str中初始数据:A B C D E F G H I J K 移位后str数据:A D C F E H G J I B K #include void loop(int s,int m,int n, char *str); main() {char buf[81]; strcpy(buf,”ABCDEFGHIJK”); puts(buf); loop(1,2,2,buf); puts(buf); } void loop(int s,int m,int n, char *str) {char c; int k,i,len; len=strlen(str); for(i=0; i
共分享92篇相关文档