C语言程序设计现代方法_第二版,CH7第七章编程题(Programming_Projects)

CH7_1

#include <stdio.h>

int main(void)
{
	int n, odd, square;

	printf("This program prints a table of squares.\n");
	printf("Enter number of entries in table: ");
	scanf("%d", &n);

	odd = 3;
	//for语句中第三句为逗号表达式
	for (int i = 1, square = 1; i <= n; printf("%10d%10d\n", i, square), square += odd, odd += 2, ++i);

	return 0;
}
//在64位机上,测得程序保持正常时,n的最大值为46340,与理论值一致;当n继续增大,输出会越界

CH7_2

#include <stdio.h>

int main(void)
{
	int n;

	printf("Enter the number: ");
	scanf("%d", &n);
	getchar();
	//第一个scanf会扫视第一个enter但并不读取
	//需要一个getchar将enter取走,防止影响后面的getchar

	for (int i = 1; i <= n; i++) {
		printf("%d\t%d\n", i, i * i);
		if (i % 24 == 0) {
			printf("Press enter to continue...");
			do {
				;
			} while (getchar() != '\n');
		}
	}

	return 0;
}

CH7_3

#include <stdio.h>

int main(void)
{
	double n, sum = 0.0;

	printf("This program sums a series of integers.\n");
	printf("Enter number (except 0): ");

	scanf("%lf", &n);
	while (n != 0.0) {
		sum += n;
		scanf("%lf", &n);
	}

	printf("The sum is:%f\n", sum);

	return 0;
}

CH7_4

#include <stdio.h>

int main(void)
{
	char ch;

	printf("Enter phone number: ");

	while ((ch = getchar()) != '\n') {
		if (ch <= 'Z' && ch >= 'A') {
			switch (ch) {
			case 65: case 66: case 67:
				ch = 50;
				break;
			case 68: case 69: case 70:
				ch = 51;
				break;
			case 71: case 72: case 73:
				ch = 52;
				break;
			case 74: case 75: case 76:
				ch = 53;
				break;
			case 77: case 78: case 79:
				ch = 54;
				break;
			case 81: case 82: case 83: case 80:
				ch = 55;
				break;
			case 84: case 85: case 86: case 87:
				ch = 56;
				break;
			case 88: case 89: case 90:
				ch = 57;
				break;
			}
		}
		printf("%c", ch);
	}

	return 0;
}

CH7_5

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    int sum = 0;
    char ch;

    printf("Enter a word: ");

    while ((ch = getchar()) != '\n')
        switch (toupper(ch)) {
        case 'D': case 'G':
            sum += 2; break;
        case 'B': case 'C': case 'M': case 'P':
            sum += 3; break;
        case 'F': case 'H': case 'V': case 'W': case 'Y':
            sum += 4; break;
        case 'K':
            sum += 5; break;
        case 'J': case 'X':
            sum += 8; break;
        case 'Q': case 'Z':
            sum += 10; break;
        default:
            sum++; break;
        }

    printf("Scrabble value: %d\n", sum);

    return 0;
}

CH7_6

#include <stdio.h>

int main(void)
{
    printf("Size of short: %d\n", (int)sizeof(short));
    printf("Size of int: %d\n", (int)sizeof(int));
    printf("Size of long: %d\n", (int)sizeof(long));
    printf("Size of long long: %d\n", (int)sizeof(long long));
    printf("Size of float: %d\n", (int)sizeof(float));
    printf("Size of double: %d\n", (int)sizeof(double));
    printf("Size of long double: %d\n", (int)sizeof(long double));

    return 0;
}

CH7_7

#include <stdio.h>

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", &num1, &denom1);
	ch = getchar();
	scanf("%d/%d", &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;
	default: printf("ERROR");
	}

	//以下为化简最简成最简分式 
	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;
}

CH7_8

#include <stdio.h>

int main(void)
{
	int hours, minutes;
	int time;
	char ch;

	printf("Enter a 12-hour time:");
	scanf("%d:%d %c", &hours, &minutes, &ch);

	if (ch == 'a' || ch == 'A') {
		time = hours * 60 + minutes;
	}
	else if (ch == 'p' || ch == 'P') {
		time = hours * 60 + minutes + 720;
	}
	else {
		printf("ERROR.\n");
		return 0;
	}

	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;
}

CH7_9

#include <stdio.h>
#include <ctype.h>

int main(void)
{
	int hours, minutes;
	int time;
	char ch;

	printf("Enter a 12-hour time:");
	scanf("%d:%d %c", &hours, &minutes, &ch);

	ch = toupper(ch);

	if (ch == 'A') {
		time = hours * 60 + minutes;
	}
	else if (ch == 'P') {
		time = hours * 60 + minutes + 720;
	}
	else {
		printf("ERROR");
		return 0;
	}

	hours = time / 60;
	minutes = time % 60;

	printf("The 24-hour time is:%d:%d", hours, minutes);

	return 0;
}

CH7_10

#include <stdio.h>
#include <ctype.h>

int main(void)
{
	int n = 0;
	char ch = 'b';

	printf("Enter a sentence: ");

	while (ch != '\n') {
		ch = tolower(getchar());
		if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
			n++;
		}
	}

	printf("Your sentence contains %d vowels.", n);

	return 0;
}

CH7_11

#include <stdio.h>
#include <ctype.h>

int main(void)
{
	char ch;
	char ch_;

	printf("Enter the name:");

	ch_ = toupper(getchar());

	while (getchar() != ' ') {

	}

	while ((ch = getchar()) != '\n') {
		printf("%c", ch);
	}

	printf(", %c.\n", ch_);

	return 0;
}

CH7_12

#include <stdio.h>

int main(void)
{
	char ch;
	double a, b;

	printf("Enter an expression: ");
	scanf("%lf", &a);

	while ((ch = getchar()) != '\n') {
		scanf("%lf", &b);

		switch (ch) {
		case '+': a = a + b; break;
		case '-': a = a - b; break;
		case '*': a = a * b; break;
		case '/': a = a / b; break;
		default: printf("ERROR\n"); break;
		}
	}

	printf("The value is %f\n", a);

	return 0;
}

CH7_13

#include <stdio.h>

int main(void)
{
	char ch, chn;
	int n = 0;
	int m = 1;
	double a = 0.0;

	printf("Enter a sentence:");

	while ((ch = getchar()) != '\n') {
		if (ch != ' ') {
			n++;
		}
		else {
			m++;
		}
	}

	a = (double)n / m;

	printf("Average word length: %.2f\n", a);

	return 0;
}

CH7_14

#include <stdio.h>
#include <math.h>

int main(void)
{
	double a, x = 1.0;
	int k = 0;

	printf("Enter the number:");
	scanf("%lf", &a);

	while (fabs(x * x - a) > 1e-15) {
		x = (x + a / x) / 2;
		k++;
	}

	printf("%d times.\n", k);	//for test
	printf("Square root:%.5f\n", x);

	return 0;
}

CH7_15

#include <stdio.h>

int main(void)
{
	int num;
	long long res = 1;

	printf("Enter a positive integer: ");
	scanf("%d", &num);

	for (int i = 1; i <= num; i++) {
		res *= i;
	}

	printf("Factorial of %d: %lld", num, res);

	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值