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

嘿,各位!

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)
    {

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 很抱歉,我没有接触过 "C primer plus 第6" 中文答案。我可以回答关于 C 语言相关问题,但是我不能提供书中特定章节的答案。我可以尝试解释 C 语言的特定概念或者提供代码示例来帮助你解决问题。 ### 回答2: 《C Primer Plus》是一本经典的C语言入门教材,第六中文为读者提供了详尽的C语言知识,并结合了实际应用案例进行演示,使读者能够全面了解C语言的基础知识和开发技能。 本书共包括18章内容,从C语言的基础语法开始讲解,逐步深入探讨了数据类型、运算符、输入输出、数组、结构体、函数、指针、内存管理、文件操作等C语言重要概念,同时也包括了C标准库的使用方法。 此外,本书还通过实例演示的方式,让读者能够在实际工作中应用所学技能。例如,在第十章分析了一个完整的程序,通过这个例子,读者可以清晰地了解程序的执行过程和调试技巧;在第十五章介绍了如何读取和写入二进制文件,并提供了使用这些技术来保存和恢复内存结构的实际示例。 总体而言,本书是一本深入浅出、覆盖面广、实用性强的C语言入门教材。无论是初学者还是有经验的程序员,都可以通过学习本书中的内容,掌握C语言的基础技能,从而更加高效地进行代码编写和程序开发。该书还提供了大量的习题和实验,读者可以通过这些练习来加深对知识点的理解和掌握程度。 ### 回答3: 《C Primer Plus》第六中文是一本针对C语言初学者的优秀教材。本书分为18个章节,逐步介绍C语言的基本概念、语句、数据类型、运算符、函数、指针、内存管理、结构和文件操作等内容。全书注重理论与实践相结合,采用图文并茂的方式进行讲解,有助于读者深入理解和掌握C语言的重要概念和技术。 本书首先介绍了C语言的开发环境、书写规则和基本语法,帮助读者快速上手编写简单的C程序。随后讲解了C语言的基本数据类型、运算符和表达式,帮助读者深入理解计算机程序的运算逻辑,同时引导读者编写简单的控制流程序和逻辑判断程序。 在介绍函数、数组和指针的章节中,本书逐步展示了C语言的强大功能与优势,通过实例演示和练习,让读者掌握函数库的使用、数组与指针的相关操作和处理技巧。这些内容是掌握C语言编程的基本技能,也是编写高效程序的关键所在。 在后续的内存管理与结构章节中,本书讲解了动态内存分配、指针运算、结构体和联合体的定义和使用方法,这些内容对于编写大型程序和优化程序性能至关重要。 最后,本书介绍了文件操作的相关知识,帮助读者了解文件的IO操作、异常处理和二进制文本处理等技术,这些知识对于编写完整的应用程序是必需的。 总之,《C Primer Plus》第六中文是一本权威、系统、实用的C语言教材,对于希望学习和掌握C语言编程的读者来说是一本不可多得的好书。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值