现在的位置: 首页 > 14 二级C语言 > 正文

2010春 C语言 完善程序(12分)

2011年05月30日 14 二级C语言 ⁄ 共 555字 ⁄ 字号 暂无评论

二级C的30分填空题中,第3部分(最后)是完善程序题,共4小题,12分

14. 以下程序的功能是:统计一个字符串中数字字符"0"到"9"各自出现的次数,统计结果保存在数组count中。例如,如果字符串为"lenterschar4543123564879ffgh",则统计结果为:1:2 2:1 3:2 4:3 5:2 6:1 7:1 8:1 9:1。试完善程序以达到要求的功能。

1
2
3
4
(19) *p (20) *p-48  
(21) n/2  (22) min=max=i 
(23) k++ (24) strcpy(&s1[i],s3) (25) strcat(s1,temp)  (26) k  
(27) h2==NULL  (28) s->next (29) h2 (30) return h1

15.下列程序的功能是对a数组a[0]~a[n-1]中存储的n个整数从小到大排序。排序算法是:第一趟通过比较将n个整数中的最小值放在a[0]中,最大值放在a[n-1]中; 第二趟通过比较将n个整数中的次小值放在a[1]中,次大值放在a[n-2]中;......,依次类推,直到待排序序列为递增序列。试完善程序以达到要求的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
 
void fun(char *t,int count[])
{
    char *p=t;
 
    while (*p!='\0') {
        if(*p>='0' && *p<='9')
            count[*p-'0']++;
        p++;
    }
}
 
int main()
{
    char s[80]="1enterschar4543123564879ffgh";
    int count[10]= {0},i;
 
    fun(s,count);
    for(i=0; i<10; i++)
        if(count[i]) printf("%d:%d  ",i,count[i]);
 
    return 0;
}

16. 下列程序中函数find_replace的功能是: 在s1指向的字符串中查找s2指向的字符串,并用s3指向的字符串替换在s1中找到的所有s2字符串。若sl字符串中没有出现s2字符串,则不做替换并使函数返回0,否则函数返回1。试完善程序以达到要求的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#define  N 7
 
void sort(int a[],int n)
{
    int i,j,min,max,t;
 
    for(i=0; i<n-1; i++) {
        min = i;
        max = n-i-1;
        for(j=i+1; j<n-i; j++)
            if(a[j]<a[min]) min=j;
            else if(a[j]>a[max])max=j;
        if(min!=i) {
            t=a[min];  a[min]=a[i];  a[i]=t;
        }
 
        if(max!=n-i-1)
            if(max==i) {
                t=a[min];  a[min]=a[n-i-1];  a[n-i-1]=t;
            } else {
                t=a[max];  a[max]=a[n-i-1];  a[n-i-1]=t;
            }
    }
}
 
int main()
{
    int a[N]= {8,4,9,3,2,1,5},i;
 
    sort(a,N);
    printf("sorted:\n");
    for(i=0; i<N; i++)
        printf("%d\t",a[i]);
    printf("\n");
    return 0;
}

17. 设hl和h2分别为两个单链表的头指针,链表中结点的数据结构为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
 
int find_replace(char s1[],char s2[],char s3[])
{
    int i,j,k,t=0;
    char temp[80];
    if(s1[0]=='\0'||s2[0]=='\0')return t;
    for(i=0; s1[i]!='\0'; i++) {
        k=0;
        j=i;
        while(s1[j]==s2[k]&&s2[k]!='\0') {
            j++;
            k++;
        }
        if(s2[k]=='\0') {
            strcpy(temp,&s1[j]);
            strcpy(&s1[i], s3);
            i=i+strlen(s3);
            strcpy(&s1[i], temp);
            t=1;
        }
    }
    return t;
}
 
int main(int argc, char *argv[])
{
    char line[80]="This is a test program and a test data.";
    char substr1[10]="test",substr2[10]="actual";
    int k;
 
    k=find_replace(line,substr1,substr2);
    if(k==1)
        puts(line);
    else
        printf("not found\n");
 
    return 0;
}

sea_del函数的功能是:删除hl指向的链表中首次出现的与h2指向的链表中数据完全匹配的若干个连续结点,函数返回hl指向链表的头指针。例如,初态下,hl指向链表和h2指向链表如下图所示:
试完善函数sea_del以达到要求的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
typedef struct node {
    int data;
    struct  node *next;
} NODE;
 
NODE *sea_del(NODE *h1,NODE *h2)
{
    NODE *p,*ph,*q,*s;
    ph=NULL;
    p=q=h1;
    s=h2;
 
    if(h1==NULL|| h2==NULL) return h1;
    while(p!=NULL && s!=NULL)
    {
        while(q->data==s->data&&q&&s)
        {
            q=q->next;
            s=s->next;
        }
        if(s!=NULL)    /*失配时,h1起始结点后移,h2从首结点开始*/
        {
            ph=p;
            p=q=p->next;
            s=h2;
        }
        else
            if(ph==NULL)  h1=q;
            else ph->next=q;
    }
    return h1  ;
}

抱歉!评论已关闭.