《C Primer Plus》第6版 编程练习答案参考 第十四章

本文提供了《C Primer Plus》第6版第14章的编程练习答案,包括14-1到14-11共11道题目。涉及的编程任务包括处理月份名、计算一年中到指定日期的总天数、加减乘除运算程序以及数组元素的转换函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嘿,各位!

Long time no see!

不知不觉地又拖了这么久......

少说费话,就直接开始吧!

这样的目录真的有点丑,有没有大佬教教我怎么把空白去掉.....

目录(如果无法使用,可以选用右侧的目录选项)教程连接

 

先看看这里:

求助:

源码 + 题目 + 运行效果:

P14-1:

P14-2:

P14-3:

P14-4:

P14-5:

P14-6:

P14-7:

 

P14-8:

P14-9:

P14-10:

P14-11:


 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

先看看这里:

博主的编译环境:(我也推荐大家用Visual Studio 编码)

VS2017 community

运行环境:

Windows 10 

如果不想直接复制的朋友可以从下面的连接里直接获取源码 :

链接:https://pan.baidu.com/s/1YOAMrXZm5Jb3A-LgZBwLEA 
提取码:uh57 

还有一点得说的,在上一篇博客里,引用代码块有时实在是太长了,

而且以后编写的代码量肯定还会更长,所以博主以后都直接用块引用来放置代码.

所以从美感上可能说不了什么了.......当然博主会尽量做到优质的代码规范

求助:

有没有朋友知道怎么删除博客页面多余的空行吗?

我试了好久都没有用........求助.

 


 


 

源码 + 题目 + 运行效果:

P14-1:

/*
    14.1 重新编写复习题 5,用月份名的拼写代替月份号(别忘了使用strcmp() )。在一个简单的程序中测试该函数。
*/

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

#define LENTH 20            //月份名的长度
#define MONTHNUM 12  //12 个月份

//月份结构
typedef struct month
{
    char month_name[LENTH];    //月份名
    char m_name[4];                     //月份的缩写
    int days;                                  //月份的天数
}MONTH;

//获取一个月份名
char * Get_Month_Name(char * buf, int lenth);

//将字符串转换成小写
char * change_into_low(char * string);

//从结构中查找月份值,并打印出来
void Chr_Month(struct month * months, int total_month, char * name);

//返回一年中到该月为止(包括该月)的总天数
int total_days_from(MONTH * months, int monthnumber);


int main(void)
{
    char name[LENTH] = { '\0' };        //储存输入,初始化为空串

    //初始化十二个月份,这里用小写是为了方便比较字符串
    MONTH months[MONTHNUM] =
    {
        {"january", "jan", 31},
        {"february", "feb", 28},
        {"march", "mar", 31},
        {"april", "apr", 30},
        {"may", "may", 31},
        {"june", "jun", 30},
        {"july", "jul", 31},
        {"august", "aug", 31},
        {"september", "sep", 30},
        {"october", "otc", 31},
        {"november", "nov", 30},
        {"december", "dec", 31}
    };
    //提示用户输入1 个月份名
    printf("Please enter a month name (q to quit): ");
    while (Get_Month_Name(name, LENTH) != NULL)
    {
        //将输入转换成小写
        change_into_low(name);

        //从结构中查找月份值,并打印出来
        Chr_Month(months, MONTHNUM, name);

        //提示用户输入1 个月份名
        printf("Please next month name (q to quit): ");    
    }

    printf("Bye!\n");
    return 0;
}

//获取一个月份名
char * Get_Month_Name(char * buf, int lenth)
{
    while (gets_s(buf, LENTH) == NULL || buf[0] == '\0')
    {
        printf("That's not a value string, try again: ");
        continue;
    }
    if (buf[0] == 'q')    //输入q 以结束程序
        return NULL;

    return buf;
}

//将字符串转换成小写
char * change_into_low(char * string)
{
    while (*string)
    {
        *string = tolower(*string);
        string++;
    }

    return string;
}

//从结构中查找月份值,并打印出来
void Chr_Month(struct month * months, int total_month, char * name)
{
    for (int ct = 0; ct < total_month; ct++)
    {
        //查询对应的月份值
        if (strcmp(name, months[ct].month_name) == 0)
        {
            if (ct + 1 == 1)    //如果输入的是1 月    
            {
                printf("January has %d days.\n", months[0].days);
            }
            else                //输入的是其他月份
            {
                printf("There are %d days from %s to %s.\n",
                    total_days_from(months, ct + 1), months[0].month_name, name);
            }
            break;
        }
        //输入不属于任何月份
        else if (ct + 1 == total_month)
            printf("Sorry, there isn't a month named %s\n", name);
    }
}

//返回一年中1月到 n 月为止(包括该月)的总天数
int total_days_from(MONTH * months, int monthnumber)
{
    int total = 0;
    int ct = 0;        //count缩写
    for (ct = 1; ct <= monthnumber; ct++)
    {
        total += months[ct - 1].days;
    }
    return total;
}

 

P14-2:

/*
    14.2    编写一个函数,提示用户输入日,月。月份可以是月份号,月份名或月份名缩写。然后该程序应返回
    一年中到用户指定的日子(包括这一天)的总天数
*/

#include <stdio.h>
#include <stdlib.h>        //atoi()函数
#include <string.h>        //strcmp()函数
#include <ctype.h>        //tolower()函数

#define LENTH 40                //月份名的最大长度
#define MONTHNUM 12        //12 个月份

//月份结构
typedef struct month
{
    char month_name[LENTH];        //月份名
    char m_name[4];                         //月份的缩写
    int days;                                       //月份的天数
    int month_n;                                 //月份号
}MONTH;

//用户输入  结构
struct info
{
    char month_name[LENTH];    //月份名(缩写或者全称)/月份号
    char year[10];                          //这俩个使用char类型
    char days[10];                         //是为了后面好一口气转换成int类型
};

//查找两个空格    格式为月 日 年之间有两个空格
int chr_two_spaces(char * string, int length);

//获取年月日信息
char * Get_info(char * buf, int lenth);

//将字符串转换成小写
char * change_into_low(char * string);

//分割用户的输入
struct info divide_info(struct info message, char * buf);

//返回一年中到该月为止(包括该月)的总天数
int total_days_from(MONTH * months, int monthnumber);

//判断传入的字符是否是数字
int judge_digit(char ch);

//从结构中查找月份值,并打印出来
void Chr_Month(struct month * months, int total_month, struct info message);

int main(void)
{
    char info[LENTH] = { '\0' };        //储存用户输入,初始化为空串
    struct info message =               //初始化所以信息为0
    {
        {'\0'},{'\0'}, {'\0'}
    };

    //初始化十二个月份,这里用小写是为了方便比较字符串
    MONTH months[MONTHNUM] =
    {
        {"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", "otc", 31, 10},
        {"november", "nov", 30, 11},
        {"december", "dec", 31, 12}
    };

    //提示用户输入1 个月份名
    printf("Please enter a month name, the days and the year, like 10 21 2018 (q to quit): ");
    while (Get_info(info, LENTH) != NULL)
    {
        //分割用户的输入
        message = divide_info(message, info);

        //将输入转换成小写
        change_into_low(message.month_name);

        //从结构中查找月份值,并打印出来
        Chr_Month(months, MONTHNUM, message);

        //提示用户下一个月份名
        printf("Please next month name (q to quit): ");    
    }

    printf("Bye!\n");
    return 0;
}

//查找两个空格
int chr_two_spaces(char * string, int length)
{
    int count = 0, i = 0;
    while (*string++ != '\0')
    {
        if (*string == ' ')
            count++;
    }

    if (count != 2)
        return 0;
    else
        return 1;
}
//获取一个月份名
char * Get_info(char * buf, int lenth)
{
    while (gets_s(buf, LENTH) == NULL || buf[0] == '\0' 
        || buf[0] == ' ' || chr_two_spaces(buf, LENTH) == 0)        //处理一些非法输入
    {
        if (buf[0] == 'q')        //输入q 以结束程序
            return NULL;
                                        //反之提示重新输入
        printf("That's not a value string, try again: ");
        continue;
    }

    return buf;
}

//将字符串转换成小写
char * change_into_low(char * string)
{
    while (*string)
    {
        *string = tolower(*string);
        string++;
    }

    return string;
}

//分割用户的输入
struct info divide_info(struct info message, char * buf)
{
    int count = 0, i = 0;

    //分割月份
    while (buf[count] != ' ')
    {
        message.month_name[i++] = buf[count++];
    }
    i = 0;
    count++;

    //分割日期
    while (buf[count] != ' ')
    {
        message.days[i++] = buf[count++];
    }
    i = 0;
    count++;

    //分割年份
    while (buf[count] != '\0')
    {
        message.year[i++] = buf[count++];
    }
    return message;
}

//返回一年中1月到 n 月为止(包括该月)的总天数
int total_days_from(MONTH * months, int monthnumber)
{
    int total = 0;
    int ct = 0;            //count缩写
    for (ct = 0; ct < monthnumber; ct++)
    {
        total += months[ct].days;
    }
    return total;
}

//判断传入的字符是否是数字
int judge_digit(char ch)
{
    if (ch >= '0' && ch <= '9')
        return ch - '0';
    else
        return 0;
}

//从结构中查找月份值,并打印出来
void Chr_Month(struct month * months, int total_month, struct info message)
{
    int flag = 0;                                                                //月份未找到的标记
    int num_of_month = atoi(message.month_name);        //获取月份号
    int num_of_days = atoi(message.days);                        //获取该月已经过去的天数
    int num_of_year = atoi(message.year);                        //获取当前年号

    if (num_of_month< 0 || num_of_month> 12)            //非法月份
    {
        printf("The number of the month is invalid.\n");
        flag = 1;
        return;
    }
    if (num_of_days < 0 || num_of_days > 31)                //非法天数
    {
        printf("The number of the days is invalid.\n");
        return;
    }
    if (num_of_year < 0)            //非法年数
    {
        printf("The number of the year is invalid.\n");
        return;
    }

    for (int ct = 0; ct < total_month; ct++)
    {
        //输入的是月份名
        if (strcmp(message.month_name, months[ct].month_name) == 0)
        {
            printf("%d has passed %d days.\n", num_of_year, num_of_days + total_days_from(months, ct));
            flag = 1;
            break;
        }
        //如果输入的是缩写
        else if (strcmp(message.month_name, months[ct].m_name) == 0)
        {
            printf("%d has passed %d days.\n", num_of_year, num_of_days + total_days_from(months, ct));
            flag = 1;
            break;
        }
        //如果是输入的是月份号
        else if (num_of_month != 0)                //检查首字符为数字 (如果首字符不是数字,则这里应该是0 )
        {        
            printf("%d has passed %d days.\n", num_of_year, num_of_days + total_days_from(months, num_of_month - 1));
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        printf("The number of the month is invalid.\n");
}
 

P14-3:

/*
    14.3    修改程序清单14.2中的图书目录程序,使其按照输入图书的顺序输出图书的信息,
        然后按照标题字母的声明输出图书,最后按照价格的升序输出图书的信息

        1)按照输入图书的顺序输出图书的信息
        2)按照字母表顺序输出图书的信息
        3)按照价格的升序输出图书的信息
*/

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

#define MAXTITLE    40
#define MAXAUTL    40
#define MAXBKS        100                    //书籍的最大数量

//建立 book 模板
struct book
{
    char title[MAXTITLE];
    char author[MAXAUTL];
    float value;
};

//接收输入函数
char * s_gets(char * st, int n);

//拷贝原始数据
void CopySource(struct book * target, struct book * source, int count);

//输出图书的信息
void ShowInfo(const struct book * Pbook, int count);

//将输入图书的顺序按照标题字母的声明顺序排序
void TitleOrder(struct book * Pbook, int count);

//将输入图书的顺序按照价格的升序输出图书
void PriceOrder(struct book * Pbook, int count);

int main(void)
{
    struct book library[MAXBKS];                    // book 类型结构的数组
    struct book temp[MAXBKS];                        //储存原始数据的克隆体
    int count = 0;

    printf("Please enter the book title.\n");
    printf("Press [entet[ at the start of a line to stop.\n");
    while (count < MAXBKS && s_gets(library[count].title, MAXTITLE) != NULL
        && library[count].title[0] != '\0')            //获取书名
    {
        printf("Now enter the author.\n");            //获取作者名称
        s_gets(library[count].author, MAXAUTL);

        printf("Now enter the value.\n");
        scanf_s("%f", &library[count].value);        //获取价格

        count++;                                                //递增
        while (getchar() != '\n')
            continue;                                            //清理输入行
        if (count < MAXBKS)
            printf("Enter the next title.\n");
    }

 

    //结束输入后
    if (count > 0)
    {
        CopySource(temp, library, count);    //拷贝一份数据,用于保存原始数据

        printf("Here is the list of your books(by root order): \n");
        ShowInfo(temp, count);                //按原有的顺序输出信息
        putchar('\n');

        printf("Here is the list of your books(by title order): \n");
        TitleOrder(temp, count);                /* 将结构按照标题进行排序 */
        ShowInfo(temp, count);                //按标题顺序输出信息
        putchar('\n');

        printf("Here is the list of your books(by price order): \n");
        PriceOrder(temp, count);                /* 将结构按照价格升序进行排序 */
        ShowInfo(temp, count);
        putchar('\n');

        //printf("Here is the list of your books(by root order): \n");
        //ShowInfo(library, count);                //按原有的顺序输出信息(这段代码可要可不要)
    }
    else
    {
        printf("No books? Too bad.\n");
    }

    return 0;
}

//接收输入函数
char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val != NULL)
    {
        find = strchr(st, '\n');            //查找换行符
        if (find != NULL)                //如果地址不是NULL
            *find = '\0';                    //在此放置一个空字符
        else
        {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

//拷贝原始数据
void CopySource(struct book * target, struct book * source, int count)
{
    for (int i = 0; i < count; i++)
    {
        target[i] = source[i];
    }
}

//输出图书的信息
void ShowInfo(const struct book * Pbook, int count)
{
    int index;
    for (index = 0; index < count; index++)
    {
        printf("%s by %s: $%.2f\n", Pbook[index].title, Pbook[index].author, Pbook[index].value);
    }
    return;
}

//将输入图书的顺序按照标题字母的声明顺序排序(使用冒泡排序)
void TitleOrder(struct book * Pbook, int count)
{
    struct book temp;
    int top, seek;              

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值