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

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2026gjyycxsj

这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2026gjyycxsj/homework/16751

学号:0102600127

姓名:林嘉宇


设计一个getchar和putchar联合使用的程序,使得程序遇到空格符时结束。

#include <stdio.h>

int main(){
	char c;
	printf("只要不停下脚步……\n");
	do {
	c=getchar();
	printf("\n道路就会……不断延伸!");
	putchar(c);
	} while (c!=' ');
	printf("所以……不要停下来啊");
}

建立一个in.txt和out.txt,分别使用这两个文件进行重定位代替键盘输入和屏幕输出in.txt存放一些字符小写字符,大写字符,数字字符,程序将这些小写字符转换成大写字符,然后全部输出到out.txt。

image


从键盘上输入数据并进行加、减、乘、除四则运算(以“a运算b”的形式输入),判断输入的数据是否可以进行计算,若能计算,则输出计算结果,保留小数点后2位,否则显示“wrong input”。另外,碰到除数为0是能提示“division by zero”。

#include <stdio.h>
#include <math.h> 

int main(){
	double a,b,c;
	char op;
	scanf("%lf%c%lf",&a,&op,&b);
	switch (op){
		case '+':c=a+b;break;
		case '-':c=a-b;break;
		case '*':c=a*b;break;
		case '/':
			if (fabs(b)<1e-6){
				printf("division by zero");
				return 0;
			}
			else {
				c=a/b;break;
			}
		default:
			printf("wrong input");
			return 0;
	}
	printf("%.2f",c);
	return 0;
}

混合读入字符、数值,形式参考第八章PPT19页。

问题描述:
先读入一个字符,再读入一个整数,最后又读入一个字符;然后分别输出它们
输入格式:n个空格和/或<回车>,一个字符,m个空格和/或<回车>,一个整数,k个空格和/或<回车>,一个字符
输出格式:字符整数字符

#include <stdio.h>
#include <math.h> 

int main(){
	char a,b,c;
	int op;
	scanf(" %c %d %c",&a,&op,&b);
	printf("%c%d%c",a,op,b);
	return 0;
}

编写并运行书本第8章8.11编程练习题目中的第3~7题。

8.11.3

#include <stdio.h>
#include <ctype.h>

int main(){
 int ch;
 unsigned long uct = 0,lct = 0,oct = 0;
 
 while ((ch = getchar()) != EOF)
 if (isupper(ch))
 uct++;
 else if (islower(ch))
 lct++;
 else
 oct++;
 printf("%lu uppercase characters read\n", uct);
 printf("%lu lowercase characters read\n", lct);
 printf("%lu other characters read\n", oct);
 
 return 0;
}

8.11.4

#include <stdio.h>
#include <ctype.h>

int main(){
 int ch,zn=0;
 unsigned long wct = 0,lct = 0;
 
 while ((ch = getchar()) != EOF){
 if (isspace(ch))zn=0;
 else if (isalpha(ch)){
  if (!zn){
   wct++;
   zn=1;}
  lct++;
 }
}
 
 printf("%f words in average\n", (double)lct/wct);
 return 0;
}

编写并运行书本第9章9.11编程练习题目中的第1~4,8,9,11题。

设计一个多源代码文件程序的编译。例如,first.c源代码文件包含主函数和一些头文件(second.h),second.c源代码文件是一些自定义的函数功能实现,second.h文件是一些常量声明和second.c中自定义函数的函数原型声明。

posted @ 2026-11-17 21:55  D_Karma'fier  阅读(4)  评论(0)    收藏  举报