From b931a3f5fe0d4fc22f993253a0bf7981f1ef17f7 Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 11 Jul 2023 01:33:28 +0800 Subject: [PATCH] =?UTF-8?q?day6=20homework:=20=E6=95=B0=E7=BB=84=E5=92=8C?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day6/homework/h1.c | 40 ++++++++++++ day6/homework/h10.c | 146 ++++++++++++++++++++++++++++++++++++++++++++ day6/homework/h2.c | 37 +++++++++++ day6/homework/h3.c | 47 ++++++++++++++ day6/homework/h4.c | 59 ++++++++++++++++++ day6/homework/h5.c | 39 ++++++++++++ day6/homework/h6.c | 26 ++++++++ day6/homework/h7.c | 29 +++++++++ day6/homework/h8.c | 68 +++++++++++++++++++++ day6/homework/h9.c | 69 +++++++++++++++++++++ 10 files changed, 560 insertions(+) create mode 100644 day6/homework/h1.c create mode 100644 day6/homework/h10.c create mode 100644 day6/homework/h2.c create mode 100644 day6/homework/h3.c create mode 100644 day6/homework/h4.c create mode 100644 day6/homework/h5.c create mode 100644 day6/homework/h6.c create mode 100644 day6/homework/h7.c create mode 100644 day6/homework/h8.c create mode 100644 day6/homework/h9.c diff --git a/day6/homework/h1.c b/day6/homework/h1.c new file mode 100644 index 0000000..cdee5c1 --- /dev/null +++ b/day6/homework/h1.c @@ -0,0 +1,40 @@ +// 编写程序,键盘输入字符串是否为回文字符串 +#include +#include + +int isPalindrome(const char *str); + +int main() +{ + char str[32]; + printf("请输入字符串:"); + scanf("%s", str); + + if (isPalindrome(str)) + printf("%s 是回文字符串", str); + else + printf("%s 不是回文字符串", str); + + return 0; +} + +int isPalindrome(const char *str) +{ + // int len = strlen(str); + // 用以实现 strlen 的相同功能 + int len = 0; + while (str[len]) + len++; + printf("%d\n", len); + + int i = 0, j = len - 1; + while (i < j) + { + if (str[i] != str[j]) + return 0; // 返回 0 为假 + i++; + j--; + } + + return 1; // 返回非 0 为真 +} \ No newline at end of file diff --git a/day6/homework/h10.c b/day6/homework/h10.c new file mode 100644 index 0000000..b6490ff --- /dev/null +++ b/day6/homework/h10.c @@ -0,0 +1,146 @@ +// 编程设计打字游戏: +// 1)随机函数 +// A.srand((unsigned)time(NULL)); +// 以当前时间为准,设置随机种子 +// 注意:此函数,在每次开始游戏后调用一次即可 +// B.ch = rand(); +// 注意:rand()函数,每调用一次,产生一个随机数字 + +// 2)获得键值函数 +// ch=mygetch(); //无需按下回车,可直接获得键盘上按下的键值 + +// 还可以自己封装一个无阻塞,无回显的getch,实现如下: +// #include +// #include +// char +// mygetch() +// { +// struct termios oldt, newt; +// char ch; +// tcgetattr(STDIN_FILENO, &oldt); +// newt = oldt; +// newt.c_lflag &= ~(ICANON | ECHO); +// tcsetattr(STDIN_FILENO, TCSANOW, &newt); +// ch = getchar(); +// tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +// return ch; +// } +// 3)时间函数 +// start_time=time(NULL); +// edn_time = time(NULL); +// //可以返回系统当前时间,以秒为单位 + +// 4)system("clear");//清空屏幕 + +// 5)所需头文件 +// #include +// #include +// #include +// #include + +#include +#include +#include +#include +#include +#include + +char mygetch(); +void help(); + +int main() +{ + int NUMS_OF_CHARS = 11; // 字符总数 + char ch; // 键盘输入字符 + char str[11] = ""; // 题目字符数组 + int i; + int cnt; // 正确数 + time_t start_time, end_time; // 开始,结束时间 + + help(); // 首次进入游戏时主动显示游戏提示 + ch = mygetch(); // 按任意键开始游戏 + while (1) + { + + // help(); // 首次进入游戏时主动显示游戏提示 (这里如果解注释,则会每次都显示帮助信息) + // ch = mygetch(); // 按任意键开始游戏 + + srand((unsigned)time(NULL)); // 设置随机种子 + for (i = 0; i < NUMS_OF_CHARS - 1; i++) + { + switch (rand() % 3) // 根据每一次的随机值安顿好当前位置的字符类型 + { + case 0: + str[i] = rand() % 10 + '0'; // 0-9的数字 + break; + case 1: + str[i] = rand() % 26 + 'a'; // a-z的数字 + break; + case 2: + str[i] = rand() % 26 + 'A'; // A-Z的数字 + break; + } + } + + // str[NUMS_OF_CHARS - 1] = '\0'; // 末位补上结束符 + printf("%s\n", str); + cnt = 0; // 计数器初始化为 0 + for (i = 0; i < NUMS_OF_CHARS - 1; i++) + { + ch = mygetch(); // 获取每一次的按键输入 + if (i == 0) // 如果第一次按下任意键,则开始计时 + start_time = time(NULL); + if (ch == str[i]) + { + cnt++; // 当按下的键和题目数组对应位置的值一致时,成功计数器加一 + printf("%c", ch); // 打印正确键入的字符 + continue; // 继续下一位置的字符输入 + } + else + { + printf("*"); // 如果输入的键和题目不匹配,则显示为 * + continue; + } + } + + end_time = time(NULL); // 全部输入结束后,记录结束时间 + printf("\n你的打字正确率为 %d %c\n", cnt * 100 / (NUMS_OF_CHARS - 1), '%'); + printf("总共用时为 %lld 秒\n", (long long int)end_time - start_time); + while (1) // 再次获取一下键盘输入 + { + ch = mygetch(); + if (ch == 32) + { + system("clear"); // 清空屏幕 + break; // 按 SPACE (ASCII=32) 跳出小循环,进入大循环,即重新一轮游戏 + } + else if (ch == 27) + return 0; // 按下 ESC (ASCII=27) 键退出游戏 (return 0 --- 程序结束) + } + } + return 0; // 程序结束 +} + +char mygetch() // 无阻塞,无回显的getch +{ + struct termios oldt, newt; + char ch; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + ch = getchar(); + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + return ch; +} + +void help() // 游戏提示信息 +{ + printf("\n *******************************************"); + printf("\n *请按所给字母敲击键盘! *"); + printf("\n *请按任意键开始测试,按下首字母时开始计时!*"); + printf("\n *输入出错则以 * 表示 *"); + printf("\n *按空格键继续游戏 (新一轮游戏) *"); + printf("\n *按 ESC 退出游戏 *"); + printf("\n *******************************************\n"); +} \ No newline at end of file diff --git a/day6/homework/h2.c b/day6/homework/h2.c new file mode 100644 index 0000000..d33a6c7 --- /dev/null +++ b/day6/homework/h2.c @@ -0,0 +1,37 @@ +// 编写程序,键盘输入10位学生的成绩,按大到小排序并输出 +#include + +int main() +{ + float stuScore[10]; + int i = 0; + while (i < 10) + { + printf("请输入第%d位学生的成绩: ", i + 1); + scanf("%f", &stuScore[i]); + i++; + } + + float temp; + int len = sizeof(stuScore) / sizeof(float); + for (int i = 0; i < len - 1; i++) + { + for (int j = 0; j < len - i - 1; j++) + { + if (stuScore[j] < stuScore[j + 1]) + { + temp = stuScore[j]; + stuScore[j] = stuScore[j + 1]; + stuScore[j + 1] = temp; + } + } + } + + for (int i = 0; i < len; i++) + { + printf("%.2f ", stuScore[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/day6/homework/h3.c b/day6/homework/h3.c new file mode 100644 index 0000000..7561604 --- /dev/null +++ b/day6/homework/h3.c @@ -0,0 +1,47 @@ +// 编写程序,随机生成10位学生3门课程成绩,汇总每位学生的总成绩和每门课程的平均成绩。 【提示】随机生成成绩的范围[0, 100] +#include +#include +#include + +int main() +{ + float stuScores[10][3]; // 学生成绩数组 + float sumScores[10]; // 每位学生总成绩 + float avgScores[3]; // 每门课程平均成绩 + + printf("生成随机成绩"); + srand(time(NULL)); // 随机种子 + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 3; j++) + { + float tempRandScore = rand() % 101; + stuScores[i][j] = tempRandScore; + } + } + printf("\n"); + + printf("每位学生的总成绩为: "); + for (int i = 0; i < 10; i++) + { + for (int j = 0; j < 3; j++) + { + sumScores[i] += stuScores[i][j]; + } + printf("%.1f ", sumScores[i]); + } + printf("\n"); + + printf("每门课程的平均成绩为: "); + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 10; j++) + { + avgScores[i] += stuScores[j][i] / 10.0; + } + printf("%.1f ", avgScores[i]); + } + printf("\n"); + + return 0; +} diff --git a/day6/homework/h4.c b/day6/homework/h4.c new file mode 100644 index 0000000..e3960ce --- /dev/null +++ b/day6/homework/h4.c @@ -0,0 +1,59 @@ +// 编写程序,扩展第3题,为10位学生输入姓名和课程名,输出学生名和总成绩、课程名和平均成绩 。 +#include +#include +#include +#include + +const int stuNum = 10; // 定义学生数 +const int courseNum = 3; // 定义课程数 + +int main() +{ + char stuNames[stuNum][20]; // 学生姓名数组 + char courseNames[courseNum][20]; // 课程名数组 + float stuScores[stuNum][courseNum]; // 学生成绩数组 + float sumScores[stuNum]; // 每位学生总成绩 + float avgScores[courseNum]; // 每门课程平均成绩 + + // printf("生成随机成绩"); + srand(time(NULL)); // 随机种子 + for (int i = 0; i < stuNum; i++) + { + printf("请输入第 %d 位学生的姓名: ", i + 1); + fgets(stuNames[i], sizeof(stuNames[i]), stdin); + stuNames[i][strlen(stuNames[i]) - 1] = '\0'; // 消除结尾的换行 + for (int j = 0; j < courseNum; j++) + { + printf("请输入课程名: "); + fgets(courseNames[j], sizeof(courseNames[j]), stdin); + courseNames[j][strlen(courseNames[j]) - 1] = '\0'; + float tempRandScore = rand() % 101; + stuScores[i][j] = tempRandScore; + } + } + printf("\n"); + + // printf("每位学生的总成绩为: "); + for (int i = 0; i < stuNum; i++) + { + for (int j = 0; j < courseNum; j++) + { + sumScores[i] += stuScores[i][j]; + } + printf("%s 的总成绩为: %.1f\n", stuNames[i], sumScores[i]); + } + printf("\n"); + + // printf("每门课程的平均成绩为: "); + for (int i = 0; i < courseNum; i++) + { + for (int j = 0; j < stuNum; j++) + { + avgScores[i] += stuScores[j][i] / stuNum; + } + printf("%s 课的平均成绩为: %.1f\n", courseNames[i], avgScores[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/day6/homework/h5.c b/day6/homework/h5.c new file mode 100644 index 0000000..6c07c69 --- /dev/null +++ b/day6/homework/h5.c @@ -0,0 +1,39 @@ +// 编写程序,键盘输入5个整数,输出它们的所有排列组合 +#include + +void fun1(int *nums); + +int main() +{ + int nums[5]; + + printf("请输入 5 个整数: "); + int i = 0; + while (i < 5) + { + scanf("%d", &nums[i++]); + } + + // 笨办法 + fun1(nums); + + return 0; +} + +void fun1(int *nums) +{ + + int a, b, c, d, e; + for (a = 0; a < 5; a++) + for (b = 0; b < 5; b++) + for (c = 0; c < 5; c++) + for (d = 0; d < 5; d++) + for (e = 0; e < 5; e++) + if ( + a != b && a != c && a != d && a != e && + b != c && b != d && b != e && + c != d && c != e && + d != e) + printf("%d%d%d%d%d\n", nums[a], nums[b], nums[c], nums[d], nums[e]); + return; +} \ No newline at end of file diff --git a/day6/homework/h6.c b/day6/homework/h6.c new file mode 100644 index 0000000..17b2a5b --- /dev/null +++ b/day6/homework/h6.c @@ -0,0 +1,26 @@ +// 编写程序并设计函数, 接收两个整数参数,输出这两个整数的所有公约数。 【提示】在main函数中通过键盘输入两个整数,并调用设计的函数。 +#include + +void commonDivisors(int numA, int numB); + +int main() +{ + int a, b; + printf("请输入两个整数: "); + scanf("%d %d", &a, &b); + commonDivisors(a, b); + + return 0; +} + +void commonDivisors(int numA, int numB) +{ + int min = numA < numB ? numA : numB; + while (min) + { + if (numA % min == 0 && numB % min == 0) + printf("%d ", min); + min--; + } + printf("\n"); +} \ No newline at end of file diff --git a/day6/homework/h7.c b/day6/homework/h7.c new file mode 100644 index 0000000..c7bb5a2 --- /dev/null +++ b/day6/homework/h7.c @@ -0,0 +1,29 @@ +// 编写程序并设计函数, 接收一个整数值,验证这个数值是否为水仙花数。 【提示】水仙花数:每一位数字的3次幂累加的结果和这个数本身相同 +#include +#include + +int isNumberOfDaffodils(int); + +int main() +{ + int n; + printf("请输入一个三位整数: "); + scanf("%d", &n); + if (isNumberOfDaffodils(n)) + printf("%d 是水仙花数", n); + else + printf("%d 不是水仙花数", n); + + return 0; +} + +int isNumberOfDaffodils(int num) +{ + int i, j, k; + i = num / 100; + j = (num % 100) / 10; + k = num % 10; + if ((pow(i, 3) + pow(j, 3) + pow(k, 3)) == num) + return 1; // 是水仙花数返回非 0 + return 0; +} \ No newline at end of file diff --git a/day6/homework/h8.c b/day6/homework/h8.c new file mode 100644 index 0000000..5b58429 --- /dev/null +++ b/day6/homework/h8.c @@ -0,0 +1,68 @@ +// 编写程序并设计函数,接收年、月、日三个整数,函数返回是这一年的第几天 【提示】使用数组的方式计算,将每月的天数放在一个数组中 +#include + +int todayIs(int year, int month, int day); // 数组加 switch-case 穿透方法 +int todayIsFun2(int year, int month, int day); // 数组循环方法 + +int main() +{ + int y, m, d; + printf("请输入年,月,日(用,分隔): "); + scanf("%d,%d,%d", &y, &m, &d); + printf("今天是 %d 年的第 %d 天\n", y, todayIsFun2(y, m, d)); + + return 0; +} + +int todayIs(int year, int month, int day) +{ + int days = day; + int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + switch (month) + { + case 12: + days += months[10]; + case 11: + days += months[9]; + case 10: + days += months[8]; + case 9: + days += months[7]; + case 8: + days += months[6]; + case 7: + days += months[5]; + case 6: + days += months[4]; + case 5: + days += months[3]; + case 4: + days += months[2]; + case 3: + days += months[1]; + case 2: + days += months[0]; + } + + // 判断是否闰年?加一天:不加 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + days += 1; + + return days; +} + +int todayIsFun2(int year, int month, int day) +{ + int days = day; + int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + for (int i = 0; i < month - 1; i++) + { + days += months[i]; + } + + // 判断是否闰年?加一天:不加 + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + days += 1; + + return days; +} \ No newline at end of file diff --git a/day6/homework/h9.c b/day6/homework/h9.c new file mode 100644 index 0000000..b8af654 --- /dev/null +++ b/day6/homework/h9.c @@ -0,0 +1,69 @@ +// 请编程,设计计算个人所得税的函数,返回应缴纳所得税的金额。免税额为3500,7级超额累进税率: +// 全月应纳税所得额 税率 速算扣除数(元) +// 全月应纳税额不超过1500元 3 % +// 0 +// 全月应纳税额超过1500元至4500元 10 % +// 105 +// 全月应纳税额超过4500元至9000元 20 % +// 555 +// 全月应纳税额超过9000元至35000元 25 % +// 1005 +// 全月应纳税额超过35000元至55000元 30 % +// 2755 +// 全月应纳税额超过55000元至80000元 35 % +// 5505 +// 全月应纳税额超过80000元 45 % +// 13505 +#include + +void *result(int, double *, int *); + +int main() +{ + int pretexIncome; // 税前收入 + double rates[8] = {0, 0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45}; // 税率数组 + int deducts[8] = {0, 0, 105, 555, 1005, 2755, 5505, 13505}; // 速算扣除数数组 + + printf("请输入你的税前收入: "); + scanf("%d", &pretexIncome); + + result(pretexIncome, rates, deducts); + + // int *res = result(pretexIncome, rates, deducts); + // printf("%d%d", res[0], res[1]); + // printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", result(pretexIncome, rates, deducts)[1], result(pretexIncome, rates, deducts)[0]); + + return 0; +} + +void *result(int pretexIncomeArgs, double *rateArgs, int *deductArgs) +{ + int level; // 税率等级 + int tex; // 缴纳税额 + int aftertexIncome; // 税后收入 + int diff = pretexIncomeArgs - 3500; // 征税差值 + if (diff <= 0) + level = 0; + else if (diff > 0 && diff <= 1500) + level = 1; + else if (diff > 1500 && diff <= 4500) + level = 2; + else if (diff > 4500 && diff <= 9000) + level = 3; + else if (diff > 9000 && diff <= 35000) + level = 4; + else if (diff > 35000 && diff <= 55000) + level = 5; + else if (diff > 55000 && diff <= 80000) + level = 6; + else if (diff > 80000) + level = 7; + + tex = diff * rateArgs[level] - deductArgs[level]; + aftertexIncome = pretexIncomeArgs - tex; + + printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", aftertexIncome, tex); + + // int outNums[2] = {(int *)aftertexIncome, (int *)tex}; + // return *outNums; // 返回结果数组 +} \ No newline at end of file