c++primer 第l六章编程练习答案
6.11.1
#include<iostream>
#include<cctype> int main() {
using namespace std;
char ch;
cin.get(ch);
while (ch != '@') {
if (isdigit(ch))
cout << ch;
else if (isupper(ch)) {
ch = tolower(ch);
cout << ch;
}
else if (islower(ch)) {
ch = toupper(ch);
cout << ch;
}
cin.get(ch);
}
}
6.11.2
#include<iostream>
#include<cctype> int main() {
using namespace std;
double donations[];
double donation;
double average, sum = ;
int i, num=;
cout << "input donation,at most 10\n";
for (i = ; (cin >> donation) && (i < ); i++) {
donations[i] = donation;
sum += donations[i];
}
average = sum / i;
for (i = ; i < ; i++) {
if (donations[i] > average)
num++;
}
cout << "average is " << average << " and " << num << " numbers bigger than avergae.";
}
6.11.3
#include<iostream> int main() {
using namespace std;
char ch;
cout << "Please enter one of the following choices:\n"
"c) carnivore p) pianist\n"
"t) tree g) game\n";
cout << "Please enter a c,p,t,or g:";
cin >> ch;
//cout << "A maple is a ";
while (ch!='f') {
switch (ch)
{
case'c':cout << "A maple is a carnivore.";
break;
case'p':cout << "A maple is a pianist.";
break;
case't':cout << "A maple is a tree.";
break;
case'g':cout << "A maple is a game.";
break;
default:cout << "Please enter a c,p,t,or g:";
}
cin >> ch;
}
}
6.11.4
#include<iostream> using namespace std; const int strsize = ;
const int memberSize = ; struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void name(bop *member);
void title(bop *member);
void bopname(bop *menber);
void preference(bop *member); int main() {
char choose;
bop member[] = {
{"zhang","manager","boss",},
{"li","programer","pg",},
{"wang","teacher","tc",}
};
cout << "Benevolent Order of Programmers Report\n"
"a.display by name b.display by title\n"
"c.display by bopname d.display by preference\n"
"q.quit\n"
"Enter your choice: ";
cin >> choose; while (choose!='q')
{
switch (choose)
{
case 'a':name(member);
break;
case 'b':title(member);
break;
case 'c':bopname(member);
break;
case 'd':preference(member);
break;
default:cout << "Not good choose\n";
break;
}
cout << "Next choose:";
cin >> choose;
}
} void name(bop * member)
{
for (int i = ; i < memberSize; i++) {
cout << member[i].fullname << endl;
}
} void title(bop * member)
{
for (int i = ; i < memberSize; i++) {
cout << member[i].title << endl;
}
} void bopname(bop * member)
{
for (int i = ; i < memberSize; i++) {
cout << member[i].bopname << endl;
}
} void preference(bop * member)
{
for (int i = ; i < memberSize; i++) {
switch (member[i].preference)
{
case :cout << member[i].fullname << endl;
break;
case :cout << member[i].title << endl;
break;
case :cout << member[i].bopname << endl;
break;
default:
cout << "存在非法的偏好";
break;
}
}
}
6.11.5
#include<iostream> int main() {
using namespace std;
int money;
double tax = ;
cout << "Please input your income: ";
cin >> money;
cout << fixed;
cout.precision();
cout.setf(ios::showpoint);
while (cin.good()&&(money>=))
{
if (money<=)
{
tax = 0.0;
break;
}
else if ((money > ) && (money <= )) {
tax = (money - )*0.1;
break;
}
else if ((money > ) && (money <= )) {
tax = (money - )*0.15 + * 0.1;
break;
}
else {
tax = (money - )*0.2 + * 0.15 + * 0.1;
break;
}
}
cout << "Your personal income tax is " << tax << " tvarps";
}
6.11.6
#include<iostream>
#include<string> using namespace std; struct donations
{
string name;
double money;
}; int main() {
int donorSize, grand_patron_size = , patrons_size = ;
cout << "How many people donate?\n"
"donorSize: ";
cin >> donorSize;
donations *donor = new donations[donorSize];
donations *grand_patrons = new donations[donorSize];
donations *patrons = new donations[donorSize];
cout << "Please enter donor's contribution amount:" << endl;
cout << fixed;
cout.precision();
cout.setf(ios_base::showpoint);
for (int i = ; i < donorSize; i++)
{
cout << "name:";
cin >> donor[i].name;
//cin.get();
//cout << "\t";
cout << "contribution amout:";
cin >> donor[i].money;
}
for (int i = ; i < donorSize; i++)
{
if (donor[i].money > ) {
grand_patrons[grand_patron_size] = donor[i];
++grand_patron_size;
}
else {
patrons[patrons_size] = donor[i];
++patrons_size;
}
}
cout << "=============== Grand Partrons ================" << endl;
if (grand_patron_size != ) {
for (int i = ; i < grand_patron_size; i++)
{
cout << "name: " << grand_patrons[i].name << "\tmoney:" << grand_patrons[i].money << endl;
}
}
else
{
cout << "None";
}
cout << "================= Partrons ====================" << endl;
if (patrons_size != ) {
for (int i = ; i < patrons_size; i++)
{
cout << "name: " << patrons[i].name << "\tmoney:" << patrons[i].money << endl;
}
}
else
{
cout << "None";
}
delete[] donor;
delete[] grand_patrons;
delete[] patrons;
}
6.11.7
#include<iostream>
#include<string>
#include<cctype> int main() {
using namespace std;
int vowel = , consonant = , other = ;
string word;
cout << "Enter word (q to quit): \n";
cin >> word;
while (cin.good() && word != "q")
{
if (isalpha(word[])) {
switch (word[])
{
case 'a':
vowel++;
break;
case 'A':
vowel++;
break;
case 'e':
vowel++;
break;
case 'E':
vowel++;
break;
case 'i':
vowel++;
break;
case 'I':
vowel++;
break;
case 'o':
vowel++;
break;
case 'O':
vowel++;
break;
case 'u':
vowel++;
break;
case 'U':
vowel++;
break;
default:
consonant++;
}
}
else
other++;
cin >> word;
}
cout << vowel << " words beginning with vowels." << endl;
cout << consonant << " words beginning with consonants." << endl;
cout << other << " others.";
}
6.11.8
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string> int main() {
using namespace std;
ifstream infile;
string str;
int sum = ;
infile.open("D:\\visual studio 2015\\Projects\\homework\\homework\\Debug\\TextFile1.txt");
if (infile.is_open()) {
while (infile.good())
{
str += infile.get();
++sum;
}
cout << "The file contains " << sum << " characters.";
}
else
{
cout << "Failed to read file.";
exit(EXIT_FAILURE);
}
infile.close();
}
6.11.9
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib> using namespace std; struct donations
{
string name;
double money;
}; int main() {
int donorSize, grand_patron_size = , patrons_size = ;
string donor_information;
ifstream infile;
infile.open("D:\\visual studio 2015\\Projects\\homework\\homework\\TextFile2.txt");
if (infile.is_open()) {
infile >> donorSize;
infile.get();
cout << donorSize << endl;
donations *donor = new donations[donorSize];
donations *grand_patrons = new donations[donorSize];
donations *patrons = new donations[donorSize]; cout << fixed;
cout.precision();
cout.setf(ios_base::showpoint); for (int i = ; i < donorSize; i++)
{ getline(infile, donor[i].name);
infile >> donor[i].money;
infile.get();
}
for (int i = ; i < donorSize; i++)
{
cout << donor[i].name << endl;
cout << donor[i].money << endl;
}
for (int i = ; i < donorSize; i++)
{
if (donor[i].money > ) {
grand_patrons[grand_patron_size] = donor[i];
++grand_patron_size;
}
else {
patrons[patrons_size] = donor[i];
++patrons_size;
}
}
cout << "=============== Grand Partrons ================" << endl;
if (grand_patron_size != ) {
for (int i = ; i < grand_patron_size; i++)
{
cout << "name: " << grand_patrons[i].name << "\tmoney:" << grand_patrons[i].money << endl;
}
}
else
{
cout << "None";
}
cout << "================= Partrons ====================" << endl;
if (patrons_size != ) {
for (int i = ; i < patrons_size; i++)
{
cout << "name: " << patrons[i].name << "\tmoney:" << patrons[i].money << endl;
}
}
else
{
cout << "None";
}
delete[] donor;
delete[] grand_patrons;
delete[] patrons;
}
else
{
cout << "Failed to open file.";
exit(EXIT_FAILURE);
}
infile.close();
}
c++primer 第l六章编程练习答案的更多相关文章
- c++ primer plus 第六章 课后题答案
#include <iostream> #include <cctype> using namespace std; int main() { char in_put; do ...
- 【C++】《C++ Primer 》第六章
第六章 函数 一.函数基础 函数定义:包括返回类型.函数名字和0个或者多个形参(parameter)组成的列表和函数体. 调用运算符:调用运算符的形式是一对圆括号 (),作用于一个表达式,该表达式是函 ...
- c primer plus(五版)编程练习-第六章编程练习
1.编写一个程序,创建一个具有26 个元素的数组,并在其中存储26 个小写字母.并让该程序显示该数组的内容. #include<stdio.h> #define SIZE 26 int m ...
- C++Primer 第十六章
//1.模板定义以关键字template开始,后跟一个模板参数列表,此列表不能为空.编译器用推断出的模板参数来实例化一个特定版本的函数.类型参数前必须使用class或者typename(推荐使用typ ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习9
#include <iostream> #include <fstream> #include <cstdlib> #include <string> ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8
#include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习7
#include <iostream> #include <string> #include <cctype> using namespace std; int m ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习6
#include <iostream> #include <string> using namespace std; const int MSIZE=100; struct j ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习5
#include <iostream> using namespace std; const double N1=35000; const int N2=15000; const int ...
随机推荐
- 《Mining of Massive Datasets》笔记(一)
数据挖掘基本概念 数据挖掘定义 最广为接受得到定义是,数据挖掘是数据"模型"的发现过程.而"模型"却可以有多种含义. 1)统计建模 统计学家认为数据挖掘就是统计 ...
- sqlserver整理的实用资料
1 --- 创建 备份数据的 device 2 3 USE DB_ZJ 4 EXEC sp_addumpdevice 'disk', 'testBack', 'c:\MyNwind_1.dat' 5 ...
- 01 Spring框架 基本介绍
相信学习java,并且走Web道路的道友都应该知道Spring的大名,它的地位相信也不需要我在这里多说什么,接下来的文章就Spring的配置和使用来进行一些讲解. 首先学习框架我们都要考虑和做到以下几 ...
- 吐槽 MySQL数据库jdbc操作,varchar类型占位符问题——单引号造孽
很长时间不写代码动手能力明显下降很多常见的错误还是经常发生,今天吐血了一次. 简单的坑总是要多跳几次才能甘心.很清晰的记得大学的时候在此坑差点闷死,现在又跳进这个坑了,搞了半天终于知道错在哪里. St ...
- 【TopCoder】SRM160 DIV1总结
做了两道题之后才发现做的是DIV1,不是DIV2,DIV1的第二道题是DIV1的第三道题,果断决定第3题就不看了=.= 250分题:给定一个时间起点8:00 AM DAY 1,再给出一组时间终点,格式 ...
- 蓝牙固件升级(OTA升级)原理设计
转:http://blog.csdn.net/yueqian_scut/article/details/50849033 固件空中升级(OTA)与固件二次引导的原理和设计 原创 2016年03月10日 ...
- 什么是JavaBeans?
参看维基百科,归纳出以下几条: JavaBeans是指符合某些标准的类, Bean这个名称用于涵盖这个标准, 其目的在于创建可重用的Java组件. 由于Bean是很“死板”的东西,因此它可以持久存储, ...
- 算法总结之 数组的partition调整 三个值的升序
给定一个数组arr, 其中只可能有 0,1,2三个值,请实现arr排序 另一种问法: 有一个数组,只有红 蓝 黄 球,请事先红球全放在数组的左边,蓝球放中间,黄球放右边 另一种问法: 有一个数组,再给 ...
- Datax官方笔记总结
# DataX DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.SQL Server.Oracle.PostgreSQL.HDFS.Hive.HBase.OTS. ...
- spring security在spring mvc的action中获取登录人信息
@RequestMapping("/index") public ModelAndView login( @RequestParam(value = "error&quo ...