zoukankan      html  css  js  c++  java
  • C primer plus 第14章编程练习

    第一题:

    #include<stdio.h>
    #include<string.h>
    #define LEN 20
    char *s_gets(char *st, int n);
    
    struct month
    {
        char name[LEN];
        char abb_name[LEN];
        int day;
        int num_month;
    };
    
    int main(void)
    {
        char arr[LEN];
        int i;
        int total = 0;
        struct month m[12]={
            {"January","Jan",31,1},
            {"February","Feb",29,2},
            {"March","Mar",31,3},
            {"April","Apr",30,4},
            {"May","May",31,5},
            {"June","Jun",30,6},
            {"July","Jul",31,7},
            {"Augest","Aug",31,8},
            {"September","Sep",30,9},
            {"October","Oct",31,10},
            {"November","Nov",30,11},
            {"December","Dec",31,12},
        };
    
        printf("input the month as January:");
        s_gets(arr, LEN);
        // fun_com(m, LEN, arr);
        for(i=0; i<LEN; i++)
        {
            total += m[i].day;
            if(strcmp(arr,m[i].name) == 0)
            {
                printf("%d
    ",m[i].day);
                break;
            }
        }
        printf("%d
    ", total);
    
        return 0;
    }
    
    char * s_gets(char * st , int n)
    {
        char * ret_val ;
        char * find ;
     
        if (ret_val = fgets(st , n , stdin))
        {
            if (find = strchr(st , '
    '))
                *find = '' ;
            else 
                while (getchar() != '
    ');
        }
     
        return ret_val ;
    }

     第四题:

    #include<stdio.h>
    #include<string.h>
    #define NAMELEN 80
    #define MAXPEOPLE 5
    
    struct name
    {
        char lname[NAMELEN]; 
        char mname[NAMELEN]; 
        char fname[NAMELEN];
    };
    struct insu
    {
        char sec_num[NAMELEN];
        struct name name;
    };
    
    char * s_gets(char * st , int n);
    void print(struct insu informa[5]);
    void p_print(struct insu *);
    
    int main(void)
    {
        int i = 0;
        int count;
        struct insu people[MAXPEOPLE];
        struct insu *p;
    
        p = people;
    
        printf("please enter the insurance number.
    ");
        printf("press [enter] at the start of a line to stop.
    ");
        while(i<MAXPEOPLE && s_gets(people[i].sec_num, NAMELEN) != NULL && people[i].sec_num[0] != '')
        {
            printf("now enter the first name.
    ");
            s_gets(people[i].name.fname, NAMELEN);
            printf("now enter the middle name.
    ");
            s_gets(people[i].name.mname, NAMELEN);
            printf("now enter the last name.
    ");
            s_gets(people[i].name.lname, NAMELEN);
            i++;
            while(getchar() != '
    ')
                continue;
            if(i<MAXPEOPLE)
                printf("enter the next people number:
    ");
        }
    
        print(people);
        printf("*************
    ");
        p_print(p);
    
        return 0;
    }
    
    void print(struct insu informa[])
    {
        int count;
     
        for(count = 0; count < 5; count++)
        {
            if(informa[count].name.mname[0] == '')
                printf("%s, %s -- %s
    ", informa[count].name.fname, informa[count].name.lname, informa[count].sec_num);
            else
                printf("%s, %s %c. -- %s
    ", informa[count].name.fname, informa[count].name.lname, informa[count].name.mname[0], informa[count].sec_num);
        }
    }
    
    void p_print(struct insu * pst)
    {
        int i = 0;
     
        while(i < 5)
        {
            if (pst->name.mname[0] == '')
                printf("%s, %s -- %s
    ", pst->name.fname, pst->name.lname, pst->sec_num);
            else
                printf("%s, %s %c. -- %s
    ", pst->name.fname, pst->name.lname, pst->name.mname[0], pst->sec_num);
     
            i++;
            pst++;
        }
    }
    
    char * s_gets(char * st , int n)
    {
        char * ret_val ;
        char * find ;
     
        if (ret_val = fgets(st , n , stdin))
        {
            if (find = strchr(st , '
    '))
                *find = '' ;
            else 
                while (getchar() != '
    ');
        }
     
        return ret_val ;
    }

     第五题:

    #include<stdio.h>
    #include<string.h>
    #define LEN 80
    #define CSIZE 4
    
    struct name
    {
        char fname[LEN];
        char lname[LEN];
    };
    struct student
    {
        struct name child;
        float grade[3];
        float average;
        
    };
    
    void ave_grade(struct student *p);
    void print(struct student info[]);
    char * s_gets(char * st , int n);
    
    int main(void)
    {
        struct student test_grade[CSIZE];
        struct student *p;
        int i = 0;
        int j = 0;
        int index;
        
        printf("input three grades:
    ");
    
        for(j = 0; j < 3; j++)
        {
    
        }
    
        while(i < CSIZE )
        {
            for(j = 0; j < 3; j++)
            {
                scanf("%f",&test_grade[i].grade[j]);
                getchar();
            }
            printf("input first name:
    ");
            s_gets(test_grade[i].child.fname,LEN);
            printf("input last name:
    ");
            s_gets(test_grade[i].child.lname,LEN);
            i++;
            if(i < CSIZE)
                printf("please input the next:
    ");
        }
        p = test_grade;
    
        ave_grade(p);
        print(test_grade);
        
        return 0;
    }
    
    void ave_grade(struct student *p)
    {
        int i,j;
        float grade = 0;
    
        for(i = 0; i < CSIZE; i++)
        {
            for(j = 0; j < 3; j++)
            {
                grade += p->grade[j];
            }
            p->average = grade/3;
            grade = 0;
            p++;
        }
    }
    
    void print(struct student info[])
    {
        int i;
    
        for(i = 0; i < CSIZE; i++)
        {
    
            printf("%s %s's grades is [%f %f %f], average grade is %f
    ",
            info[i].child.fname,info[i].child.lname,info[i].grade[0],
            info[i].grade[1],info[i].grade[2],info[i].average);
        }
    }
    
    
    char * s_gets(char * st , int n)
    {
        char * ret_val ;
        char * find ;
     
        if (ret_val = fgets(st , n , stdin))
        {
            if (find = strchr(st , '
    '))
                *find = '' ;
            else 
                while (getchar() != '
    ');
        }
     
        return ret_val ;
    }
  • 相关阅读:
    ios添加第三方字体
    IOS 适应各种iphone屏幕尺寸
    sqlite第三方类库FMDB的使用
    IOS--沙盒机制
    用plist建立UITabController
    Xcode7 使用NSURLSession发送HTTP请求报错[转]
    网络开发--NSURLConnection类的简单介绍
    TF-IDF
    《汇编语言》——王爽 第12章 内中断
    操作系统 L4操作系统接口+L5系统调用的实现(网易公开课)
  • 原文地址:https://www.cnblogs.com/cokefentas/p/12392442.html
Copyright © 2011-2022 走看看