C++ Primer Pluse_6_课后题
#include <iostream>
#include <cctype>
#include <array>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib> using namespace std; int t6_1()
{ char ch; cout << "Enter text for analysis, and type # to terminate input.\n";
cin.get(ch);
while (ch != '@')
{
if (isdigit(ch))
{
cin.get(ch);
continue;
}
else if (isupper(ch))
{
ch = tolower(ch);
}
else if (islower(ch))
{
ch = toupper(ch);
}
cout << ch; cin.get(ch);
}
cout << endl;
system("pause");
return 0;
} const int MaxSize = 10;
int t6_2()
{
array<double, MaxSize>donations = { 0 }; cout << "input the donations:\n"; cout << "donation#1:";
int i = 0;
while (i <MaxSize && cin >> donations[i])
{
if (++i <MaxSize)
{
cout << "donation#" << i + 1<<":";
}
} double total = 0;
double average = 0; for (int j = 0; j < i; j++)
{
total += donations[j];
}
if (i==0)
{
cout << "No donations.\n";
}
else
{
average = total / i;
cout << "Average is " << average << endl;
cout << "Bigger than average: ";
for (int j = 0; j < i; j++)
{ if (donations[j] > average)
{
cout << donations[j] << " ";
}
}
} system("pause");
return 0;
} int t6_3()
{
char ch; cout << "Please enter aone of the following choices:\n";
cout << "c) carnivore\t\t\t p) pianist\n";
cout << "t) tree\t\t\t\t g) game\n";
cin.get(ch).get(); while (true)
{
switch (ch)
{
case 'c': cout << "A map is a carnivore.\n"; break;
case 'p': cout << "A map is a pianist.\n"; break;
case 't': cout << "A map is a tree.\n"; break;
case 'g': cout << "A map is a game.\n"; break;
default:
cout << "Please enter a c, p, t, g:";
break;
}
cin.get(ch).get();
} system("pause");
return 0;
}
const int strsize = 20;
typedef struct Bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
}bop;
int t6_4()
{
bop b1 = { "bopp1", "title1", "bop1", 1 };
bop b2 = { "bopp2", "title2", "bop2", 0 };
bop b3 = { "bopp3", "title3", "bop3", 2 }; cout << "BOP report.\n";
cout << "a.display by name.\t\t\t b.display by title.\n";
cout << "c.display by bopname.\t\t d.display by preference.\n";
cout << "q.quit.\n";
cout << "Enter your choice:";
char ch;
cin.get(ch).get();
while (ch != 'q')
{
switch (ch)
{
case 'a':
cout << b1.fullname << endl;
cout << b2.fullname << endl;
cout << b3.fullname << endl;
break;
case 'b':
cout << b1.title << endl;
cout << b2.title << endl;
cout << b3.title << endl;
break;
case 'c':
cout << b1.bopname << endl;
cout << b2.bopname << endl;
cout << b3.bopname << endl;
break;
case 'd':
if (b1.preference== 0)
{
cout << b1.fullname << endl;
}
else if (b1.preference ==1)
{
cout << b1.title << endl;
}
else
{
cout << b1.bopname << endl;
} if (b2.preference == 0)
{
cout << b2.fullname << endl;
}
else if (b2.preference == 1)
{
cout << b2.title << endl;
}
else
{
cout << b2.bopname << endl;
} if (b3.preference == 0)
{
cout << b3.fullname << endl;
}
else if (b3.preference == 1)
{
cout << b3.title << endl;
}
else
{
cout << b3.bopname << endl;
}
break; default:
cout << "Please input a a, b, c, d ,q:";
}
cin.get(ch).get();
}
cout << "Bye!"; system("pause");
return 0;
} int t6_5()
{
double wage = 0;
double tax = 0; while (cin>>wage && wage >=0)
{
if (wage <= 5000)
{
tax = 0;
}
else if (wage > 5000 && wage <= 1500)
{
tax = 5000 * 0 + (wage - 5000)*0.1;
}
else if (wage > 1500 && wage <= 3500)
{
tax = 5000 * 0 + 10000 * 0.1 + (wage - 15000)*0.15;
}
else
{
tax = 5000 * 0 + 10000 * 0.1 + 20000 * 0.15 + (wage - 35000)*0.2;
} cout << "tax = " << tax << endl;
} system("pause");
return 0;
} typedef struct Donor
{
string name;
double money;
}donor;
int t6_6()
{
int donors = 0; cout << "Enter the number of donors.\n";
cin >> donors;
cin.get();
donor *donorSet = new donor[donors]; for (int i = 0; i < donors; i++)
{
cout << "Enter donor#" << i + 1 << ":\n";
getline(cin, donorSet[i].name);
cin >> donorSet[i].money;
cin.get();
} cout << "\nGrand Patrons:\n";
int flag = 0;
for (int i = 0; i < donors; i++)
{
if (donorSet[i].money > 10000)
{
cout << donorSet[i].name << endl;
flag = 1;
}
}
if (flag == 0)
{
cout << "None.\n";
} cout << "Patrons:\n";
flag = 0;
for (int i = 0; i < donors; i++)
{
if (donorSet[i].money <= 10000)
{
cout << donorSet[i].name << endl;
flag = 1;
}
}
if (flag == 0)
{
cout << "None.\n";
} system("pause");
return 0;
} int t6_7()
{
int vowelCount = 0;
int consonantsCount = 0;
char ch; cout << "Enter words (q to quit):\n";
cin.get(ch);
while (ch != 'q')
{
if (isalpha(ch))
{
if (tolower(ch) == 'a' || tolower(ch) == 'e' || tolower(ch) == 'i' || tolower(ch) == 'o' || tolower(ch) == 'u')
{
vowelCount++;
}
else
consonantsCount++; cin.get(ch);
while (ch != ' '&& ch != '\n')
{
cin.get(ch);
}
}
else
cin.get(ch);
} cout << vowelCount << " words beginning with vowels.\n";
cout << consonantsCount << " words beginning with consonants.\n";
//system("pause");
return 0;
} const int SIZE = 60;
int test6_8()
{
char filename[SIZE];
ifstream inFile;
cout << "Enter the name of the data file:";
cin.getline(filename,SIZE);
inFile.open(filename); if (!inFile.is_open())//failed to open file
{
cout << "Could not open the file.\n";
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
} char ch;
int count = 0; inFile >> ch;
while (inFile.good())
{
count++;
inFile >> ch;
cout << ch;
} if (inFile.eof())
{
cout << "End of the file.\n";
}
else if (inFile.fail())
{
cout << "Input terminated by data mismatch.\n";
}
else
cout << "Input terminated for unknown reason.\n"; inFile.close(); cout << "file has " << count << "characters.\n"; return 0;
} int test6_9()
{
char filename[SIZE];
ifstream inFile;
cout << "Enter the file name: ";
cin.getline(filename, SIZE); inFile.open(filename);
if (!inFile.is_open())
{
cout << "File cannot be opened.\n";
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
} int donors = 0; cout << "Enter the number of donors.\n";
inFile >> donors;
inFile.get();
donor *donorSet = new donor[donors]; for (int i = 0; i < donors; i++)
{
cout << "Enter donor#" << i + 1 << ":\n";
getline(inFile, donorSet[i].name);
inFile >> donorSet[i].money;
inFile.get();
} if (inFile.eof())
{
cout << "file end.\n";
}
else if (inFile.fail())
{
cout << "Input file terminated by mismatch.\n";
}
else
cout << "Input file terminated by unknown reasons.\n"; inFile.close(); cout << "\nGrand Patrons:\n";
int flag = 0;
for (int i = 0; i < donors; i++)
{
if (donorSet[i].money > 10000)
{
cout << donorSet[i].name << endl;
flag = 1;
}
}
if (flag == 0)
{
cout << "None.\n";
} cout << "Patrons:\n";
flag = 0;
for (int i = 0; i < donors; i++)
{
if (donorSet[i].money <= 10000)
{
cout << donorSet[i].name << endl;
flag = 1;
}
}
if (flag == 0)
{
cout << "None.\n";
} return 0;
} int main()
{
test6_9();
system("pause");
return 0;
}
C++ Primer Pluse_6_课后题的更多相关文章
- C++ Primer Pluse_8_课后题
#include <iostream> #include <string> #include<cstring> using namespace std; void ...
- C++ Primer Pluse_7_课后题
#include <iostream> using namespace std; double Sum2(double x, double y) { double sum = 0; if ...
- 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
前文 的最后给出了玉伯的一道课后题,今天我们来讲讲这题的思路. 题目是这样的: Number.MAX_VALUE + 1 == Number.MAX_VALUE; Number.MAX_VALUE + ...
- 算法(JAVA)----两道小小课后题
LZ最近翻了翻JAVA版的数据结构与算法,无聊之下将书中的课后题一一给做了一遍,在此给出书中课后题的答案(非标准答案,是LZ的答案,猿友们可以贡献出自己更快的算法). 1.编写一个程序解决选择问题.令 ...
- 课后题2.87&2.86
课后题2.86&2.87 单纯就是想加点分第十章的题目都被做过了就做下第二章的,正好复习一下前面学的知识,第二章给我剩下的题目也不多了,我就挑了这个题目. 2.86 考虑一个基于IEEE浮点格 ...
- c++面向对象程序设计 课后题 答案 谭浩强 第四章
c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...
- 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案
国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...
- 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案
初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...
- Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...
随机推荐
- MVC 依赖注入扩展
需求: 小明想要完成一个功能F,需要一把锤子T. 有两种办法可以实现: 1)小明很爱动手,精力很旺盛,于是,自己创建一个具有功能F的锤子T,并使用T来完成F: 2)小明很懒,天天睡大觉,于是,他叫小健 ...
- Dynamic Expression.Call Any
public class Foo { public IList<string> Strings { get; set; } } class Program { static void Ma ...
- Odoo Entypo Regular Icon List
参考地址: http://www.fontslog.com/entypo-regular-otf-33800.htm#custompreview 或 http://www.w3cplus.com/w3 ...
- 简单Qt网络通信
最近要用到Qt的Socket部分,网上关于这部分的资料都比较复杂,我在这总结一下,把Socket的主要部分提取出来,实现TCP和UDP的简单通信. 1.UDP通信 UDP没有特定的server端和cl ...
- thinkphp的html模板中if的使用
写的时候正好出错,我就纠结是{if}还是手册中的<if condition>,当然我使用的是手册中的用法,但是点击按钮时候还是没展开(if后的条件没执行).如图 试了好多写法,也检查了多次 ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- Mininet实验 基于Mininet实现BGP路径挟持攻击实验
参考:基于Mininet实现BGP路径挟持攻击实验 实验目的: 掌握如何mininet内模拟AS. 掌握BGP路径挟持的原理和分析过程. 实验原理: 互联网是由相互连接的自治系统AS组成的,通过一个通 ...
- Nginx 笔记与总结(13)Nginx 的 gzip 压缩
使用 FireFox(40.0)访问博客园(http://www.cnblogs.com/),观察 http 头信息 请求头信息: Accept-Encoding gzip, deflate 表示浏览 ...
- windows下Gulp安装
目录: 1.安装nodejs2.使用命令行3.npm介绍4.选装cnpm5.全局安装gulp6.新建package.json文件7.本地安装gulp插件8.新建gulpfile.js文件9.运行gul ...
- Android 通过网页打开自己的APP(scheme)
Android 通过网页打开自己的APP(scheme) 分类: android2014-07-09 17:35 8565人阅读 评论(2) 收藏 举报 通过用手机的浏览器(内置,第三方都可)访问一个 ...