C prime plus(第六版) 中文版 第十四章课后练习题答案

第一题:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * s_gets(char *st,int n);
struct month{
    char month[10];
    char sub[4];
    int day;
    int num;
};
int main()
{
    printf("14.18.1\n");
    struct month a[12]={
    {"January","Jan",31,1},
    {"February","Feb",28,2},
    {"March","Mar",31,3},
    {"April","Apr",30,4},
    {"May","May",31,5},
    {"June","Jun",30,6},
    {"July","Jul",31,7},
    {"August","Aug",31,8},
    {"September","Sep",30,9},
    {"October","Oct",31,10},
    {"November","Nov",30,11},
    {"December","Dec",31,12},
    };
    printf("Enter the month: ");
    char b[10];
    int cnt=0,sum=0;
    s_gets(b,10);
    while(strcmp(a[cnt].sub,b)!=0){
        cnt++;
    }
    for(int i=0;i<=cnt;i++){
        sum+=a[i].day;
    }
    printf("%d",sum);
    return 0;
}
char * s_gets(char *st,int n){
    char *ret_val;
    char *find;
    ret_val=fgets(st,n,stdin);
    if(ret_val){
        find=strchr(st,'\n');
        if(find){
            *find='\0';
        }
        else{
            while(getchar()!= '\n'){
                continue;
            }
        }
    }
    return ret_val;
}

第二题:

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

struct month{
    char month[10];
    char sub[4];
    int day;
    int num;
};
struct month a[12]={
    {"January","Jan",31,1},
    {"February","Feb",28,2},
    {"March","Mar",31,3},
    {"April","Apr",30,4},
    {"May","May",31,5},
    {"June","Jun",30,6},
    {"July","Jul",31,7},
    {"August","Aug",31,8},
    {"September","Sep",30,9},
    {"October","Oct",31,10},
    {"November","Nov",30,11},
    {"December","Dec",31,12},
    };
int tol_day();
void get_year();
int get_month();
int get_day(int n);
char * s_gets(char *st,int n);
int main()
{
    printf("14.18.2:\n");

    int total=0;
    total=tol_day();
    printf("The day sum is %d ",total);
    return 0;
}
int tol_day(){
    int sum=0;
    int m;
    get_year();
    m=get_month();
    sum=get_day(m);
    return sum;
}
void get_year(){
    int y;
    printf("Enter the year: ");
    while(scanf("%d",&y)!=1){
        while(getchar()!='\n'){
            continue;
        }
        printf("Enter the integer: ");
    }
    if((y%4==0 && y%100!=0)|| y%400==0){
        a[1].day=29;
    }
    else{
        a[1].day=28;
    }
}
int get_month(){
    printf("Enter the month: ");
    getchar();
    char mon[12];
    s_gets(mon,12);
    char *p;
    p=mon;
    int cnt=0;
    if(strlen(mon)<3){
        int num=atoi(p);
        return num;
    }
    else{
       while(strncmp(a[cnt].month,mon,3)!=0){
            cnt++;
        }
        return cnt;
    }

}
int get_day(int n){
    int total=0;
    int day;
    printf("Enter the day: ");
    scanf("%d",&day);
    total+=day;
    for(int i=0;i<(n-1);i++){
        total+=a[i].day;
    }
    return total;
}
char * s_gets(char *st,int n){
    char *ret_val;
    char *find;
    ret_val=fgets(st,n,stdin);
    if(ret_val){
        find=strchr(st,'\n');
        if(find){
            *find='\0';
        }
        else{
            while(getchar()!= '\n'){
                continue;
            }
        }
    }
    return ret_val;
}

第三题:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * s_gets(char *st,int n);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100

struct book{
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

void book_print(int count,struct book lib[]);
void book_print2(int count,struct book a[]);
void book_print3(int count,struct book b[]);

int main()
{
    printf("14.18.3\n");
    struct book library[MAXBKS];
    int count=0;
    printf("Enter the book title.\n");
    printf("Enter the enter to stop.\n");
    while(count<MAXBKS && s_gets(library[count].title,MAXTITL) != NULL &&
          library[count].title[0]!='\0'){
        printf("Enter the author.\n");
        s_gets(library[count].author,MAXAUTL);
        printf("Enter the value.\n");
        scanf("%f",&library[count++].value);
        while(getchar()!='\n')
            continue;
        if(count<MAXBKS)
            printf("Enter the next title.\n");
    }
    book_print(count, library);
    book_print2(count,library);
    book_print3(count,library);
    return 0;
}
char * s_gets(char *st,int n){
    char *ret_val;
    char *find;
    ret_val=fgets(st,n,stdin);
    if(ret_val){
        find=strchr(st,'\n');
        if(find){
            *find='\0';
        }
        else{
            while(getchar()!= '\n'){
                continue;
            }
        }
    }
    return ret_val;
}

void book_print(int count,struct book lib[]){
    if(count>0){
        printf("Here is the list of your books:\n");
        for(int i=0;i<count;i++)
            printf("%s by %s: $%.2f.\n",lib[i].title,lib[i].author,lib[i].value);
    }
}

void book_print2(int count,struct book a[]){
    struct book temp;
    for(int i=0;i<count-1;i++){
        for(int j=i+1;j<count;j++){
            if(strcmp(a[i].title,a[j].title)>0){
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    book_print(count,a);
}
void book_print3(int count,struct book b[]){
    struct book temp;
    for(int i=0;i<count-1;i++){
        for(int j=i+1;j<count;j++){
            if(b[i].value>b[j].value){
                temp=b[i];
                b[i]=b[j];
                b[j]=temp;
           }
        }
    }
    book_print(count,b);
}

第四题:
4.a:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct name{
    char fname[12];
    char lname[12];
    char mname[12];
};

struct member{
    char num[10];
    struct name n;
};
struct member arr[5]={
    {"302039823",{"Dribble","Flossie","March"}},
    {"302039824",{"Elle","Sue",""}},
    {"302039825",{"Fly","Lily",""}},
    {"302039826",{"Ge","Ossie","Zoo"}},
    {&#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值