6.11.1
#include <iostream>
#include <cctype>
using namespace std;
int main(void)
{
char ch;
while(cin >> ch && ch != '@')
{
if(isupper(ch))
ch = tolower(ch);
else if(islower(ch))
ch = toupper(ch);
cout << ch;
}
return 0;
}
6.11.2
#include <iostream>
using namespace std;
int main(void)
{
double donation[10]{0};
int i;
for(i = 0; i < 10 && cin >> donation[i]; i++);
double pj = 0;
for(int j = 0; j < i; j++)
pj += donation[j];
pj /= i;
cout << "平均值为:" << pj << endl;
int count = 0;
for(int j = 0; j < i; j++)
if(donation[j] > pj)
count++;
cout << "一共有" << count << "个数大于平均数" << endl;
return 0;
}
6.11.3
#include <iostream>
using namespace std;
void Show(void);
int main(void)
{
Show();
char ch;
cout << "Please enter a c, p, t, or g: ";
while(cin >> ch && ch != '#')
{
switch(ch)
{
case 'c':
cout << "Tigers are carnivores." << endl;
break;
case 'p':
cout << "You're a pianist." << endl;
break;
case 't':
cout << "A maple is a tree." << endl;
break;
case 'g':
cout << "Let's finish the game." << endl;
break;
}
cout << "Please enter a c, p, t, or g: ";
}
return 0;
}
void Show(void)
{
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore\t\tp) pianist" << endl;
cout << "t) tree\t\t\tg) game" << endl;
}
6.11.4
#include <iostream>
using namespace std;
const int strsize = 30;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void show(void);
int main(void)
{
show();
bop array[4] =
{
{"Wimp Macho", "xswl", "HLX", 0},
{"Raki Rhodes", "sb", "NMSL", 2},
{"Celia Laiter", "nt", "WSND", 1},
{"Hoppy Hipman", "WDNMD", "nmzl", 2}
};
char ch;
cout << "Enter your choice: ";
while(cin >> ch && ch != 'q')
{
switch(ch)
{
case 'a':
for(int i = 0; i < 4; i++)
cout << array[i].fullname << endl;
break;
case 'b':
for(int i = 0; i < 4; i++)
cout << array[i].title << endl;
break;
case 'c':
for(int i = 0; i < 4; i++)
cout << array[i].bopname << endl;
break;
case 'd':
for(int i = 0; i < 4; i++)
{
switch (array[i].preference)
{
case 0:
cout << array[i].fullname << endl;
break;
case 1:
cout << array[i].title << endl;
break;
case 2:
cout << array[i].bopname << endl;
break;
}
}
break;
default:
cout << "错误输入" << endl;
}
}
return 0;
}
void show(void)
{
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name\tb. display by title" << endl;
cout << "c. display by bopname d. display by preference" << endl;
cout << "q. quit" << endl;
}
6.11.5
#include <iostream>
using namespace std;
int main(void)
{
float tvarp;
cout << "请输入您的收入: ";
while(cin >> tvarp && tvarp > 0)
{
if(tvarp <= 5000)
cout << "您的税收为0" << endl;
else if(tvarp > 5000 && tvarp <= 15000)
{
tvarp = (tvarp - 5000) * 0.1;
cout << "您的税收为" << tvarp << endl;
}
else if(tvarp > 15000 && tvarp <= 35000)
{
tvarp = 10000 * 0.1 + (tvarp - 15000) * 0.15;
cout << "您的税收为" << tvarp << endl;
}
else if(tvarp > 35000)
{
tvarp = 10000 * 0.1 + 20000 * 0.15 + (tvarp - 35000) * 0.2;
cout << "您的税收为" << tvarp << endl;
}
cout << "请输入您的收入: ";
}
return 0;
}
6.11.6
#include <iostream>
#include <string>
using namespace std;
struct Organization
{
string name;
double deposit;
};
int main(void)
{
int num;
cout << "请输入捐献者的数目: ";
cin >> num;
Organization * p_O = new Organization[num];
for(int i = 0; i < num; i++)
{
cout << "请输入第" << i + 1 << "位捐献者的姓名: ";
cin >> p_O[i].name;
cout << "请输入第" << i + 1 << "位捐献者的捐款数目: ";
cin >> p_O[i].deposit;
}
cout << "***********************" << endl;
cout << " Grand Patrons" << endl;
for(int i = 0; i < num; i++)
if(p_O[i].deposit > 10000)
cout << p_O[i].name << '\t' << p_O[i].deposit << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << " Patrons" << endl;
for(int i = 0; i < num; i++)
if(p_O[i].deposit < 10000)
cout << p_O[i].name << '\t' << p_O[i].deposit << endl;
cout << "***********************" << endl;
delete [] p_O;
return 0;
}
6.11.7
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main(void)
{
char word[30];
int vowel = 0;
int consonant = 0;
int other = 0;
cout << "Enter words (q to quit):" << endl;
while(cin >> word)
{
if(strlen(word) == 1 && word[0] == 'q')
break;
if (isalpha(word[0]))
{
if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'u' || word[0] == 'o')
vowel++;
else
consonant++;
}
else
other++;
}
cout << "以元音字母开头的有" << vowel << endl
<< "以辅音字母开头的有" << consonant << endl
<< "不属于这俩类的有" << other << endl;
return 0;
}
6.11.8
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ifstream infile;
int count = 0;
int ch;
infile.open("???");
if(!infile.is_open())
return 1;
while(ch = infile.get() && !infile.eof())
count++;
infile.close();
cout << "一共有" << count << "个字符" << endl;
return 0;
}
6.11.9
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Organization
{
string name;
double deposit;
};
int main(void)
{
int num;
ifstream infile;
infile.open("??");
if(!infile)
return 1;
infile >> num;
Organization * p_O = new Organization[num];
for(int i = 0; i < num; i++)
{
infile >> p_O[i].name;
infile >> p_O[i].deposit;
}
infile.close();
cout << "***********************" << endl;
cout << " Grand Patrons" << endl;
for(int i = 0; i < num; i++)
if(p_O[i].deposit > 10000)
cout << p_O[i].name << '\t' << p_O[i].deposit << endl;
cout << "***********************" << endl;
cout << "***********************" << endl;
cout << " Patrons" << endl;
for(int i = 0; i < num; i++)
if(p_O[i].deposit < 10000)
cout << p_O[i].name << '\t' << p_O[i].deposit << endl;
cout << "***********************" << endl;
delete [] p_O;
return 0;
}