/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第一题
/********************************************************************************************************************************************************/
#include<iostream>
using namespace std;
int main(void)
{
char ch;
while(cin.get(ch))
{
if (ch == '@')
break;
if (isdigit(ch))
continue;
if (isupper(ch))
ch = tolower(ch);
if (islower(ch))
ch = toupper(ch);
cout << ch;
}
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第二题
/********************************************************************************************************************************************************/
#include<iostream>
using namespace std;
int main(void)
{
double data[10] = { 0 };
int count = 0;
int count_large = 0;
double average = 0;
for (count = 0; count < 10; ++count)
{
cin >> data[count];
if (cin.fail())
break;
else
average = average + data[count];
}
average = average / count;
for (int i = 0; i < count; ++i)
{
if (data[i] > average)
count_large++;
}
cout << "The average is " << average << " and there are " << count_large
<< " numbers larger than average.\n";
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第三题
/********************************************************************************************************************************************************/
#include<iostream>
using namespace std;
int main(void)
{
char choice = ' ';
cout << "Please enter one of the following choices:" << endl
<< "c) carnivore p) pianist" << endl
<< "t) tree g) game" << endl;
while (choice != 'q')
{
cin >> choice;
switch (choice)
{
case 'c': cout << "A maple is a tree.\n"; break;
case 'p': cout << "Me is a pianist.\n"; break;
case 't': cout << "You is a tree.\n"; break;
case 'g': cout << "This is a game.\n"; break;
case 'q': break;
default: cout << "Please enter a c, p, t, or g:\n";
}
}
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第四题
/********************************************************************************************************************************************************/
#include<iostream>
using namespace std;
const int strsize = 20;
const int bopsize = 5;
struct bop
{
char fullname[strsize]; //real name
char title[strsize]; //job title
char bopname[strsize]; // secret BOP name
int preference; //0 = fullname, 1 = title, 2 = bopname
};
bop people[bopsize] = { { "Wimp Macho", "programer", "WM", 0 }, { "Raki Rhodes", "programer", "RR", 1 },
{ "Celia Laiter", "manager", "CL", 2 }, { "Hoppy Hipman", "boss", "HH", 0 }, { "Pat Hand", "programer", "PH", 0 } };
void display_by_name();
void display_by_title();
void display_by_bopname();
void dispaly_by_preference();
int main(void)
{
cout << "Benevolent order of programers report\n";
cout << "a. display by name b. display by title\n"
<< "c. display by bopname d. display by preference\n"
<< "q. quit\n";
char choice = 0;
cout << "Enter your choice: ";
cin >> choice;
while (choice != 'q')
{
switch (choice)
{
case 'a': display_by_name(); break;
case 'b': display_by_title(); break;
case 'c': display_by_bopname(); break;
case 'd': dispaly_by_preference(); break;
default: cout << "error input.\n"; break;
}
cout << "Enter your choice: ";
cin >> choice;
}
return 0;
}
void display_by_name()
{
for (int i = 0; i < bopsize; ++i)
cout << people[i].fullname << endl;
}
void display_by_title()
{
for (int i = 0; i < bopsize; ++i)
cout << people[i].title << endl;
}
void display_by_bopname()
{
for (int i = 0; i < bopsize; ++i)
cout << people[i].bopname << endl;
}
void dispaly_by_preference()
{
for (int i = 0; i < bopsize; ++i)
{
switch (people[i].preference)
{
case 0: cout << people[i].fullname << endl; break;
case 1: cout << people[i].title << endl; break;
case 2: cout << people[i].bopname << endl; break;
}
}
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第五题
/********************************************************************************************************************************************************/
#include<iostream>
using namespace std;
int main(void)
{
double incomes = 0;
double tax = 0;
cout << "Enter your incomes:\n";
cin >> incomes;
if (incomes <= 5000)
tax = 0;
else if (incomes > 5000 && incomes <= 15000)
tax = (incomes - 5000) * 0.1 ;
else if (incomes > 15000 && incomes <= 35000)
tax = (incomes - 15000) * 0.15 + 10000 * 0.1;
else
tax = 10000 * 0.1 + 20000 * 0.15 + (incomes - 35000) * 0.2;
cout << tax;
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第六题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;
struct donation
{
string fullname;
double fund;
};
int main(void)
{
int patrons = 0;
int count = 0;
cout << "Enter the number of patrons:\n";
cin >> patrons;
donation * record_donation = new donation[patrons];
cout << "Please enter the full name of every patrons:\n";
for (int i = 0; i < patrons; ++i)
{
getline(cin, (*(record_donation + i)).fullname, ':');
(cin >> (*(record_donation+i)).fund).get();
}
cout << "The grand patrons are following:\n"; //显示重要捐款人
for (int i = 0; i < patrons; ++i)
{
if ((*(record_donation + i)).fund >= 10000)
{
cout << (*(record_donation + i)).fullname << " : " << (*(record_donation + i)).fund << endl;
++count;
}
}
if (count == 0) cout << "none\n";
count = 0;
cout << "The patrons are following:\n"; //显示其他捐款人
for (int i = 0; i < patrons; ++i)
{
if ((*(record_donation + i)).fund < 10000)
{
cout << (*(record_donation + i)).fullname << " : " << (*(record_donation + i)).fund << endl;
++count;
}
}
if (count == 0) cout << "none\n";
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第七题
/********************************************************************************************************************************************************/
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
cout << "Enter words (q to quit):\n";
string temp;
int number_of_vowels = 0;
int number_of_consonants = 0;
int number_of_others = 0;
cin >> temp;
while (temp != "q")
{
if (isalpha(temp[0]))
{
if (temp[0] == 'a' || temp[0] == 'e' || temp[0] == 'i' || temp[0] == 'o' || temp[0] == 'u')
++number_of_vowels;
else
++number_of_consonants;
}
else
++number_of_others;
cin >> temp;
}
cout << number_of_vowels << " words beginning with vowels" << endl
<< number_of_consonants << " words beginning with consonants" << endl
<< number_of_others << " others" << endl;
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第八题
/********************************************************************************************************************************************************/
#include<iostream>
#include<fstream>
using namespace std;
int main(void)
{
ifstream fin;
fin.open("text.txt");
if (!fin.is_open())
cout << "can not open the file\n";
long double count = 0;
char ch;
fin >> ch;
while (fin.good())
{
++count;
fin >> ch;
}
cout << count << " chars in this file.\n";
fin.close();
return 0;
}
/********************************************************************************************************************************************************/
C++ primer plus(第六版)第六章 第九题
/********************************************************************************************************************************************************/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct donation
{
string fullname;
double fund;
};
int main(void)
{
ifstream fin;
fin.open("donation.txt");
if (!fin.is_open())
cout << "Can not open this file.\n";
int count = 0;
string temp;
fin >> count;
getline(fin, temp); //读取数字后面的空行
donation * record_donation = new donation[count];
for (int i = 0; i < count; ++i)
{
getline(fin, (*(record_donation + i)).fullname);
fin >> (*(record_donation + i)).fund;
getline(fin, temp);
}
cout << "The grand patrons are following:\n";
for (int i = 0; i < count; ++i)
{
if ((*(record_donation + i)).fund >= 10000)
cout << (*(record_donation + i)).fullname << " : " << (*(record_donation + i)).fund << endl;
}
cout << "others patrons are following:\n";
for (int i = 0; i < count; ++i)
{
if ((*(record_donation + i)).fund < 10000)
cout << (*(record_donation + i)).fullname << " : " << (*(record_donation + i)).fund << endl;
}
return 0;
}