C++PrimerPlus(第6版)中文版第六章实例代码
实例6.1
#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";
system("pause");
return 0;
}
实例6.2
#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;
else
std::cout << ++ch;
std::cin.get(ch);
}
std::cout << "\nPlease excuse the slight confusion.\n";
system("pause");
return 0;
}
实例6.3
#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:";
else
cout << Fave << " is right!\n";
} while (n != Fave);
system("pause");
return 0;
}
实例6.4
#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";
else
cout << "That wasn't a y or n!Apparently you "
"can't follow\ninstructions,so "
"I'll trash your disk anyway.\a\a\a\n";
system("pause");
return 0;
}
实例6.5
#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";
else
cout << "That wasn't a y or n!Apparently you "
"can't follow\ninstructions,so "
"I'll trash your disk anyway.\a\a\a\n";
system("pause");
return 0;
}
实例6.6
#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;
else
index = 3;
cout << "You qualify for the " << qualify[index];
system("pause");
return 0;
}
实例6.7
#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";
system("pause");
return 0;
}
bool is_int(double x)
{
if (x <= INT_MAX && x >= INT_MIN)
return true;
else
return false;
}
实例6.8
#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++;
else
others++;
cin.get(ch);
}
cout << chars << " letters."
<< whitespace << " whitespace."
<< digits << " digits."
<< punct << " punctuations."
<< others << " others.\n";
system("pause");
return 0;
}
实例6.9
#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;
system("pause");
return 0;
}
实例6.10
#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
#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";
system("pause");
return 0;
}
实例6.12
#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";
system("pause");
return 0;
}
实例6.13
#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";
else
cout << total / i << " = average weight of "
<< i << " fish\n";
cout << "Done.\n";
system("pause");
return 0;
}
实例6.14
#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";
system("pause");
return 0;
}
实例6.15
#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); //设置精确度为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();
system("pause");
return 0;
}
实例6.16
#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";
else
cout << "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();
system("pause");
return 0;
}