高级程序语言设计第五次个人作业
| 这个作业属于哪个课程 | <班级的链接> |
|---|---|
| 这个作业要求在哪里 | <作业链接> |
| 学号 | 092300303 |
| 姓名 | 池博洋 |
@
目录
作业基本信息
一、设计作业
1.使得程序遇到空格符时结束
点击查看代码
#include <stdio.h>
int main() {
char c;
while ((c = getchar()) != ' ') {
putchar(c);
}
return 0;
}
结果:

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;
}
结果:


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;
}
结果:

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;
}
结果:

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;
}
结果:

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;
}
结果:

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; // 数字比猜测值小
else
printf("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;
}
结果:

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;
}
结果:

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;
else
gross_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;
else
taxes = 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;
}
结果:

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;
}
结果:

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');
}
}
结果:

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", ×);
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');
}
}
结果:

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);
}
结果:

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的任何正次幂为0
if (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;
}
结果:

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的任何正次幂为0
if (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);
// 处理负指数(递归)
else
return 1.0 / power(n, -p);
}
结果:

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;
}
结果:

程序设计:多源代码文件
math_functions.h文件
点击查看代码
#ifndef MATH_FUNCTIONS_H
#define MATH_FUNCTIONS_H
int 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;
}
结果:

编写一个程序,在该程序中输出主函数内定义的变量的地址,以及函数中定义的同名变量的地址。
点击查看代码
#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");
}
结果:

浙公网安备 33010602011771号