高级程序语言设计第五次个人作业

news/2025/11/14 0:33:50/文章来源:https://www.cnblogs.com/fantasyaaazzz/p/19219998
这个作业属于哪个课程 <班级的链接>
这个作业要求在哪里 <作业链接>
学号 092300303
姓名 池博洋

@

目录
  • 一、设计作业
    • 1.使得程序遇到空格符时结束
    • 2.文件重定向,小写转大写
    • 3. 四则运算计算器
    • 4.混合读入字符、数值
  • 8.11
    • 3
    • 4
    • 5
    • 6
    • 7
  • 9.11
    • 1
    • 2
    • 3
    • 4
    • 8
    • 9
    • 11
  • 程序设计:多源代码文件
  • 编写一个程序,在该程序中输出主函数内定义的变量的地址,以及函数中定义的同名变量的地址。

作业基本信息

一、设计作业

1.使得程序遇到空格符时结束

点击查看代码
#include <stdio.h>int main() {char c;while ((c = getchar()) != ' ') {putchar(c);}return 0;
}

结果:
image

2.文件重定向,小写转大写

点击查看代码
#include <stdio.h>
#include <ctype.h>int main() {FILE *fin = fopen("in.txt", "r");FILE *fout = fopen("out.txt", "w");char c;while ((c = fgetc(fin)) != EOF) {if (islower(c)) {c = toupper(c);}fputc(c, fout);}fclose(fin);fclose(fout);return 0;
}

结果:
image

image

3. 四则运算计算器

点击查看代码
#include <stdio.h>int main() {double a, b;char op;if (scanf("%lf %c %lf", &a, &op, &b) == 3) {switch (op) {case '+':printf("%.2f\n", a + b);break;case '-':printf("%.2f\n", a - b);break;case '*':printf("%.2f\n", a * b);break;case '/':if (b != 0) {printf("%.2f\n", a / b);} else {printf("division by zero\n");}break;default:printf("wrong input\n");}} else {printf("wrong input\n");}return 0;
}

结果:
image

4.混合读入字符、数值

点击查看代码
#include <stdio.h>int main() {char ch1, ch2;int num;// 读取第一个字符(跳过前面的空格和回车)do {ch1 = getchar();} while (ch1 == ' ' || ch1 == '\n' || ch1 == '\t');// 读取整数(scanf会自动跳过空白字符)scanf("%d", &num);// 读取第二个字符(跳过后面的空格和回车)do {ch2 = getchar();} while (ch2 == ' ' || ch2 == '\n' || ch2 == '\t');// 输出格式:字符整数字符(连续输出,没有空格)printf("%c%d%c\n", ch1, num, ch2);return 0;
}

结果:
image

8.11

3

点击查看代码
#include <stdio.h>
#include <ctype.h>int main(void)
{int ch;int upper_count = 0;int lower_count = 0;printf("请输入字符(以EOF结束):\n");while ((ch = getchar()) != EOF){if (isupper(ch))upper_count++;else if (islower(ch))lower_count++;}printf("大写字母个数:%d\n", upper_count);printf("小写字母个数:%d\n", lower_count);return 0;
}

结果:
image

4

点击查看代码
#include <stdio.h>
#include <ctype.h>int main(void)
{int ch;int in_word = 0;int letter_count = 0;printf("请输入文本(以EOF结束):\n");while ((ch = getchar()) != EOF){if (isalpha(ch)){if (!in_word){in_word = 1;letter_count = 0;}letter_count++;}else if (isspace(ch) || ispunct(ch)){if (in_word && letter_count > 0){printf("单词字母数:%d\n", letter_count);in_word = 0;}}}// 处理最后一个单词if (in_word && letter_count > 0){printf("单词字母数:%d\n", letter_count);}return 0;
}

结果:
image

5

点击查看代码
#include <stdio.h>int main(void)
{int guess;int low = 1;int high = 100;char response;printf("Pick an integer from 1 to 100. I will try to guess ");printf("it.\nRespond with a y if my guess is right and with");printf("\nan n if it is wrong.\n");do {guess = (low + high) / 2;printf("Well, then, is it %d?\n", guess);printf("Please respond with y (yes) or n (no): ");// 读取响应并处理换行符response = getchar();// 清除输入缓冲区中的剩余字符(包括换行符)while (getchar() != '\n')continue;if (response == 'y'){printf("I knew I could do it!\n");break;}else if (response == 'n'){printf("Is your number higher or lower than %d?\n", guess);printf("Please respond with h (higher) or l (lower): ");response = getchar();// 再次清除输入缓冲区while (getchar() != '\n')continue;if (response == 'h')low = guess + 1;      // 数字比猜测值大else if (response == 'l')high = guess - 1;     // 数字比猜测值小elseprintf("Please enter h or l.\n");}else{printf("Please enter y or n.\n");}// 检查是否出现矛盾(用户可能欺骗了程序)if (low > high){printf("I think you are cheating! Let's start over.\n");low = 1;high = 100;}} while (response != 'y');return 0;
}

结果:
image

6

点击查看代码
#include <stdio.h>
#include <ctype.h>// 函数声明
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);int main(void)
{char choice;while ((choice = get_choice()) != 'q'){switch (choice){case 'a': printf("Buy low, sell high.\n");break;case 'b': putchar('\a');  /* ANSI 响铃字符 */break;case 'c': count();break;default: printf("Program error!\n");break;}}printf("Bye.\n");return 0;
}void count(void)
{int n, i;printf("Count how far? Enter an integer:\n");n = get_int();for (i = 1; i <= n; i++)printf("%d\n", i);// 清除输入缓冲区中的剩余字符while (getchar() != '\n')continue;
}char get_choice(void)
{int ch;printf("Enter the letter of your choice:\n");printf("a. advice       b. bell\n");printf("c. count        q. quit\n");ch = get_first();while ((ch < 'a' || ch > 'c') && ch != 'q'){printf("Please respond with a, b, c, or q.\n");ch = get_first();}return ch;
}// 修改后的get_first()函数 - 返回第一个非空白字符
char get_first(void)
{int ch;// 跳过所有空白字符(空格、制表符、换行符等)while (isspace(ch = getchar()))continue;// 清除该行剩余的字符while (getchar() != '\n')continue;return ch;
}int get_int(void)
{int input;char ch;while (scanf("%d", &input) != 1){// 处理非整数输入while ((ch = getchar()) != '\n')putchar(ch);  // 显示错误的输入printf(" is not an integer.\nPlease enter an ");printf("integer value, such as 25, -178, or 3: ");}return input;
}

修改后的函数:

点击查看代码
char get_first(void)
{int ch;// 跳过所有空白字符(空格、制表符、换行符等)while (isspace(ch = getchar()))continue;// 清除该行剩余的字符while (getchar() != '\n')continue;return ch;
}

结果:
image

7

点击查看代码
#include <stdio.h>
#include <ctype.h>#define BASIC_RATE1 8.75
#define BASIC_RATE2 9.33
#define BASIC_RATE3 10.00
#define BASIC_RATE4 11.20#define TAX_RATE1 0.15    // 前300美元的税率
#define TAX_RATE2 0.20    // 续150美元的税率
#define TAX_RATE3 0.25    // 余下的税率#define TAX_BREAK1 300.0  // 第一税率档次
#define TAX_BREAK2 450.0  // 第二税率档次 (300 + 150)char get_choice(void);
char get_first(void);int main(void)
{char choice;double hours;double gross_pay, taxes, net_pay;double pay_rate;while ((choice = get_choice()) != 'q'){// 根据用户选择设置工资等级switch (choice){case 'a':pay_rate = BASIC_RATE1;break;case 'b':pay_rate = BASIC_RATE2;break;case 'c':pay_rate = BASIC_RATE3;break;case 'd':pay_rate = BASIC_RATE4;break;default:printf("程序错误!\n");continue;}// 获取工作时间printf("请输入一周工作的小时数: ");while (scanf("%lf", &hours) != 1 || hours < 0){printf("请输入一个有效的正数: ");while (getchar() != '\n')continue;}// 计算工资总额(考虑加班)if (hours <= 40)gross_pay = hours * pay_rate;elsegross_pay = 40 * pay_rate + (hours - 40) * pay_rate * 1.5;// 计算税金if (gross_pay <= TAX_BREAK1)taxes = gross_pay * TAX_RATE1;else if (gross_pay <= TAX_BREAK2)taxes = TAX_BREAK1 * TAX_RATE1 + (gross_pay - TAX_BREAK1) * TAX_RATE2;elsetaxes = TAX_BREAK1 * TAX_RATE1 + (TAX_BREAK2 - TAX_BREAK1) * TAX_RATE2 + (gross_pay - TAX_BREAK2) * TAX_RATE3;// 计算净收入net_pay = gross_pay - taxes;// 显示结果printf("\n工资总额: $%.2f\n", gross_pay);printf("税金: $%.2f\n", taxes);printf("净收入: $%.2f\n\n", net_pay);// 清除输入缓冲区while (getchar() != '\n')continue;}printf("程序结束。\n");return 0;
}// 显示菜单并获取用户选择
char get_choice(void)
{char ch;printf("***************************************************\n");printf("请输入对应所需工资等级或操作的字母:\n");printf("a) $%.2f/小时    b) $%.2f/小时\n", BASIC_RATE1, BASIC_RATE2);printf("c) $%.2f/小时   d) $%.2f/小时\n", BASIC_RATE3, BASIC_RATE4);printf("q) 退出\n");printf("***************************************************\n");ch = get_first();// 验证输入是否有效while ((ch < 'a' || ch > 'd') && ch != 'q'){printf("请输入 a, b, c, d 或 q: ");ch = get_first();}return ch;
}// 获取第一个非空白字符
char get_first(void)
{int ch;// 跳过空白字符while (isspace(ch = getchar()))continue;// 清除该行剩余的字符while (getchar() != '\n')continue;return ch;
}

结果:
image

9.11

1

点击查看代码
#include <stdio.h>double min(double x, double y);int main(void)
{double a, b;printf("请输入两个double类型的值:\n");scanf("%lf %lf", &a, &b);printf("较小的值是:%.2f\n", min(a, b));return 0;
}double min(double x, double y)
{return (x < y) ? x : y;
}

结果:
image

2

点击查看代码
#include <stdio.h>void chline(char ch, int i, int j);int main(void)
{char ch;int rows, cols;printf("请输入要打印的字符:");scanf(" %c", &ch);printf("请输入列数:");scanf("%d", &cols);printf("请输入行数:");scanf("%d", &rows);chline(ch, cols, rows);return 0;
}void chline(char ch, int i, int j)
{for (int row = 0; row < j; row++){for (int col = 0; col < i; col++){putchar(ch);}putchar('\n');}
}

结果:
image

3

点击查看代码
#include <stdio.h>void print_char(char ch, int times, int lines);int main(void)
{char ch;int times, lines;printf("请输入要打印的字符:");scanf(" %c", &ch);printf("请输入每行打印的次数:");scanf("%d", &times);printf("请输入打印的行数:");scanf("%d", &lines);print_char(ch, times, lines);return 0;
}void print_char(char ch, int times, int lines)
{for (int i = 0; i < lines; i++){for (int j = 0; j < times; j++){putchar(ch);}putchar('\n');}
}

结果:
image

4

点击查看代码
#include <stdio.h>double harmonic_mean(double x, double y);int main(void)
{double a, b;printf("请输入两个double类型的值:\n");scanf("%lf %lf", &a, &b);printf("%.2f 和 %.2f 的调和平均数是:%.2f\n", a, b, harmonic_mean(a, b));return 0;
}double harmonic_mean(double x, double y)
{// 调和平均数 = 2 / (1/x + 1/y) = 2xy / (x + y)if (x == 0 || y == 0){printf("错误:不能为0\n");return 0;}return 2.0 * x * y / (x + y);
}

结果:
image

8

点击查看代码
// power_improved.c -- 计算数的整数幂(改进版)
#include <stdio.h>double power(double n, int p); // ANSI函数原型int main(void)
{double x, xpow;int exp;printf("Enter a number and the integer power");printf(" to which\nthe number will be raised. Enter q");printf(" to quit.\n");while (scanf("%lf%d", &x, &exp) == 2){xpow = power(x, exp); // 函数调用printf("%.3g to the power %d is %.5g\n", x, exp, xpow);printf("Enter next pair of numbers or q to quit.\n");}printf("Hope you enjoyed this power trip -- bye!\n");return 0;
}double power(double n, int p) // 函数定义
{double pow = 1;int i;// 处理0的任何正次幂为0if (n == 0 && p > 0)return 0;// 处理任何数的0次幂为1(包括0^0)if (p == 0){if (n == 0)printf("Note: 0^0 is undefined, but we'll use 1.\n");return 1;}// 处理正指数if (p > 0){for (i = 1; i <= p; i++)pow *= n;}// 处理负指数else{for (i = 1; i <= -p; i++)pow *= n;pow = 1.0 / pow;  // 取倒数}return pow;
}

结果:
image

9

点击查看代码
// power_recursive.c -- 使用递归计算数的整数幂
#include <stdio.h>double power(double n, int p); // ANSI函数原型int main(void)
{double x, xpow;int exp;printf("Enter a number and the integer power");printf(" to which\nthe number will be raised. Enter q");printf(" to quit.\n");while (scanf("%lf%d", &x, &exp) == 2){xpow = power(x, exp); // 函数调用printf("%.3g to the power %d is %.5g\n", x, exp, xpow);printf("Enter next pair of numbers or q to quit.\n");}printf("Hope you enjoyed this power trip -- bye!\n");return 0;
}double power(double n, int p) // 递归函数定义
{// 处理0的任何正次幂为0if (n == 0 && p > 0)return 0;// 处理任何数的0次幂为1(包括0^0)if (p == 0){if (n == 0)printf("Note: 0^0 is undefined, but we'll use 1.\n");return 1;}// 处理正指数(递归)if (p > 0)return n * power(n, p - 1);// 处理负指数(递归)elsereturn 1.0 / power(n, -p);
}

结果:
image

11

点击查看代码
#include <stdio.h>unsigned long Fibonacci(unsigned n);int main(void)
{unsigned n;printf("请输入要计算的斐波那契数的位置(0-50):\n");while (scanf("%u", &n) == 1 && n <= 50){printf("第 %u 个斐波那契数是:%lu\n", n, Fibonacci(n));printf("请输入下一个位置(q退出):\n");}return 0;
}unsigned long Fibonacci(unsigned n)
{unsigned long fib_prev = 0;  // F(0)unsigned long fib_curr = 1;  // F(1)unsigned long fib_next;unsigned i;if (n == 0)return fib_prev;if (n == 1)return fib_curr;for (i = 2; i <= n; i++){fib_next = fib_prev + fib_curr;fib_prev = fib_curr;fib_curr = fib_next;}return fib_curr;
}

结果:
image

程序设计:多源代码文件

math_functions.h文件

点击查看代码
#ifndef MATH_FUNCTIONS_H
#define MATH_FUNCTIONS_Hint add(int a, int b);
int multiply(int a, int b);
int square(int x);#endif#pragma once

math_functions.c

点击查看代码
#include "math_functions.h"int add(int a, int b)
{return a + b;
}int multiply(int a, int b)
{return a * b;
}int square(int x)
{return x * x;
}

main.c

点击查看代码
#include <stdio.h>
#include "math_functions.h" // 包含自定义头文件int main(void)
{int x = 5, y = 3;printf("简单多文件程序示例\n");printf("=========================\n");  // 修正了冒号位置printf("x = %d, y = %d\n", x, y);printf("x + y = %d\n", add(x, y));      // 修正为加法printf("x * y = %d\n", multiply(x, y));return 0;
}  

结果:
image

编写一个程序,在该程序中输出主函数内定义的变量的地址,以及函数中定义的同名变量的地址。

点击查看代码
#include <stdio.h>// 函数声明
void test_function(void);int main(void)
{int number = 100;  // 主函数中定义的变量printf("=== 主函数中的变量 ===\n");printf("变量名: number\n");printf("变量值: %d\n", number);printf("变量地址: %p\n", (void*)&number);printf("\n");// 调用函数,函数内部也有同名变量test_function();// 再次显示主函数中变量的地址(确认地址不变)printf("\n=== 返回主函数后 ===\n");printf("主函数中 number 的地址仍然是: %p\n", (void*)&number);return 0;
}void test_function(void)
{int number = 200;  // 函数中定义的同名变量printf("=== 函数中的同名变量 ===\n");printf("变量名: number\n");printf("变量值: %d\n", number);printf("变量地址: %p\n", (void*)&number);// 显示两个变量的区别printf("\n=== 对比分析 ===\n");printf("虽然变量名相同,但它们是不同的变量:\n");printf("- 存储在不同的内存位置\n");printf("- 有各自独立的作用域\n");printf("- 互不影响\n");
}

结果:
image

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/964856.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025年贵州贵阳母婴护理机构最新TOP5评测:守护母婴健康的专业力量

随着母婴健康意识的提升,专业母婴护理机构成为新家庭的重要选择。本榜单基于环境设施、人员资质、服务质量、安全管理四大核心维度,结合行业服务标准与用户口碑数据,全面解析2025年贵阳五大母婴护理品牌综合实力,为…

2025贵州贵阳月子会所最新TOP5评测:产后恢复优选,守护母婴健康

随着现代家庭对产后护理需求的升级,专业月子会所已成为新生代父母的核心选择。本榜单基于服务专业性、环境设施、护理团队、客户口碑四大维度,结合《2025中国母婴护理行业发展报告》及本地宝妈真实评价,权威解析贵阳…

2025年贵州贵阳月子中心最新TOP5专业评测:守护母婴健康新标杆

随着母婴护理需求的精细化升级,贵阳月子中心市场呈现专业化、高端化发展趋势。本榜单基于服务资质、护理团队专业性、设施环境、客户口碑四大核心维度,结合行业最新标准与真实用户反馈,权威解析2025年贵州贵阳五大月…

Excel VBA 自定义排序

现有数据如下:此时,如果需要根据单元格底色来排序,红色在上,其次是黄色,最后是白色(其实是无底色)。那么代码如下: Sub te()Dim wbk As WorkbookDim sht As WorksheetDim last_row As IntegerDim iCounter As …

基于GWO灰狼优化的XGBoost序列预测算法matlab仿真

1.算法运行效果图预览 (完整程序运行后无水印)3.部分核心程序 (完整版代码包含详细中文注释和操作步骤视频)................................................................ %最大迭代次数 paramters.maxiter …

2025广东住房公积金提取机构最新TOP5评测:因为正规,所以高效

在住房公积金提取需求日益增长的背景下,选择专业可靠的代办服务机构成为许多人高效解决公积金提取难题的关键。本榜单基于服务专业性、业务覆盖范围、客户满意度等核心维度,结合市场反馈与行业数据,对广东及周边地区…

2025广东公积金提取代办中介最新TOP5评测:高效引领行业合规标准

随着住房公积金在居民生活中的重要性日益凸显,公积金提取代办服务需求持续攀升。本榜单依据合规资质、服务覆盖范围、客户满意度及办理效率四大核心维度,结合行业权威数据与用户反馈,对广东地区公积金代办机构进行综…

2025年深圳公积金提取最新TOP5评测:专业高效合规,引领行业标准

随着社会经济的发展,公积金作为一项重要的住房保障制度,其提取需求日益增长。然而,公积金提取流程复杂、政策多变,让许多人感到困扰。专业的公积金提取服务机构应运而生,为大众提供便捷、高效的服务。本榜单基于机…

《Chrome 开发者工具:前端调试必备》

1、非修改序列算法 这些算法不会改变它们所操作的容器中的元素。 1.1 find 和 find_if find(begin, end, value):查找第一个等于 value 的元素,返回迭代器(未找到返回 end)。 find_if(begin, end, predicate):查找…

使用 vLLM 本地部署 Qwen3-Embedding-8B 模型并接入 Dify 完整指南 - yi

使用 vLLM 本地部署 Qwen3-Embedding-8B 模型并接入 Dify 完整指南使用 vLLM 本地部署 Qwen3-Embedding-8B 模型并接入 Dify 完整指南 环境准备与验证 在开始部署前,需要确保本地环境满足基本要求。以下是环境验证步骤…

《VS Code:高效编程的插件与配置》

1、非修改序列算法 这些算法不会改变它们所操作的容器中的元素。 1.1 find 和 find_if find(begin, end, value):查找第一个等于 value 的元素,返回迭代器(未找到返回 end)。 find_if(begin, end, predicate):查找…

11.13 NOTE

P9350 [JOI 2023 Final] 宣传 2 / Advertisement 2 题目传送门 思路 通过题目给出的式子,我们可以推出,对于一个贡献,需要满足的条件是: \[E_i-X_i \ge E_j-X_j 或 E_i+X_i \ge E_j+X_j \]那我们就可以发现,我们可…

2025广州公积金提取服务最新TOP5权威评测:专业合规引领行业标杆

随着广州地区公积金提取需求的持续增长,选择正规、专业的服务机构成为市民关注焦点。本榜单基于合规资质、服务效率、客户口碑、业务覆盖四大核心维度,结合市场调研与用户反馈,客观评测广州地区五家领先公积金提取服…

用Rust 解析验证码:结合 Tesseract OCR 进行文本识别

环境准备 1.1 安装 RustRust 可通过官方的 rustup 进行安装: curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs | sh 安装完成后,检查 Rust 是否可用: rustc --version 1.2 安装 Tesseract OCR Linux(Ubu…

10.26 NOTE

P4742 [Wind Festival] Running In The Sky 题目传送门 思路 没啥营养,和所驼门王那一题一样,Tarjan 缩点,而后 DAG 上 DP。甚至还更简单一点。唯一需要注意的是要仔细考虑一下状态转移方程,这点很重要,不然会出大…

10.22 NOTE

P9352 [JOI 2023 Final] 训猫 / Cat Exercise 题目传送门 思路 要求猫移动次数的最大值,显然,当只留了一条路时, 猫的移动方向是固定的,也就是说,我们可以决定这只猫走的方向,而这是一个树形结构,显然可以树形 …

题解:CF2106D Flower Boy

题目翻译 题目传送门(vjudge) 给定一个长度为 \(n\) 的数组 \(a\) 和一个长度为 \(m\) 的数组 \(b\)。 要在 \(a\) 中从左到右选取 \(m\) 个数按从左到右的顺序组成一个新的数列,使得选出来的数大于等于 \(b\) 数组…

使用 Maven 内置的版本号(Version)统一控制功能

从Maven 3.5 -beta-1开始 支持内置的 ${revision} (${sha1} and/or ${changelist}的使用方法,请查看[Maven 文档][1])占位符作为 标签的值,用来控制整个项目的版本号。 <project><groupId>xxx</gr…

使用 Maven 内置的版本号(Version)统一控制功能

从Maven 3.5 -beta-1开始 支持内置的 ${revision} (${sha1} and/or ${changelist}的使用方法,请查看[Maven 文档][1])占位符作为 标签的值,用来控制整个项目的版本号。 <project><groupId>xxx</gr…