1.通过试验(即编写带有此类问题的程序)观察系统如何处理整数上 溢、浮点数上溢和浮点数下溢的情况。
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main() {
// 整数上溢
int intOverflow = INT_MAX + 1;
printf("整数上溢结果:%d\n", intOverflow);
// 浮点数上溢
float floatOverflow = FLT_MAX * 2;
printf("浮点数上溢结果:%f\n", floatOverflow);
// 浮点数下溢
float floatUnderflow = FLT_MIN / 2;
printf("浮点数下溢结果:%f\n", floatUnderflow);
return 0;
}
2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印 输入的字符。
#include <stdio.h>
int main() {
int ascii; // 用于存储用户输入的ASCII码值
printf("请输入一个ASCII码值: ");
scanf("%d", &ascii);
printf("对应的字符是: %c\n", ascii);
return 0;
}
3.编写一个程序,发出一声警报,然后打印下面的文本: Startled by the sudden sound, Sally shouted, "By the Great Pumpkin, what was that!"
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("\a"); // 发出一声警报
printf("Startled by the sudden sound, Sally shouted,\n");
printf("\"By the Great Pumpkin, what was that!\"\n");
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() {
double num;
printf("请输入一个浮点数: ");
scanf("%lf", &num);
printf("小数点形式:%f\n", num);
printf("指数形式:%e\n", num);
printf("p记数法:%a\n", num);
return 0;
}
5.一年大约有3.156×10 7秒。编写一个程序,提示用户输入年龄,然后显 示该年龄对应的秒数。
#include <stdio.h>
int main() {
unsigned int age;
double seconds_per_year = 3.156e7;
printf("请输入您的年龄:");
scanf("%u", &age);
double total_seconds = age * seconds_per_year;
printf("您的年龄对应的秒数为:%.0lf\n", total_seconds);
return 0;
}
6.1个水分子的质量约为3.0×10 −23克。1夸脱水大约是950克。编写一个 程序,提示用户输入水的夸脱数,并显示水分子的数量。
#include <stdio.h>
int main() {
// 声明变量
double quarts; // 存储用户输入的夸脱数
// 定义常量
const double MASS_PER_MOLECULE = 3.0e-23; // 每个水分子的质量(单位为克)
const double GRAMS_PER_QUART = 950.0; // 每夸脱水的重量(单位为克)
const double MOLECULES_PER_GRAM = 1.0 / MASS_PER_MOLECULE; // 每克水中所含的水分子数量
// 提示用户输入夸脱数,并读取输入值
printf("请输入水的夸脱数:");
scanf("%lf", &quarts);
// 计算水分子的数量
double grams = quarts * GRAMS_PER_QUART; // 总重量(单位为克)
double molecules = grams * MOLECULES_PER_GRAM; // 水分子的总数量
// 输出结果
printf("水分子的数量为:%e\n", molecules);
return 0;
}
7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英 寸),然后以厘米为单位显示身高。
#include <stdio.h>
int main() {
double inches, centimeters;
const double INCHES_TO_CM = 2.54;
// 提示用户输入身高(英寸),并读取输入值
printf("请输入身高(英寸):");
scanf("%lf", &inches);
// 将身高从英寸转换为厘米
centimeters = inches * INCHES_TO_CM;
// 显示转换后的身高(厘米)
printf("身高为:%.2f厘米\n", centimeters);
return 0;
}
8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等 于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以 品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用 浮点类型比整数类型更合适?
#include <stdio.h>
int main() {
// 声明四个双精度浮点型变量,用于存储容量值
double cups, fluid_ounces, tablespoons, teaspoons;
// 定义三个常量,表示各单位之间的转换系数
const double CUPS_TO_FLUID_OUNCES = 8.0;
const double FLUID_OUNCES_TO_TABLESPOONS = 2.0;
const double TABLESPOONS_TO_TEASPOONS = 3.0;
// 提示用户输入杯数,并读取输入值
printf("请输入杯数:");
scanf("%lf", &cups);
// 将容量从杯数转换为不同的单位
fluid_ounces = cups * CUPS_TO_FLUID_OUNCES;
tablespoons = fluid_ounces * FLUID_OUNCES_TO_TABLESPOONS;
teaspoons = tablespoons * TABLESPOONS_TO_TEASPOONS;
// 显示转换后的容量
printf("杯数为%.2f,相当于:\n", cups);
printf("%.2f品脱\n", cups / 2.0);
printf("%.2f盎司\n", fluid_ounces);
printf("%.2f大汤勺\n", tablespoons);
printf("%.2f茶勺\n", teaspoons);
return 0;
}
/*
使用浮点类型比整数类型更合适是因为涉及到的单位之间存在小数转换关系,
例如每个杯子等于8个盎司,而8个盎司并不是整数。如果使用整数类型进行
计算,将可能导致精度损失和计算错误,而使用浮点类型可以更准确地表示
小数转换关系和计算结果。
*/