编译环境VS Code+WSL GCC 源码请到文末下载 。 我给第一题写了Linux shell脚本,感兴趣的同学可以尝试修改并运行一下。

/*第1题*************************/
#include<stdio.h>
int main(void)
{
int cnt = 0;
while (getchar() != EOF)
{
cnt++;
}
printf("总共读取了%d个字符\n",cnt);
return 0;
}
/*第2题*************************/
#include<stdio.h>
void format_print(int ch);
int main(void)
{
int cnt = 0,ch = 0;
while ((ch = getchar()) != EOF)
{
cnt++;
format_print(ch);
if(0 == cnt % 9)
putchar(10);
}
printf("\n总共读取了%d个字符\n",cnt);
return 0;
}
void format_print(int ch)
{
if(ch < 33)
{
printf("^%c:%-5d",ch+32,ch);
}
else if(ch < 127)
{
printf("%c:%-5d",ch,ch);
}
else
{
printf("END:%-5d",ch);
}
}
/*第3题*************************/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
int cnt_lower = 0,cnt_upper = 0,ch = 0;
while ((ch = getchar()) != EOF)
{ if(islower(ch))
{
cnt_lower++;
}
else if (isupper(ch))
{
cnt_upper++;
}
}
printf("总共读取了%d个大写字母,%d个小写字母\n",cnt_upper,cnt_lower);
return 0;
}
/*第4题*************************/
#include<stdio.h>
#include<ctype.h>
#define YES -1
#define NO 0
int main(void)
{
int cnt_words = 0,cnt_alpha = 0,start_cnt = NO,ch = 0;
while ((ch = getchar()) != EOF)
{ if(isalpha(ch) && start_cnt == NO)
{ /*如果没有开始计数并且字符是个字母,则开始计数*/
start_cnt = YES;
}
else if (!isalpha(ch) && start_cnt == YES)
{ /*如果已经有开始计数并且字符不是个字母,则停止计数,单词数量+1*/
start_cnt = NO;
cnt_words++;
}
else if (isalpha(ch) && start_cnt == YES)
{ /*如果已经有开始计数并且字符是个字母,开始字母计数,字母数量+1*/
cnt_alpha++;
} }
printf("总共读取了%d单词,%d字母,平均每个单词%g个字母\n",cnt_words,cnt_alpha,1.0 * cnt_alpha / cnt_words);
return 0;
}
/*第5题*************************/
/* guess.c -- an inefficient and faulty number-guesser */
#include <stdio.h>
int main(void)
{
int guess_max = 100,guess_min = 0,guess = 50; printf("想一个1~100的整数,我会尝试猜出它!\n");
printf("如果我猜错了,请输入'n'\n如果我猜对了,请输入'y'\n");
printf("呃...你想的是 %d?\n", guess);
while (getchar() != 'y') /* get response, compare to y */
{
while (getchar() != '\n')/*清空stdin缓存*/
continue;
printf("你想的数比%d大吗?(y\\n)\n", guess);
if(getchar() == 'y')
{
guess_min = guess;
guess = (guess_max + guess_min) / 2;
}
else
{
guess_max = guess;
guess = (guess_max + guess_min) / 2;
}
while (getchar() != '\n')/*清空stdin缓存*/
continue;
printf("呃...你想的是 %d?\n", guess);
} printf("我就知道我能猜中!\n"); return 0;
} /*第6题*************************/
/* menuette.c -- menu techniques */
#include <stdio.h>
#include <ctype.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
void count(void); while ( (choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a' : printf("Buy low, sell high.\n");
break;
case 'b' : putchar('\a'); /* ANSI */
break;
case 'c' : count();
break;
default : printf("Program error!\n");
break;
}
}
printf("Bye.\n"); return 0;
} void count(void)
{
int n,i; printf("Count how far? Enter an integer:\n");
n = get_int();
for (i = 1; i <= n; i++)
printf("%d\n", i);
while ( getchar() != '\n')
continue;
} char get_choice(void)
{
int ch; printf("Enter the letter of your choice:\n");
printf("a. advice b. bell\n");
printf("c. count q. quit\n");
ch = get_first();
while ( (ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
} return ch;
} char get_first(void)
{
int ch;
while (isspace(ch = getchar()))
continue;
return ch;
} int get_int(void)
{
int input;
char ch; while (scanf("%d", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // dispose of bad input
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
} return input;
} /*第7题*************************/
#include<stdio.h>
#include<ctype.h>
//#define BASE_TIME 10
#define OVER_TIME (BASE_TIME * 1.5)
#define BASE_TAX 0.15
#define ADD_TAX 0.2
#define OTHER_TAX 0.25
int main()
{ //总工作时间,总工资,净工资,总税收
float work_times,all_salary,clear_salary,all_tax,BASE_TIME;
int case_num; while (1)
{
printf("*************************************\n\n");
printf("请输入数子对应的工资等级,输入q退出程序:\n");
printf("1) $8.75/hr 2) $9.33/hr\n");
printf("3) $10.0/hr 4) $11.2/hr\n");
printf("q) exit\n");
printf("*************************************\n");
if(!isalnum(case_num = getchar()))
{ //排除非数字和不在范围内
printf("输入错误,请重新输入!\n");
while (getchar() != '\n')
continue;//清理缓存
continue;//跳过下面的内容,重新开始循环
}
while (getchar() != '\n')
continue;//清理缓存
switch (case_num)
{
case '1':
BASE_TIME =8.75;
break;
case '2':
BASE_TIME =9.33;
break;
case '3':
BASE_TIME =10;
break;
case '4':
BASE_TIME =11.2;
break;
case 'q':
printf("程序退出!\n");
goto end;//跳出两重循环直接到程序结尾
default:
printf("输入错误,请重新输入!\n");
break;
}
}
printf("您一周工作了多少小时?\n");
scanf("%f",&work_times);
if(work_times > 40)
{
all_salary = (work_times * BASE_TIME) + (work_times - 40) * OVER_TIME;
}
else
{
all_salary = work_times * BASE_TIME;
} if(all_salary <= 300)
{
all_tax = all_salary * BASE_TAX;
}
else if(all_salary <= 450 && all_salary >300)
{
all_tax = 300 * BASE_TAX + (all_salary - 300) * ADD_TAX;
}
else
{
all_tax = 300 * BASE_TAX + 150 * ADD_TAX + (all_salary - 450) * OTHER_TAX;
}
clear_salary =all_salary - all_tax;
printf("总工作时间:%g\n总工资:%g\n净工资:%g\n总税收:%g\n",work_times,all_salary,clear_salary,all_tax);
end:return 0;
}
/*第8题*************************/
#include<stdio.h>
#include<ctype.h>
void calculator(int i);
int main()
{
int case_num;
while (1)
{
printf("*************************************\n\n");
printf("请选择计算符号,输入5退出程序:\n");
printf("1) + 2) -\n");
printf("3) * 4) /\n");
printf("5) exit\n");
printf("*************************************\n");
if(scanf("%d",&case_num) != 1 && case_num < 5 && case_num > 0)
{ //排除非数字和不在范围内
printf("输入错误,请重新输入!\n");
while (getchar() != '\n')
continue;//清理缓存
continue;//跳过下面的内容,重新开始循环
}
switch (case_num)
{
case 1:
calculator(case_num);
break;
case 2:
calculator(case_num);
break;
case 3:
calculator(case_num);
break;
case 4:
calculator(case_num);
break;
case 5: default:
printf("程序退出!\n");
return 0;
break;
}
}
}
void calculator(int i)
{
float l,r;
while (1)
{
printf("请输入两个数,例如2.1 3:\n");
if(scanf("%f %f",&l,&r) != 2)
{ //排除非数字和不在范围内
printf("输入错误,请重新输入!\n");
while (getchar() != '\n')
continue;//清理缓存
continue;//跳过下面的内容,重新开始循环
}
break;
}
switch (i)
{
case 1:
printf("%g + %g = %g\n",l,r,l+r);
break;
case 2:
printf("%g - %g = %g\n",l,r,l-r);
break;
case 3:
printf("%g * %g = %g\n",l,r,l*r);
break;
case 4:
printf("%g / %g = %g\n",l,r,l/r);
break;
default:
break;
}
}

C Primer Plus 第6版 第八章 编程练习参考答案的更多相关文章

  1. C Primer Plus 第六版—— 6.16 编程练习题(附代码)

    1.编写一个程序,创建一个包含26个元素的数组,并在其中存储26个小写字母.然后打印数组的所有内容. #include <stdio.h> int main(void) { int num ...

  2. c++ primer plus 第6版 部分二 5- 8章

    ---恢复内容开始--- c++ primer plus 第6版 部分二    5-  章 第五章 计算机除了存储外 还可以对数据进行分析.合并.重组.抽取.修改.推断.合成.以及其他操作 1.for ...

  3. C++ Primer Plus 第六版笔记

    C++ Primer Plus 第六版笔记 关于对象声明的思考 转自:http://www.cnblogs.com/weiqubo/archive/2009/11/02/1930042.html C+ ...

  4. C Primer Plus(第五版)1

    这是C Primer Plus(第五版)的第一章,上传上来主要是方便我进行做笔记,写注释,还有我会删掉一些“废话”等. 1.1 C语言的起源 贝尔实验室的 Dennis Ritchie 在1972年开 ...

  5. 【原创】一起学C++ 之指针、数组、指针算术 ---------C++ primer plus(第6版)

    C++ Primer Plus 第6版 指针和数组基本等价的原因在于指针算术! 一.指针 ⑴整数变量+1后,其值将增加1: ⑵指针变量+1后,增加的量等于它指向的类型的字节数: ⑶C++将数组名解析为 ...

  6. 推荐《C Primer Plus(第五版)中文版》【worldsing笔记】

      老外写的C书,看了你会有一种哇塞的感觉,这里提供PDF扫描版的下在,包含数内的例程,请大家支持原版!! C Primer Plus(第五版)中文版.pdf  下载地址:http://pan.bai ...

  7. 【原创】一起学C++ 之 字符串 ---------C++ primer plus(第6版)

    C++ Primer Plus 第6版 字符串:是存储在内存的连续字节中的一系列字符. C++处理字符串的方式有2种: 一.来自C语言.常被称为C-风格字符串(C-Style-string) 1)从字 ...

  8. C++ Primer中文版(第5版)

    <C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼)    Josee Lajoie(约瑟 拉乔伊)    Barbar ...

  9. 《C++ Primer》 第四版 第7章 函数

    <C++ Primer> 第四版 第7章 函数 思维导图笔记 超级具体.很具体,图片版,有利于复习查看 http://download.csdn.net/detail/onlyshi/94 ...

  10. c++ primer plus 第6版 部分三 9章 - 章

    c++ primer plus 第6版                                               部分三 9章 - 章 第9章   内存模型和名称空间 1.单独编译 ...

随机推荐

  1. synchronized锁的内容

    synchronized锁的内容 import java.util.concurrent.TimeUnit; class Test1 { public static void main(String[ ...

  2. linux终端高级玩法详细介绍

    专注于收集整理更多好玩技巧 更改终端命令行颜色 vi /etc/profile PS1='[\[\e[32m\]\u\[\e[0m\]\[\e[35m\]@\[\e[0m\]\[\e[33m\]\h\ ...

  3. Mac 中 NSTrackingArea 鼠标移动事件捕获

    在Mac系统中处理View的鼠标进入,退出以及移动事件时,需要把捕获的区域设置为view的bounds 不能设置为frame. 1 self.trackingArea = [[[NSTrackingA ...

  4. vue3:computed

    扫码或者点击文字后台提问 原文: https://mp.weixin.qq.com/s/36dd--oj6jmkZblfJRh4iw computed 支持选项式写法 和 函数式写法 1.选项式写法 ...

  5. ARC134C The Majority

    ARC134C The Majority link:[ARC134C] The Majority 小清新数学题.(反正我做不出来) 简要题意 有\(K\)个箱子,编号为\(1\)到\(K\)的箱子.起 ...

  6. etcdv3与etcdv2特性比较

    1 客户端通信方式 etcdv3的客户端使用gRPC与server进行通信,通信的消息协议使用protobuf进行约定,代替了v2版本的HTTP-json格式,使用二进制替代文本,更加节省空间. 同时 ...

  7. 【深入Java虚拟机】之七:Java编译与JIT编译

    编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理机的目标代码或虚拟机能执行的指令集之前,都会按照如下图所示的各个步骤进行: 其中绿色的模块可以选择性实现.很容易看出,上图中 ...

  8. Redis集群之常用操作

    Redis Cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持(手动命令行添加集群的方式不变),集合到redis-cli里,避免了再安装ruby的相关环境.直接使用red ...

  9. MySQL中INSERT INTO ... ON DUPLICATE KEY UPDATE浅析

    最近在做一个阅读次数的需求的时候,有这样一个场景,如果数据库中没有数据,就进行INSERT操作,有数据的话,阅读次数就+1.此处有两种实现方式,一种是想将数据查出来,在Java中进行处理,没有就INS ...

  10. 问题解决:windows主机开机不插屏幕不能自动进入桌面

    操作系统一般都有这种设定,不论是windows还是Linux系统,那就是主机开机不插屏幕不能自动进入桌面操作系统一般都有这种设定,不论是windows还是Linux系统,那就是主机开机不插屏幕不能自动 ...