现附上我对第三章的习题的解答,仅供参考,欢迎交流
//3.11.2
//输入一个ascll码值,打印出对应的字符
# include <stdio.h>
int main(void)
{
int i;
printf("Please enter a ascll number\n");
scanf("%d", &i);
printf("The output character is %c", i);
return 0;
}
//3.11.3
//打印特定内容
# include <stdio.h>
int main(void)
{
printf("\a");
printf("Startled by the sudden sound, Sally shouted,\n");
printf("\"By the Great Pumpkin, what was that!\"");
return 0;
}
//3.11.4
/*读取一个浮点数,再打印成各种形式*/
# include <stdio.h>
int main(void)
{
float i; /*定义浮点数*/
printf("Enter a floating-point value: "); /*打印浮点数*/
scanf("%f", &i);
printf("fixed-point notation: %f\n", i);
printf("exponential notation: %e\n", i);
printf("p notation: %a", i);
return 0;
}
//3.11.5
//输入年龄,输出该年龄对应的秒数
# include <stdio.h>
int main(void)
{
long age; //定义年龄
int seconds; //定义秒数
printf("Please enter your age :");
scanf("%d", &age);
seconds = age * 3.156e7;
printf("The age convert to seconds is %ld", seconds);
return 0;
}
//3.11.6
//输入水的夸脱数,显示水分子的数量
# include <stdio.h>
int main(void)
{
float quarts; //定义夸脱数
double molecule; //定义水分子数
printf("Please enter the number of quarts of water:");
scanf("%f", &quarts);
molecule = 950 / (3.0e-23) * quarts;
printf("The number of molecule is %e", molecule);
return 0;
}
//3.11.7
//输入身高(/英寸),输出厘米
# include <stdio.h>
int main(void)
{
float inch; //定义英寸
float cm; //定义厘米
printf("Please enter your height :(inch)\n");
scanf("%f", &inch);
cm = inch * 2.54; //计算厘米值
printf("Your height is %.2fcm", cm);
return 0;
}
//3.11.8
//输入杯数,输出其他单位的等价容量
# include <stdio.h>
int main(void)
{
float cups;
float pint;
float ounce;
float soupspoon;
float teaspoon; //分别定义变量
printf("Please enter the number of cups:\n");
scanf("%f", &cups); //获取cups变量值
pint = cups * 0.5;
ounce = cups * 8;
soupspoon = ounce * 2;
teaspoon = soupspoon * 3; //对变量进行计算
printf("%.2fcups = %.2fpint = %.2founce = %.2fsoupspoon = %.2fteaspoon", cups, pint, ounce, soupspoon, teaspoon);
//显示各变量转化后的结果
return 0;
}
//对于该程序,使用浮点数类型比整数类型更合适
//1.从程序本身要求来看,即使输入的变量是整型变量
//但进行变量计算时仍会出现浮点型变量
//所以不如起始时直接浮点型变量
//2.从实际生活来看,杯数是整型变量的情况较少
//且浮点型变量也能表达出整型变量
//所以使用浮点型变量更方便
如有问题,欢迎指正