#include <iostream>
#include <string>
#include <ctime>
#include <cstring>
#include <fstream>
#include <cctype>
using namespace std;
void test1()
{
char ch;
cin.get(ch);
while (ch != '@')
{
if (islower(ch)) // 是小写
// cout << char(ch - 32) ;
cout << (char) toupper(ch);
else if (isupper(ch)) //是大写
// cout << (char)(ch + 32) ;
cout << (char) tolower(ch);
else if (isdigit(ch)) // 是数字
;
else
cout << ch;
cin >> ch;
}
cout << ch << endl;
return;
}
运行结果:
void p6_2()
{
double donation , sum = 0.0, averge = 0.0, array[10] = { 0.0 };
int count = 0;
cout << "Please enter words: ";
while ((cin >> donation) && !isdigit(donation))
{
if (count < 10){
array[count] = donation;
sum += array[count];
++count;
}
else
{
cout << "There are more than 10 words.\n";
break;
}
}
cout << "count = " << count << endl;
averge = sum / (count + 1);
cout << "averge value = " << averge << endl;
for (int j = 0; j <= count; j++)
if (array[j] > averge){
cout << "greater than averge: " << array[j] << endl;
}
return;
}

void p6_3()
{
char ch;
cout << "Please enter one of the following choices:\n";
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
while (cin >> ch){
switch(ch)
{
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;
default:
cout << "Please enter a c, p, t, or g: ";
continue;
}
}
return;
}
运行结果;

Wim
void p6_4()
{
typedef struct bop
{
char fullname[size];
char title[size];
char bopname[size];
int preference; // 0 = fullname, 1 = title, 2 = bopname
}tbop;
tbop BOP[5] = {
{"Wimp Macho", "teacher", "A", 0},
{"Raki Rhodes", "Junior Progammer", "B", 1},
{"Celia Laiter", "docter", "MIPS", 2},
{"Hoppy Hipman", "Analyst Trainee", "C", 1},
{"Pat Hand", "officer", "LOOPY", 2}
};
char ch;
cout << "Benevolent Order of Programers 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: ";
while (cin >> ch && ch != 'q'){
switch(ch)
{
case 'a':
cout << BOP[0].fullname << endl;
cout << BOP[1].fullname << endl;
cout << BOP[2].fullname << endl;
cout << BOP[3].fullname << endl;
cout << BOP[4].fullname << endl;
break;
case 'b':
cout << BOP[0].title << endl;
cout << BOP[1].title << endl;
cout << BOP[2].title << endl;
cout << BOP[3].title << endl;
cout << BOP[4].title << endl;
break;
case 'c':
cout << BOP[0].bopname << endl;
cout << BOP[1].bopname << endl;
cout << BOP[2].bopname << endl;
cout << BOP[3].bopname << endl;
cout << BOP[4].bopname << endl;
break;
case 'd':
for (int i = 0; i < 5; i++)
{
if(BOP[i].preference == 0)
cout << BOP[i].fullname << endl;
if(BOP[i].preference == 1)
cout << BOP[i].title << endl;
if(BOP[i].preference == 2)
cout << BOP[i].bopname << endl;
}
break;
default:
break;
}
cout << "Next choice: ";
}
cout << "Bye!\n";
return;
}

void p6_5()
{
int income = 0;;
double tax = 0.0;
cout << "Please enter income of user: ";
// 检测输入为负数或者非数字, 退出循环
while (cin >> income && income >= 0 && !isdigit(income))
{
if (income >= 0 && income <= 5000)
tax = 0.0;
else if (income >= 5001 && income <= 15000)
tax = (income - 5000) * 0.1;
else if (income >= 15001 && income <= 35000)
tax = 10000 * 0.1 + (income - 15000) * 0.15;
else
tax = 10000 * 0.1 + 15000 * 0.15 + (income - 35000) * 0.20;
cout << "income of user is " << income << " tvarps.\n";
cout << "tax of user is " << tax << " tvarps.\n";
}
return;
}
输出结果
void p6_6()
{
struct Team {
string name;
double donation;
};
cout << "捐赠者的数量: " ;
int num , count = 0;
cin >> num;
Team* team = new Team[num];
for (int i = 0; i < num; i++)
{
cout << "输入第" << i+1 << "位姓名: ";
cin >> team[i].name;
cout << "输入第" << i+1 << "位款项: ";
cin >> team[i].donation;
}
cout << "Grand Patrons\n";
for (int i = 0; i < num; i++)
{
if (team[i].donation >= 10000){
cout << team[i].name << " " << team[i].donation << endl;
count++;
}
}
if (count == 0)
cout << "none" << endl;
count = 0;
cout << "Patrons" << endl;
for (int i = 0; i < num; i++)
{
if (team[i].donation < 10000){
cout << team[i].name << " " << team[i].donation << endl;
count++;
}
}
if (count == 0)
cout << "none" << endl;
return;
}