我是靠谱客的博主 欢喜海燕,最近开发中收集的这篇文章主要介绍C Primer Plus(第六版)第八章编程题答案参考8-18-28-38-48-58-8,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

8-1

#include <stdio.h>
int main(void) {
int count = 0;
while (getchar() != EOF) {
count++;
}
printf("%d ", count);
return 0;
}

8-2

#include <stdio.h>
int main(void){
char ch;
int count = 0;
printf("请输入字符串(Ctrl+z作为结尾):");
while ((ch = getchar()) != EOF){
count++;
if (ch < ' '){//空格前的字符为非打印字符,要特殊处理
if (ch == 't'){
putchar('\');
putchar('t');
printf(":%dt", ch);
}
else if (ch == 'n'){
count = 0;
putchar('\');
putchar('n');
printf(":%dn", ch);
printf("请输入字符串(Ctrl+z作为结尾):");
}
else{
putchar('^');
putchar(ch + 64);
printf(":%dt", ch);
}
}
else{//不是非打印字符的直接输出
putchar(ch);
printf(":%d ", ch);
}
if (count % 10 == 0){//count计数,每十对换行
count = 0;
printf("n");
}
}
return 0;
}

8-3

#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
int count_lower = 0;
int count_upper = 0;
printf("请输入字符串:");
while ((ch = getchar()) != EOF) {
if (islower(ch)) {
count_lower++;
}
else if (isupper(ch)) {
count_upper++;
}
}
printf("大写字母:%dn", count_upper);
printf("小写字母:%dn", count_lower);
return 0;
}

8-4

#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
int count_letter = 0;
int count_word = 1;
int average_letter = 0;
printf("请输入一系列单词or输入整个句子:");
while ((ch = getchar()) != EOF) {
if (ch != ' ' && !ispunct(ch) && ch != 'n') {
count_letter++;
}
else if (ch == ' ') {
count_word++;
}
else if (ch == 'n') {
printf("请继续输入(输入Ctrl+z结束程序):");
}
}
average_letter = count_letter / count_word;
printf("总共有%d个字母,%d个单词,平均每个单词的字母数为:%d", count_letter, count_word, average_letter);
return 0;
}

8-5

#include <stdio.h>
int main(void) {
int guess = 50;
int lower_limit = 1;
int upper_limit = 100;
char response;
printf("Pick an integer from 1 to
100, I will try to guess it.n");
printf("Respond with a y if my guess is right and with a l if it is larger and with a s if it is smaller.n");
printf("Uh…is your number %d?n", guess);
while ((response = getchar()) != 'y') {
if (response == 'l') {//猜的太大了
upper_limit = guess;
guess = (lower_limit + upper_limit) / 2;
printf("Well, then, is it %d?n", guess);
}
else if (response == 's') {//猜的太小了
lower_limit = guess;
guess = (lower_limit + upper_limit) / 2;
printf("Well, then, is it %d?n", guess);
}
while (getchar() != 'n') {
continue;
}
}
printf("I knew I can do it!n");
return 0;
}

8-8

#include <stdio.h>
void menu();
double add(float x, float y);
double subtract(float x, float y);
double multiply(float x, float y);
double divide(float x, float y);
int main(void) {
menu();
float n1, n2;
char choice;
double result;
while ((choice = getchar()) != 'q') {
if (choice == 'a' || choice == 's' || choice == 'm' || choice == 'd') {//如果输入了指定的字母,则接下来输入需要计算的数字
printf("Enter first number:");
while (scanf("%f", &n1) != 1) {//若输入的不是数字,则不断提醒用户输入数字,并清空scanf缓存
printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
while (getchar() != 'n') {
continue;
}
}
printf("Enter second number:");
while (scanf("%f", &n2) != 1) {//若输入的不是数字,则不断提醒用户输入数字,并清空scanf缓存
printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
while (getchar() != 'n') {
continue;
}
}
}
else {
printf("Please enter the right choice:n");
}
switch (choice) {
case 'a':
result = add(n1, n2);
printf("%.3lfn", result);
break;
case 's':
result = subtract(n1, n2);
printf("%.3lfn", result);
break;
case 'm':
result = multiply(n1, n2);
printf("%.3lfn", result);
break;
case 'd':
result = divide(n1, n2);
printf("%.3lfn", result);
break;
default:
break;
}
while (getchar() != 'n') {
continue;
}
menu();
}
printf("Bye!");
return 0;
}
void menu() {
printf("Enter the operation of your choice:n");
printf("a. add
s. subtractn");
printf("m. multiply
d. dividen");
printf("q. quitn");
return;
}
double add(float x, float y) {
return x + y;
}
double subtract(float x, float y) {
return x - y;
}
double multiply(float x, float y) {
return x * y;
}
double divide(float x, float y) {
while (y == 0) {
printf("Enter a number other than 0:");
scanf("%f", &y);
}
return x / y;
}

最后

以上就是欢喜海燕为你收集整理的C Primer Plus(第六版)第八章编程题答案参考8-18-28-38-48-58-8的全部内容,希望文章能够帮你解决C Primer Plus(第六版)第八章编程题答案参考8-18-28-38-48-58-8所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部