编程题部分:
1,通过试验观察系统如何处理整数上溢,浮点数上溢和浮点数下溢的情况。
整数上溢:编译器会不加提示的依次使用 int ,long ,unsigned long ,long long ,unsigned long long 当达到它能表示的最大值时,他会重新从起始点开始。
#include <stdio.h>
int main(void){
int i = 2147483647;
unsigned int j = 4294967295;
float t = 3.2E38*100;
float p = 0.1234E-2 / 10;
printf("%d %d \n",i,i+1); //int的取值范围在[-32767,32767]
//而long类型的取值范围在[-2147483647,2147483647],int类型的i转换为long类型的i
//所以此时的i为long的最大值,因此i+1从起始点{0}开始。
printf("%d %d \n",j,j+1); //同理,unsigned long 取值范围为[0,4294967295]
printf("%f %f \n",t,p); //浮点型上溢,会给该值赋一个表示无穷大的值
//浮点型下溢,则会损失精度
}
运行结果:
2,编写一个程序,要求提示输入一个ASCII码值,然后打印输入的字符。
#include <stdio.h>
int main(void)
{
int ascii;
printf("please enter an ascii code:\n");
scanf("%d",&ascii);
printf("%d is ascii code of %c",ascii,ascii);
return 0;
}
运行结果:
3,编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that "
#include <stdio.h>
int main(void)
{
printf("\aStartled by the sudden sound, Sally shouted,\n\"By the Great Pumpkin, what was that\"");
return 0;
}
运行结果:
4,编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p计数法(即十六进制计数法)。按以下格式输出:
Enter a floating-point value: 64.25
fixed-point notation : 64.250000
exponential notation :6.425000e+01
p notation : 0x1.01p+6
#include <stdio.h>
int main(void)
{
float num;
printf("Enter a floating-point value: ");
scanf("%f",&num);
printf("fixed-point notation: %f\nexponential notation: %e\np notation: %.2a",num,num,num);
return 0;
}
运行结果:
5,一年大约有3.156x10^7秒。编写一个程序:提示用户输入年龄,然后显示该年龄对应的秒数。
#include <stdio.h>
int main(void)
{
int age;
printf("please enter your age: ");
scanf("%d",&age);
printf("your age is %d and the seconds of your age is : %.1e",age,3.0e-23*age);
return 0;
}
运行结果:
6,1个水分子的质量约为3.0x10^-23克,一夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。
#include <stdio.h>
int main(void)
{
int quart;
printf("please enter the number of quart: ");
scanf("%d",&quart);
printf("%d quarts of water is %.1e water molecules",quart,950*quart/3.0e-23);
return 0;
}
运行结果:
7,1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。
这道题第一章已经写过就不写了
8,在美国的体积测量系统中,一品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱,盎司,汤勺,茶勺为单位显示等价容量。思考对于该程序,为何使用浮点型比整数类型更合适。
#include <stdio.h>
int main(void)
{
float num;
int num1;
printf("请输入杯数: ");
scanf("%f",&num);
num1 = num;
printf("浮点型:\n对应的品脱数:%.1f 盎司数:%.f 汤勺数:%.f 茶勺数:%.f\n",num/2,num*8,num*16,num*48);
printf("整型:\n对应的品脱数:%d 盎司数:%d 汤勺数:%d 茶勺数:%d",num1/2,num1*8,num1*16,num1*48);
return 0;
}
运行结果: