c++ primer plus第六版第六章编程练习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c++ primer plus第六版第六章编程练习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8830字,纯文字阅读大概需要13分钟。
内容图文
![c++ primer plus第六版第六章编程练习](/upload/InfoBanner/zyjiaocheng/610/b8104025b68748ecb34ea5870283f4df.jpg)
- 编写程序读取键盘输入,回显除数字外字符,同时大写转小写,小写转大写,遇’@'就停止。
//练习6.1 读取键盘输入,回显输出(除数字),另外大写字母和小写字母互转,遇“@”则退出程序。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while ((ch = cin.get()) != '@') //遇'@'则停
{
if (isdigit(ch)) //判断是否为数字,是则跳过
{
continue;
}
else if (islower(ch)) //判断是否为小写
{
cout << (char)toupper(ch);
}
else if (isupper(ch)) //判断是否为大写
{
cout << (char)towlower(ch);
}
else //输出字母数字以外字符
cout << ch;
}
cout << "Done." << endl;
system("pause");
return 0;
}
- 最多输入10个donation,数值存储于数组(可用array实现),遇到非数字输入(我都不知道有那么调皮的人)停止,最后计算平均值和存储数值中超过平均值的数的数目。
放码:
//练习6.2 最多输入10个捐献值到double数组中,遇到非数字结束输入
//并报告数字的平均值以及数组中有多少个数字大于平均值
#include <iostream>
using namespace std;
const int Max = 10;
int main()
{
double donation[Max];
double sum = 0.0;
double average = 0.0;
int n = 0;
int lager_ave_n = 0;
cout << "How many times do you want to enter:";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Please enter the donation #" << i + 1 << ':';
cin >> donation[i];
sum += donation[i];
}
average = sum / n;
cout << "The average value of the donation is " << average << '.' << endl;
for (int j = 0; j < n; j++)
{
if (donation[j] > average)
lager_ave_n++;
}
cout << "There are " << lager_ave_n << " donations exceed the average." << endl;
system("pause");
return 0;
}
我这边测试结果只进行了最大值输入进行测试,没有测试之外的,有问题请告知。
3. 编写驱动程序雏形,显示可选菜单,并对应输入相应。
//练习6.3 编写驱动程序雏形,提供四个选项的菜单
//每个选项有一个字母标记,用户输入有效选项以内的输入得到相应,否则提示输入
#include <iostream>
#include <cstdlib>
using namespace std;
const int Max = 10;
void showmenu();
void quit();
int main()
{
showmenu();
char choice;
while (cin >> choice && choice != 'q')
{
switch (choice)
{
case 'a':
cout << "Let's start the journey." << endl;
break;
case 'c':
cout << "Really?Don't you wanna try?" << endl;
break;
case 'p':
cout << "Panist is cool,right?" << endl;
break;
case 't':
cout << "As the tree in the new world." << endl;
break;
case 'g':
cout << "Here we go!" << endl;
break;
default:
cout << "It's not invalid.Please try again:" << endl;
break;
}
cout << "You can try another:" << endl;
}
quit();
system("pause");
return 0;
}
void showmenu() //菜单展示
{
cout << "Please enter one of the following choices:" << endl;
cout << "a)start c)cry p)panist" << endl;
cout << "t)tree g)game q)quit" << endl;
}
void quit()
{
cout << "Bye." << endl;
long long wait = 0;
while (wait < 10000000000) //设置一个退出时延
wait++;
exit(EXIT_SUCCESS);
}
在程序退出我设置了一个退出时延,其实就是让系统计算一个数累加而已,可自调。
4.
//练习6.4 用户可以通过加入者姓名、头衔和BOP来了解到个人
//结构体存储成员信息,结构体数组存储对应成员
//用户选择展示方式
//等熟悉后面标准库的使用,可以改成输入个人姓名(或BOP),系统输出个人信息,这样更符合期望
#include <iostream>
using namespace std;
const int strsize = 30;
const int Max = 5;
void showmenu();
void showname();
void showtitle();
void hopname();
void show_preference();
void quit();
struct bop
{
char name[strsize];
char bopname[strsize];
char title[strsize];
int preference;
};
bop person[Max] = {
{"Wimp Macho", "Mach", "Junior Programmer", 1},
{"Raki Rhodes", "Rhodes", "Intermediate Programmer", 2},
{"Celia Laiter", "Laiter", "Senior Programmer", 3},
{"Hoppy Hipman", "Hipman", "Green Hand", 0},
{"Jack.H", "Jim", "Boss", 4},
};
int main()
{
showmenu();
char choice;
while (cin >> choice && choice != 'q')
{
switch (choice)
{
case 'a':
showname();
break;
case 'b':
showtitle();
break;
case 'c':
hopname();
break;
case 'd':
show_preference();
break;
default:
cout << "It's not invalid.Please try again:" << endl;
break;
}
cout << "You can try another:" << endl;
}
quit();
system("pause");
return 0;
}
void showmenu() //菜单展示
{
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 q)quit" << endl;
}
void showname()
{
for (int i = 0; i < Max; i++)
{
cout << person[i].name << endl;
}
}
void showtitle()
{
for (int j = 0; j < Max; j++)
{
cout << '\t' << person[j].name << person[j].title << endl;
}
cout << endl;
}
void hopname()
{
for (int k = 0; k < Max; k++)
{
cout << person[k].bopname << endl;
}
}
void show_preference()
{
int small = 0;
for (int m = 0; m < Max; m++)
{
small = person[m].preference;
switch (small)
{
case 0:
cout <<person[3].name<<' '<<person[3].title << endl;
break;
case 1:
cout <<person[0].name<<' '<< person[0].title << endl;
break;
case 2:
cout <<person[1].name<<' '<< person[1].title << endl;
break;
case 3:
cout <<person[2].name<<' '<< person[2].title << endl;
break;
default:
cout <<person[4].name<<' '<< person[4].title << endl;
break;
}
}
}
void quit()
{
cout << "Bye." << endl;
long long wait = 0;
while (wait < 4000000000) //设置一个退出时延
wait++;
exit(EXIT_SUCCESS);
}
本来是想做得智能点的,结果越走越偏,就成这么臃肿了,有改进方法请指教指教。
- 编写一个计算纳税查询程序,其实可以加一个效果是遇特殊字符退出之类的,不过我懒就没改。
//练习6.5 编写计算纳税的程序
//货币:tvarp
//纳税规则:5000(不纳税);5001-15000(0.1);15001-35000(0.15);大于35000(0.2)
//简单计算38000tvarp收入就是:sum=5000x0.0+10000x0.10+20000x0.15+3000x0.2
#include <iostream>
using namespace std;
int main()
{
double income = 0.0;
double sum = 0.0;
int n;
cout << "Please enter your income:";
cin >> income;
n = income / 5000;
switch (n)
{
case 0:
cout << "You don't have to pay for tax.Sorry and happy for you." << endl;
break;
case 1:
case 2:
case 3:
sum = (income - 5000) * 0.10;
break;
case 4:
case 5:
case 6:
case 7:
sum = (income - 15000) * 0.15 + 1000.0;
break;
default:
sum = (income - 35000) * 0.2 + 3000.0 + 1000.0;
}
cout << "You should pay tax " << sum << " tvarps,thank you." << endl;
system("pause");
return 0;
}
- 捐款信息处理
//练习6.6 编写关于捐献人和捐款的信息处理程序
//建立结构体donate包含捐款人和捐款额度,建立动态结构体数组donator存储
//捐款超过10000的为重要捐款人(Grand Patrons),读取数据后,对重要捐款人信息进行列表显示
//输出列表以Patrons开头,无名捐款人用none显示
#include <iostream>
#include <string>
using namespace std;
const int Max = 10; //动态结构体数组大小
struct donation
{
string name;
double money;
};
int main()
{
donation *donator = new donation[Max];
donation *grand_patrons = new donation[Max];
int m, n = 0;
int k = 0;
cout << "Please enter the numbers of the donators:";
cin >> m;
cin.get();
for (int i = 0; i < m; i++)
{
cout << "Please input the name(Anonymous donator can replace with none):";
getline(cin, donator[i].name);
cout << "Please input the donation:";
cin >> donator[i].money;
cin.get();
}
for (int j = 0; j < m; j++)
{
if (donator[j].money >= 10000)
{
grand_patrons[k] = donator[j];
k++;
}
}
if (k)
{
cout << "Patrons" << endl;
for (int z = 0; z < k + 1; z++)
{
cout << grand_patrons[z].name << '\t' << grand_patrons[z].money << endl;
}
}
else
cout << "There is no one donated over 10000." << endl;
delete[] donator;
delete[] grand_patrons;
system("pause");
return 0;
}
其实后面还有不恰当输出,但我到瓶颈了,就先放一放。
7. 编写识别输入字符信息的程序(代码是有问题的,没错,有问题,得不到理想输出,但我不知道怎么解决)
//练习6.7 单词输入存储
//直到只输入q就停止输入,指出包含多少个元音打头的单词和有多少个辅音打头单词,另外还有不在此类的数目
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
int vowel = 0;
int consonant = 0;
int other = 0;
string s;
cout << "Enter words (q to quit):" << endl;
while ((cin >> s))
{
if (s[0] = 'q' && s.size() == 1)
{
break;
}
else if (isalpha(s[0]))
{
if (s[0] == 'a' || s[0] == 'e' || s[0] == 'i' || s[0] == 'o' || s[0] == 'u')
vowel++;
else
consonant++;
}
else
other++;
}
cout << vowel << " words beginning with vowels," << endl;
cout << consonant << " words beginning with consonant," << endl;
cout << other << " others." << endl;
system("pause");
return 0;
}
- 读取文件,识别包含多少个单词
//练习6.7 编写打开文本文件程序,读取字符并输出包含字符数
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
const int Max = 1000;
using namespace std;
int main()
{
string note;
ifstream inFile;
cout << "Please enter the name fo data file:";
getline(cin, note);
inFile.open(note);
while (!inFile.is_open())
{
cout << "Could not open the file " << note << endl;
cout << "Programming terminating." << endl;
exit(EXIT_FAILURE);
}
string str;
int sum = 0;
int count = 0;
getline(inFile, str, ' ');
while (inFile)
{
sum++;
count++;
inFile >> str;
}
if (inFile.eof())
cout << "End of file reached." << endl;
else if (inFile.fail())
cout << "Input terminated by data mismatch." << endl;
else
cout << "Input terminated for unknown reason." << endl;
if (count == 0)
cout << "No data processed." << endl;
else
cout << "There are " << sum << " charcters." << endl;
inFile.close();
system("pause");
return 0;
}
- 暂定,请期待。
内容总结
以上是互联网集市为您收集整理的c++ primer plus第六版第六章编程练习全部内容,希望文章能够帮你解决c++ primer plus第六版第六章编程练习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。