C语言程序设计现代方法第二版,第七章课后编程习题全部答案

自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出

但这些代码确实已通过编译,实现了书上的输出结果,还希望能给抱有期待之人作为个小参考


7.1 这个直接粘上原答案吧!

7.2

#include  

int main (void)
{
 	int i, n;
 	
 	printf ("This program prints a table of squares. \n");
 	printf ("Enter number of entries in table: ");
 	scanf ("%d", &n);
 	getchar ();     //接收scanf留下的回车 
 	
 	for (i = 1; i <= n; i++) {
 		printf ("%10d%10d\n", i, i * i);
 		if (i % 24 == 0) {
 			printf ("Press Enter to continue...");
			getchar ();
		 }
	 }

 
	return 0;
 } 

7.3

#include 

int main (void) 
{
	double n, sum = 0.0;
	
	printf ("This program sums a series of integers.\n");
	printf ("Enter inetgers (0 to terminate): ");
	
	scanf ("%lf", &n);                     //这里scanf是用的%lf, printf是用的%f 
	while (n != 0.0) {
		sum += n;
		scanf ("%lf", &n);
	}
	
	printf ("The sum is:%f\n", sum);
	
	return 0;
}

7.4

#include 

int main (void)
{
	int ch;
	
	printf ("Enter phone number: ");
	
	while ((ch = getchar ()) != '\n') {
		if (ch <= 'Z' && ch >= 'A') {
			switch (ch) {
				case 65: case 66: case 67:
					printf ("2");
					break;
				case 68: case 69: case 70:
					printf ("3");
					break;
				case 71: case 72: case 73:
					printf ("4");
					break;
				case 74: case 75: case 76:
					printf ("5");
					break;
				case 77: case 78: case 79: 
					printf ("6");
					break;
				case 81: case 82: case 83: case 80:
					printf ("7");
					break;
				case 84: case 85: case 86: case 87:
				 	printf ("8");
					break;
				case 88: case 89: case 90:
					printf ("9");
					break; 
			}
			continue;
		}
		
		printf ("%c", ch);
		
	}
	
	return 0;
}

7.5

#include 

int main (void)
{
	int sum = 0; 
	char ch;
	
	printf ("Enter a word: ");

	while ((ch = getchar ()) != '\n') {
		if ((ch<=65&&ch>=90)&&(ch<=97&&ch>=122))
			printf ("Illegal input!");
			
		switch (ch) {
			case 'A': case 'a': case 'E': case 'e': case 'I': case 'i':
			case 'L': case 'l': case 'N': case 'n': case 'O': case 'o': case 'R':
			case 'r': case 'S': case 's': case 't': case 'T': case 'U': case 'u':
				sum += 1;
				break;
			case 'd': case 'D': case 'G': case 'g':
				sum += 2;
				break;
			case 'B': case 'b': case 'C': case 'c': case 'M': case 'm':
			case 'P': case 'p':
				sum += 3;
				break;
			case 'F': case 'f': case 'H': case 'h': case 'V': case 'v':
			case 'W': case 'w': case 'Y': case 'y':
				sum += 4;
				break;
			case 'K': case 'k':
				sum += 5;
				break;
			case 'J': case 'j': case 'X': case 'x':
				sum += 8;
				break;
			case 'Q': case 'q': case 'Z': case 'z':
				sum += 10;
				break;
		}
	}
	
	printf ("Scrabble value: %d", sum);
	
	return 0;
}

上面是反面教材,不认真审题的后果就是自己瞎写,搞得贼麻烦。调用ctype.h头文件用toupper可以省不少事,这里懒得改了

7.6

#include 

int main (void)
{
	printf ("%d\n", (int)sizeof(int));
	printf ("%d\n", (int)sizeof(short));
	printf ("%d\n", (int)sizeof(long));
	printf ("%d\n", (int)sizeof(float));
	printf ("%d\n", (int)sizeof(double));
	printf ("%d\n", (int)sizeof(long double));
	
	return 0;
}

7.7

#include 

int main (void)
{
	int num1, denom1, num2, denom2, result_num, result_denom;
	char ch;
	
	printf ("Enter two fractions separated by a sign which wanted: ");
	scanf ("%d/%d%c%d/%d", &num1, &denom1, &ch, &num2, &denom2);
	
	switch (ch) {
		case '+':
			result_num = num1 *denom2 + num2 * denom1;
			result_denom = denom1 * denom2;
			break;
		case '-':
			result_num = num1 *denom2 - num2 * denom1;
			result_denom = denom1 * denom2;
			break;
		case '*':
			result_num = num1 * num2;
			result_denom = denom1 * denom2;
			break;
		case '/':
			result_num = num1 *denom2;
			result_denom = num2 * denom1;
			break;
	}
	
	
	//以下为化简最简成最简分式 
	int n, m, temp;
	n = result_num;
	m = result_denom;
	while (n != 0) {
		temp = n;
		n = m % n;
		m = temp;
	}
	
	result_num = result_num / m;
	result_denom = result_denom / m;
	
	printf ("The result is %d/%d", result_num, result_denom);
	
	return 0;
}

7.8

#include 
#include 

int main (void)
{
	int hours, minutes;
	int time;
	char ch;
	
	printf ("Enter a 12-hour time:");
	scanf ("%d:%d %c", &hours, &minutes, &ch);
	
	switch (toupper(ch)) {
		case 'P': 
			time = hours * 60 + minutes + 12 * 60;
			break;
		case 'A' :                        
			time = hours * 60 + minutes;
			break;
	}
	
	// 480 583 679 767 840 945 1140 1305 这是几个起飞时间换算为分钟的结果 
	if (time < 480){
		printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
	} else if (time < 583) {
		if ((time-480) < (583-time)) printf ("Closest departure time is 8:00 a.m., arriving at 10:16 a.m.");
		else printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
	} else if (time < 679) {
		if ((time-583) < (679-time)) printf ("Closest departure time is 9:43 a.m., arriving at 11:52 a.m.");
		else printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m");
	} else if (time < 767) {
		if ((time-679) < (767-time)) printf ("Closest departure time is 11:19 a.m., arriving at 1:31 p.m.");
		else printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m");
	} else if (time < 840) {
		if ((time-767) < (840-time)) printf ("Closest departure time is 12:47 a.m., arriving at 3:00 p.m.");
		else printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
	} else if (time < 945) {
		if ((time-840) < (945-time)) printf ("Closest departure time is 2:00 p.m., arriving at 4:08 p.m.");
		else printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
	} else if (time < 1140) {
		if ((time-945) < (1140-time)) printf ("Closest departure time is 3:45 p.m., arriving at 5:55 p.m.");
		else printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
	} else {
		if ((time-1140) < (1305-time)) printf ("Closest departure time is 7:00 p.m., arriving at 9:20 p.m.");
		else printf ("Closest departure time is 9:45 p.m., arriving at 11:58 p.m.");
	}
	
	return 0;	
} 

7.9

#include 
#include 

int main (void)
{
	int hours, minutes;
	char ch;
	
	printf ("Enter a 12-hour time: ");
	scanf ("%d:%d %c", &hours, &minutes, &ch);
	
	if (toupper(ch) == 'P') hours = hours + 12;
	
	printf ("Equivalent 24-hour time: %d:%d", hours, minutes);
	
	return 0;
 } 

7.10

#include 

int main (void)
{
	char ch;
	int sum = 0;
	
	printf ("Enter a sentence: ");
	
	while ((ch = getchar()) != '\n') {
		if ((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
			sum++;
	}
	
	printf ("Your sentence contains %d vowels", sum);
	
	return 0;
}

7.11

#include 

int main (void)
{
	int ch1, ch2;
	
	printf ("Enter a first and last name: ");
	scanf ("%c", &ch1);
	
	while ((getchar()) != ' ')
		;
	
	while ((ch2 = getchar()) != '\n') {
		printf ("%c", ch2);
	}
	printf (", %c.", ch1);
	
	return 0;
}

7.12

#include 

int main (void)
{
	double num1, num2;
	char sym;
	
	printf ("Enter an expression: ");
	scanf ("%lf", &num1);
	
	while (1) {	
		sym = getchar();
		if (sym == '\n') break;
		scanf ("%lf", &num2);
		switch (sym) {
			case '+':
				num1 = num1 + num2;
				break;
			case '-':
				num1 = num1 - num2;
				break;
			case '*':
				num1 = num1 * num2;
				break;
			case '/':
				num1 = num1 / num2;
				break;
		}
		 
	}
	
	printf ("Value of expression: %.1f", num1);
	
	return 0;
}

7.13

#include 

int main (void)
{
	char ch;
	int sum = 0, num = 1;
	
	printf ("Enter a sentence: ");
	
	while ((ch= getchar()) != '\n') {      		//输入字符直到遇到回车为止     	   
		sum++;													
		if (ch == ' ') {								
			num++;								//若遇到空格,则单词个数+1,该单词字母数-1 
			sum--;								
		} 
	}
	
	printf ("Average word length: %.1f", (float)sum/num);
	
	return 0;
}

7.14

#include 
#include 

int main (void)
{
	double x, y = 1.0;
	
	printf ("Enter a positive number: ");
	scanf ("%lf", &x);
	
	while ((fabs (y - ((y + x / y) / 2))) >= 0.00001 * y) {
		y = ((y + x / y) /2);
	}
	
	printf ("Square root: %.5f", y);
	
	return 0;
}

7.15

#include 

int main (void)
{
	int num;           // 具体换成不同的基本类型 
	int res = 1;
	
	printf ("Enter a positive integer: ");
	scanf ("%d", &num);
	
	for (int i = 1; i <= num; i++) {
		res *= i;
	}
	
	printf ("Factorial of %d: %d", num, res);
	
	return 0;
}

 

 

 

你可能感兴趣的:(C语言程序设计现代方法第二版,第七章课后编程习题全部答案)