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\n",m[i].day);
            break;
        }
    }
    printf("%d\n", 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 , '\n'))
            *find = '\0' ;
        else 
            while (getchar() != '\n');
    }
 
    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.\n");
    printf("press [enter] at the start of a line to stop.\n");
    while(i<MAXPEOPLE && s_gets(people[i].sec_num, NAMELEN) != NULL && people[i].sec_num[0] != '\0')
    {
        printf("now enter the first name.\n");
        s_gets(people[i].name.fname, NAMELEN);
        printf("now enter the middle name.\n");
        s_gets(people[i].name.mname, NAMELEN);
        printf("now enter the last name.\n");
        s_gets(people[i].name.lname, NAMELEN);
        i++;
        while(getchar() != '\n')
            continue;
        if(i<MAXPEOPLE)
            printf("enter the next people number:\n");
    }

    print(people);
    printf("*************\n");
    p_print(p);

    return 0;
}

void print(struct insu informa[])
{
    int count;
 
    for(count = 0; count < 5; count++)
    {
        if(informa[count].name.mname[0] == '\0')
            printf("%s, %s -- %s\n", informa[count].name.fname, informa[count].name.lname, informa[count].sec_num);
        else
            printf("%s, %s %c. -- %s\n", 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] == '\0')
            printf("%s, %s -- %s\n", pst->name.fname, pst->name.lname, pst->sec_num);
        else
            printf("%s, %s %c. -- %s\n", 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 , '\n'))
            *find = '\0' ;
        else 
            while (getchar() != '\n');
    }
 
    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:\n");

    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:\n");
        s_gets(test_grade[i].child.fname,LEN);
        printf("input last name:\n");
        s_gets(test_grade[i].child.lname,LEN);
        i++;
        if(i < CSIZE)
            printf("please input the next:\n");
    }
    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\n",
        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 , '\n'))
            *find = '\0' ;
        else 
            while (getchar() != '\n');
    }
 
    return ret_val ;
}

 

上一篇:C Primer Plus(第六版)第六章编程题答案参考


下一篇:C++ primer练习3.17问题解答