"C Primer Plus" (sixth edition) answers (3.11)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/99874174

To use the "C Primer Plus" C beginners learning to prepare.

  1. Observe how to pass the test (to write the program in question) system handles integer overflow, underflow underflow floating-point and floating-point numbers.
#include <stdio.h>
int main(int argc, char const **argv) {
	/*32位的有符号整型的上界是2^32-1(4294967295) 再加一就溢出了(想要深入了解可以看看计算机组成原理)*/
	int a = 4294967295;
	a = a + 1;
	printf("%d\n", a);
	
	/*10e100次方上溢 0.00000000012下溢*/
	float b = 10e100;
	printf("%f", b);
	b = 0.00000000012; 
	printf(" %f", b); 
	 
	return 0;
} 
  1. Write a program that requires prompt enter the ASCII code, and then print the characters entered.
#include <stdio.h>
int main(int argc, char const **argv) {
	char c;
	printf("Input a number:");
	scanf("%d", &c);
	//%c表示字符格式输出 
	printf("%d in ASCII is %c.", c, c);
	return 0;
} 
  1. Write a program, uttered an alarm. Then print the following text.
    Text Picture
#include <stdio.h>
int main(int argc, char const **argv) {
	/*转义字符\a代表警报*/
	printf("\a");
	
	printf("Started by the sudden sound, Sally shouted,\n");
	/*想要输出"得使用转义字符*/ 
	printf("\"By the Great Pumpkin, what was that!\"");
	
	return 0;
} 
  1. Write a program that reads a floating-point, first printed in decimal form, then print an exponential form. Then, if the system supports, and then printed notation p (hexadecimal notation). Output in the following format (the number of bits actually displayed index may vary from system to system).
    Output Format
#include <stdio.h>
int main(int argc, char const **argv) {
	float f;
	
	printf("Enter a float-point value:");
	scanf("%f", &f);
	
	printf("fixed-point notation:%f\n", f);
	printf("exponential notation:%e\n", f);
	printf("p notation:%a\n", f);
	
	return 0; 
} 
  1. A year about 3.156 × 10 7 seconds. Write a program that prompts the user to output age, and then outputs a corresponding number of seconds.
#include <stdio.h>
int main(int argc, char const **argv) {
	/*用float可能会溢出(我没多想,你可以试试)*/ 
	double d = 3.165e7;
	int age;
	
	printf("Input your age:");
	scanf("%d", &age);
	
	/*整型和浮点型进行运算会自动转成浮点型*/
	printf("The result is %.0lfs.", age*d);
	
	return 0;
} 
  1. A mass of a water molecule is about 3.0 x 10 -23 grams. 1 quart of water is about 950 grams. Write a program that prompts the user to enter the number of quarts of water, and displays the number of water molecules.
#include <stdio.h>
int main(int argc, char const **argv) {
	double d = 3.0e-23;
	double a;
	
	printf("输入水的夸脱数:");
	scanf("%lf", &a);
	
	printf("%lf夸脱水中水分子的数量是:%.0lf", a, (a*950)/d);
	
	return 0;
} 
  1. 1 inch corresponds to 2.54 cm. Write a program that prompts the user to enter height (inches), and then display height in centimeters.
#include <stdio.h>
int main(int argc, char const **argv) {
	float f;
	
	printf("输入身高(英寸):");
	scanf("%f", &f);
	
	printf("你的身高是%.2f(cm)", f*2.54);
	
	return 0;
} 
  1. US volume measurement system, 2 is equal to 1 pint cups, cup equals 8 ounces, 1 ounce equals 2 tablespoon, 1 tablespoon teaspoon equal to 3. Write a program that prompts the user to enter the number of cups and pints, ounces, tablespoons, teaspoons display equivalent capacity.
#include <stdio.h>
int main(int argc, char const **argv) {
	float f;
	
	printf("输入杯数:");
	scanf("%f", &f);
	
	float pint = f / 2;//品脱 
	float ounce = f * 8;//盎司 
	float spoon = ounce * 2;//汤勺 
	float s = spoon * 3;//药勺 不知道英文是什么 就这样命名吧
	printf("品脱:%.1f	盎司:%.1f	汤勺:%.1f	药勺:%.1f", pint, ounce, spoon, s);
	
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/99874174