C + + Primer pluse_6_ post-lesson questions

Source: Internet
Author: User

#include <iostream> #include <cctype> #include <array> #include <string> #include <cstring > #include <fstream> #include <cstdlib>using namespace Std;int t6_1 () {char ch;cout << "Enter text for Analysis, and the type # to terminate input.\n "; cin.get (ch); while (ch! = ' @ ') {if (IsDigit (ch)) {cin.get (ch); continue;} else if (isupper (ch)) {ch = tolower (ch);} else if (islower (ch)) {ch = toupper (ch);} cout << ch;cin.get (CH);} cout << endl;system ("pause"); return 0;} const int MaxSize = 10;int T6_2 () {array<double, maxsize>donations = {0};cout << "input the donations:\n"; cou T << "donation#1:"; int i = 0;while (i <maxsize && cin >> Donations[i]) {if (++i <maxsize) {cout &L t;< "donation#" << i + 1<< ":";}} Double total = 0;double average = 0;for (int j = 0; J < i; J + +) {total + = Donations[j];} if (i==0) {cout << "No donations.\n";} Else{average = Total/i;cout << "Average is" << average << Endl;cout << "bigger than average:"; for (int j = 0; J < I; J + +) {if (Donations[j] > average) {cout << donations[j] << "";}} System ("pause"); return 0;} int T6_3 () {char ch;cout << "Please enter aone of the following choices:\n"; cout << "c) carnivore\t\t\t p) pian  Ist\n "; cout <<" T) tree\t\t\t\t g) game\n "; cin.get (CH). get (); while (true) {switch (ch) {case ' C ': cout <<" A map is a carnivore.\n "; Break;case ' P ': cout << "A map is a pianist.\n"; Break;case ' t ': cout << "A map is a tree.\n"; Break;case ' G ': cout << "A map is a game.\n"; Break;default:cout << "Please enter a C, p, T, G:"; break;} Cin.get (CH). get ();} System ("pause"); return 0;} const int strsize = 20;typedef struct Bop{char fullname[strsize];char title[strsize];char bopname[strsize];int Preference;} Bop;int T6_4 () {Bop B1 = {"Bopp1", "Title1", "Bop1", 1};bop b2 = {"Bopp2", "Title2", "Bop2", 0};bop B3 = {"Bopp3", "Ti Tle3 "," BOP3 ", 2};cout <<" BOP repoRt.\n "; cout <<" A.display by name.\t\t\t B.display by title.\n "; cout <<" C.display by bopname.\t\t D.display b Y preference.\n "; cout <<" q.quit.\n "; cout <<" Enter Your choice: "; char ch;cin.get (CH). get (); while (ch! = ' Q ') {switch (ch) {case ' a ': cout << b1.fullname << endl;cout << b2.fullname << endl;cout << b3.ful  LName << endl;break;case ' B ': cout << b1.title << endl;cout << b2.title << endl;cout << B3.title << endl;break;case ' C ': cout << b1.bopname << endl;cout << b2.bopname << endl;cout << b3.bopname << endl;break;case ' d ': if (b1.preference== 0) {cout << b1.fullname << Endl;} else if (b1.preference ==1) {cout << b1.title << Endl;} Else{cout << b1.bopname << Endl;} if (b2.preference = = 0) {cout << b2.fullname << Endl;} else if (b2.preference = = 1) {cout << b2.title << Endl;} Else{cout << B2.bopname <&Lt Endl;} if (b3.preference = = 0) {cout << b3.fullname << Endl;} else if (b3.preference = = 1) {cout << b3.title << Endl;} Else{cout << b3.bopname << Endl;} Break;default:cout << "Please input a A, B, C, D, Q:";} Cin.get (CH). get ();} cout << "bye!"; System ("pause"); return 0;} int T6_5 () {Double wage = 0;double tax = 0;while (cin>>wage && wage >=0) {if (wage <=) {tax = 0;} else if (wage > wage && <=) {tax = 0 + (wage-5000) *0.1;} else if (Wage > && wage <= 3500) {tax = 0 + 10000 * 0.1 + (wage-15000) *0.15;} Else{tax = 0 + 10000 * 0.1 + 20000 * 0.15 + (wage-35000) *0.2;} cout << ' tax = ' << tax << Endl;} System ("pause"); return 0;} typedef struct DONOR{STRING name;double money;} Donor;int t6_6 () {int donors = 0;cout << "Enter the number of donors.\n"; Cin >> Donors;cin.get ();d Onor *donorse t = new Donor[donors];for (int i = 0; i < donors; i++){cout << "Enter donor#" << i + 1 << ": \ n"; getline (cin, donorset[i].name); Cin >> Donorset[i].money; Cin.get ();}  cout << "\ngrand patrons:\n"; int flag = 0;for (int i = 0; i < donors; i++) {if (Donorset[i].money > 10000) {cout << donorset[i].name << endl;flag = 1;}} if (flag = = 0) {cout << "none.\n";} cout << "patrons:\n"; flag = 0;for (int i = 0; i < donors; i++) {if (Donorset[i].money <= 10000) {cout << D Onorset[i].name << endl;flag = 1;}} if (flag = = 0) {cout << "none.\n";} System ("pause"); return 0;}  int t6_7 () {int vowelcount = 0;int Consonantscount = 0;char ch;cout << "Enter words (Q to quit): \ n"; cin.get (ch); while (ch! = ' Q ') {if (Isalpha (CH)) {if (ToLower (ch) = = ' A ' | | tolower (ch) = = ' E ' | | tolower (ch) = = ' I ' | | tolower (ch) = = ' O ' | | tolower (CH) = = ' U ') {vowelcount++;} Elseconsonantscount++;cin.get (CH); while (ch! = ' && ch! = ' \ n ') {cin.get (ch);}} Elsecin.get (CH);} cout << vowelcount << "wordsBeginning with vowels.\n ", cout << consonantscount <<" Words beginning with consonants.\n ";//system (" Pause ") ; return 0;} const int SIZE = 60;int Test6_8 () {char filename[size];ifstream infile;cout << "Enter The name of the data file:"; cin . Getline (Filename,size); Infile.open (filename); if (!infile.is_open ())//failed to open file{cout << ' Could not Open the file.\n "; cout <<" program terminating.\n "; exit (exit_failure);} Char ch;int count = 0;infile >> ch;while (Infile.good ()) {count++;infile >> ch;cout << ch;} if (infile.eof ()) {cout << "End of the File.\n";} else if (Infile.fail ()) {cout << "Input terminated by Data mismatch.\n";} Elsecout << "Input terminated for unknown reason.\n", Infile.close () cout << "file has" << count <&lt ; "characters.\n"; return 0;} int Test6_9 () {char filename[size];ifstream infile;cout << "Enter the file name:"; cin.getline (filename, SIZE); Infile.open (filename); if (!infile.is_open ()) {cout << "File cannot be opened.\n", cout << "program terminating.\n"; exit (exit_failure);} int donors = 0;cout << "Enter the number of donors.\n"; InFile >> donors;infile.get ();d onor *donorset = new Don or[donors];for (int i = 0; i < donors; i++) {cout << "Enter donor#" << i + 1 << ": \ n"; Getline (InFile, Donorset[i].name); InFile >> donorset[i].money;infile.get ();} if (infile.eof ()) {cout << "file end.\n";} else if (Infile.fail ()) {cout << "Input file terminated by mismatch.\n";} Elsecout << "Input file terminated by unknown reasons.\n"; Infile.close () cout << "\ngrand patrons:\n"; int fla g = 0;for (int i = 0; i < donors; i++) {if (Donorset[i].money > 10000) {cout << donorset[i].name << endl;f lag = 1;}} if (flag = = 0) {cout << "none.\n";} cout << "patrons:\n"; flag = 0;for (int i = 0; i < donors; i++) {if (Donorset[i].money <= 10000) {cout << D Onorset[i].name << endl;flag = 1;}} if (flag = = 0) {cout << "none.\n";} return 0;} int main () {test6_9 (); System ("pause"); return 0;}

  

C + + Primer pluse_6_ post-lesson questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.