C Primer Plus 第6版 第四章编程练习 1-12题 答案

所有代码均在VS2017最新版下编译通过,程序运行正常。这是我自己的答案,太懒了,所以第十二题之后没做(明明自己是个菜鸡还不练 )。这不是官方答案(好像官方答案是挑着题给的,不是全给),仅供参考,如有错误还请海涵。

【第一题】

#include <stdio.h>
int main(void)
{
	char lowercase[26];
	char ch;
	int count;
	for (count = 0,ch = 'a'; count < 26,ch <= 'z'; count++,ch++) {
		lowercase[count] = ch;
	}
	for (count = 0; count < 26; count++) {
		printf("%c ", lowercase[count]);
	}
	return 0;
}

【第二题】

#include <stdio.h>
int main(void)
{
	char dollar_sign;
	int row, column;
	dollar_sign = '$';
	for (row = 1; row <= 5; row++) {
		for (column = 1; column <= row; column++) {
			printf("%c", dollar_sign);
		}
		printf("\n");
	}
	return 0;
}

【第三题】

#include <stdio.h>
int main(void)
{
	char letter_shown;
	int row,column;
	for (row = 1; row <= 6; row++) {
		for (column = 1, letter_shown = 'F'; column <= row; column++, letter_shown--) { 
			printf("%c", letter_shown);
		}
		printf("\n");
	}
	return 0;
}

【第四题】

#include <stdio.h>
int main(void)
{
	char letter_shown;
	int row, column;
	letter_shown = 'A'; 
	for (row = 1; row <= 6; row++) {
		for (column = 1; column <= row; column++) {
			printf("%c", letter_shown);
			letter_shown++;
		}
		printf("\n");
	}
	return 0;
}

【第五题】

#include <stdio.h>
int main(void)
{
	char letter_input;
	char letter_shown;
	int row, count_a, count_b, count_c;
	printf("Please input the extended aim:\n");
	scanf_s("%c", &letter_input);//用户输入终止字母
	const count = letter_input - (int)'A' + 1;//定义静态变量count
	for (row = 1; row <= count; row++) {
		for (count_a = 1; count_a <= (count - row); count_a++) {
			printf(" "); //打印升序部分空格
		}
		for (count_b = 1, letter_shown = 'A'; count_b <= row; count_b++, letter_shown++) {
			printf("%c", letter_shown);//打印升序字母
		}
		for (count_c = 1, letter_shown = letter_input - count + row - 1; count_c <= (row - 1); count_c++, letter_shown--) {
			printf("%c", letter_shown);//打印除输入字母外的降序字母
			//该循环打印的首字母 = (count - row) + 1,观察得知
		}
		for (count_a = 1; count_a <= (count - row); count_a++) {
			printf(" "); //打印降序部分空格
		}
		printf("\n");
	}
	return 0;
}

【第六题】

#include <stdio.h>
int main(void)
{
	int lower_limit, upper_limit;
	int row;
	int ans_square, ans_cube,linshi_integer;
	printf("Please input the lower limit number in integer:\n");
	scanf_s("%d", &lower_limit);
	printf("Please input the upper limit number in integer:\n");
	scanf_s("%d", &upper_limit);
	printf("\n");
	printf("%16s%16s%16s\n", "Integer", "Square", "Cube");
	for (row = 1,linshi_integer = lower_limit; row <= (upper_limit - lower_limit + 1); row++,linshi_integer++) {
		ans_square = linshi_integer * linshi_integer;
		ans_cube = linshi_integer * linshi_integer * linshi_integer;
		printf("%16d%16d%16d\n", linshi_integer, ans_square, ans_cube);
	}
	return 0;
}

【第七题】

#include <stdio.h>
#include <string.h>
int main(void)
{
	char input[41];
	int count;
	printf("Please input a single word:\n");
	scanf_s("%s", input,41);
	printf("Here is the backward word:\n");
	for (count = strlen(input) - 1; count >= 0; count--) { 
		printf("%c", input[count]);
	}
	return 0;
}

【第八题】

#include <stdio.h>
int main(void)
{
	double a, b;
	double linshi1, linshi2,ans;
	printf("Please input the first floating-point number:\n");
	while (scanf_s("%lf", &a) == 1) {
		printf("Please input the second floating-point number:\n");
		scanf_s("%lf", &b);
		linshi1 = a - b;
		linshi2 = a * b;
		ans = linshi1 / linshi2;
		printf("The answer is: %f\n", ans);
		printf("Please input the first floating-point number:\n");
	}
	return 0;
}

【第九题】

#include <stdio.h>
double calculation(double a, double b);
int main(void)
{
	double a, b,ans;
	printf("Please input the first floating-point number:\n");
	while (scanf_s("%lf", &a) == 1) {
		printf("Please input the second floating-point number:\n");
		scanf_s("%lf", &b);
		ans = calculation(a, b);
		printf("The answer is: %f\n", ans);
		printf("Please input the first floating-point number:\n");
	}
	return 0;
}
double calculation(double a, double b) {
	double linshi1, linshi2, ans;
	linshi1 = a - b;
	linshi2 = a * b;
	ans = linshi1 / linshi2;
	return ans;
}

【第十题】

#include <stdio.h>
int main(void)
{
	int lower_limit, upper_limit, count,sum = 0,square;
	printf("Enter lower and upper integer limits:");
	scanf_s("%d %d", &lower_limit, &upper_limit);
	while (upper_limit > lower_limit) {
		for (count = lower_limit; count <= upper_limit; count++) {
			square = count * count;
			sum += square;
		}
		printf("The sums of the squares from %d to %d is %d\n", lower_limit * lower_limit, upper_limit * upper_limit, sum);
		sum = 0;
		printf("Enter next set of limits:");
		scanf_s("%d %d", &lower_limit, &upper_limit);
	}
	printf("Done\n");
	return 0;
}

【第十一题】

#include <stdio.h>
int main(void)
{
	int input[8],count;
	printf("Please input eight integers and press the enter key:\n");
	for (count = 0; count < 8; count++) {
		scanf_s("%d",& input[count]);
	}
	printf("Here are the reverse order results:\n");
	for (count = 7; count >= 0; count--) {
		printf("%d ", input[count]);
	}
	return 0;
}

【第十二题】

#include <stdio.h>
int main(void)
{
	double sum = 0.0,count;
	int limit;
	printf("Please enter the limit of terms:");
	scanf_s("%d", &limit);
	while (limit > 0) {
		if (limit % 2 == 0) { //even term number given
			for (count = 1.0;count < (double)limit; count += 2.0) {
				sum += (1.0 / count);
			}
		}
		else if (limit % 2 != 0) { //odd term number given
			for (count = 1.0;count <= (double)limit; count += 2.0) {
				sum += (1.0 / count);
			}
		}
		printf("Sum is %lf\n", sum * 2.0);
		sum = 0.0;
		printf("Please enter the limit of terms:");
		scanf_s("%d", &limit);
	}
	return 0;
}

弄了一个下午+半个晚上,水平还是有待提高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值