#include<iostream>#include<cctype>usingnamespacestd;
int main()
{
char ch;
cin.get(ch);
while (ch != '@')
{
if (!isdigit(ch))
if (islower(ch))
cout << (char) toupper(ch);
elseif (isupper(ch))
cout << (char) tolower(ch);
elsecout << ch;
cin.get(ch);
}
cin.get();
cin.get();
}
6.2
#include<iostream>#include<cctype>usingnamespacestd;
int main()
{
double number[10];
int i = 0, max = 10;
cout << "number #1: ";
while (cin >> number[i] && i < max)
{
if (++i < max)
cout << "number #" << i + 1 << ": ";
}
if (!cin)
{
cin.clear();
cin.get();
} //此处添加的if是为了防止一输入非数字程序直接关闭cin.get();
cin.get();
}
6.3
#include<iostream>usingnamespacestd;
int main()
{
char ch;
cout << "Please enter one of the folowing choices:\n"
<< "c) carnivore\tp) pianist\n"
<< "t) tree\t\tg) game\n";
do
{
cin >> ch;
switch (ch)
{
case'c': cout << "A maple is a carnivore.";
break;
case'p': cout << "A maple is a pianist.";
break;
case't': cout << "A maple is a tree.";
break;
case'g': cout << "A maple is a game.";
break;
default: cout << "Please enter a c, p, t or g: ";
}
} while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g');
cin.get();
cin.get();
}
6.4
#include<iostream>usingnamespacestd;
constint strSize = 20;
constint num_people = 3;
struct bop {
char fullname[strSize];
char title[strSize];
char bopname[strSize];
int preference;
};
int main()
{
char choice;
bop people[num_people] =
{
{"Wimp Macho", "imp", "acho", 1 },
{"Riki Rhodes", "riki","rhodes", 2},
{"Cellia Laiter", "cellia", "Laiter", 3},
};
cout << "Benevolet Order of Programmers Report\n"
<< "a. display by name\tb. display by title\n"
<< "c. display by bopname\td. display by preference\n"
<< "q. quit\n";
cout << "Enter your choice: ";
cin >> choice;
while (choice != 'q')
{
switch (choice)
{
case'a': for (int i = 0; i < num_people; ++i)
{
cout << people[i].fullname << endl;
}
break;
case'b': for (int i = 0; i < num_people; ++i)
{
cout << people[i].title << endl;
}
break;
case'c': for (int i = 0; i < num_people; ++i)
{
cout << people[i].bopname << endl;
}
break;
case'd': for (int i = 0; i < num_people; ++i)
{
switch (people[i].preference)
{
case1: cout << people[i].fullname << endl;
break;
case2: cout << people[i].title << endl;
break;
case3: cout << people[i].bopname << endl;
break;
};
}
break;
default: cout << "Please enter a, b, c, d or q: ";
};
if (choice == 'a' || choice == 'b' || choice == 'c' || choice == 'd')
cout << "Next choice: ";
cin >> choice;
}
cin.get();
cin.get();
}