6.11 编程练习
- 第一题
#include <iostream>
#include <cctype>
int main() {
using std::cin;
using std::cout;
char ch;
while (cin.get(ch) && ch != '@')
{
if(!isdigit(ch))
if(islower(ch))
cout << char(toupper(ch));
else if(isupper(ch))
cout << char(tolower(ch));
else
cout << ch;
}
return 0;
}
运行结果:
zxcVBNm@
ZXCvbnM
- 第二题
#include <iostream>
#include <array>
const int MaxArray = 10;
int main() {
using std::cin;
using std::cout;
using std::endl;
std::array<double, MaxArray> stu;
double all=0, ave=0;
int num = 0;
int big = 0;
while ((cin >> stu[num]) && (num < MaxArray))
{
all += stu[num];
num += 1;
}
ave = all/num;
for (int i=0;i<10;i++)
{
if (stu[i] > ave)
big += 1;
}
cout << "数字的平均值为:" << ave << endl;
cout << "比平均值大的值的个数为:" << big << endl;
// 重置cin以接收新的输入;删除错误输出
if(!cin)
{
cin.clear();
cin.get();
}
int j;
cin >> j;
cout << j;
return 0;
}
运行结果:
1 2 3 z
数字的平均值为:2
比平均值大的值的个数为:1
5
5
这里特意说明一下,输入z导致cin输入出现错误,因为类型要求是double,如果没有第29行的if语句进行重置cin输入和删除错误输入,那么是不会执行后面的语句的,有兴趣的可以删除if语句,结果会直接结束,不会给用户输入j数值的机会。
- 第三题
该题有很大的发挥空间,这里就简单的按题目要求来做
#include <iostream>
using namespace std;
void showmenu();
int main() {
showmenu();
char ch;
int flag = 1;
while (flag && cin >> ch)
{
switch (ch) {
case 'c':
cout << "you choose a carnivore.\n";
flag = 0;
break;
case 'p':
cout << "you choose a pianist.\n";
flag = 0;
break;
case 't':
cout << "A maple is a tree.\n";
flag = 0;
break;
case 'g':
cout << "you choose a game.\n";
flag = 0;
break;
default:
cout << "Please enter a c, p, t, or g:";
}
}
return 0;
}
void showmenu()
{
cout << "Please enter one of the following choices:\n"
"c) carnivore p) pianist\n"
"t) tree g) game\n";
}
运行结果:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
f
Please enter a c, p, t, or g:q
Please enter a c, p, t, or g:t
A maple is a tree.
- 第四题
#include <iostream>
using namespace std;
const int strsize = 20;
const int people = 5;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void showmenu();
void display_name(bop []);
void display_title(bop []);
void display_bopname(bop []);
void display_preference(bop []);
int main() {
bop product[people] = {
{"Wimp Macho", "Wimp Macho", "WM", 0},
{"Raki Rhodes", "Junior Programmer", "RR", 1},
{"Celia Laiter", "MIPS", "CL", 2},
{"Hoppy Hipman", "Analyst Trainee", "HH", 2},
{"Pat Hand", "LOOPY", "PH", 1}
};
showmenu();
cout << "Enter your choice:";
char ch;
int flag = 1;
while (flag && cin >> ch)
{
switch (ch) {
case 'a':
display_name(product);
cout << "Next choice:";
break;
case 'b':
display_title(product);
cout << "Next choice:";
break;
case 'c':
display_bopname(product);
cout << "Next choice:";
break;
case 'd':
display_preference(product);
cout << "Next choice:";
break;
default:
cout << "Bye!";
flag = 0;
}
}
return 0;
}
void showmenu()
{
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";
}
void display_name(bop * product)
{
for (int i=0;i<people;i++)
{
cout << product[i].fullname << endl;
}
}
void display_title(bop * product)
{
for (int i=0;i<people;i++)
{
cout << product[i].title << endl;
}
}
void display_bopname(bop * product)
{
for (int i=0;i<people;i++)
{
cout << product[i].bopname << endl;
}
}
void display_preference(bop * product)
{
for (int i=0;i<people;i++)
{
if (product[i].preference == 0)
cout << product[i].fullname << endl;
else if (product[i].preference == 1)
cout << product[i].title << endl;
else
cout << product[i].bopname << endl;
}
}
运行结果:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d. display by preference
Enter your choice:a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice:d
Wimp Macho
Junior Programmer
CL
HH
LOOPY
Next choice:q
Bye!
- 第五题
#include <iostream>
using namespace std;
int main() {
int tvarp;
int fax;
cout << "请输入货币值:\n";
while (cin >> tvarp && tvarp >= 0)
{
if (tvarp > 35000)
fax = (tvarp-35000)*0.20 + 5000*0.00 + 10000*0.10 + 20000*0.15;
else if (15001 <= tvarp && tvarp <= 35000)
fax = (tvarp-15000)*0.15 + 5000*0.00 + 10000*0.10;
else if (5001 <= tvarp && tvarp <= 15000)
fax = (tvarp-5000)*0.10 + 5000*0.0;
else
fax = 0;
cout << "所得税为:" << fax << endl;
}
return 0;
}
运行结果:
请输入货币值:
38000
所得税为:4600
5
所得税为:0
l
- 第六题
#include <iostream>
#include <string>
using namespace std;
struct donation
{
string name;
double money;
};
int main() {
cout << "请输入捐赠者的数量:";
int num;
cin >> num;
cin.get();
donation * don = new donation [num];
cout << "请输入捐赠者的信息:\n";
for (int i=0;i<num;i++)
{
getline(cin, don[i].name);
cin >> don[i].money;
cin.get();
}
int num_grand = 0;
cout << "Grand Patrons\n";
for (int i=0;i<num;i++)
{
if (don[i].money > 10000)
{
cout << don[i].name << " " << don[i].money << endl;
num_grand += 1;
}
}
if (num_grand == 0)
cout << "none\n";
int num_normal = 0;
cout << "Patrons\n";
for (int i=0;i<num;i++)
{
if (don[i].money < 10000)
{
cout << don[i].name << " " << don[i].money << endl;
num_normal += 1;
}
}
if (num_normal == 0)
cout << "none\n";
delete [] don;
return 0;
}
运行结果:
请输入捐赠者的数量:3
请输入捐赠者的信息:
zxc 10001
asd 456
qwe 789
Grand Patrons
zxc 10001
Patrons
asd 456
qwe 789
另外一种情况:
请输入捐赠者的数量:2
请输入捐赠者的信息:
xzc 10001
asd 10002
Grand Patrons
xzc 10001
asd 10002
Patrons
none
- 第七题
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string ch;
cout << "Enter words (q to quit):\n";
int vowels=0, consonants=0, others=0;
while (cin >> ch && ch != "q")
{
if (isalpha(ch[0]))
{
tolower(ch[0]);
switch (ch[0]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels += 1;
break;
default:
consonants += 1;
}
}
else
others += 1;
}
cout << vowels << " words beginning with vowels\n";
cout << consonants << " words beginning with consonants\n";
cout << others << " others\n";
return 0;
}
运行结果:
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 <fstream>
#include <cstdlib>
using namespace std;
int main() {
ifstream fin;
char ch;
fin.open("../test.txt");
// 判断文件是否被打开
if (!fin.is_open())
{
cout << "Could not open the file\n";
exit(EXIT_FAILURE);
}
int num = 0;
fin >> ch;
while (fin.good()) // 如果没有问题,使用good方法会返回true
{
num += 1;
fin >> ch;
}
if (fin.eof())
cout << "End of file reached.\n"; // 读取数据遇到EOF
else if (fin.fail())
cout << "Input terminated by data mismatch.\n"; // 读取数据遇到类型不匹配和EOF
else
cout << "Input terminated for unknown reason.\n"; // bad方法,读取数据遇到文件受损或者硬件故障
cout << "文件中包含" << num << "个字符" << endl;
fin.close();
return 0;
}
运行结果:
End of file reached.
文件中包含12个字符
文本中内容:
This is a test!
- 第九题
在该题目中我首先把数据写入文件中,然后再从文件中读取数据进行输出,比题目要求的复杂一点,没有直接将数据手动放入文档中。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct donation
{
string name;
double money;
};
int main() {
ofstream outfile;
outfile.open("../donation.txt");
if (!outfile.is_open())
{
cout << "Could not open the file\n";
exit(EXIT_FAILURE);
}
cout << "请输入捐赠者的数量:";
int num;
cin >> num;
cin.get();
outfile << num << endl;
donation * don = new donation [num];
cout << "请输入捐赠者的信息并写入到文档中:\n";
// 写
for (int i=0;i<num;i++)
{
getline(cin, don[i].name);
outfile << don[i].name << endl;
cin >> don[i].money;
outfile << don[i].money << endl;
cin.get();
}
// 读
ifstream infile;
int all;
infile.open("../donation.txt");
infile >> all;
infile.get();
cout << all << endl;
donation * don_1 = new donation [all];
for (int i=0;i<all;i++)
{
getline(infile, don_1[i].name);
infile >> don_1[i].money;
infile.get();
}
// 对读出的数据分类
int num_grand = 0;
cout << "Grand Patrons\n";
for (int i=0;i<all;i++)
{
if (don_1[i].money > 10000)
{
cout << don_1[i].name << " " << don_1[i].money << endl;
num_grand += 1;
}
}
if (num_grand == 0)
cout << "none\n";
int num_normal = 0;
cout << "Patrons\n";
for (int i=0;i<all;i++)
{
if (don_1[i].money < 10000)
{
cout << don_1[i].name << " " << don_1[i].money << endl;
num_normal += 1;
}
}
if (num_normal == 0)
cout << "none\n";
delete [] don;
delete [] don_1;
return 0;
}
/* 方便复制黏贴测试
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
*/
运行结果:
请输入捐赠者的数量:4
请输入捐赠者的信息并写入到文档中:
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
4
Grand Patrons
Freida Flass 100500
Rich Raptor 55000
Patrons
Sam Stone 2000
Tammy Tubbs 5000