C++PrimerPlus(第6版)中文版第五章实例代码

实例5.1

#include<iostream>
int main()
{
	using namespace std;
	int i;

	for (i = 0; i < 5; i++)
		cout << "C++ knows loops.\n";
	cout << "C++ knows when to stop.\n";
	system("pause");
	return 0;
}

实例5.2

#include<iostream>
int main()
{
	using namespace std;
	cout << "Enter the starting countdown value:";
	int limit;
	cin >> limit;
	int i;
	for (i = limit; i; i--)
		cout << "i = " << i << "\n";
	cout << "Done now that i = " << i << "\n";
	system("pause");
	return 0;
}

实例5.3

#include<iostream>
int main()
{
	using namespace std;
	int x;

	cout << "The expression x = 100 has the value ";
	cout << (x = 100) << endl;
	cout << "Now x = " << x << endl;
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << endl;
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << endl;
	cout.setf(ios_base::fixed, ios_base::boolalpha);
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << endl;
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << endl;
	system("pause");
	return 0;
}

实例5.4

#include<iostream>
int main()
{
	using namespace std;
	int x;

	cout << "The expression x = 100 has the value ";
	cout << (x = 100) << endl;
	cout << "Now x = " << x << endl;
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << endl;
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << endl;
	cout.setf(ios_base::fixed, ios_base::boolalpha);
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << endl;
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << endl;
	system("pause");
	return 0;
}

实例5.5

#include<iostream>
int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	cout << "Enter an integer:";
	int by;
	cin >> by;
	cout << "Counting by " << by << "s:\n";
	for (int i = 0; i < 100; i=i+by)
		cout << i << endl;
	system("pause");
	return 0;
}

实例5.6

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	cout << "Enter a word:";
	string word;
	cin >> word;

	for (int i = word.size() - 1; i >= 0; i--)
		cout << word[i];
	cout << "\nBye.\n";
	system("pause");
	return 0;
}

实例5.7

#include<iostream>
int main()
{
	using std::cout;
	int a = 20;
	int b = 20;
	cout << "a = " << a << ";b = " << b << "\n";
	cout << "a++ = " << a++ << ";++b = " << ++b << "\n";
	cout << "a = " << a << ";b = " << b << "\n";
	system("pause");
	return 0;
} 

实例5.8

#include<iostream>
int main()
{
	using namespace std;
	cout << "The Amazing Accounto will sum and average ";
	cout << "five numbers for you.\n";
	cout << "Please enter five values:\n";
	double number;
	double sum = 0.0;
	for (int i = 1; i <= 5; i++)
	{
		cout << "Value " << i << ":";
		cin >> number;
		sum += number;
	}
	cout << "Five exquisite choices indeed!";
	cout << "They sum to " << sum << endl;
	cout << "and average to " << sum / 5 << ".\n";
	cout << "The Amazing Accounto bids you adiue!\n";
	system("pause");
	return 0;
}

实例5.9

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	cout << "Enter a word:";
	string word;
	cin >> word;

	char temp;
	int i, j;
	for (j = 0,i = word.size() - 1; j < i;--i, ++j)
	{
		temp = word[i];
		word[i] = word[j];
		word[j] = temp;
	}
	cout << word << "\nDone\n";
	system("pause");
	return 0;
}

实例5.10

#include<iostream>
int main()
{
	using namespace std;
	int quizscores[10] = { 20,20,20,20,20,19,20,18,20,20 };

	cout << "Doing it right:\n";
	int i;
	for (i = 0; quizscores[i] == 20; i++)
		cout << "quiz " << i << " is a 20\n";

	cout << "Doing it dangerously wrong:\n";
	for (i = 0; quizscores[i] = 20; i++)
		cout << "quiz " << i << "is a 20\n";
	system("pause");
	return 0;
}

实例5.11

#include<iostream>
#include<cstring>
int main()
{
	using namespace std;
	char word[5] = "?ate";
	for (char ch = 'a'; strcmp(word, "mate"); ch++)
	{
		cout << word << endl;
		word[0] = ch;
	}
	cout << "After loop ends,word is " << word << endl;
	system("pause");
	return 0;
}

实例5.12

#include<iostream>
#include<string>
int main()
{
	using namespace std;
	string word = "?ate";
	for (char ch = 'a'; word != "mate"; ch++)
	{
		cout << word << endl;
		word[0] = ch;
	}
	cout << "After loop ends,word is " << word << endl;
	system("pause");
	return 0;
}

实例5.13

#include<iostream>
const int ArSize = 20;
int main()
{
	using namespace std;
	char name[ArSize];
	cout << "Your first name,please:";
	cin >> name;
	cout << "Here is your name,verticalized and ASCIIized:\n";
	int i = 0;
	while (name[i] != '\0')
	{
		cout << name[i] << ":" << int(name[i]) << endl;
		i++;
	}
	system("pause");
	return 0;
}

实例5.14

#include<iostream>
#include<ctime>
int main()
{
	using namespace std;
	cout << "Enter the delay time,in seconds:";
	float secs;
	cin >> secs;
	clock_t delay = secs * CLOCKS_PER_SEC;
	cout << "starting\a\n";
	clock_t start = clock();
	while (clock() - start < delay)
		cout << "done \a\n";
	system("pause");
	return 0;
}

实例5.15

#include<iostream>
int main()
{
	using namespace std;
	int n;

	cout << "Enter numbers in the range 1-10 to find ";
	cout << "my favourite number\n";
	do
	{
		cin >> n;
	} while (n != 7);
	cout << "Yes,7 is my favorite.\n";
	system("pause");
	return 0;
}

实例5.16

#include<iostream>
int main()
{
	using namespace std;
	char ch;
	int count = 0;
	cout << "Enter characters;enter # to quit:\n";
	cin >> ch;
	while (ch != '#')
	{
		cout << ch;
		++count;
		cin >> ch;
	}
	cout << endl << count << " characters read\n";
	system("pause");
	return 0;
}

实例5.17

#include<iostream>
int main()
{
	using namespace std;
	char ch;
	int count = 0;

	cout << "Enter characters;enter # to quit:\n";
	cin.get(ch);
	while (ch != '#')
	{
		cout << ch;
		++count;
		cin.get(ch);
	}
	cout << endl << count << " characters read\n";
	system("pause");
	return 0;
}

实例5.18

#include<iostream>
int main()
{
	using namespace std;
	char ch;
	int count = 0;
	cin.get(ch);
	while (cin.fail() == false)
	{
		cout << ch;
		++count;
		cin.get(ch);
	}
	cout << endl << count << " characters read\n";
	system("pause");
	return 0;
}

实例5.19

#include<iostream>
int main(void)
{
	using namespace std;
	int ch;
	int count = 0;

	while ((ch = cin.get())!=EOF)
	{
		cout.put(char(ch));
		++count;
	}
	cout << endl << count << " characters read\n";
	system("pause");
	return 0;
}

实例5.20

#include<iostream>
const int Cities = 5;
const int Years = 4;
int main()
{
	using namespace std;
	const char *cities[Cities] =
	{
		"Gribble City",
		"Gribbletown",
		"New Gribble",
		"San Gribble",
		"Gribble Vista"
	};
	
	int maxtemps[Years][Cities] = 
	{
		{96,100,87,101,105},
		{96,98,91,107,104},
		{97,101,93,108,107},
		{98,103,95,109,108}
	};

	cout << "Maximum temperatures for 2008 - 2011\n\n";
	for (int city = 0; city < Cities; ++city)
	{
		cout << cities[city] << ":\t";
		for (int year = 0; year < Years; ++year)
			cout << maxtemps[year][city] << "\t";
		cout << endl;
	}
	system("pause");
	return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐