C Primer Plus第三章练习如下:
1.编写一个程序,要求提示输入一个ASCII码值(如,66), 然后打印输入的字符。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
int ASCII;
printf("请输入一个ASCII码:");
scanf("%d",& ASCII);
printf("\n所输入ASCII码对应的字符为:%c\n", ASCII);
system("pause");
return 0;
}
运行结果:
2.编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"
代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("\a Startled by the sudden sound,Sally shouted,\n");
printf("\"By the Great Pumpkin, what was that!\"\n");
system("pause");
return 0;
}
运行结果:
3.
编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。按以下格式输出(实际显示的指数位数因系统而异):
Enter a floating-point value: 64.25
fixed-point notation: 64.250000
exponential notation: 6. 425000e+01
p notation: 0x1.01p+6
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
float f;
printf("Enter a floating-point value:");
scanf("%f", &f);
printf("\nfixed-point notation:%f\n", f);
printf("exponential notation:%e\n", f);
printf("p notation:%a\n", f);
system("pause");
return 0;
}
运行结果:
4.一年大约有3.156X10^7秒。 编写一个程序,提示用户输入年龄,然后显示该年龄对应的秒数。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
int age;
printf("请输入您的年龄:");
scanf("%d", &age);
printf("\n您的年龄所对应的秒数为:%f\n", age * 3.156e7);
system("pause");
return 0;
}
运行结果:
5.1个水分子的质量约为3.0X10^-23克。1夸脱水大约是950克。编写一个程序,提示用户输入水的夸脱数,并显示水分子的数量。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
float quart;
float mole;
printf("请输入水的夸脱数:");
scanf("%d", &quart);
printf("\n");
//1quart=950g,m水=3.0*10^-23
//水分子数=quart*950/(3.0e-23)
mole = quart * 950 / (3.0e-23);
printf("水分子的数量为:%e", mole);
system("pause");
return 0;
}
运行结果:
6.1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
int height;
float hei;
printf("请输入身高(英寸)");
scanf("%d", &height);
//1英寸相当于2.54厘米
hei = 2.54*height;
printf("您的身高是%f厘米\n", hei);
system("pause");
return 0;
}
运行结果:
7.在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1 盎司等于2大汤勺,1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(){
float cup;
float pint;
float Oz;
float spoon;
float teaspoon;
printf("请输入杯数:");
scanf("%f", &cup);
printf("\n");
//1品脱等于2杯,1杯等于8盎司
//1 盎司等于2大汤勺(即1杯等于16大汤勺)
//1大汤勺等于3茶勺(即1杯等于48茶勺)
pint = 2 * cup;
Oz = cup / 8;
spoon = cup / 16;
teaspoon = cup / 48;
printf("%f杯等于%f品脱\n", cup, pint);
printf("%f杯等于%f盎司\n", cup, Oz);
printf("%f杯等于%f大汤勺\n", cup, spoon);
printf("%f杯等于%f茶勺\n", cup, teaspoon);
system("pause");
return 0;
}
运行结果: