高级语言程序设计课程第五次个人作业
高级语言程序设计课程第五次个人作业
这个作业属于哪个课程:2026高级语言程序设计
这个作业要求在哪里:第五次个人作业
学号:072506135
姓名:张锦程
getchar 和 putchar:空格结束
#include <stdio.h>
int main(void) {
int ch;
printf("请输入字符(空格结束):");
while ((ch = getchar()) != EOF && ch != ' ') putchar(ch);
putchar('\n');
return 0;
}

文件重定向:小写转大写
#include <stdio.h>
#include <ctype.h>
int main(void) {
int ch;
while ((ch = getchar()) != EOF) putchar(toupper((unsigned char)ch));
return 0;
}

四则运算及输入判断
#include <stdio.h>
#include <ctype.h>
int main(void) {
char line[128], op, extra;
double a, b, result;
printf("请输入算式:");
if (!fgets(line, sizeof line, stdin) ||
sscanf(line, " %lf %c %lf %c", &a, &op, &b, &extra) != 3) {
puts("wrong input"); return 0;
}
switch (op) {
case '+': result = a+b; break;
case '-': result = a-b; break;
case '*': result = a*b; break;
case '/': if (b == 0) { puts("division by zero"); return 0; }
result = a/b; break;
default: puts("wrong input"); return 0;
}
printf("结果:%.2f\n", result); return 0;
}

混合读取字符和整数
#include <stdio.h>
int main(void) {
char a, b; int n;
printf("请输入字符、整数和字符:");
if (scanf(" %c %d %c", &a, &n, &b) == 3) printf("%c%d%c\n", a, n, b);
return 0;
}

第 8 章练习 3:大小写字母计数
#include <stdio.h>
#include <ctype.h>
int main(void) {
int ch, upper=0, lower=0;
while ((ch=getchar()) != EOF) {
if (isupper((unsigned char)ch)) ++upper;
else if (islower((unsigned char)ch)) ++lower;
}
printf("大写字母:%d,小写字母:%d\n", upper, lower); return 0;
}

第 8 章练习 4:平均单词长度
#include <stdio.h>
#include <ctype.h>
int main(void) {
int ch, inword=0, words=0, letters=0;
while ((ch=getchar()) != EOF) {
if (isspace((unsigned char)ch)) inword=0;
else { if (!inword) { ++words; inword=1; } ++letters; }
}
printf("单词数:%d,平均长度:%.2f\n", words, words ? (double)letters/words : 0.0);
return 0;
}

第 8 章练习 5:二分猜数
#include <stdio.h>
int main(void) {
int low=1, high=100; char response;
puts("请在 1 到 100 之间想一个数;h 表示猜大,l 表示猜小,c 表示猜中。");
while (low <= high) {
int guess=low+(high-low)/2;
printf("我猜 %d:",guess);
if (scanf(" %c", &response)!=1) return 0;
if (response=='c') { puts("猜中了!"); return 0; }
if (response=='h') high=guess-1;
else if (response=='l') low=guess+1;
else puts("请输入 h、l 或 c。");
}
puts("回答不一致。协议结束。"); return 0;
}

第 8 章练习 6:第一个非空白字符
#include <stdio.h>
#include <ctype.h>
int get_first(void) {
int ch;
while ((ch=getchar()) != EOF && isspace((unsigned char)ch)) {}
return ch;
}
int main(void) {
int ch; printf("请输入内容:"); ch=get_first();
if (ch != EOF) printf("首个非空白字符:%c\n",ch);
return 0;
}

第 8 章练习 7:工资等级菜单
#include <stdio.h>
#define TAX1 0.15
#define TAX2 0.20
#define TAX3 0.25
int main(void) {
char choice; double hours, rate, gross, tax;
for (;;) {
puts("a) 8.75 b) 9.33 c) 10.00 d) 11.20 q) 退出");
printf("请选择:"); if (scanf(" %c",&choice)!=1 || choice=='q') break;
switch(choice) { case 'a':rate=8.75;break; case 'b':rate=9.33;break;
case 'c':rate=10.0;break; case 'd':rate=11.2;break;
default:puts("请输入有效选项。");continue; }
printf("请输入工作小时:"); if (scanf("%lf",&hours)!=1 || hours<0) break;
gross=hours<=40 ? hours*rate : (40+(hours-40)*1.5)*rate;
if(gross<=300) tax=gross*TAX1;
else if(gross<=450) tax=300*TAX1+(gross-300)*TAX2;
else tax=300*TAX1+150*TAX2+(gross-450)*TAX3;
printf("工资:%.2f,税金:%.2f,净收入:%.2f\n",gross,tax,gross-tax);
} return 0;
}

第 9 章练习 1:较小值
#include <stdio.h>
double min(double x,double y) { return x<y ? x:y; }
int main(void) { printf("较小值:%.2f\n",min(3.5,2.8)); return 0; }

第 9 章练习 2:指定行列打印字符
#include <stdio.h>
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'); }
}
int main(void) { chline('*',5,3); return 0; }

第 9 章练习 3:字符矩形
#include <stdio.h>
void draw(char ch,int count,int rows) {
for(int i=0;i<rows;i++) { for(int j=0;j<count;j++) putchar(ch); putchar('\n'); }
}
int main(void) { draw('#',6,4); return 0; }

第 9 章练习 4:调和平均数
#include <stdio.h>
double harmonic(double x,double y) { return 2.0/(1.0/x+1.0/y); }
int main(void) { printf("调和平均数:%.2f\n",harmonic(3.0,6.0)); return 0; }

第 9 章练习 8:循环求整数次幂
#include <stdio.h>
double power(double base,int exponent) {
double result=1.0; long long n=exponent;
if(base==0 && n==0) { puts("0 的 0 次幂未定义,按 1 处理。"); return 1.0; }
if(base==0 && n<0) { puts("0 的负次幂未定义。"); return 0.0; }
if(base==0) return 0.0;
if(n<0) { base=1.0/base; n=-n; }
while(n-- > 0) result*=base;
return result;
}
int main(void) { printf("2 的 -3 次幂:%.3f\n",power(2,-3)); return 0; }

第 9 章练习 9:递归求整数次幂
#include <stdio.h>
double positive_power(double x,long long n) {
if(n==0) return 1.0;
double half=positive_power(x,n/2);
return n%2 ? half*half*x : half*half;
}
double power(double x,int exponent) {
long long n=exponent;
if(x==0 && n==0) { puts("0 的 0 次幂未定义,按 1 处理。"); return 1.0; }
if(x==0 && n<0) { puts("0 的负次幂未定义。"); return 0.0; }
if(x==0) return 0.0;
return n<0 ? 1.0/positive_power(x,-n) : positive_power(x,n);
}
int main(void) { printf("2 的 -3 次幂:%.3f\n",power(2,-3)); return 0; }

第 9 章练习 11:循环计算斐波那契数
#include <stdio.h>
unsigned long long Fibonacci(unsigned n) {
unsigned long long a=0,b=1;
for(unsigned i=0;i<n;i++) { unsigned long long next=a+b; a=b; b=next; }
return a;
}
int main(void) { for(unsigned i=0;i<10;i++) printf("%llu ",Fibonacci(i)); putchar('\n'); return 0; }

多源文件编译
first.c
#include <stdio.h>
#include "second.h"
int main(void) { printf("%d 与 %d 的和:%d\n", A, B, add(A,B)); return 0; }
second.h
#ifndef SECOND_H
#define SECOND_H
#define A 12
#define B 8
int add(int x,int y);
#endif
second.c
#include "second.h"
int add(int x,int y) { return x+y; }

同名局部变量的地址
#include <stdio.h>
void show(void) { int value=20; printf("函数中 value 的地址:%p\n",(void *)&value); }
int main(void) { int value=10; printf("主函数中 value 的地址:%p\n",(void *)&value); show(); return 0; }

编写和运行过程中遇到的问题及解决方法
无
总结和思考
本次练习加深了我对字符输入、文件重定向、输入验证和函数的理解。处理输入时需要注意空白字符和文件结束标志;编写函数时要明确参数、返回值及边界条件。通过多文件编译,也进一步理解了头文件声明和函数实现之间的关系。

浙公网安备 33010602011771号