欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > C语言程序设计:现代设计方法习题笔记《chapter5》下篇

C语言程序设计:现代设计方法习题笔记《chapter5》下篇

2024/10/30 6:36:38 来源:https://blog.csdn.net/weixin_44748012/article/details/143258264  浏览:    关键词:C语言程序设计:现代设计方法习题笔记《chapter5》下篇

第七题

                        b7a518bac16e4a3ba21c539655666c9d.png

题目分析:求最大最小值转换为条件判断问题,最大值有四种可能,最小值相应有三种情况,给出下列代码。

示例代码:

#include <stdio.h>int main() {int num1, num2, num3, num4; // 定义四个变量来存储输入的数字int max, min; // 定义变量来保存最大值和最小值printf("Enter four integere: ");scanf_s("%d %d %d %d", &num1, &num2, &num3, &num4);// 初始化最大值和最小值为第一个数字max = num1;min = num1;if (num2 > max || num3 > max || num4 > max) {max = num2 > num3 ? (num2 > num4 ? num2 : num4) : (num3 > num4 ? num3 : num4);}if (num2 < min || num3 < min || num4 < min) {min = num2 < num3 ? (num2 < num4 ? num2 : num4) : (num3 < num4 ? num3 : num4);}// 输出结果printf("Largest: %d\n", max);printf("Smallest: %d\n", min);return 0;
}

输出

        ​​​​​​​        ​​​​​​​        ​​​​​​​        733948c1345441438afdef04040b4918.png

第八题

 30bb35713858404b9fad2a86522a949a.png

 题目分析:根据输入时间,匹配最接近的下一时刻,然后匹配对应的到达时间,得出代码。

示例代码

#include <stdio.h>
#include <string.h>int main() {int hour, minu, input;char found[100], time[100];// 输入时间printf("Enter a 24-hour time: ");scanf_s("%d:%d", &hour, &minu);input = hour * 60 + minu;// 飞机起飞时间int fly_time1 = 8 * 60;int fly_time2 = 9 * 60 + 43;int fly_time3 = 11 * 60 + 19;int fly_time4 = 12 * 60 + 47;int fly_time5 = 14 * 60;int fly_time6 = 15 * 60 + 45;int fly_time7 = 19 * 60;int fly_time8 = 21 * 60 + 45;const char* time1 = "8:00 a.m.";const char* time2 = "9:43 a.m.";const char* time3 = "11:19 a.m.";const char* time4 = "12:47 p.m.";const char* time5 = "14:00 p.m.";const char* time6 = "15:45 p.m.";const char* time7 = "19:00 p.m.";const char* time8 = "21:45 p.m.";int closestGreater = -1; // 初始化为一个不可能的值// 检查每一个单独的值if (fly_time1 > input) {closestGreater = fly_time1;strcpy_s(time, time1);strcpy_s(found, "10:16 a.m.");}if (fly_time2 > input && (closestGreater == -1 || fly_time2 - input < closestGreater - input)) {closestGreater = fly_time2;strcpy_s(time, time2);strcpy_s(found, "11:52 a.m.");}if (fly_time3 > input && (closestGreater == -1 || fly_time3 - input < closestGreater - input)) {closestGreater = fly_time3;strcpy_s(time, time3);strcpy_s(found, "1:31 p.m.");}if (fly_time4 > input && (closestGreater == -1 || fly_time4 - input < closestGreater - input)) {closestGreater = fly_time4;strcpy_s(time, time4);strcpy_s(found, "3:00 p.m.");}if (fly_time5 > input && (closestGreater == -1 || fly_time5 - input < closestGreater - input)) {closestGreater = fly_time5;strcpy_s(time, time5);strcpy_s(found, "4:08 p.m.");}if (fly_time6 > input && (closestGreater == -1 || fly_time6 - input < closestGreater - input)) {closestGreater = fly_time6;strcpy_s(time, time6);strcpy_s(found, "5:05 p.m.");}if (fly_time7 > input && (closestGreater == -1 || fly_time7 - input < closestGreater - input)) {closestGreater = fly_time7;strcpy_s(time, time7);strcpy_s(found, "9:20 p.m.");}if (fly_time8 > input && (closestGreater == -1 || fly_time8 - input < closestGreater - input)) {closestGreater = fly_time8;strcpy_s(time, time8);strcpy_s(found, "11:58 p.m.");}if (closestGreater != -1) {printf("Closest departure time is %s, arriving at %s\n", time, found);}else {printf("没有找到大于输入时间的飞行时间。\n");}return 0;
}

输出

        ​​​​​​​        ​​​​​​​     b689646b03b74fd790a3755cb26833c7.png

第九题

        ​​​​​​​        97fe098f4ae0472c9c6f3984a2319063.png

示例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>// 函数声明
int compareDates(int m1, int d1, int y1, int m2, int d2, int y2);int main() {int month1, day1, year1;int month2, day2, year2;// 提示用户输入第一个日期printf("Enter first date(mm/dd/yy): ");if (scanf_s("%d/%d/%d", &month1, &day1, &year1) != 3) {printf("输入错误。\n");return 1;}// 提示用户输入第二个日期printf("Enter second date(mm/dd/yy): ");if (scanf_s("%d/%d/%d", &month2, &day2, &year2) != 3) {printf("输入错误。\n");return 1;}// 比较两个日期if (compareDates(month1, day1, year1, month2, day2, year2)) {printf("%02d/%02d/%02d is earlier than %02d/%02d/%02d\n", month1, day1, year1, month2, day2, year2);}else {printf("%02d/%02d/%02d is earlier than %02d/%02d/%02d\n", month2, day2, year2, month1, day1, year1);}return 0;
}// 比较两个日期,返回 1 表示第一个日期更早,0 表示第二个日期更早
int compareDates(int m1, int d1, int y1, int m2, int d2, int y2) {if (y1 < y2 || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 < d2)) {return 1;}else if (y1 > y2 || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2)) {return 0;}else {return 0; // 如果两个日期相同}
}

输出

        ​​​​​​​        ​​​​​​​        66779f6c197740cb963eacec17cbf3a6.png

 

第十题

        e085fd6b9369423b8e1fe9677f738a77.png

示例代码

#include <stdio.h>int main() {int score; // 存储成绩分数// 提示用户输入成绩分数printf("Enter a numerical grade: ");scanf_s("%d", &score);// 检查输入的成绩分数是否在合理范围内if (score < 0 || score > 100) {printf("错误:成绩分数应在 0 到 100 之间。\n");return 1; // 返回错误代码}// 使用 switch 语句判断成绩等级switch (score / 10) {case 10:printf("Letter grade: A\n");break;case 9:       printf("Letter grade: A\n");break;case 8:printf("Letter grade: B\n");break;case 7:printf("Letter grade: C\n");break;case 6:printf("Letter grade: D\n");break;default:printf("Letter grade: F\n");break;}return 0;
}

输出

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        fa28de508a2840c8b51938fe32f5b5f2.png

第十一题

        271500e17b5f4f2db34f1ea679ab8e29.png 

示例代码

 

#include <stdio.h>
#include <string.h>int main() {int number; // 存储输入的两位数char result[50] = ""; // 存储英文单词的结果// 提示用户输入一个两位数printf("Enter a two-digit number: ");if (scanf_s("%d", &number) != 1) {printf("输入错误。\n");return 1;}// 检查输入的数字是否在合理范围内if (number < 10 || number > 99) {printf("错误:输入的数字应为两位数(10-99)。\n");return 1; // 返回错误代码}// 获取十位和个位上的数字int tens = number / 10;int ones = number % 10;// 特殊处理 11 到 19 的情况if (tens == 1 && ones >= 1) {switch (number) {case 11:strcpy_s(result, sizeof(result), "eleven");break;case 12:strcpy_s(result, sizeof(result), "twelve");break;case 13:strcpy_s(result, sizeof(result), "thirteen");break;case 14:strcpy_s(result, sizeof(result), "fourteen");break;case 15:strcpy_s(result, sizeof(result), "fifteen");break;case 16:strcpy_s(result, sizeof(result), "sixteen");break;case 17:strcpy_s(result, sizeof(result), "seventeen");break;case 18:strcpy_s(result, sizeof(result), "eighteen");break;case 19:strcpy_s(result, sizeof(result), "nineteen");break;}}else {// 处理其他情况switch (tens) {case 2:strcpy_s(result, sizeof(result), "twenty");break;case 3:strcpy_s(result, sizeof(result), "thirty");break;case 4:strcpy_s(result, sizeof(result), "forty");break;case 5:strcpy_s(result, sizeof(result), "fifty");break;case 6:strcpy_s(result, sizeof(result), "sixty");break;case 7:strcpy_s(result, sizeof(result), "seventy");break;case 8:strcpy_s(result, sizeof(result), "eighty");break;case 9:strcpy_s(result, sizeof(result), "ninety");break;}// 如果个位数不为零,追加连字符和个位数的英文单词if (ones != 0) {char temp[10];strcpy_s(temp, sizeof(temp), "-");strcat_s(result, sizeof(result), temp);switch (ones) {case 1:strcat_s(result, sizeof(result), "one");break;case 2:strcat_s(result, sizeof(result), "two");break;case 3:strcat_s(result, sizeof(result), "three");break;case 4:strcat_s(result, sizeof(result), "four");break;case 5:strcat_s(result, sizeof(result), "five");break;case 6:strcat_s(result, sizeof(result), "six");break;case 7:strcat_s(result, sizeof(result), "seven");break;case 8:strcat_s(result, sizeof(result), "eight");break;case 9:strcat_s(result, sizeof(result), "nine");break;}}}// 对于 10 的情况if (number == 10) {strcpy_s(result, sizeof(result), "ten");}// 输出结果printf("You entered the number:%s\n", result);return 0;
}

输出

        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​       1acebf2d4d424e9eb4b46744c2bef137.png

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com