《C Primer Plus》编程训练—第14章

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
《C Primer Plus》编程训练

第14章

1.exercise1.c

计划一个结构模板存储一个月份名、该月份名的3个字母缩写、该月的天数以及月份号。定义一个数组,内含12个结构并初始化为一个年份(非闰年)。假设在全部函数的外部声明了该结构模板和一个该结构范例的数组。编写一个函数,用户提供月份号,该函数就返回一年中到该月为止(包括该月)的总天数。用月份名的拼写代替月份号(别忘了利用strcmp())。在一个简单的程序中测试该函数。
  1. //exercise14.1
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. struct month {
  6.    
  7.     char name[20];
  8.     char abbrev[4];
  9.     int days;
  10.     int monumb;
  11. };//结构模板
  12. const struct month months[12] = {
  13.    
  14.     {
  15.    "January", "Jan", 31, 1},
  16.     {
  17.    "February", "Feb", 28, 2},
  18.     {
  19.    "March", "Mar", 31, 3},
  20.     {
  21.    "April", "Apr", 30, 4},
  22.     {
  23.    "May", "May", 31, 5},
  24.     {
  25.    "June", "Jun", 30, 6},
  26.     {
  27.    "July", "Jul", 31, 7},
  28.     {
  29.    "August", "Aug", 31, 8},
  30.     {
  31.    "September", "Sep", 30, 9},
  32.     {
  33.    "October", "Oct", 31, 10},
  34.     {
  35.    "November", "Nov", 3, 11},
  36.     {
  37.    "December", "Dec", 31, 12}
  38. };//结构数组
  39. int days(char * input);//获取月份名,计算天数
  40. int main(void)
  41. {
  42.    
  43.     char input[20];
  44.     int total_days;
  45.     printf("请输入月份名:(按q退出程序)\n");
  46.     while (scanf("%s",input) == 1 && input[0] != 'q')//输入q才停止循环
  47.     {
  48.    
  49.         total_days = days(input);//调用函数
  50.         if (total_days > 0) //总天数大于0,说明有对应月份号
  51.         {
  52.    
  53.             printf("总天数是:%d\n",total_days);
  54.         }
  55.         else
  56.         {
  57.    
  58.             printf("输入有误。\n");
  59.         }
  60.         printf("请输入下一个月份名:(按q退出程序)\n");
  61.     }
  62.     printf("程序结束\n");
  63.     return 0;
  64. }
  65. int days(char * input)
  66. {
  67.    
  68.     int i;
  69.     int total = 0;
  70.     int num = 0;
  71.     input[0] = toupper(input[0]);//第一个字母转成大写
  72.     for (i = 1; input[i] != '\0'; i++) //循环到输入为止,都转为小写字母
  73.     {
  74.    
  75.         input[i] = tolower(input[i]);
  76.     }
  77.     for (i = 0; i < 12; i++)
  78.     {
  79.    
  80.         if (strcmp(input, months[i].name) == 0) //和结构数组每一个元素的名字比较
  81.         {
  82.    
  83.             num = months[i].monumb;//找到对应月份,把月份号赋值给num
  84.             break;
  85.         }
  86.     }
  87.     if (num == 0) //没有对应月份号
  88.     {
  89.    
  90.         total = 0;
  91.     }
  92.     else
  93.     {
  94.    
  95.         for (i = 0; i < num; i++) //有对应月份号
  96.         {
  97.    
  98.             total += months[i].days;//求和
  99.         }
  100.     }
  101.     return total;//返回总天数
  102. }
复制代码
输入示例:
  1. 请输入月份名:(按q退出程序)
  2. febRuary
  3. 总天数是:59
  4. 请输入下一个月份名:(按q退出程序)
  5. q
  6. 程序结束
复制代码
2.exercise2.c

编写一个函数,提示用户输入日、月和年。月份可以是月份号、月份名或月份名缩写。然后该程序应返回一年中到用户指定日子(包括这一天)的总天数。
  1. //exercise14.2
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. struct month {
  6.    
  7.     char name[20];
  8.     char abbrev[4];
  9.     int days;
  10.     int monumb;
  11. };//结构模板
  12. struct month months[12] = {
  13.    
  14.     {
  15.    "January", "Jan", 31, 1},
  16.     {
  17.    "February", "Feb", 28, 2},
  18.     {
  19.    "March", "Mar", 31, 3},
  20.     {
  21.    "April", "Apr", 30, 4},
  22.     {
  23.    "May", "May", 31, 5},
  24.     {
  25.    "June", "Jun", 30, 6},
  26.     {
  27.    "July", "Jul", 31, 7},
  28.     {
  29.    "August", "Aug", 31, 8},
  30.     {
  31.    "September", "Sep", 30, 9},
  32.     {
  33.    "October", "Oct", 31, 10},
  34.     {
  35.    "November", "Nov", 3, 11},
  36.     {
  37.    "December", "Dec", 31, 12}
  38. };//结构数组
  39. int days(int day, char * input);//获取月份名,计算天数
  40. void leapyear(int year);
  41. int main(void)
  42. {
  43.    
  44.         int day, year;
  45.     char input[20];
  46.     int total_days;
  47.     printf("请输入日,月份名,年:(按q退出程序)\n");
  48.     while (scanf("%d %s %d", &day, input, &year) == 3 && input[0] != 'q')
  49.     {
  50.    
  51.             leapyear(year);
  52.         total_days = days(day, input);//调用函数
  53.         if (total_days > 0) //总天数大于0,说明有对应月份号
  54.         {
  55.    
  56.             printf("总天数是:%d\n",total_days);
  57.         }
  58.         else
  59.         {
  60.    
  61.             printf("输入有误。\n");
  62.         }
  63.         months[1].days = 28;//将2月份重置为28天
  64.         printf("请输入下一个日,月份名,年:(按q退出程序)\n");
  65.     }
  66.     printf("程序结束\n");
  67.     return 0;
  68. }
  69. int days(int day, char * input)
  70. {
  71.    
  72.     int i;
  73.     int total = 0;
  74.     int num = 0;
  75.     input[0] = toupper(input[0]);//第一个字母转成大写
  76.     for (i = 1; input[i] != '\0'; i++) //循环到输入为止,都转为小写字母
  77.     {
  78.    
  79.         input[i] = tolower(input[i]);
  80.     }
  81.     for (i = 0; i < 12; i++)
  82.     {
  83.    
  84.         if (strcmp(input, months[i].name) == 0) //和结构数组每一个元素的名字比较
  85.         {
  86.    
  87.             num = months[i].monumb;//找到对应月份,把月份号赋值给num
  88.             break;
  89.         }
  90.     }
  91.     if (num == 0) //没有对应月份号
  92.     {
  93.    
  94.         total = 0;
  95.     }
  96.     else
  97.     {
  98.    
  99.         for (i = 0; i < num; i++) //有对应月份号
  100.         {
  101.    
  102.             total += months[i].days;//求和
  103.         }
  104.         total += day;//再加上日期数
  105.     }
  106.     return total;//返回总天数
  107. }
  108. void leapyear(int year)
  109. {
  110.    
  111.         if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//判断是否为闰年
  112.         {
  113.    
  114.                 months[1].days = 29;//闰年2月为29天
  115.         }
  116. }
复制代码
输出示例:
  1. 请输入日,月份名,年:(按q退出程序)
  2. 1 march 2020
  3. 总天数是:92
  4. 请输入下一个日,月份名,年:(按q退出程序)
  5. 1 march 2019
  6. 总天数是:91
  7. 请输入下一个日,月份名,年:(按q退出程序)
  8. q
  9. 程序结束
复制代码
没有做过多的输入验证,默认是按照正常输入。
3.exercise3.c

修改程序manybook.c中的图书目录程序,使其按照输入图书的顺序输出图书的信息,然后按照书名的字母顺序输出图书的信息,末了按照价格的升序输出图书的信息。
  1. //exercise14.3
  2. #include <stdio.h>
  3. #include <string.h>
  4. char * s_gets(char * st, int n);
  5. #define MAXTITL   40
  6. #define MAXAUTL   40
  7. #define MAXBKS   100              /* 书籍的最大数量  */
  8. struct book {
  9.                         /* 建立book模板   */
  10.     char title[MAXTITL];
  11.     char author[MAXAUTL];
  12.     float value;
  13. };
  14. void print(struct book library[MAXBKS], int count);//原样打印
  15. void print_al(struct book library[MAXBKS], int count);//按字母顺序打印
  16. void print_price(struct book library[MAXBKS], int count);//按价格升序打印
  17. int main(void)
  18. {
  19.    
  20.     struct book library[MAXBKS]; /* book类型的结构数组 */
  21.     int count = 0;
  22.    
  23.     printf("请输入书名:\n");
  24.     printf("在开始处按enter键结束程序。\n");
  25.     while (count < MAXBKS && s_gets(library[count].title, MAXTITL) != NULL
  26.            && library[count].title[0] != '\0')
  27.     {
  28.    
  29.         printf("输入作者名:\n");
  30.         s_gets(library[count].author, MAXAUTL);
  31.         printf("输入书的价格\n");
  32.         scanf("%f", &library[count++].value);
  33.         while (getchar() != '\n')
  34.             continue;          /* 清理输入行  */
  35.         if (count < MAXBKS)
  36.             printf("输入下一本书的名字:\n");
  37.     }
  38.    
  39.     if (count > 0)
  40.     {
  41.    
  42.             printf("这是你的书的顺序:\n");
  43.         print(library, count);
  44.         print_al(library, count);
  45.         print_price(library, count);
  46.     }
  47.     else
  48.             printf("没有书。\n");
  49.    
  50.     return 0;
  51. }
  52. char * s_gets(char * st, int n)
  53. {
  54.    
  55.     char * ret_val;
  56.     char * find;
  57.    
  58.     ret_val = fgets(st, n, stdin);
  59.     if (ret_val)
  60.     {
  61.    
  62.         find = strchr(st, '\n');   // 查找换行符
  63.         if (find)                  // 如果地址不是NULL,
  64.             *find = '\0';          // 在此处放置一个空字符
  65.         else
  66.             while (getchar() != '\n')
  67.                 continue;          // 处理输入行中剩余的字符
  68.     }
  69.     return ret_val;
  70. }
  71. void print(struct book library[MAXBKS], int count)
  72. {
  73.    
  74.         int index;
  75.    
  76.         for (index = 0; index < count; index++)
  77.         {
  78.    
  79.                 printf("%s by %s: $%.2f\n", library[index].title,
  80.                    library[index].author, library[index].value);
  81.         }        
  82. }
  83. void print_al(struct book library[MAXBKS], int count)
  84. {
  85.    
  86.         struct book temp;//标记
  87.         int index;
  88.     int i;
  89.    
  90.     printf("书按字母顺序输出:\n");
  91.         for (index = 0; index < count - 1; index++)//冒泡排序
  92.     {
  93.    
  94.         for (i = index + 1; i < count; i++)
  95.         {
  96.    
  97.                 if (strcmp(library[index].title, library[i].title) > 0)
  98.                                 //后面的书的字母排在前面的书的前面
  99.                         {
  100.    
  101.                                 temp = library[index];//交换
  102.                                 library[index] = library[i];
  103.                                 library[i] = temp;
  104.                         }
  105.                 }
  106.         }
  107.         print(library, count);//打印排序后的
  108. }
  109. void print_price(struct book library[MAXBKS], int count)
  110. {
  111.    
  112.         struct book temp;
  113.         int index;
  114.     int i;
  115.    
  116.     printf("书按价格顺序输出:\n");
  117.         for (index = 0; index < count - 1; index++)
  118.     {
  119.    
  120.         for (i = index + 1; i < count; i++)
  121.         {
  122.    
  123.                 if (library[index].value > library[i].value)
  124.                         //前面的书价格高于后面的书
  125.                         {
  126.    
  127.                                 temp = library[index];
  128.                                 library[index] = library[i];
  129.                                 library[i] = temp;
  130.                         }
  131.                 }
  132.         }
  133.         print(library, count);
  134. }
复制代码
输出示例:
  1. 请输入书名:
  2. 在开始处按enter键结束程序。
  3. c study
  4. 输入作者名:
  5. sttphen
  6. 输入书的价格
  7. 56
  8. 输入下一本书的名字:
  9. python
  10. 输入作者名:
  11. peter
  12. 输入书的价格
  13. 23
  14. 输入下一本书的名字:
  15. java
  16. 输入作者名:
  17. lily
  18. 输入书的价格
  19. 35
  20. 输入下一本书的名字:
  21. 这是你的书的顺序:
  22. c study by sttphen: $56.00
  23. python by peter: $23.00
  24. java by lily: $35.00
  25. 书按字母顺序输出:
  26. c study by sttphen: $56.00
  27. java by lily: $35.00
  28. python by peter: $23.00
  29. 书按价格顺序输出:
  30. python by peter: $23.00
  31. java by lily: $35.00
  32. c study by sttphen: $56.00
复制代码
4.exercise4.c

编写一个程序,创建一个有两个成员的结构模板:
a. 第1个成员是社会保险号,第2个成员是一个有3个成员的结构,第1个成员代表名,第2个成员代表中间名,第3个成员表现姓。创建并初始化一个内含5个该范例结构的数组。该程序以下面的格式打印数据:
Dribble, Flossie M. – 302039823
如果有中间名,只打印它的第1个字母,背面加一个点(.); 如果没有中间名,则不用打印点。编写一个程序举行打印,把结构数组转达给这个函数。
b. 修改a部分,转达结构的值而不是结构的地址。
  1. //exercise14.4a
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define LEN 20
  5. struct names {
  6.    
  7.         char fname[LEN];
  8.         char mname[LEN];
  9.         char lname[LEN];
  10. };
  11. struct message {
  12.    
  13.         char sonum[LEN];
  14.         struct names name;
  15. };
  16. char * s_gets(char * st, int n);
  17. void print(const struct message num[], int n);
  18. int main(void)
  19. {
  20.    
  21.         struct message num[5];//结构数组
  22.         int count = 0;
  23.        
  24.         printf("请输入保险号:\n");
  25.     printf("在开始处按enter键结束程序。\n");
  26.     while (count < 5 && s_gets(num[count].sonum, LEN) && num[count].sonum[0] != '\0')
  27.     {
  28.    //输入不超过5个,或者直接输入enter退出
  29.             printf("请输入名:\n");
  30.             s_gets(num[count].name.fname, LEN);
  31.             printf("请输入中间名:\n");
  32.             s_gets(num[count].name.mname, LEN);
  33.             printf("请输入姓:\n");
  34.             s_gets(num[count].name.lname, LEN);
  35.             if (count < 5)//输入不超过5个,继续输入
  36.             {
  37.           
  38.                     printf("请输入下一个保险号:\n");
  39.                 }
  40.                 count++;
  41.         }
  42.         if (count > 0)//输入了成员
  43.         {
  44.    
  45.                 printf("以下是所有成员:\n");
  46.                 print(num, count);       
  47.         }
  48.         else//没输入成员
  49.         {
  50.    
  51.                 printf("没有成员。\n");
  52.         }
  53.         printf("程序结束。\n");
  54.        
  55.         return 0;
  56. }
  57. char * s_gets(char * st, int n)
  58. {
  59.    
  60.     char * ret_val;
  61.     char * find;
  62.    
  63.     ret_val = fgets(st, n, stdin);
  64.     if (ret_val)
  65.     {
  66.    
  67.         find = strchr(st, '\n');   // 查找换行符
  68.         if (find)                  // 如果地址不是NULL,
  69.             *find = '\0';          // 在此处放置一个空字符
  70.         else
  71.             while (getchar() != '\n')
  72.                 continue;          // 处理输入行中剩余的字符
  73.     }
  74.     return ret_val;
  75. }
  76. void
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表