【程序填空】C语言练习题(二)

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:下列给定程序中,函数fun的功能是:从形参ss所指字符串数组中,删除所有串长
      超过k的字符串,函数返回剩余字符串的个数。ss所指字符串数组中共有N个字符串,
      且串长小于M。-------------------------------------------------------*/

#include  <stdio.h>
#include  <string.h>

#define   N   5
#define   M   10

int fun(char  (*ss)[M], int  k)
{
        int  i,j=0,len;
/***********SPACE***********/
        for(i=0; i< k ; i++)
        {  
                len=strlen(ss[i]);
/***********SPACE***********/
                if(len<=strlen(ss[j]))
/***********SPACE***********/
                        strcpy(ss[j++],j++);
        }
        return  j;
}
main()
{ 
        char  x[N][M]={"Beijing","Shanghai","Tianjing","Nanjing","Wuhan"};
        int  i,f;
        printf("\nThe original string\n\n");
        for(i=0;i<N;i++)puts(x[i]);  printf("\n");
        f=fun(x,7);
        printf("The string witch length is less than or equal to 7 :\n");
        for(i=0; i<f; i++)  puts(x[i]);printf("\n");
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:下列给定程序中,函数fun的功能是:在形参ss所指字符串数组中查找与形参t所指
      字符串相同的串,找到后返回该串在字符串数组中的位置(即下标值),若未找到则
      返回-1。ss所指字符串数组中共有N个内容不同的字符串,且串长小于M。

-------------------------------------------------------*/

#include  <stdio.h>
#include  <string.h>

#define   N   5
#define   M   8

int fun(char  (*ss)[M],char  *t)
{  
        int  i;
/***********SPACE***********/
        for(i=0; i< 【?】 ; i++)
                if(strcmp(ss[i],t)==0 ) 
/***********SPACE***********/
                        return  【?】 ;
        return -1;
}
main()
{
        char  ch[N][M]={"if","while","switch","int","for"},t[M];
        int  n,i;
        printf("\nThe original string\n\n");
        for(i=0;i<N;i++)puts(ch[i]); 
                printf("\n");
        printf("\nEnter a string for search:  "); 
        gets(t);
        n=fun(ch,t);
/***********SPACE***********/
        if(n==  【?】) 
                printf("\nDon't found!\n");
        else  
                printf("\nThe position is  %d .\n",n);
}

 

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:下列给定程序中,函数fun的功能是:找出100~999之间(含100和999)所有整数中各
     位上数字之和为x(x为正整数)的整数,并输出;符合条件的整数个数作为函数值返回。

例如:当x值为5时,100~999之间各位上数字之和为5的整数有:104、113、122、131、
      140、203、212、221、230、302、311、320、401、410、500,共有15个。
      当x值为27时,各位数字之和为27的整数是:999,只有1个。

-------------------------------------------------------*/

#include  <stdio.h>

int fun(int  x)
{ 
        int  n, s1, s2, s3, t;
        n=0;
        t=100;
/***********SPACE***********/
        while(t<=999)
        {
                s1=t%10;
/***********SPACE***********/
                s2=(t/10)%10; 
                s3=t/100;
/***********SPACE***********/
                if(s1+s2+s3==x)
                { 
                        printf("%d ",t);
                        n++;
                }
                t++;
        }
        return  n;
}
main()
{ 
        int x=-1;
        while(x<0)
        {
                printf("Please input(x>0): "); 
                scanf("%d",&x);
        }
        printf("\nThe result is: %d\n",fun(x));
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件
      中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。

-------------------------------------------------------*/

#include  <stdio.h>

void fun(char  *s, int  a, double  f)
{
/***********SPACE***********/
        【?】 fp;
        char  str;
        fp = fopen("file1.txt", "w");
        fprintf(fp, "%s %d %f\n", s, a, f);
        fclose(fp);
        fp = fopen("file1.txt", "r");
        printf("\nThe result :\n\n");
        str = fgetc(fp);
/***********SPACE***********/
        while (!feof(【?】)) 
        {
/***********SPACE***********/
                putchar(【?】); 
                str = fgetc(fp);  
        }
        putchar('\n');
        fclose(fp);
}
main()
{ 
        char  a[10]="Hello!";  
        int  b=12345;
        double  c= 98.76;
        fun(a,b,c);
}

 

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,
      再用字符串方式从此文本文件中逐个读入,并调用库函数atoi和atof将字符串转换
      成相应的整数、浮点数,然后将其显示在屏幕上。 

-------------------------------------------------------*/

#include  <stdio.h>
#include  <stdlib.h>

void fun(char  *s, int  a, double  f)
{
/***********SPACE***********/
        【?】fp;
        char  str[100], str1[100], str2[100];
        int  a1;  
        double  f1;
        fp = fopen("file1.txt", "w");
        fprintf(fp, "%s  %d  %f\n", s, a, f);
/***********SPACE***********/
        【?】 ;
        fp = fopen("file1.txt", "r");
/***********SPACE***********/
        fscanf(【?】,"%s%s%s", str, str1, str2);
        fclose(fp);
        a1 = atoi(str1);
        f1 = atof(str2);
        printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);
}
main()
{ 
        char  a[10]="Hello!";   
        int  b=12345;
        double  c= 98.76;
        fun(a,b,c);
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:下列给定程序中,函数fun的功能是:统计带头结点的单向链表中结点的个数,并存放
      在形参n所指的存储单元中。

-------------------------------------------------------*/

#include    <stdio.h>
#include    <stdlib.h>

#define    N    8

typedef  struct list
{  
        int  data;
        struct list  *next;
} SLIST;
SLIST *creatlist(int  *a);
void outlist(SLIST  *);
void fun( SLIST  *h, int  *n)
{  
        SLIST  *p;
/***********SPACE***********/
        【?】=0;
        p=h->next;
        while(p)
        { 
                (*n)++;
/***********SPACE***********/
                p=p->【?】;
        }
}
main()
{  
        SLIST  *head;
        int  a[N]={12,87,45,32,91,16,20,48}, num;
        head=creatlist(a);   
        outlist(head);
/***********SPACE***********/
        fun(【?】, &num);
        printf("\nnumber=%d\n",num);
}
SLIST *creatlist(int  a[])
{  
        SLIST  *h,*p,*q;    
        int  i;
        h=p=(SLIST *)malloc(sizeof(SLIST));
        for(i=0; i<N; i++)
        {  
                q=(SLIST *)malloc(sizeof(SLIST));
                q->data=a[i]; 
                p->next=q; 
                p=q;
        }
        p->next=0;
        return  h;
}
void outlist(SLIST  *h)
{ 
        SLIST  *p;
        p=h->next;
        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");
        }
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:程序通过定义学生结构体变量,存储了学生的学号、姓名和三门课的成绩。所有学
      生数据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件
      中最后一个学生的数据,即用新的学生数据覆盖该学生原来的数据,其他学生的数
      据不变。

-------------------------------------------------------*/

#include  <stdio.h>

#define    N    5

typedef struct  student 
{
        long  sno;
        char  name[10];
        float  score[3];
} STU;
void fun(char  *filename, STU  n)
{ 
        FILE  *fp;
/***********SPACE***********/
        fp = fopen(【?】, "rb+");
/***********SPACE***********/
        fseek(【?】, -(long)sizeof(STU), SEEK_END);
/***********SPACE***********/
        fwrite(&n, sizeof(STU), 1, 【?】);
        fclose(fp);
}
main()
{ 
        STU  t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
             {10003,"LiSi", 85, 70, 78},    {10004,"FangFang", 90, 82, 87},
             {10005,"ZhangSan", 95, 80, 88}};
        STU  n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
        int  i,j;    
        FILE  *fp;
        fp = fopen("student.dat", "wb");
        fwrite(t, sizeof(STU), N, fp);
        fclose(fp);
        fp = fopen("student.dat", "rb");
        fread(ss, sizeof(STU), N, fp);
        fclose(fp);
        printf("\nThe original data :\n\n");
        for (j=0; j<N; j++)
        {  
                printf("\nNo: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
                for (i=0; i<3; i++)
                        printf("%6.2f ", ss[j].score[i]);
                printf("\n");
        }
        fun("student.dat", n);
        printf("\nThe data after modifing :\n\n");
        fp = fopen("student.dat", "rb");
        fread(ss, sizeof(STU), N, fp);
        fclose(fp);
        for (j=0; j<N; j++)
        {  
                printf("\nNo: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
                for (i=0; i<3; i++) 
                        printf("%6.2f ", ss[j].score[i]);
                printf("\n");
        }
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:给定程序中,函数fun的功能是:找出N×N矩阵中每列元素中的最大值,并按顺序
      依次存放于形参b所指的一维数组中。

-------------------------------------------------------*/

#include  <stdio.h>

#define  N  4

void fun(int  (*a)[N], int  *b)
{ 
        int  i,j;
        for(i=0; i<N; i++)
        {
/***********SPACE***********/
                b[i]=【?】;
                for(j=1; j<N; j++)
/***********SPACE***********/
                        if(b[i]【?】 a[j][i])
                                b[i]=a[j][i];
        }
}
main()
{ 
        int  x[N][N]={ {12,5,8,7},{6,1,9,3},{1,2,3,4},{2,8,4,3} },y[N],i,j;
        printf("\nThe matrix :\n");
        for(i=0;i<N; i++)
        {  
                for(j=0;j<N; j++)
                        printf("%4d",x[i][j]);
                printf("\n");
        }
/***********SPACE***********/
        fun(【?】);
        printf("\nThe result is:");
        for(i=0; i<N; i++) 
                printf("%3d",y[i]);
        printf("\n");
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:给定程序中,函数fun的功能是将带头结点的单向链表结点数据域中的数据从小到
      大排序。即若原链表结点数据域从头至尾的数据为:10、4、2、8、6,排序后链
      表结点数据域从头至尾的数据为:2、4、6、8、10。

-------------------------------------------------------*/

#include  <stdio.h>
#include  <stdlib.h>

#define    N    6

typedef struct node 
{
        int  data;
        struct node  *next;
} NODE;
void fun(NODE  *h)
{ 
        NODE  *p, *q;  
        int  t;
/***********SPACE***********/
        p = 【?】;
        while (p)
        {
/***********SPACE***********/
                q = 【?】;
                while (q)
                {
                        if (p->data > q->data)
                        { 
                                t = p->data;
                                p->data = q->data; 
                                q->data = t; 
                        }
                        q = q->next;
                }
                p = p->next;
        }
}
NODE *creatlist(int  a[])
{ 
        NODE  *h,*p,*q;     
        int  i;
        h = (NODE *)malloc(sizeof(NODE));
        h->next = NULL;
        for(i=0; i<N; i++)
        {  
                q=(NODE *)malloc(sizeof(NODE));
                q->data=a[i];
                q->next = NULL;
/***********SPACE***********/
                if (h->next == 【?】) 
                        h->next = p = q;
                else    
                {  
                        p->next = q;
                        p = q;   
                }
        }
        return  h;
}
void outlist(NODE  *h)
{ 
        NODE  *p;
        p = h->next;
        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]= {0, 10, 4, 2, 8, 6 };
        head=creatlist(a);
        printf("\nThe original list:\n");
        outlist(head);
        fun(head);
        printf("\nThe list after sorting :\n");
        outlist(head);
}

/*-------------------------------------------------------
【程序填空】
---------------------------------------------------------

题目:给定程序中,函数fun的功能是:把形参s所指字符串中最右边的n个字符复制到形
      参t所指字符数组中,形成一个新串。若s所指字符串的长度小于n,则将整个字符
      串复制到形参t所指字符数组中。

例如:形参s所指的字符串为:abcdefgh,n的值为5,程序执行后t所指字符数组中的字符
      串应为:defgh。

-------------------------------------------------------*/

#include  <stdio.h>
#include  <string.h>

#define   N   80

void fun(char  *s, int  n, char  *t)
{ 
        int len,i,j=0;
        len=strlen(s);
        if(n>=len) 
/***********SPACE***********/
                strcpy(【?】);
        else 
        {
                for(i=len-n; i<=len-1; i++) 
/***********SPACE***********/
                        t[j++]= 【?】 ;
/***********SPACE***********/
                t[j]= 【?】 ;
        }
}
main()
{ 
        char  s[N],t[N];  
        int  n;
        printf("Enter a string:  ");
        gets(s);
        printf( "Enter n:"); 
        scanf("%d",&n);
        fun(s,n,t);
        printf("The string t :  ");  
        puts(t);
}
  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值