《C++ Primer Plus》(第6版)第6章编程练习

news/2024/4/27 1:44:10/文章来源:https://blog.csdn.net/ProgramNovice/article/details/129265746

《C++ Primer Plus》(第6版)第6章编程练习

  • 《C++ Primer Plus》(第6版)第6章编程练习
    • 1. 大小写转换
    • 2. 平均值
    • 3. 菜单
    • 4. 成员
    • 5. 收入所得税
    • 6. 捐款
    • 7. 统计单词
    • 8. 统计文件字符数
    • 9. 重写编程练习6

《C++ Primer Plus》(第6版)第6章编程练习

1. 大小写转换

编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)。

代码:

#include <iostream>
#include <cctype>
using namespace std;int main()
{char c;cout << "Enter text for analysis(enter @ to quit):\n";while (cin >> c && c != '@'){if (islower(c))c = toupper(c);else if (isupper(c))c = tolower(c);if (!isdigit(c))cout << c;}cout << "Done!\n";system("pause");return 0;
}

在这里插入图片描述

2. 平均值

编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

代码:

#include <iostream>
#include <array>
using namespace std;
#define ArrSize 10int main(void)
{array<double, ArrSize> donation;cout << "Enter the elements you want to exist in the array "<< "( a non number input to terminate):" << endl;int i = 0;double sum = 0;int elem = 0;int count = 0;while (i < ArrSize && cin >> donation[i]){elem++;sum += donation[i];i++;}double average = sum / elem;for (i = 0; i < ArrSize; i++){if (donation[i] > average)count++;}cout << "average = " << average << endl;cout << count << " numbers greater than average.\n";system("pause");return 0;
}

运行结果:

在这里插入图片描述

3. 菜单

编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:

Please enter one of the following choices :
c) carnivore       p) pianist
t) tree                 g) game
f
Please enter a c, p, t, or g: qPlease enter a c, p,t, or g: tA maple is a tree.

代码:

#include <iostream>
using namespace std;
int main()
{char ch;cout << "Please enter one of the following choices:" << endl;cout << "c) carnivore           p) pianist" << endl;cout << "t) tree                g) game" << endl;while (cin >> ch){switch (ch){case 'c':cout << "A tiger is a carnivore." << endl;break;case 'p':cout << "Langlang is a pianist." << endl;break;case 't':cout << "A maple is a tree." << endl;break;case 'g':cout << "Golf is a game." << endl;break;default:cout << "Please enter a c, p, t, or g: ";}}system("pause");return 0;
}

运行结果:

在这里插入图片描述

4. 成员

加入 Benevolent Order of Programmer后,在 BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:

// Benevolent Order of Programmers name structure
struct bop {
char fullname [strsize] ;// real namechar title[strsize] ;         // job title
char bopname [strsize] ;  //secret BOP name
int preference;                //0 = fullname,1 = title, 2 = bopname);

该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:

a. display by name         b. display by title
c. display by bopname      d. display by preference
q. quit

注意,“display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:

Benevolent order of Programmers Report
a. display by name         b. display by title
c. display by bopname      d. display by preference
q. quit
Enter your choice: a
wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOFY
Next choice: q
Bye!

代码:

#include <iostream>
using namespace std;
#define LEN 5
#define strsize 20
// Benevolent Order of Programmers name structure
struct bop
{char fullname[strsize]; // real namechar title[strsize];    // job titlechar bopname[strsize];  // secret BOP nameint preference;         // 0 = fullname,1 = title, 2 = bopname);
};void display_by_name(bop *);
void display_by_title(bop *);
void display_by_bopname(bop *);
void display_by_preference(bop *);int main()
{char ch;bop member[LEN] ={{"Wimp Mache", "BOSS", "AS", 0},{"Raki Rhodes", "Junior Programmer", "MA", 1},{"Celia Laiter", "Manager", "MIPS", 2},{"Hoppy Hipman", "Analyst Trainee", "CL", 1},{"Pat Hand", "Student", "LOOPY", 2}};cout << "Benevolent Order of Programmers Report\n";cout << "a. display by name     b. display by title\n";cout << "c. display by bopname  d. display by preference\n";cout << "q. quit\n";cout << "Enter your choice: ";while (cin >> ch && ch != 'q'){switch (ch){case 'a':display_by_name(member);break;case 'b':display_by_title(member);break;case 'c':display_by_bopname(member);break;case 'd':display_by_preference(member);break;}cout << "Next choice: ";}cout << "Bye!\n";system("pause");return 0;
}
void display_by_name(bop *b)
{for (int i = 0; i < LEN; i++)cout << b[i].fullname << endl;
}
void display_by_title(bop *b)
{for (int i = 0; i < LEN; i++)cout << b[i].title << endl;
}
void display_by_bopname(bop *b)
{for (int i = 0; i < LEN; i++)cout << b[i].bopname << endl;
}
void display_by_preference(bop *b)
{for (int i = 0; i < LEN; i++){switch (b[i].preference){case 0:cout << b[i].fullname << endl;break;case 1:cout << b[i].title << endl;break;case 2:cout << b[i].bopname << endl;break;}}
}

运行结果:

在这里插入图片描述

5. 收入所得税

在 Ncutronia王国,货币单位是tvarp,收入所得税的计算方式如下:

5000 tvarps:不收税
5001~15000 tvarps:10%
15001~35000 tvarps:15%
35000 tvarps 以上:20%

例如,收入为38000 tvarps 时,所得税为5000 × 0.00 + 10000 × 0.10 +20000 × 0.15 + 3000 × 0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

代码:

#include <iostream>
using namespace std;
#define RATE1 0.10
#define RATE2 0.15
#define RATE3 0.20
double cal_tax(int);
int main()
{int income;double tax;cout << "Enter your income(enter negative number or q to quit): ";while (cin >> income && income >= 0){cout << "your tax is " << cal_tax(income) <<".\n";cout << "Enter your income: ";}system("pause");return 0;
}
double cal_tax(int x)
{double tax;if (x <= 5000)tax = 0.0;else if (x <= 15000)tax = RATE1 * (x - 5000);else if (x <= 35000)tax = RATE1 * (15000 - 5000) + RATE2 * (x - 15000);elsetax = RATE1 * (15000 - 5000) + RATE2 * (35000 - 15000) + RATE3 * (x - 35000);return tax;
}

运行结果:

在这里插入图片描述

6. 捐款

编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来存储款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。

代码:

#include <iostream>
#include <cstring>
using namespace std;
struct Donor
{string name;double money;
};void showGrand(Donor *, int);
void showOther(Donor *, int);int main()
{int num = 0;cout << "How many donors are there? ";cin >> num;cin.get(); // 清除缓存Donor *donor = new Donor[num];for (int i = 0; i < num; i++){cout << "Please enter the " << i + 1 << "-th name: ";getline(cin, donor[i].name);cout << "Please enter the " << i + 1 << "-th money: ";cin >> donor[i].money;cin.get();}cout << "Grand Patrons:\n";showGrand(donor, num);cout << "Patrons:\n";showOther(donor, num);system("pause");return 0;
}void showGrand(Donor *d, int num)
{int count = 0;for (int j = 0; j < num; j++){if (d[j].money > 10000){cout << d[j].name << "\t" << d[j].money << endl;count++;}}if (count == 0)cout << "none\n";
}void showOther(Donor *d, int num)
{int count = 0;for (int j = 0; j < num; j++){if (d[j].money <= 10000){cout << d[j].name << "\t" << d[j].money << endl;count++;}}if (count == 0)cout << "none\n";
}

运行结果:

在这里插入图片描述

7. 统计单词

编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha( )来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:

Enter words (q to quit) :
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others

代码:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;#define ArrSize 30int main(void)
{char str[ArrSize];int count_other = 0, count_vowel = 0, count_consonant = 0;cout << "Enter words(q to quit) :" << endl;while (cin >> str){if (strcmp(str, "q") == 0)break;char ch = str[0];if (isalpha(ch)){switch (ch){case 'a':case 'e':case 'i':case 'o':case 'u':count_vowel++;break;default:count_consonant++;}}elsecount_other++;}cout << count_vowel << " words beginning with vowels\n";cout << count_consonant << " words beginning with consonants\n";cout << count_other << " others\n";system("pause");return 0;
}

运行结果:

在这里插入图片描述

8. 统计文件字符数

编写一个程序,它打开一个文件文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。

代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;#define SIZE 60int main()
{char filename[SIZE];ifstream inFile;cout << "Enter name of data file:";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Could not open the file " << filename << endl;cout << "Program terminating." << endl;exit(EXIT_FAILURE);}int count = 0;char ch;inFile >> ch;while (inFile.good()){count++;inFile >> ch;// cout << ch;}if (inFile.eof())cout << "End of file reached." << endl;else if (inFile.fail())cout << "Input terminated by data mismatch." << endl;elsecout << "Input terminated for unknown reason." << endl;cout << "A total of " << count << " characters were read." << endl;inFile.close();system("pause");return 0;
}

运行结果:

在这里插入图片描述

9. 重写编程练习6

完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面;

4
sam stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000

代码:

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;#define SIZE 30struct Donor
{string name;double money;
};void showGrand(Donor *, int);
void showOther(Donor *, int);int main()
{char fileName[SIZE];ifstream infile;int num = 0;cout << "Enter the filename: ";cin.getline(fileName, SIZE);infile.open(fileName);if (!infile.is_open()){cout << "Can't open file " << fileName << endl;cout << "program terminating." << endl;exit(EXIT_FAILURE);}infile >> num;infile.get(); // 清除缓存Donor *donor = new Donor[num];for (int i = 0; i < num; i++){getline(infile, donor[i].name);infile >> donor[i].money;infile.get(); // 清除缓存}cout << "Grand Patrons:\n";showGrand(donor, num);cout << "Patrons:\n";showOther(donor, num);system("pause");return 0;
}void showGrand(Donor *d, int num)
{int count = 0;for (int j = 0; j < num; j++){if (d[j].money > 10000){cout << d[j].name << "\t" << d[j].money << endl;count++;}}if (count == 0)cout << "none\n";
}void showOther(Donor *d, int num)
{int count = 0;for (int j = 0; j < num; j++){if (d[j].money <= 10000){cout << d[j].name << "\t" << d[j].money << endl;count++;}}if (count == 0)cout << "none\n";
}

运行结果:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_75492.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

创建对象的方式和对属性的操作

javaScript支持多种编程范式&#xff0c;包括函数式编程和面向对象编程&#xff0c;javaScript的对象被设计成一组属性的无序集合&#xff0c;由key和value组成。 创建对象的两种方式 早期使用创建对象方式最多的是使用Object类&#xff0c;使用new关键字来创建一个对象&…

docker-compose安装kafka和php简单测试

docker-compose.yml内容&#xff1a; version: 3.1 services: zookeeper: container_name: zookeeper image: zookeeper:3.6 ports: - 2181:2181 kafka: image: wurstmeister/kafka container_name: kafka depends_on: - zookeeper …

解决Spring Data Jpa 实体类自动创建数据库表失败问题

先说一下我遇到的这个问题&#xff0c;首先我是通过maven创建了一个spring boot的工程&#xff0c;引入了Spring data jpa&#xff0c;结果实体类创建好之后&#xff0c;运行工程却没有在数据库中自动创建数据表。 找了半天发现是一个配置的问题! hibernate.ddl-auto节点的配…

【Python实战】激情澎湃,2023极品劲爆舞曲震撼全场,爬虫一键采集DJ大串烧,一曲醉人女声DJ舞曲,人人都听醉~(排行榜采集,妙啊~)

导语 哈喽&#xff01;大家好。我是木木子吖~今天给大家带来爬虫的内容哈。 所有文章完整的素材源码都在&#x1f447;&#x1f447; 粉丝白嫖源码福利&#xff0c;请移步至CSDN社区或文末公众hao即可免费。 今天教大家Python爬虫实战一键采集大家喜欢的DJ舞曲哦&#xff01; …

01-Oracle入门基础知识讲解

本章内容主要是讲解Oracle基础知识&#xff0c;安装完Oracle后第一次使用所必须了解的一些常用软件及命令&#xff0c;Oracle的体系结构等知识。 一、进入SQL Plus客户端软件 1.进入SQLPLUS客户端windows界面 2.进入DOS窗口界面 普通用户登录&#xff1a;conn 用户名称/密码 …

taobao.user.avatar.get

&#xffe5;开放平台基础API不需用户授权 根据混淆nick查询用户头像 公共参数 请求地址: HTTP地址 http://gw.api.taobao.com/router/rest 公共请求参数: 公共响应参数: 点击获取key和secret 请求参数 请求示例 TaobaoClient client new DefaultTaobaoClient(url, appkey,…

实现RecyclerView二级列表

自定义RecyclerView的adapter实现二级列表 图片大于5MB&#xff0c;CSDN不让上传&#xff0c;使用github链接&#xff0c;如果看不到请使用科学上网 https://github.com/nanjolnoSat/PersonalProject/blob/recyclerexpandableadapter/Recyclerexpanableadapter/pic/pic1.gif 源…

Kotlin学习:5.2、异步数据流 Flow

Flow一、Flow1、Flow是什么东西&#xff1f;2、实现功能3、特点4、冷流和热流5、流的连续性6、流的构建器7、流的上下文8、指定流所在协程9、流的取消9.1、超时取消9.2、主动取消9.3、密集型任务的取消10、背压和优化10.1、buffer 操作符10.2、 flowOn10.3、conflate 操作符10.…

同为(TOWE)防雷产品助力福建移动南平分公司防雷改造

01 公司简介中国移动通信集团福建有限公司南平分公司属于福建移动地级分公司&#xff0c;所属行业为电信、广播电视和卫星传输服务。现已建成覆盖范围广、业务品种多、通信质量高的综合通信网络&#xff0c;具备行业领先的经营管理制度。移动通信大楼的综合防雷及地接系统&…

Fedora系统安装KubeVela

话不多说直接看命令 Docker安装 Vela安装需要先安装Docker sudo yum -y install docker只需这行命令便可以自动添加 yum和dnf理论上都能成功&#xff0c;但是很看网速&#xff0c;&#xff0c;&#xff0c;实践证明yum是最好的。 如果发生报错mirrors trieds大概率就是网速超…

Kubernetes06:Controller (Deployment无状态应用)

Kubernetes06:Controller 1、什么是controller 管理和运行容器的对象&#xff0c;是一个物理概念 在集群上管理和运行容器的对象 2、Pod和Controller之间的关系 Pod是通过controller来实现应用的运维 比如伸缩、滚动升级等等操作Pod和Controller之间通过 label 标签建立关系…

Java 常用 API

文章目录一、Math二、System三、Object1. toString() 方法2. equals() 方法四、Arrays1. 冒泡排序2. Arrays 常用方法五、基本类型包装类1. Integer2. int 和 String 相互转换3. 字符串中数据排序4. 自动装箱和拆箱六、日期类1. Date2. SimpleDateFormat3. Calendar4. 二月天一…

来面试阿里测开工程师,HR问我未来3-5年规划,我给HR画个大饼。

在面试的过程中是不是经常被面试官问未来几年的职业规划?你会答吗&#xff1f;是不是经常脑袋里一片空白&#xff0c;未来规划&#xff1f;我只是想赚更多的钱啊&#xff0c;哈哈哈&#xff0c;今天我来教大家&#xff0c;如何给面试官画一个大饼&#xff0c;让他吃的不亦乐乎…

C++ STL:迭代器 Iterator

文章目录1、迭代器的类型2、traitsiterator_traitstype_traits泛化的指针&#xff0c;容器与算法的桥梁。提供一种方法&#xff0c;按照一定顺序访问一个聚合对象中各个元素&#xff0c;而又不暴露该对象的内部表示。既能对容器进行遍历&#xff0c;又可以对外隐藏容器的底层实…

数据库多主键in查询组合篇(sqlserver特殊)

此篇介绍的是oracle、mysql、sqlserver、达梦、人大金仓、南大通用数据库的单主键和复合主键select in的查询总结。 Mysql Select id,name from t_db_task where (id,name) in((915,Oracle内到外全表同步),(916,Oracle外到内全表同步),(921,Oracle外到内的触发同步)); selec…

ElasticSearch 学习笔记总结(三)

文章目录一、ES 相关名词 专业介绍二、ES 系统架构三、ES 创建分片副本 和 elasticsearch-head插件四、ES 故障转移五、ES 应对故障六、ES 路由计算 和 分片控制七、ES集群 数据写流程八、ES集群 数据读流程九、ES集群 更新流程 和 批量操作十、ES 相关重要 概念 和 名词十一、…

Java9之HttpClientAPI实战详解

Java9 之 HttpClientAPI 实战详解 前言 相信关注 java9 的小伙伴们都知道 java9 版本内置模块提供了 Http 功能&#xff0c;当然并不是说之前 jdk 之前并不支持&#xff0c;那么这次更新又多了什么呢&#xff1f;或者是解决了什么问题&#xff1f; 说明 自 JDK 1.0 以来&…

mac安装 Termius

1.下载安装包 链接: https://pan.baidu.com/s/1f5xmvYnVehCkMUD291SbsA?pwdy43k 提取码: y43k 2.打开系统偏好设置 -> 安全性与隐私 -> 通用&#xff0c;勾选“任何来源” 显示文件损坏的情况下执行下面操作 3.打开terminal终端 3.1 输入&#xff1a;sudo spctl --m…

“来源可靠、程序规范、要素合规”与“四性”

《从技术可行性的视角看电子档案的“四性”》一文中已经明确&#xff0c;笔者认为的电子档案“四性”是指“真实性、完整性、可用性和安全性”。而《从特斯拉“刹车失灵”事件看电子档案的法定要求》一文中&#xff0c;笔者对于“来源可靠、程序规范、要素合规”的解读如下&…

解决windows安装wxPython安装失败、速度过慢及PyCharm上wx包爆红问题

网上关于wxPython安装失败&#xff0c;安装速度过慢&#xff0c;以及安装成功后PyCharm中import wx仍然爆红的文章有很多&#xff0c;也特别杂&#xff0c;解决起来特别困难&#xff0c;今天在这里对问题的处理进行一个整合&#xff0c;希望能帮助到大家。 安装wxPython这里运用…