C++primer Plus课本代码(第6章)
生活随笔
收集整理的這篇文章主要介紹了
C++primer Plus课本代码(第6章)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第6章 分支語句和邏輯運算符
- 6.1 if.cpp
- 6.2 ifelse.cpp
- 6.3 ifelseif.cpp
- 6.4 or.cpp
- 6.5 and.cpp
- 6.6 more_and.cpp
- 6.7 not.cpp
- 6.8 cctypes.cpp
- 6.9 condit.cpp
- 6.10 switch.cpp
- 6.11 enum.cpp
- 6.12 jump.cpp
- 6.13 cinfish.cpp
- 6.14 cingolf.cpp
- 6.15 outfile.cpp
- 6.16 sumafile.cpp
6.1 if.cpp
#include<iostream>int main() {using std::cin;using std::cout;char ch;int spaces = 0;int total = 0;cin.get(ch);while (ch != '.'){if (ch == ' '){++spaces;}++total;cin.get(ch);}cout << spaces << " spaces, " << total;cout << " characters total in sentence\n";return 0; }6.2 ifelse.cpp
#include<iostream>int main() {char ch;std::cout<< "Type , and I shall repeat.\n";std::cin.get(ch);while (ch != '.'){if (ch == '\n'){std::cout << ch;}elsestd::cout << ++ch;std::cin.get(ch);}std::cout << "\n Please excuse the slight confusion.\n";return 0; }6.3 ifelseif.cpp
#include<iostream> const int Fave = 27; int main() {using namespace std;int n;cout << "Enter a number in the range 1-100 to find ";cout << "my favorite number:";do{cin >> n;if (n < Fave)cout << "Too low -- guess again:";else if (n > Fave)cout << "Too high -- guess again:";elsecout << Fave << " is right!\n";} while (n != Fave);return 0; }6.4 or.cpp
#include<iostream> int main() {using namespace std;cout << "This program may reformat your hard disk\n""and destory all your data.\n""Do you wish to continue?<y/n>";char ch;cin >> ch;if (ch == 'y' || ch == 'Y')cout << "You were warned!\a\a\n";else if (ch == 'n' || ch == 'N')cout << "A wise choic ...bye\n";elsecout << "That wasn't a y or n!Apparently you ""can't follow\ninstructions,so ""I'll trash your disk anyway.\a\a\a\n";return 0; }6.5 and.cpp
#include<iostream> const int ArSize = 6;int main() {using namespace std;float naaq[ArSize];cout << "Enter the NAAQs (New Age Awareness Quotients) "<< "of \nyour neighbors. Program terminatesc "<< " when you make\n" << ArSize << " entries "<< " or enter a negative value.\n";int i = 0;float temp;cout << "First valude: ";cin >> temp;while (i < ArSize && temp >= 0){naaq[i] = temp;++i;if (i < ArSize){cout << "Next value: ";cin >> temp;}}if (i == 0){cout << "No data--bye\n";}else{cout << "Enter your NAAQ: ";float you;cin >> you;int count = 0;for (int j = 0; j < i;j++){if (naaq[j] > you){++count;}}cout << count;cout << " of your neighbors have greater awareness of\n"<< "the New Age than you do.\n";}return 0; }6.6 more_and.cpp
#include<iostream> const char* qualify[4] = {"10,000-meter race.\n","mud tug-of-war.\n","masters canoe jousting.\n","pie-throwing festival.\n" }; int main() {using namespace std;int age;cout << "Enter your age in years:";cin >> age;int index;if (age > 17 && age < 35)index = 0;else if (age >= 35 && age < 50)index = 1;else if (age >= 50 && age < 65)index = 2;elseindex = 3;cout << "You qualify for the " << qualify[index];return 0; }6.7 not.cpp
#include<iostream> #include<climits> bool is_int(double); int main() {using namespace std;double num;cout << "Yo,dude!Enter an integer value:";cin >> num;while (!is_int(num)){cout << "Out of range -- please try again:";cin >> num;}int val = int(num);cout << "You've entered the integer " << val << "\nBye\n";return 0; }bool is_int(double x) {if (x <= INT_MAX && x >= INT_MIN)return true;elsereturn false; }6.8 cctypes.cpp
#include<iostream> #include<cctype> int main() {using namespace std;cout << "Enter text for analysis,and type @"" to terminate input.\n";char ch;int whitespace = 0;int digits = 0;int chars = 0;int punct = 0;int others = 0;cin.get(ch);while (ch != '@'){if (isalpha(ch))chars++;else if (isspace(ch))whitespace++;else if (isdigit(ch))digits++;else if (ispunct(ch))punct++;elseothers++;cin.get(ch);}cout << chars << " letters."<< whitespace << " whitespace."<< digits << " digits."<< punct << " punctuations."<< others << " others.\n";return 0; }6.9 condit.cpp
#include<iostream> int main() {using namespace std;int a, b;cout << "Enter two integers:";cin >> a >> b;cout << "The larger of " << a << " and " << b;int c = a > b ? a : b;cout << " is " << c << endl;return 0; }6.10 switch.cpp
#include<iostream> using namespace std; void showmenu(); void report(); void comfort(); int main() {showmenu();int choice;cin >> choice;while (choice != 5){switch (choice){case 1:cout << "\a\n";break;case 2:report();break;case 3:cout << "The boss was in all day.\n";break;case 4:comfort();break;default:cout << "That's not a choice.\n";}showmenu();cin >> choice;}cout << "Bye!\n";return 0; }void showmenu() {cout << "Please enter1,2,3,4 or 5:\n""1) alarm 2)report\n""3) alibi 4)comfort\n""5)quit\n"; }void report() {cout << "It's been an excellent week for business.\n""Sales are up 120%.Expenses are down 35%.\n"; }void comfort() {cout << "Your employees think you are the finest CEO\n""in the industry.The board of directors think\n""you are the finest CEO in the industry.\n"; }6.11 enum.cpp
#include<iostream> enum { red, orange, yellow, green, blue, violet, indigo };int main() {using namespace std;cout << "Enter color code(0-6): ";int code;cin >> code;while (code >= red && code <= indigo){switch (code){case red:cout << "Her lips were red.\n"; break;case orange:cout << "Her hair was orange.\n"; break;case yellow:cout << "Her shoes were yellow.\n"; break;case green:cout << "Her nails were green:\.\n"; break;case blue:cout << "Her sweatsuit was blue.\n"; break;case violet:cout << "Her eyes were violet.\n"; break;case indigo:cout << "Her mood was indigo.\n"; break;}cout << "Enter color code(0-6):";cin >> code;}cout << "Bye\n";return 0; }6.12 jump.cpp
#include<iostream> const int ArSize = 80; int main() {using namespace std;char line[ArSize];int spaces = 0;cout << "Enter a line of text:\n";cin.get(line, ArSize);cout << "Complete line:\n" << line << endl;cout << "Line through first period:\n";for (int i = 0; line[i] != '\0'; i++){cout << line[i];if (line[i] == '.')break;if (line[i] != ' ')continue;spaces++;}cout << "\n" << spaces << " spaces\n";cout << "Done.\n";return 0; }6.13 cinfish.cpp
#include<iostream> const int Max = 5; int main() {using namespace std;double fish[Max];cout << "Please enter the weights of your fish.\n";cout << "You may enter up to " << Max << " fish <q to terminate>.\n";cout << "fish #1: ";int i = 0;while (i < Max && cin >> fish[i]){if (++i < Max)cout << "fish #" << i + 1 << ":";}double total = 0.0;for (int j = 0; j < i; j++)total += fish[j];if (i == 0)cout << "No fish\n";elsecout << total / i << " = average weight of "<< i << " fish\n";cout << "Done.\n";return 0; }6.14 cingolf.cpp
#include<iostream> const int Max = 5; int main() {using namespace std;int golf[Max];cout << "Please enter your golf scores.\n";cout << "You must enter " << Max << " rounds.\n";int i;for (i = 0; i < Max; i++){cout << "round #" << i + 1 << ":";while (!(cin >> golf[i])){cin.clear();while (cin.get() != '\n')continue;cout << "Please enter a number:";}}double total = 0;for (int i = 0; i < Max; i++)total += golf[i];cout << total / Max << " = average score "<< Max << " rounds\n";return 0; }6.15 outfile.cpp
#include<iostream> #include<fstream>int main() {using namespace std;char automobile[50];int year;double a_price;double d_price;ofstream outFile;outFile.open("carinfo.txt");cout << "Enter the make and model of automobile:";cin.getline(automobile, 50);cout << "Enter the model year:";cin >> year;cout << "Enter the original asking price:";cin >> a_price;d_price = 0.913 * a_price;cout << fixed; cout.precision(2); cout.setf(ios_base::showpoint); cout << "Make and model:" << automobile << endl;cout << "Year:" << year << endl;cout << "Was asking $" << a_price << endl;cout << "Now asking $" << d_price << endl;outFile << fixed;outFile.precision(2);outFile.setf(ios_base::showpoint);outFile << "Make and model:" << automobile << endl;outFile << "Year:" << year << endl;outFile << "Was asking $" << a_price << endl;outFile << "Now asking $" << d_price << endl;outFile.close();return 0; }6.16 sumafile.cpp
#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";system("pause");exit(EXIT_FAILURE);}double value;double sum = 0.0;int count = 0;inFile >> value;while (inFile.good()) //判斷文件流是否符合標準{count++;sum += value;inFile >> value;}if (inFile.eof())cout << "End of file reached.\n";else if (inFile.fail())cout << "Input terminated by data mismatch.\n";elsecout << "Input terminated for unknown reason.\n";if (count == 0)cout << "No data processed.\n";else{cout << "Items read:" << count << endl;cout << "Sum:" << sum << endl;cout << "Average:" << sum / count << endl;}inFile.close();return 0; }總結
以上是生活随笔為你收集整理的C++primer Plus课本代码(第6章)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 行人跟踪之身份识别(一)
- 下一篇: 大龄程序员的4年生涯