第六章 编程练习答案
1.
#include<iostream>
#include<cctype>
int main()
{
using namespace std;
cout << "Enter text, and type @ to terminate input.\n";
char ch;
cin.get(ch);
while (ch != '@')
{
if(isdigit(ch));
else if(isalpha(ch))
{
if(isupper(ch))
{
ch = tolower(ch);
cout << ch;
}
else{
ch = toupper(ch);
cout << ch;
}
}
else
cout << ch;
cin.get(ch);
}
cout << "Done!" <<endl;
return 0;
}
2.
#include<iostream>
const int Max = 10;
int main()
{
using namespace std;
double donation[Max];
cout << "Please enter the donation.\n";
cout << "You may enter up to " << Max
<< " donation.\n";
cout << "donation #1: ";
int i = 0;
while (i < Max && cin >> donation[i])
{
if (++i < Max)
cout << "donation #" << i+1 << ": ";
}
double total = 0.0;
double average;
int j, n = 0;
for (j = 0; j < i; j++)
total += donation[j];
average = total / i;
for (j = 0; j < i; j++){
if(donation[j] > average)
n++;
}
if (i == 0)
cout << "No donation.\n";
else{
if(i == 1){
cout << "average = " << average;
cout << "No donations bigger than average.\n";
}
else{
cout << "Average of " << i << " donations = " << average << endl;
cout << n << " donations bigger than average." << endl;
}
}
cout << "Done.\n";
return 0;
}
3.
#include<iostream>
using namespace std;
void showmenu();
int main()
{
showmenu();
char choice;
do
{
cin >> choice;
switch(choice)
{
case 'c': cout << "If you describe someone as a carnvore, you are saying,\n";
cout << "especially in a humorous way, that they eat meat.\n";
break;
case 'p': cout << "I want to be a pinoest.\n";
break;
case 't': cout << "A maple is a tree.\n";
break;
case 'g': cout << "I like playing computer game.\n";
break;
default : cout << "Please enter a c, p, t, or g: ";
}
}while ((choice !='c')&&(choice !='p')&&(choice !='t')&&(choice !='g')) ;
return 0;
}
void showmenu()
{
cout << "Please enter one of the following choices:\n"
"c) carnivore p) pianist\n"
"t) tree g) game\n";
}
4.
#include<iostream>
using namespace std;
const int strsize = 20;
const int number = 5;
void showmenu();
void diplay_fullname();
void diplay_title();
void diplay_bopname();
void diplay_perference();
struct bop{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int perference;
};
bop people[number] = {
{
"Wimp Macho",
"BOSS",
"WM",
0
},
{
"Raki Rhodes",
"Manager",
"Junior Programmer",
2
},
{
"Celia Laiter",
"MIPS",
"CL",
1
},
{
"Hoppy Hipman",
"Analyst Trainee",
"AT",
1
},
{
"Pat Hand",
"Student",
"LOOPY",
2
}
};
int main()
{
showmenu();
char choice;
cout << "Enter your choice: ";
do
{
cin >> choice;
switch(choice)
{
case 'a': diplay_fullname();
break;
case 'b': diplay_title();
break;
case 'c': diplay_bopname();
break;
case 'd': diplay_perference();
break;
}
if(choice != 'q')
cout << "Next choice: ";
}while (choice !='q');
cout << "Bye!" << endl;
return 0;
}
void showmenu()
{
cout << "Benevolent Order of Programmers report\n"
"a. display by name b. display by title\n"
"c. display by bopname d. display by preference\n"
"q. quit\n";
}
void diplay_fullname()
{
int i;
for(i = 0 ; i < number ; i++)
cout << people[i].fullname << endl;
}
void diplay_title()
{
int i;
for(i = 0 ; i < number ; i++)
cout << people[i].title << endl;
}
void diplay_bopname()
{
int i;
for(i = 0 ; i < number ; i++)
cout << people[i].bopname << endl;
}
void diplay_perference()
{
int i;
for(i = 0 ; i < number ; i++)
{
if (people[i].perference == 0)
cout << people[i].fullname << endl;
if (people[i].perference == 1)
cout << people[i].title << endl;
if (people[i].perference == 2)
cout << people[i].bopname << endl;
}
}
5.
#include<iostream>
using namespace std;
const int tax1 = 5000;
const double interest1 = 0.1;
const int tax2 = 15000;
const double interest2 = 0.15;
const int tax3 = 35000;
const double interest3 = 0.2;
double tax(int);
int main()
{
int n;
double money;
do{
cout << "Enter your income: ";
cin >> n;
money=tax(n);
if (n > 0)
cout << "The tax is: " << money << " tvarps" <<endl;
}while (n > 0);
return 0;
}
double tax(int n)
{
double index;
if (n <= tax1)
index = n*0;
else if(n> tax1 && n<=tax2)
index = (n-tax1)*interest1;
else if(n> tax2 && n<=tax3)
index = (n - tax2)*interest2+(tax2 - tax1)*interest1;
else
index = (n-tax3)*interest3+(tax3 - tax2)*interest2+(tax2 - tax1)*interest1 ;
return index;
}
6.
#include<iostream>
using namespace std;
const int namesize = 20;
const double stand = 10000.0;
struct patron{
char name[namesize];
double money;
};
int main()
{
int count;
cout << "Enter the number of patrons:" << endl;
cin >> count;
cin.get();
patron * peoplelist = new patron[count];
cout << "Enter the patrons:" << endl;
for(int i = 0; i < count ;i++)
{
cout << "Patrons name:" << endl;
cin.getline(peoplelist[i].name, namesize);
cout << "donation:" << endl;
cin >> peoplelist[i].money;
cin.get();
}
cout << "\n******************" << endl;
cout << "\nGrand Patrons: " << endl;
int n = 0;
for(int i = 0; i < count ;i++){
if(peoplelist[i].money > stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
n++;
}
}
if(n == 0)
cout << "none";
int m = 0;
cout << "\nPatrons: " << endl;
for(int i = 0; i < count ;i++){
if(peoplelist[i].money <= stand){
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
m++;
}
}
if(m == 0)
cout << "none";
delete peoplelist;
return 0;
}
7.
#include<iostream>
#include<cctype>
#include<string>
int main()
{
using namespace std;
cout << "Enter words (q to quit): \n";
string ch;
int vowel = 0;
int consonant = 0;
int others = 0;
cin >> ch;
while (ch != "q")
{
if(isalpha(ch[0]))
{
switch(ch[0])
{
case 'a':
case 'A':vowel++;break;
case 'e':
case 'E':vowel++;break;
case 'i':
case 'I':vowel++;break;
case 'o':
case 'O':vowel++;break;
case 'u':
case 'U':vowel++;break;
default :consonant++;
}
}
else
others++;
cin >> ch;
}
cout << vowel << " words beginning with vowels\n" << consonant
<< " words beginning with consonants\n" << others << " others\n";
return 0;
}
8.
#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
char value;
int count = 0;
inFile >> value;
while (inFile.good())
{
++count;
inFile >> value;
}
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 data processed.\n";
else
{
cout << "chars: " << count << endl;
}
inFile.close();
return 0;
}
9.
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
const int SIZE = 60;
const double stand = 10000.0;
struct patron{
string name;
double money;
};
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
int count;
inFile >> count;
inFile.get();
patron * peoplelist = new patron[count];
for(int i = 0; i < count ;i++)
{
getline(inFile, peoplelist[i].name);
inFile >> peoplelist[i].money;
inFile.get();
}
cout << "\nGrand Patrons: " << endl;
int n = 0;
for(int i = 0; i < count ;i++)
{
if(peoplelist[i].money > stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
n++;
}
}
if(n == 0)
cout << "none";
int m = 0;
cout << "\nPatrons: " << endl;
for(int i = 0; i < count ;i++)
{
if(peoplelist[i].money <= stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
m++;
}
}
if(m == 0)
cout << "none";
delete peoplelist;
inFile.close();
return 0;
}