第六章
练习1: 读取键盘输入,直到遇到@
符号,并回显输入(数字除外),同时将大小写字母互相转换。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while(cin.get(ch) && ch != '@')
{
if (!isdigit(ch))
{
if (islower(ch))
{
ch = toupper(ch);
}
else
{
ch = tolower(ch);
}
cout << ch;
}
}
return 0;
}
练习2: 最多将10个值读入一个double
数组(或array
)中,程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
#include <iostream>
#include <array>
using namespace std;
int main()
{
const int max = 10;
array<double,max> donation;
int i = 0;
double average = 0;
double amount = 0;
int numberOfHigher = 0;
while(i < max && cin >> donation[i])
{
amount += donation[i];
i++;
}
average = amount / i;
for(double j : donation)
{
if (j > average) {
numberOfHigher++;
}
}
cout << "average is : " << average << " and there are "
<< numberOfHigher << " numbers are higher than the average number! " << endl;
return 0;
}
练习3: 程序显示一个提供4个选项的菜单,每个选项用一个字母标记,如果用户使用有效选项以外的字母进行相应,程序将提示用户输入一个有效的字母,直到用户这样做为止,然后,程序使用一条switch
语句,根据用户的选择执行一个简单操作。
#include <iostream>
#include <array>
using namespace std;
void showmenu()
{
cout<< "Please enter one of the following choices, q to quit:" << endl;
cout<< "c) carnivore\n"
<< "p) pianist\n"
<< "t) tree\n"
<< "g) game\n";
}
int main()
{
showmenu();
char choice;
cin >> choice;
while(choice != 'q')
{
switch (choice)
{
case 'c':
cout << "A maple is a " << "carnivore" << endl;
break;
case 'p':
cout << "A maple is a " << "pianist" << endl;
break;
case 't':
cout << "A maple is a " << "tree" << endl;
break;
case 'g':
cout << "A maple is a " << "game" << endl;
break;
case 'q':
cout << "Bye" << endl;
return 0;
default:
cout << "Invalid input! Please input again!\n" << endl;
}
showmenu();
cin >> choice;
}
return 0;
}
练习4: 编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员,编写程序时,使用下面的结构:
//Benevolent Order of Programers name structure
struct bop{
char fullname[strsize]; //real name
char title[strsize]; //job title
char bopname[strsize]; //secret BOP name
int preference; //0 = fullname, 1 = title, 2 = bopname
}
该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值,另外,程序使用一个循环,让用户在下面的选项中进行选择:
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
注意,display by preference
并不意味着显示成员的偏好,而是意味着根据的成员的偏好来列出成员,例如,如果偏好为1,则选择d
将显示程序员的头衔。该程序的运行情况如下:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Enter your choice: a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
程序代码:
#include <iostream>
#include <array>
using namespace std;
const int strsize = 51;
struct bop
{
char fullname[strsize]; //real name
char title[strsize]; //job title
char bopname[strsize]; //secret BOP name
int preference; //0 = fullname, 1 = title, 2 = bopname
};
void showMenu()
{
cout << "Benevolent Order of Programmers Report\n";
cout << "a. display by name b. display by title\n";
cout << "c. display by bopname d. display by preference\n";
cout << "q. quit\n";
cout << "Enter your choice, q to quit: ";
}
void displayByName(const bop *bopArray)
{
for(int i = 0;i < 3; i++)
{
cout << bopArray[i].fullname << endl;
}
}
void displayByTitle(const bop *bopArray)
{
for(int i = 0;i < 3; i++)
{
cout << bopArray[i].title << endl;
}
}
void displayByBopname(const bop *bopArray)
{
for(int i = 0;i < 3; i++)
{
cout << bopArray[i].bopname << endl;
}
}
void displayByPrefer(const bop *bopArray)
{
for(int i = 0; i < 3; i++)
{
switch (bopArray[i].preference)
{
case 0:
cout << bopArray[i].fullname << endl;
break;
case 1:
cout << bopArray[i].title << endl;
break;
case 2:
cout << bopArray[i].bopname << endl;
break;
default:
break;
}
}
}
int main()
{
bop men[3] =
{
{"name1","job1","bopname1",0},
{"name2","job2","bopname1",1},
{"name3","job3","bopname3",2}
};
showMenu();
char choice;
cin >> choice;
while(choice != 'q')
{
switch (choice)
{
case 'a':
displayByName(men);
break;
case 'b':
displayByTitle(men);
break;
case 'c':
displayByBopname(men);
break;
case 'd':
displayByPrefer(men);
break;
default:
cout << "Invalid input, please try again!\n";
}
cout << "Next choice: ";
cin >> choice;
}
cout << "Bye!";
return 0;
}
练习5: 编写一个程序,使用循环来要求用户输入收入,并报告所得税,当用户输入负数或非数字时,循环结束。
所得税的计算方式如下
5000 tvarps: 0%
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35001 tvarps and more: 20%
例如,收入为38000 tvarps 时,所得税为
>5000 * 0.00 + 10000 * 0.10 + 20000 * 0.15 + 3000 * 0.20 即 4600 tvarps
程序代码
#include <iostream>
#include <array>
#include <iomanip>
using namespace std;
double calculateor(double income)
{
if (income >= 35000)
{
return 10000 * 0.10 + 20000 * 0.15 + (income - 35000) * 0.20;
}
else if (income >= 15000)
{
return 10000 * 0.10 + (income - 15000) * 0.15;
}
else if (income >= 5000)
{
return (income - 5000) * 0.10;
}
else
{
return 0;
}
}
int main()
{
cout << "Please input your income, non-number or nagetive number to end: ";
double input = 0;
while(cin >> input && input > 0)
{
cout<<fixed << setprecision(2)
<< "You need to pay a tax of "
<< calculateor(input)
<< " tvarps!" << endl;
cout << "Next?" << endl;
}
cout << "Bye!";
return 0;
}
练习6: 编写程序记录捐助资金。
* 程序要求输入捐献者数目,然后要求输入每一个捐献者的姓名和款项。
* 这些信息存储在一个**动态分配**的**结构数组**中。
* 每个结构有两个成员:
* 用来存储姓名的字符数字或`string`对象。
* 用来存储款项的`double`成员。
* 读取所有数据后,程序将显示所有捐款超过10000的捐款者的信息(姓名和款项)。
* 该列表前应包含一个标题,指出下面的捐款者时重要捐款人`Grand Patrons`
* 然后程序将列出其他的捐款者,该列表要以`Patrons`开头。
* 如果某种类别没有捐款者,程序将打印单词`None`。
* 该程序只显示,不排序。
程序代码
#include <iostream>
using namespace std;
int numberOfPatrons = 0;
struct patron
{
string name;
double money;
};
void pick_Grand(const patron *grand)
{
int grandCount = 0;
cout << "Grand Patrons: " << endl << endl;
for(int i = 0; i < numberOfPatrons; i++)
{
if (grand[i].money >= 10000)
{
grandCount++;
cout << "name: " << grand[i].name << endl;
cout << "money: " << grand[i].money << endl << endl;
}
}
if (grandCount == 0) {
cout << "None" << endl << endl;
}
}
void pick_Nomal(const patron *nomal)
{
int nomalCount = 0;
cout << "Patrons: " << endl << endl;
for(int i = 0; i < numberOfPatrons; i++)
{
if (nomal[i].money < 10000)
{
nomalCount++;
cout << "name: " << nomal[i].name << endl;
cout << "money: " << nomal[i].money << endl << endl;
}
}
if (nomalCount == 0) {
cout << "None" << endl << endl;
}
}
int main()
{
cout << "Input number of patrons: ";
cin >> numberOfPatrons;
patron *Patrons = new patron[numberOfPatrons];
for(int i = 0; i < numberOfPatrons; i++)
{
cout << "Please input No." << i+1 << " patron's name: ";
cin.get();
getline(cin,Patrons[i].name);
cout << "Please input No." << i+1 << " patron's money: ";
cin >> Patrons[i].money;
}
pick_Grand(Patrons);
pick_Nomal(Patrons);
return 0;
}
练习7: 编写程序,每次读取一个单词,直到用户只输入q
。然后该程序指出有多少个单词以元音字母开头,有多少个单词以辅音字母开头,还有多少个单词不属于这两类。
为此,方法之一是使用
isalpha()
来区分以字母和其他字符开头的单词,然后对于通过了isalpha()
测试的单词,使用if
或switch
语句来确定。
该程序的运行情况如下:
输入
Enter word (q to quit):
The 12 awesome oxen ambled
quietly across 15 metres of lawn. q
输出
5 words beginning with vowels
4 words beginning with consonants
2 others
程序代码:
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
cout << "Enter word(q to quit): \n";
char str[51];
int vowels = 0;
int consonants = 0;
int others = 0;
while(cin >> str)
{
if (strcmp(str,"q") == 0)
break;
char ch = str[0];
if (isalpha(ch))
{
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
default:
consonants++;
break;
}
}
else
others++;
}
cout << vowels << " words beginning with vowels\n"
<< consonants << " words beginning with consonants\n"
<< others <<" others\n";
return 0;
}
练习8: 编写程序,打开一个文本文件,逐个字符地读取该文件,直到到达文件末尾,然后指出该文件中包含多少个字符。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
string filename = "d:\\Users\\wdyyy\\Desktop\\test.txt";
infile.open(filename);
if (!infile.is_open())
{
cout << "Could not open the file! "
<< filename << endl;
exit(EXIT_FAILURE);
}
char ch;
int count = 0;
while(infile.get(ch))
count++;
cout << "There are " << count
<< " characters in the file "
<< filename
<< endl;
return 0;
}
练习9: 改写编程练习6,使其从文件中读取所需信息。该文件的第一项为捐款人数,余下的内容为成对的行。在每一对中,第一行为捐款人i姓名,第二行为捐款数额
该文件类似如下格式:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000
程序代码:
#include <iostream>
#include <fstream>
using namespace std;
int numberOfPatrons = 0;
struct patron
{
string name;
double money;
};
void pick_Grand(const patron *grand)
{
int grandCount = 0;
cout << "Grand Patrons: " << endl << endl;
for(int i = 0; i < numberOfPatrons; i++)
{
if (grand[i].money >= 10000)
{
grandCount++;
cout << "name: " << grand[i].name << endl;
cout << "money: " << grand[i].money << endl << endl;
}
}
if (grandCount == 0) {
cout << "None" << endl << endl;
}
}
void pick_Nomal(const patron *nomal)
{
int nomalCount = 0;
cout << "Patrons: " << endl << endl;
for(int i = 0; i < numberOfPatrons; i++)
{
if (nomal[i].money < 10000)
{
nomalCount++;
cout << "name: " << nomal[i].name << endl;
cout << "money: " << nomal[i].money << endl << endl;
}
}
if (nomalCount == 0) {
cout << "None" << endl << endl;
}
}
int main()
{
ifstream infile;
string filename = "d:\\Users\\wdyyy\\Desktop\\test.txt";
infile.open(filename);
// cout << "Input number of patrons: ";
infile >> numberOfPatrons;
patron *Patrons = new patron[numberOfPatrons];
for(int i = 0; i < numberOfPatrons; i++)
{
// cout << "Please input No." << i+1 << " patron's name: ";
infile.get();
getline(infile,Patrons[i].name);
// cout << "Please input No." << i+1 << " patron's money: ";
infile >> Patrons[i].money;
}
pick_Grand(Patrons);
pick_Nomal(Patrons);
return 0;
}