C Primer Plus(第六版)第六章编程题答案参考

6-1

#include <stdio.h>
int main(void) {
	char letter[26];
	letter[0] = 'a';
	for (int i = 1; i < 26; i++) {
		letter[i] = letter[i - 1] + 1;
	}
	for (int i = 0; i < 26; i++) {
		printf("%c", letter[i]);
	}
	return 0;
}

6-2

#include <stdio.h>
int main(void) {
	for (int i = 1; i < 6; i++) {
		for (int j = 1; j <= i; j++) {
			printf("$");
		}
		printf("\n");
	}
	return 0;
}

6-3

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

6-4

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

6-5

#include <stdio.h>
#define A 'A'
int main(void) {
	char ch;
	char tem;
	printf("请输入一个大写字母:");
	scanf("%c", &ch);
	int row = ch - A + 1;
	for (int i = 1; i <= row; i++) {
		//char tem;
		for (int j = 0; j < row - i; j++) { //左边的空格
			printf(" ");
		}
		for (int j = 0; j < i; j++) {//左边的字母
			tem = A + j;
			printf("%c", tem);
		}
		for (int j = 1; j < i; j++) {//右边的字母
			tem = tem - 1;
			printf("%c", tem);
		}
		printf("\n");
	}
	return 0;
}

6-6

#include <stdio.h>
int main(void) {
	int min_num, max_num;
	/*printf("请输入表格的上下限:");
	scanf("%d%d", &max_num, &min_num);*/
	printf("请输入表格的上限(最大值):");
	scanf("%d", &max_num);
	printf("请输入表格的下限(最小值):");
	scanf("%d", &min_num);
	for (int i = min_num; i <= max_num; i++) {
		printf("%d\t%d\t%d\n", i, i * i, i * i * i);
	}
	return 0;
}

6-7

#include <stdio.h>
#include <string.h>
int main(void) {
	char str[20];
	int sz;
	printf("请输入一个单词:");
	scanf("%s", str);//scanf无法接收带空格的字符串
	sz = strlen(str);
	for (int i = sz - 1; i >= 0; i--) {
		printf("%c", str[i]);
	}
	return 0;
}

6-8

#include <stdio.h>
int main(void) {
	float f1, f2;
	printf("请输入两个浮点数:");
	int ret = scanf("%f %f", &f1, &f2);
	while (ret == 2) {
		printf("两数之差/两数乘积 = %f\n", (f1 - f2) / (f1 * f2));
		printf("请输入两个浮点数(输入q退出程序):");
		ret = scanf("%f %f", &f1, &f2);
	}
	printf("Done!");
	return 0;
}

6-9

#include <stdio.h>
float Cum(float f1, float f2);
int main(void) {
	float f1, f2;
	printf("请输入两个浮点数:");
	int ret = scanf("%f %f", &f1, &f2);
	while (ret == 2) {
		printf("两数之差/两数乘积 = %f\n", Cum(f1,f2));
		printf("请输入两个浮点数(输入q退出程序):");
		ret = scanf("%f %f", &f1, &f2);
	}
	printf("Done!");
	return 0;
}
float Cum(float f1, float f2) {
	float res = (f1 - f2) / (f1 * f2);
	return res;
}

6-10

#include <stdio.h>
int main(void) {
	int min_num, max_num;
	printf("Enter lower and upper integer limits:");
	scanf("%d %d", &min_num, &max_num);
	while (max_num > min_num) {
		int sum = 0;//不要忘记初始化!
		for (int i = min_num; i <= max_num; i++) {
			sum = i * i + sum;
		}
		printf("The sums of the squares from %d to %d is %d\n", min_num * min_num, max_num * max_num, sum);
		printf("Enter next set of limits:");
		scanf("%d %d", &min_num, &max_num);
	}
	printf("Done!");
	return 0;
}

6-11

#include <stdio.h>
int main(void) {
	int num[8];
	printf("请输入8个整数:");
	for (int i = 0; i < 8; i++) {
		scanf("%d", &num[i]);
	}
	for (int j = 0; j < 8; j++) {
		printf("%d\t", num[7 - j]);
	}
	return 0;
}

6-12

#include <stdio.h>
int main(void) {
	int num;
	float sum1 = 0.0;
	float sum2 = 0.0;
	printf("请输入项数:");
	scanf("%d", &num);
	while (num > 0) {
		for (int i = 1; i <= num; i++) {
			sum1 = sum1 + 1.0 / i;
		}
		printf("sum1 = %f\n", sum1);
		for (int i = 1; i <= num; i++) {
			if (i % 2 == 0) {
				sum2 = sum2 - 1.0 / i;
			}
			else {
				sum2 = sum2 + 1.0 / i;
			}
		}
		printf("sum2 = %f\n", sum2);
		printf("请输入项数(输入0退出程序):");
		scanf("%d", &num);
	}
	printf("Done!");
	return 0;
}

6-13

#include <stdio.h>
int main(void) {
	int num[8];
	num[0] = 2;
	int j = 0;
	for (int i = 1; i < 8; i++) {
		num[i] = 2 * num[i - 1];
	}
	do {
		printf("%d\t", num[j]);
		j++;
	} while (j < 8);

	return 0;
}

6-14

#include <stdio.h>
int main(void) {
	double num[8];
	double sum[8];
	int sum8 = 0;
	printf("请输入8个数:");
	for (int i = 0; i < 8; i++) {
		scanf("%lf", &num[i]);
	}
	for (int j = 0; j < 8; j++) {
		sum8 = sum8 + num[j];
		sum[j] = sum8;
	}
	for (int i = 0; i < 8; i++) {
		printf("%lf\t", num[i]);
	}
	printf("\n");
	for (int i = 0; i < 8; i++) {
		printf("%lf\t", sum[i]);
	}

	return 0;
}

6-15

#include <stdio.h>
#include <string.h>
int main(void) {
	char input[256];
	printf("请输入一行字符串:");
	int i = 0;
	do {
		scanf("%c", &input[i]);
	} while (input[i] != '\n' && ++i);//此时结束i下标为最后一个\n处
	for (--i; i >= 0; i--) {//让i先落到字符串最后一个字符处
		printf("%c", input[i]);
	}
	return 0;
}

6-16

#include <stdio.h>
#define RATE_DA 0.1
#define RATE_DE 0.05
int main(void) {
	double sum_da = 100;
	double sum_de = 100;
	int count_year = 1;
	do {
		sum_da = sum_da + 100 * RATE_DA;
		sum_de = sum_de + sum_de * RATE_DE;
		count_year++;
	} while (sum_de < sum_da);
	printf("第%d年,Deirdre的投资额超过了Daphne.\n", count_year);
	return 0;
}

6-17

#include <stdio.h>
#define RATE 0.08
int main(void) {
	double count = 100;
	int year = 0;
	do {
		count = count + count * RATE;
		count -= 10;
		year++;
	} while (count > 9);
	printf("第%d年账户钱会被取完", year);
	return 0;
}

6-18

#include <stdio.h>
int main(void) {
	//int firends = 5;
	//int week = 1;
	for (int week = 1, friends = 5; friends <= 150; week++) {
		friends = friends - week;
		friends = friends * 2;
		printf("第%d周Raund博士有%d个朋友。\n", week, friends);
	}
	return 0;
}
上一篇:C Primer Plus(第6版)—第4章编程练习


下一篇:C primer plus 第14章编程练习