1、通过试验(即编写带有此类问题的程序)观察系统如何处理整数上道、浮占数上溢和浮点数下溢的
int main(int argc, char** argv)
{
int intMax = 2147483647;
float floatMax = 3.402823466e+38f;
float floatMin = -3.402823466e+38f;
printf("intMax:%d, floatMax:%f, floatMin:%f\n", intMax, floatMax, floatMin);
printf("intMax + 1:%d, floatMax * 10:%f, floatMin / 10:%f\n", intMax + 1, floatMax * 10, floatMin / 10);
return 0;
}
2.编写一个程序,要求提示输入一个ASCII码值(如,66),然后打印输入的字符。
int main(int argc, char** argv)
{
int ascii;
printf("Enter an ASCII code: ");
scanf_s("%d", &ascii);
printf("%d is the ASCII code for %c.\n", ascii, ascii);
return 0;
}
3.编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted
"By the Great Pumpkin, what was that!"
int main(int argc, char** argv)
{
printf("%c", '\a');
printf("Startled 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
int main(int argc, char** argv)
{
float num;
printf("Enter a floating-point value: ");
scanf_s("%f", &num);
printf("fixed-point notation: %f\n", num);
printf("exponential notation: %e\n", num);
printf("p notation: %a\n", num);
return 0;
}
5.一年大约有3.156×10'秒。编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。
int main(int argc, char** argv)
{
int age;
printf("Enter your age:");
scanf_s("%d", &age);
printf("You are %e seconds old\n", age * 3.156e7);
return 0;
}
6. 1个水分子的质量约为3.0×10-3克。1夸脱水大约是950克。编写一个程序,提示用户输入水的夸
脱数,并显示水分子的数量。
int main(int argc, char** argv)
{
float mass_mol = 3.0e-23; /* mass of water molecule in grams */
float mass_qt = 950; /* mass of quart of water in grams */
float quarts;
float molecules;
printf("Enter the number of quarts of water: ");
scanf_s("%f", &quarts);
molecules = quarts * mass_qt / mass_mol;
printf("%f quarts of water contain %e molecules.\n", quarts, molecules);
return 0;
}
7.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。
int main(int argc, char** argv)
{
int height;
printf("Enter your height(inch):");
scanf_s("%d", &height);
printf("You are %.2f cm height\n", height * 2.54);
return 0;
}
8.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于
3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。
int main(int argc, char** argv)
{
int cups;
printf("Enter the cups:");
scanf_s("%d", &cups);
float pint = cups * 0.5;
float ounce = cups * 8;
float spoons = ounce * 2;
float tea_spoons = spoons * 3;
printf("%d cups equals %.2f pint, %.2f ounce, %.2f spoons, %.2f tea_spoons\n", cups, pint, ounce, spoons, tea_spoons);
return 0;
}