//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;
//--------------------------------------------------------------------------------------------------------------;
//5.9 - 1.cpp
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum = 0;
cout << "Please enter the first integer: ";
cin >> num1;
cout << "Please enter the second integer: ";
cin >> num2;
for (int i = num1; i <= num2; i++)
{
sum += i;
}
cout << "Sum of " << num1 << " to " << num2 << " are " << sum << endl;
return 0;
}
//-------------
//5.9 - 2.cpp
#include <iostream>
#include <array>
using namespace std;
int main()
{
const int ArSize = 101;
array<long double, ArSize> factorials;
factorials[0] = factorials[1] = 1L;
for (int i = 2; i < ArSize; i++)
{
factorials[i] = i * factorials[i - 1];
}
for (int i = 0; i < ArSize; i++)
{
cout << i << "! = " << factorials[i] << endl;
}
return 0;
}
//-------------
//5.9 - 3.cpp
#include <iostream>
using namespace std;
int main()
{
long long num;
long long sum = 0LL;
while (cout << "Please enter an integer(0 to quit): ", cin >> num && num != 0)
{
//↑逗号运算符只取最后的结果作为判断条件;
sum += num;
cout << "Sum of all integers are " << sum << endl;
}
return 0;
}
//-------------
//5.9 - 4.cpp
#include <iostream>
using namespace std;
int main()
{
int n = 0;
double daphne_money = 100;
double cleo_money = 100;
while (cleo_money <= daphne_money)
{
cout << "Year " << ++n << ':' << endl;
daphne_money += 10;
cleo_money += cleo_money * 0.05;
cout << "Cleo's money = " << cleo_money;
cout << ", Daphne's money = " << daphne_money << endl;
}
cout << "After " << n << " years, ";
cout << "Cleo's money";
cout << " > Daphne's money." << endl;
return 0;
}
//-------------
//5.9 - 5.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int ArSize = 12;
const string months[ArSize] =
{
"January", "February","March",
"April", "May", "June", "July",
"August","September", "October",
"November", "December"
};
int sum = 0, sales_volume[ArSize];
for (int i = 0; i < ArSize; i++)
{
cout << "Please enter number of books sold (";
cout << months[i] << "): ";
cin >> sales_volume[i];
}
for (int i = 0; i < ArSize; i++)
{
sum += sales_volume[i];
}
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in a year." << endl;
return 0;
}
//-------------
//5.9 - 6.cpp
#include <iostream>
#include <string>
using namespace std;
const int NUM = 3;
const int ArSize = 12;
int show_result(int (*x)[ArSize], int n);
int main()
{
const string months[ArSize] =
{
"January", "February","March",
"April", "May", "June", "July",
"August","September", "October",
"November", "December"
};
int sum, total, sales_volume[NUM][ArSize];
for (int i = 0; i < NUM; i++)
{
cout << "Year " << i + 1 << ": " << endl;
for (int j = 0; j < ArSize; j++)
{
cout << "Please enter number of books sold (";
cout << months[j] << "): ";
cin >> sales_volume[i][j];
}
cout << endl;
}
sum = total = show_result(sales_volume, 0);
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the first year." << endl;
total += sum = show_result(sales_volume, 1);
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the second year." << endl;
total += sum = show_result(sales_volume, 2);
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the third year." << endl;
cout << "A total of " << total << " <<C++ For Fools>> books were sold in three years." << endl;
return 0;
}
int show_result(int (*x)[ArSize], int n)
{
int sum = 0;
for (int i = 0; i < ArSize; i++)
{
sum += x[n][i];
}
return sum;
}
//-------------
//5.9 - 7.cpp
#include <iostream>
#include <string>
using namespace std;
struct car
{
string producer;
int year_of_introducion;
};
int main()
{
int num;
cout << "How many cars do you wish to catalog? ";
(cin >> num).get();
car *many_cars = new car[num];
for (int i = 0; i < num; i++)
{
cout << "Car #" << i + 1 << ':' << endl;
cout << "Please enter the make: ";
getline(cin, many_cars[i].producer);
cout << "Please enter the year made: ";
(cin >> many_cars[i].year_of_introducion).get();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++)
{
cout << many_cars[i].year_of_introducion;
cout << ' ' << many_cars[i].producer << endl;
}
delete[] many_cars;
return 0;
}
//-------------
//5.9 - 8.cpp
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int ArSize = 256;
char str[ArSize];
unsigned int count = 0;
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> str, strcmp("done", str))
{
++count;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
//-------------
//5.9 - 9.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
unsigned int count = 0;
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> str, str != "done")
{
++count;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
//-------------
//5.9 - 10.cpp
#include <iostream>
using namespace std;
int main()
{
int row;
cout << "Enter number of rows: ";
cin >> row;
for (int i = 1; i <= row; i++)
{
for (int j = i; j <= row - 1; j++)
{
cout << ".";
}
for (int j = 1; j <= i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
//-------------
//6.11 - 1.cpp
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
cout << "Type, and I shall repeat(@ to quit)." << endl;
while (cin.get(ch) && ch != '@')
{
if (islower(ch))
{
ch = toupper(ch);
}
else if (isupper(ch))
{
ch = tolower(ch);
}
if (!isdigit(ch))
{
cout.put(ch);
}
}
cout << "\nPlease excuse the slight confusion." << endl;
return 0;
}
//-------------
//6.11 - 2.cpp
#include <iostream>
#include <array>
using namespace std;
int main()
{
int i = 0, j = 0;
unsigned int count = 0;
const int ArSize = 10;
array<double, ArSize> donations;
double total = 0.0, average = 0.0;
cout << "You may enter up to " << ArSize;
cout << " donation (q to terminate)." << endl;
cout << "donation #1: ";
while (i < ArSize && cin >> donations[i])
{
if (++i < ArSize)
{
cout << "donation #" << i + 1 << ": ";
}
}
for (j = 0; j < i; j++)
{
total += donations[j];
}
average = total / i;
for (j = 0; j < i; j++)
{
if (average < donations[j])
{
++count;
}
}
if (0 == i)
{
cout << "No donation!" << endl;
}
else
{
cout << average << " = average of ";
cout << i << " donations.\n";
cout << count << " numbers are greater than the average." << endl;
}
return 0;
}
//-------------
//6.11 - 3.cpp
#include <iostream>
using namespace std;
void show_menu();
int main()
{
char ch;
show_menu();
while (cin >> ch)
{
switch (ch)
{
case 'c':
{
cout << "Pandas are also carnivores." << endl;
break;
}
case 'p':
{
cout << "Mozart is an excellent pianist." << endl;
break;
}
case 't':
{
cout << "A maple is a tree." << endl;
break;
}
case 'g':
{
cout << "Playing game can relax yourself." << endl;
break;
}
default:
{
cout << "Please enter a c, p, t, or g: ";
break;
}
}
if ('c' == ch || 'p' == ch || 't' == ch || 'g' == ch)
{
break;
}
}
return 0;
}
void show_menu()
{
cout << "Please enter one of the following choices:" << endl;
cout << "c) carnivore p) pianist" << endl;
cout << "t) tree g) game" << endl;
}
//-------------
//6.11 - 4.cpp
#include <iostream>
using namespace std;
const int NUM = 5;
const int strsize = 20;
struct bop
{
char fullname[strsize];
char title[strsize];
char bopname[strsize];
int preference;
};
void show_menu();
int main()
{
char ch;
bop people[NUM] =
{
{"Wimp Macho", "Teacher", "WMA", 0},
{"Raki Rhodes", "Junior Programmer", "RHES", 1},
{"Celia Laiter", "Professor", "MIPS", 2},
{"Hoppy Hipman", "Analyst Trainee", "HPAN", 1},
{"Pat Hand", "Animal Keeper", "LOOPY", 2}
};
show_menu();
cout << "Enter your choice: ";
while (cin >> ch && ch != 'q')
{
switch (ch)
{
case 'a':
{
for (int i = 0; i < NUM; i++)
{
cout << people[i].fullname << endl;
}
break;
}
case 'b':
{
for (int i = 0; i < NUM; i++)
{
cout << people[i].title << endl;
}
break;
}
case 'c':
{
for (int i = 0; i < NUM; i++)
{
cout << people[i].bopname << endl;
}
break;
}
case 'd':
{
for (int i = 0; i < NUM; 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;
}
}
}
break;
}
default:
{
cout << "Illegal input!" << endl;
break;
}
}
cout << "Next choice: ";
}
cout << "Bye!" << endl;
return 0;
}
void show_menu()
{
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a. display by name b. display by title" << endl;
cout << "c. display by bopname d. display by preference" << endl;
cout << "q. quit" << endl;
}
//-------------
//6.11 - 5.cpp
#include <iostream>
using namespace std;
int main()
{
const double TVARPS_5000 = 0.0;
const double TVARPS_5000_15000 = 0.1;
const double TVARPS_15001_35000 = 0.15;
const double TVARPS_35000 = 0.2;
double wage, tax;
cout << "Please enter your wage (q or <0 to quit): ";
while (cin >> wage && wage > 0)
{
cout << "Your wage: " << wage << " tvarps.\n";
if (wage < 5000)
{
tax = 0.0;
}
else if (wage < 15000)
{
tax = (wage - 5000) * TVARPS_5000_15000;
}
else if (wage < 35000)
{
tax = (wage - 15000) * TVARPS_15001_35000 + 10000 * TVARPS_5000_15000;
}
else
{
tax = (wage - 35000) * TVARPS_35000 + 20000 * TVARPS_15001_35000 + 10000 * TVARPS_5000_15000;
}
cout << "Your tax: " << tax << " tvarps.\n";
cout << "Next wage (q or <0 to quit): ";
}
cout << "Bye." << endl;
return 0;
}
//-------------
//6.11 - 6.cpp
#include <iostream>
#include <string>
using namespace std;
const int HIGH_MONEY = 10000;
struct corporation
{
string name;
double money;
};
int main()
{
int i, num;
unsigned int patrons = 0;
unsigned int grand_patrons = 0;
cout << "Please enter the number of donators: ";
(cin >> num).get(); //吸收换行符;
corporation *people = new corporation[num];
for (i = 0; i < num; i++)
{
cout << "Please enter name #" << i + 1 << ": ";
getline(cin, people[i].name);
cout << "Please enter the amount of donation #" << i + 1 << ": ";
while (!(cin >> people[i].money)) //处理错误输入;
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Please enter a number: ";
}
cin.get(); //吸收正确输入时的换行符;
}
for (i = 0; i < num; i++)
{
HIGH_MONEY < people[i].money ? ++grand_patrons : ++patrons; //条件运算符代替条件语句;
}
cout << "\nGrand Patrons:" << endl;
if (grand_patrons != 0)
{
for (i = 0; i < num; i++)
{
if (people[i].money > HIGH_MONEY)
{
cout << "Name: " << people[i].name;
cout << "\nMoney: " << people[i].money << endl;
}
}
}
else
{
cout << "none" << endl;
}
cout << "\nPatrons:" << endl;
if (patrons != 0)
{
for (i = 0; i < num; i++)
{
if (people[i].money < HIGH_MONEY)
{
cout << "Name: " << people[i].name;
cout << "\nMoney: " << people[i].money << endl;
}
}
}
else
{
cout << "none" << endl;
}
delete[] people;
return 0;
}
//-------------
//6.11 - 7.cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string words;
unsigned int vowels = 0;
unsigned int consonants = 0;
unsigned int others = 0;
cout << "Enter words (q to quit):" << endl;
while (cin >> words, words != "q")
{
if (isalpha(words[0]))
{
switch (tolower(words[0]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
{
++vowels;
break;
}
default:
{
++consonants;
break;
}
}
}
else
{
++others;
}
}
cout << vowels << " words beginning with vowels" << endl;
cout << consonants << " words beginning with consonants" << endl;
cout << others << " others" << endl;
return 0;
}
//-------------
//6.11 - 8.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
char ch;
ifstream infile;
string filename;
unsigned int count = 0;
cout << "Please enter name of data file: ";
getline(cin, filename);
infile.open(filename);
if (!infile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating." << endl;
exit(EXIT_FAILURE);
}
while (infile.get(ch), infile.good())
{
++count;
cout.put(ch);
}
if (0 == count)
{
cout << "No data processed." << endl;
}
else
{
cout << count << " characters in the file " << filename << endl;
}
infile.close();
return 0;
}
//-------------
//6.11 - 9.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int HIGH_MONEY = 10000;
struct corporation
{
string name;
double money;
};
int main()
{
int i, num;
string filename;
ifstream infile;
unsigned int patrons = 0;
unsigned int grand_patrons = 0;
cout << "Please enter name of data file: ";
getline(cin, filename);
infile.open(filename);
if (!infile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating." << endl;
exit(EXIT_FAILURE);
}
(infile >> num).get();
corporation *people = new corporation[num];
for (i = 0; i < num && infile.good(); i++)
{
getline(infile, people[i].name);
while (!(infile >> people[i].money)) //处理错误输入;
{
infile.clear();
while (infile.get() != '\n')
continue;
}
while (infile.get() != '\n')
continue;
}
infile.close();
for (i = 0; i < num; i++)
{
HIGH_MONEY < people[i].money ? ++grand_patrons : ++patrons; //条件运算符代替条件语句;
}
cout << "\nGrand Patrons:" << endl;
if (grand_patrons != 0)
{
for (i = 0; i < num; i++)
{
if (people[i].money > HIGH_MONEY)
{
cout << "Name: " << people[i].name;
cout << "\nMoney: " << people[i].money << endl;
}
}
}
else
{
cout << "none" << endl;
}
cout << "\nPatrons:" << endl;
if (patrons != 0)
{
for (i = 0; i < num; i++)
{
if (people[i].money < HIGH_MONEY)
{
cout << "Name: " << people[i].name;
cout << "\nMoney: " << people[i].money << endl;
}
}
}
else
{
cout << "none" << endl;
}
delete[] people;
return 0;
}
//-------------
//------------------------------------------2023年1月1日 ----------------------------------------------;