第1题
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout << "Enter something: ";
while (cin.get(ch) && ch != '@')
{
if (!isdigit(ch))
{
if (isalpha(ch))
{
if (isupper(ch))
{
ch = tolower(ch);
}
else if (islower(ch))
{
ch = toupper(ch);
}
}
cout << ch;
}
}
}
第2题
#include <iostream>
#include <array>
const int num = 5;
using namespace std;
int main()
{
array <double, num> donations;
int i;
double sum = 0;
cout << "Enter donation #1: ";
for (i = 0; i < num && cin >> donations[i]; i++)
{
if ((i + 2) <= num)
{
cout << "Enter donation #" << i + 2 << ": ";
}
sum += donations[i];
}
cout << "You enter " << i << " numbers.\n";
for (int j = 0; j < i; j++)
{
cout << "donation[" << j + 1 << "] = " << donations[j] << endl;
}
cout << "sum = " << sum << endl;
double average = sum / i;
cout << "average = " << average << endl;
int average_num = 0;
for (int j = 0; j < i; j++)
{
if (donations[j] > average)
{
cout << "donations[" << j+1 << "] = " << donations[j] << " > average.\n";
average_num++;
}
}
cout << average_num << " numbers > average.\n";
}
第3题
#include <iostream>
using namespace std;
void showmenu();
int main()
{
char ch;
showmenu();
while (cin >> ch && ch != 'q' && ch != 'Q')
{
switch (ch)
{
case 'c':
case 'C':cout << "You choose carnivore.\n";
break;
case 'p':
case 'P':cout << "You choose pianist.\n";
break;
case 't':
case 'T':cout << "You choose tree.\n";
break;
case 'g':
case 'G':cout << "You choose game.\n";
break;
default:
break;
}
cout << "Please enter a c, p, t, or g: ";
}
}
void showmenu()
{
cout << "Please enter one of the following choices:\n"
"c) carnivore \t\t p) pianist\n"
"t) tree \t\t g) game\n"
"q) quit\n"
"Please enter a c, p, t, or g (q to quit): ";
}
第4题
#include <iostream>
using namespace std;
const int strsize = 20;
void showmenu();
struct bop {
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference; //0 = fullname, 1 = title, 2 = bopname
};
int main()
{
bop bops[5] =
{
{"Wimp Macho","teacher","WM",0},
{"Raki Rhodes","student","RR",1},
{"Celia Laiter","Boss","CL",2},
{"Hoppy Hipman","staff","HH",1},
{"Pat Hand","police","PH",0}
};
char ch;
showmenu();
while (cin >> ch && ch != 'q')
{
switch (ch)
{
case 'a':
case 'A':
for (int i = 0; i < 5; i++)
{
cout << bops[i].fullname << endl;
}
break;
case 'b':
case 'B':
for (int i = 0; i < 5; i++)
{
cout << bops[i].title << endl;
}
break;
case 'c':
case 'C':
for (int i = 0; i < 5; i++)
{
cout << bops[i].bopname << endl;
}
break;
case 'd':
case 'D':
for (int i = 0; i < 5; i++)
{
switch (bops[i].preference)
{
case 0:cout << bops[i].fullname << endl;
break;
case 1:cout << bops[i].title << endl;
break;
case 2:cout << bops[i].bopname << endl;
break;
default:
cout << "Unknown preference!\n";
break;
}
}
break;
default:
cout << "Unknown choice!\n";
break;
}
cout << "Next choice: ";
}
cout << "Bye!\n";
}
void showmenu()
{
cout << "Benevolent Order of Programmers Report\n"
"a. display by name \t\t b. display by title\n"
"c. display by bopname \t\t d. display by preference\n"
"q. quit\n"
"Enter your choice: ";
}
第5题
#include <iostream>
using namespace std;
int main()
{
double income;
double tax = 0;
cout << "Enter your income: ";
while (cin >> income && income >= 0)
{
if (income >= 0 && income <= 5000)
{
tax = 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 if (income >= 35001)
{
tax = 10000 * 0.1 + 20000 * 0.15 + (income - 35000) * 0.2;
}
cout << "Your tax = " << tax << endl;
cout << "Enter your income: ";
}
cout << "Bad input. Bye!\n";
}
第6题
#include <iostream>
#include <string>
using namespace std;
struct donor
{
string name;
double money=0;
};
int main()
{
int num = 0;
cout << "请输入捐献者数目:";
cin >> num;
cin.get();
while (num < 0)
{
cout << "输入不能小于0!\n"
<< "请重新输入:";
cin >> num;
cin.get();
}
if (num == 0)
{
cout << "没有捐赠者!\n";
}
else if (num > 0)
{
donor* donors = new donor[num];
for (int i = 0; i < num; i++)
{
cout << "请输入第" << i + 1 << "位捐赠者姓名:";
getline(cin, donors[i].name);
cout << "请输入" << donors[i].name << "捐款数:";
cin >> donors[i].money;
cin.get();
while (donors[i].money <= 0)
{
cout << "请输入大于0的数字!\n"
<< "请重新输入:";
cin >> donors[i].money;
cin.get();
}
}
cout << "*****Grand Patrons*****\n";
int GP_num = 0;
for (int i = 0; i < num; i++)
{
if (donors[i].money >= 10000)
{
cout << "姓名:" << donors[i].name << "\t金额:" << donors[i].money << endl;
GP_num++;
}
}
if (GP_num == 0)
{
cout << "none.\n";
}
cout << "*****Patrons*****\n";
int P_num = 0;
for (int i = 0; i < num; i++)
{
if (donors[i].money < 10000)
{
cout << "姓名:" << donors[i].name << "\t金额:" << donors[i].money << endl;
P_num++;
}
}
if (P_num == 0)
{
cout << "none.\n";
}
}
}
第7题
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
string word;
int v_count = 0;
int c_count = 0;
int o_count = 0;
cout << "Enter words (q to quit):\n";
while (cin >> word && word != "q")
{
if (isalpha(word[0]))
{
switch (word[0])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
v_count++;
break;
default:
c_count++;
break;
}
}
else
{
o_count++;
}
}
cout << v_count << " words beginning with vowels.\n"
<< c_count << " words beginning with consonants.\n"
<< o_count << " others.\n";
}
第8题
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
int count = 0;
char filename[20];
cout << "Input file name: ";
cin.getline(filename, 20);
ifstream infile;
infile.open(filename);
if (!infile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
infile.get(ch);
while (infile.good())
{
cout << ch;
if (ch != ' ' && ch != '\n')
{
count++;
}
infile.get(ch);
}
if (infile.eof())
{
cout << "\nEnd of file reached.\n";
}
else if (infile.fail())
{
cout << "Input terminated by data mismatch.\n";
}
else
{
cout << "Input terminated for unknown reason.\n";
}
if (count==0)
{
cout << "No data processed.\n";
}
else
{
cout << "Items read: " << count << endl;
}
infile.close();
}
第9题
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Patronage {
string name;
double money;
};
int main()
{
ifstream infile;
infile.open("d:\\a.txt");
if (!infile.is_open())
{
cout << "Could not open the file d:\a.txt.\n";
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
int count;
int GP_count = 0, P_count = 0;
infile >> count;
infile.get();
Patronage* patronages = new Patronage[count];
while (infile.good())
{
for (int i = 0; i < count; i++)
{
getline(infile, patronages[i].name);
infile >> patronages[i].money;
infile.get();
if (patronages[i].money >= 10000)
{
GP_count++;
}
else if (patronages[i].money > 0 && patronages[i].money < 10000)
{
P_count++;
}
}
}
if (infile.eof())
{
cout << "End of file reached.\n";
}
else if (infile.fail())
{
cout << "Input terminated by data mismatch.\n";
}
else
{
cout << "Input terminated for unknown reason.\n";
}
if (count <= 0)
{
cout << "No patron.\n";
}
else
{
cout << "***** Grand Patrons *****\n";
if (GP_count == 0)
{
cout << "none\n";
}
else
{
for (int i = 0; i < count; i++)
{
if (patronages[i].money >= 10000)
{
cout << "Name: " << patronages[i].name << "\t Patronages: " << patronages[i].money << endl;
}
}
}
cout << "***** Patrons *****\n";
if (P_count == 0)
{
cout << "none\n";
}
else
{
for (int i = 0; i < count; i++)
{
if (patronages[i].money > 0 && patronages[i].money < 10000)
{
cout << "Name: " << patronages[i].name << "\t Patronages: " << patronages[i].money << endl;
}
}
}
}
delete patronages;
infile.close();
}