以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆。仅供参考,DEV C++已通过编译运行

练习1

#include<iostream>
#include<cctype>int main()
{using namespace std;char ch;cout << "Type some sentences(@ to quit):" << endl;cin.get(ch);while (ch != '@'){if (isdigit(ch));//除外数字,倘若保留只需给下方套一个elseelse if (isalpha(ch))//处理非数字非字母字符{if (isupper(ch))cout << char(tolower(ch));//必须要用char()强制转换,否则会输出对应的整型elsecout << char(toupper(ch));}elsecout << ch;cin.get(ch);}return 0;
}

练习2

#include<iostream>
#include<array>
const int ArSize = 10;
int main()
{using namespace std;array<double, ArSize>donation;int count = 0;//实际数组数量计数器int num = 0;//大于均值计数器cout << "Enter double_type number into an array: " << endl;cout << "donation[1] : ";while (count < ArSize && cin >> donation[count])//cin>>将正确读入目标类型值,因此在输入非数字的情况下会返回false{++count;if( count < ArSize)cout << "donation[" << count + 1 << "] : ";}double sum, average;sum = 0;for (int cnt = 0; cnt < count; ++cnt)//计算平均值,count实际值可能会小于ArSize。{sum += donation[cnt];}average = sum / count;for (int cnt = 0; cnt < count; ++cnt)//找出大于均值的数{if (donation[cnt] > average)++num;}cout << "There are " << count << " numbers in donation_array" << endl;cout << "The average of donation_array is " << average << endl;cout << "There are " << num << " number over average." << endl;return 0;
}

练习3

#include<iostream>int main()
{using namespace std;cout << "Please enter one of thr following choices: " << endl;cout << "c)  carnivore         p)  pianist" << endl;cout << "t)  tree              g)  game" << endl;char ch;do{cin >> ch;switch (ch){case 'c':cout << "carnivore";break;case 'p':cout << "pianist";break;case 't':cout << "A map is a tree";break;case 'g':cout << "game";break;default: cout << "Please enter a c,p,t or g:";}} while ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g'));return 0;
}

练习4

#include<iostream>const int strsize = 30;
const int member = 5;struct bop
{char fullname[strsize];char title[strsize];char bopname[strsize];int preference;
};int main()
{using namespace std;cout << "Benevolent Order of Programmers Report" << endl;cout << "a.display by name      b.display by title" << endl;cout << "c.display by bopname   d.display by preference" << endl;cout << "q.quit" << endl;bop Bop[member] ={{"Wimp Macho","Wm","WMWM",0},{"Raki Rhodes","Junior Programmer","RRRR",1},{"Celia Laiter","Senior Programmer","MIPS",2},{"Hoppy Hipman","Analyst Trainee","HHHH",1},{"Pat hand","Student","LOOPY",2}};char ch;int cnt;cout << "Enter your choice: ";do{cin >> ch;switch (ch){case 'a':for (cnt = 0; cnt < member; ++cnt)cout << Bop[cnt].fullname << endl;break;case 'b':for (cnt = 0; cnt < member; ++cnt)cout << Bop[cnt].title << endl;break;case 'c':for (cnt = 0; cnt < member; ++cnt)cout << Bop[cnt].bopname << endl;break;case 'd':for (cnt = 0; cnt < member; ++cnt){if (Bop[cnt].preference == 0)cout << Bop[cnt].fullname << endl;else if (Bop[cnt].preference == 1)cout << Bop[cnt].title << endl;elsecout << Bop[cnt].bopname << endl;}break;}if (ch != 'q')cout << "Next choice: ";elsecout << "Bye!" << endl;} while (ch != 'q');return 0;
}

练习5

#include<iostream>
const double tax1 = 0.1;
const double tax2 = 0.15;
const double tax3 = 0.2;int main()
{using namespace std;double income, tax;cout << "Please input your income: ";while (cin >> income && income >= 0){if (income > 35000)tax = (income - 35000) * tax3 + 20000 * tax2 + 10000 * tax1;else if (income > 15000)tax = (income - 15000) * tax2 + 10000 * tax1;else if (income > 5000)tax = (income - 5000) * tax1;elsetax = 0;cout << "Your personal tax is : " << tax << endl;cout << "Please input your income: ";}return 0;
}

练习6

#include<iostream>
#include<string>
using namespace std;
const double max = 10000;struct Donation
{string name;double money;
};int main()
{int num, gpnum, pnum;gpnum = pnum = 0;cout << "Enter the number of donation: ";cin >> num;cin.get();Donation* donation = new Donation[num];for (int cnt = 0; cnt < num; ++cnt){cout << "Enter the name: ";getline(cin, donation[cnt].name);cout << "Enter the money: ";cin >> donation[cnt].money;cin.get();}cout << "Input complete!" << endl;cout << endl;cout << "Grand Patrons:" << endl;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money >= max){cout << donation[cnt].name << "  ";cout << donation[cnt].money << endl;++gpnum;}}if (gpnum == 0)cout << "none." << endl;cout << "Patrons: " << endl;;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money < max){cout << donation[cnt].name << "  ";cout << donation[cnt].money << endl;++pnum;}}if (pnum == 0)cout << "none." << endl;delete[]donation;return 0;
}

练习7

#include<iostream>
#include<cctype>
#include<cstring>//string比较好一些const int ArSize = 30;int main()
{using namespace std;cout << "Enter words(q to quit):" << endl;char word[ArSize];int vowels, consonants, others;vowels = consonants = others = 0;cin >> word;while (strcmp(word,"q"))//C语言形式比较,两个都为字符串,string对象则只需要逻辑运算{if (isalpha(word[0])){switch (word[0]){case 'a':case 'A': ++vowels;break;case 'e':case 'E':++vowels;break;case 'i':case 'I':++vowels;break;case 'o':case 'O':++vowels;break;case 'u':case 'U':++vowels;break;default:++consonants;break;}}else++others;cin >> word;//切勿忘记在没有接收到q指令循环还在继续,需要一个继续键入的过程}cout << vowels << " words beginning with vowels" << endl;cout << consonants << " words beginning with consonants" << endl;cout << others << " others" << endl;return 0;
}

练习8

#include<iostream>
#include<fstream>
#include<cstdlib>const int SIZE = 30;int main()
{using namespace std;char filename[SIZE];ifstream inFile;//TXT文件需与cpp文件在同一个根目录下cout << "Enter name of file: ";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Couldn't open the file " << filename << endl;cout << "Program terminating." << endl;exit(EXIT_FAILURE);}char ch;int count = 0;inFile >> ch;//除去空格//inFile.get(ch);//包括空格while (inFile.good()){++count;cout << ch;inFile >> ch;//inFile.get(ch);}if (inFile.eof())cout << "End of file reached." << endl;else if (inFile.fail())cout << "Input termainated by data mismatch." << endl;elsecout << "Input termainated by unknowed error." << endl;if (count == 0)cout << "No data processed." << endl;elsecout << count << " chars in this txt." << endl;inFile.close();return 0;
}

练习9

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>using namespace std;
const int SIZE = 30;
const double max = 10000;struct Donation
{string name;double money;
};int main()
{char filename[SIZE];ifstream inFile;//TXT文件需与cpp文件在同一个根目录下cout << "Enter name of file: ";cin.getline(filename, SIZE);inFile.open(filename);if (!inFile.is_open()){cout << "Couldn't open the file " << filename << endl;cout << "Program terminating." << endl;exit(EXIT_FAILURE);}if (inFile.eof())cout << "End of file reached." << endl;else if (inFile.fail())cout << "Input termainated by data mismatch." << endl;elsecout << "Input termainated by unknowed error." << endl;int num,gpnum, pnum;gpnum = pnum = 0;inFile >> num;Donation* donation = new Donation[num];for (int cnt = 0; cnt < num; ++cnt){//inFile.ignore();inFile.get();getline(inFile, donation[cnt].name);inFile >> donation[cnt].money;}cout << "Input complete!" << endl;cout << endl;cout << "Grand Patrons:" << endl;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money >= max){cout << donation[cnt].name << "  ";cout << donation[cnt].money << endl;++gpnum;}}if (gpnum == 0)cout << "none." << endl;cout << "Patrons: " << endl;;for (int cnt = 0; cnt < num; ++cnt){if (donation[cnt].money < max){cout << donation[cnt].name << "  ";cout << donation[cnt].money << endl;++pnum;}}if (pnum == 0)cout << "none." << endl;delete[]donation;inFile.close();return 0;
}

2020 我的C++学习之路 C++PrimerPlus第六章课后习题相关推荐

  1. 2020 我的C++学习之路 C++PrimerPlus第十一章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 .第5题第6题改写过于麻烦,机械性重复就不写了,写的犯恶心. 练习1 //p1.h #in ...

  2. 2020 我的C++学习之路 C++PrimerPlus第七章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 #include<iostream> using namespac ...

  3. 2020 我的C++学习之路 C++PrimerPlus第五章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 #include<iostream>int main() {int ...

  4. 2020 我的C++学习之路 C++PrimerPlus第四章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行. 练习1 #include <iostream> int main() {us ...

  5. 2020 我的C++学习之路 C++PrimerPlus第十章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 //p1.h #include<string>class Acco ...

  6. 2020 我的C++学习之路 C++PrimerPlus第九章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1 //golf.h const int Len = 40; struct gol ...

  7. 2020 我的C++学习之路 C++PrimerPlus第八章课后习题

    以C++ Primer Plus为参考书籍,自身归纳知识点,加深记忆.仅供参考,DEV C++已通过编译运行 . 练习1:题目我是看了好久才反应过来,不得不说翻译的不太行 #include<io ...

  8. 我的学习之路_第十六章_xml

    [XML] 可扩展的标记语言 作用:存放数据.配置文件 [XML的语法] xml的文件扩展名必须为: xml xml的内容必须满足以下要求: 1.固定的文档声明 2.合格的元素和属性 3.正确的注释 ...

  9. 鸟哥的Linux私房菜基础学习篇(第二版)第二章课后习题与答案

    习题: 1.计算机总是出现问题,有一个错误信息为"fatal:SASL per- connection security setup",请找出可能的原因. 答:先跑到 http:/ ...

最新文章

  1. spring cloud config将配置存储在数据库中
  2. 可访问范围 与 visual 或 abstract “修饰符”
  3. RxJava 基础扫盲
  4. Map<String,Object>接收参数,Long类型降级为Integer,报类型转换异常
  5. idea gblfy常用快捷键
  6. antimalware service executable占用内存过高_SQLServer占用服务器内存过高,更改这个设置就能降低内存使用率
  7. gorm 多条件搜索db处理问题
  8. 前端代码部署后通过html访问,将前端项目部署到nginx上
  9. Gulp解决发布线上文件(CSS和JS)缓存问题
  10. HDOJ--1285--确定比赛名次
  11. 引用echar显示图形
  12. CGB2202-DAY02-IO低级流与高级流
  13. 服务器XP系统打印机共享设置,WinXP系统网络打印机怎么设置?WinXP打印机共享设置方法...
  14. 管理学概念区分(行为科学、科学管理、古典管理理论)
  15. 利用爬虫获得疫情信息,并存入表格
  16. linux下好用的chm阅读器
  17. Java经典面试题总结(附答案)-java经典面试题大全总结以及整理
  18. 记录Mybatis报Mapped Statements collection already contains value for 的错误的原因
  19. png格式的图片体积太大怎么办?如何压缩png图片?
  20. win10安装sshpass_Windows上SSH服务器的配置以及客户端的连接

热门文章

  1. [记录]-Cortex-A76仅EL0支持aarch32
  2. Git 查看提交历史
  3. [WUSTCTF2020]level3
  4. 【网络安全】NFS服务安全加固
  5. 带你了解DDoS攻击的原理,让你轻松学会DDoS攻击原理及防护措施
  6. python判断计算机是否有网络连接
  7. 16、HTML密码框
  8. 1.21 Lambda表达式
  9. 【PAT乙级】1092 最好吃的月饼 (20 分)
  10. 476. 数字的补数 【位运算】