6.1
//programming exercise 6.1
#include
#include
int main()
{
using namespace std;
char ch = 0;
while ((ch = cin.get()) != '@')
{
if (isdigit(ch))
continue;
else if (islower(ch))
cout << (char)toupper(ch);
else if (isupper(ch))
cout << (char)tolower(ch);
else
cout << ch;
}
cout << endl << "Done!" << endl;
cin.get();
cin.get();
return 0;
}
6.2
//programming exercise 6.2
#include
#include
int main()
{
using namespace std;
double donation[10];
int num = 0;
double total = 0.0;
double ave = 0.1;
int large_ave = 0;
cout << "Enter up to ten double value, Non-digital to exit: " << endl;
while (num < 10 && (cin >> donation[num]))
{
total += donation[num];
num++;
}
ave = total / num;
for (int i = 0; i < num; ++i)
{
if (donation[i] > ave)
large_ave++;
}
cout << "The average value is " << ave << "," << endl
<< "and there are " << large_ave
<< " values in the array are larger than the agerage."
<< endl;
system("pause");
return 0;
}
6.3
//programming exercise 6.3
#include
#include
int main()
{
using namespace std;
cout << "Please enter one of the following choices: " << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) gane" << endl;
char users_input;
bool exit = false;
while (!exit && cin >> users_input)
{
switch (users_input)
{
case 'c': cout << "A maple is a carnivore.\n"; exit = true; break;
case 'p': cout << "A maple is a pianist.\n"; exit = true; break;
case 't': cout << "A maple is a tree.\n"; exit = true; break;
case 'g': cout << "A maple is a gane.\n"; exit = true; break;
default: cout << "Please enter a c, p, t, or g: "; break;
}
}
system("pause");
return 0;
}
6.4
//programming exercise 6.4
#include
#include
const int strsize = 50;
using namespace std;
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
};
void display_by_name(bop *bopArray, int size)
{
for (int i = 0; i < size; i++)
cout << bopArray[i].fullname << endl;
}
void display_by_title(bop *bopArray, int size)
{
for (int i = 0; i < size; i++)
cout << bopArray[i].title << endl;
}
void display_by_bopname(bop *bopArray, int size)
{
for (int i = 0; i < size; i++)
cout << bopArray[i].bopname << endl;
}
void display_by_preference(bop *bopArray, int size)
{
for (int i = 0; i < size; i++)
{
if (bopArray[i].preference == 0)
cout << bopArray[i].fullname << endl;
else if (bopArray[i].preference == 1)
cout << bopArray[i].title << endl;
else if (bopArray[i].preference == 2)
cout << bopArray[i].bopname << endl;
}
}
int main()
{
bop mybop[5] =
{
{"Wimp Macho","AAAAAA","aaa",0},
{"Raki Rhodes", "Junior Programmer","bbb",1},
{"Celia Laiter", "CCCCCC","MIPS",2},
{"Hoppy Hipman", "Analyst Trainee","ddd",1},
{"Pat Hand","EEEEEE","LOOPY",2}
};
cout << "Benevolent Order of Programmers 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";
char users_ch;
cout << "Enter your choice: ";
cin >> users_ch;
while (cin)
{
if (users_ch == 'q')
break;
switch (users_ch)
{
case 'a': display_by_name(mybop, 5); break;
case 'b': display_by_title(mybop, 5); break;
case 'c': display_by_bopname(mybop, 5); break;
case 'd': display_by_preference(mybop, 5); break;
default: cout << "Error!" << endl;
}
cout << "Next choice: " << endl;
cin >> users_ch;
}
system("pause");
return 0;
}
6.5
//programming exercise 6.5
#include
#include
const double RATE1 = 0.1;
const double RATE2 = 0.15;
const double RATE3 = 0.2;
int main()
{
using namespace std;
double income = 0.0;
double tax = 0.0;
cout << "Enter your income: " << endl;
while (cin >> income && income >= 0.0)
{
if (income <= 5000)
tax = 0.0;
else if (income > 5000 && income <= 15000)
tax = (income - 5000) * RATE1;
else if (income >15000 && income <= 35000)
tax = 10000 * RATE1 + (income - 15000) * RATE2;
else
tax = 10000 * RATE1 + 20000 * RATE2 + (income - 35000) * RATE3;
cout << "income = " << income << ", tax = " << tax << endl;
}
system("pause");
return 0;
}
6.6
//programming exercise 6.6
#include
#include
#include
using namespace std;
struct list
{
string name;
double amount;
};
int main()
{
cout << "Enter the number off contributors: ";
int num = 0;
cin >> num;
cin.get();
list * mylist = new list[num];
for (int i = 0; i < num; i++)
{
cout << "Enter the name of " << i + 1 << " contributor: ";
getline(cin, mylist[i].name);
cout << "Enter the amount of donation: ";
cin >> mylist[i].amount;
cin.get();
}
cout << "Grand Pators:" << endl;
int tmp = 0;
for (int i = 0; i < num; i++)
{
if (mylist[i].amount > 10000)
{
cout << "Contributor name: " << mylist[i].name << endl;
cout << "Contrubutor amount: " << mylist[i].amount << endl;
tmp++;
}
}
if (tmp == 0)
cout << "none" << endl;
tmp = 0;
cout << "Pators:" << endl;
for (int i = 0; i < num; i++)
{
if (mylist[i].amount < 10000)
{
cout << "Contributor name: " << mylist[i].name << endl;
cout << "Contrubutor amount: " << mylist[i].amount << endl;
tmp++;
}
}
if (tmp == 0)
cout << "none" << endl;
system("pause");
return 0;
}
6.7
//programming exercise 6.7
#include
#include
#include
int main()
{
using namespace std;
int vowels = 0;
int consonants = 0;
int others = 0;
cout << "Enter words (q to quit):\n";
string mychar;
while (cin >> mychar)
{
if (mychar.length() == 1 && mychar[0] == 'q')
break;
if (isalpha(mychar[0]))
{
if (mychar[0] == 'a' || mychar[0] == 'e' || mychar[0] == 'i' || mychar[0] == 'o' || mychar[0] == 'u' || mychar[0] == 'A' || mychar[0] == 'E' || mychar[0] == 'I' || mychar[0] == 'O' || mychar[0] == 'U')
vowels++;
else
consonants++;
}
else
others++;
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
system("pause");
return 0;
}
6.8
//programming exercise 6.8
#include
#include
#include
const int SIZE = 60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout << "Enter name of file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file!" << endl;
//exit(EXIT_FAILURE);
}
else
{
int num = 0;
char ch = 0;
while ((ch = inFile.get()) != EOF)
num++;
cout << "There are " << num << " characters in " << filename << " file." << endl;
}
system("pause");
return 0;
}
6.9
//programming exercise 6.9
#include
#include
#include
#include
using namespace std;
const int SIZE = 50;
struct list
{
string name;
double amount;
};
int main()
{
char filename[SIZE];
ifstream inFile;
cout << "Enter name of file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file!" << endl;
//exit(EXIT_FAILURE);
}
else
{
int num = 0;
inFile >> num;
inFile.get();
list * mylist = new list[num];
for (int i = 0; i < num; i++)
{
getline(inFile, mylist[i].name);
inFile >> mylist[i].amount;
inFile.get();
}
cout << "Grand Pators:" << endl;
int tmp = 0;
for (int i = 0; i < num; i++)
{
if (mylist[i].amount > 10000)
{
cout << "Contributor name: " << mylist[i].name << endl;
cout << "Contrubutor amount: " << mylist[i].amount << endl;
tmp++;
}
}
if (tmp == 0)
cout << "none" << endl;
tmp = 0;
cout << "Pators:" << endl;
for (int i = 0; i < num; i++)
{
if (mylist[i].amount < 10000)
{
cout << "Contributor name: " << mylist[i].name << endl;
cout << "Contrubutor amount: " << mylist[i].amount << endl;
tmp++;
}
}
if (tmp == 0)
cout << "none" << endl;
}
system("pause");
return 0;
}