C++ primer plus(第六版)编程练习答案 第6章 分支语句和逻辑运算符

一、程序清单

if.cpp

// if.cpp -- using the if statement
#include <iostream>
int main()
{
	using std::cin;     // using declarations
	using std::cout;
	char ch;
	int spaces = 0;
	int total = 0;
	cin.get(ch);
	while (ch != '.')   // quit at end of sentence
	{
		if (ch == ' ')  // check if ch is a space
			++spaces;
		++total;        // done every time
		cin.get(ch);
	}
	cout << spaces << " spaces, " << total;
	cout << " characters total in sentence\n";
	// cin.get();
	// cin.get();
	return 0;
}

执行结果:

The balloonist was an airhead
with lofty goals.
7 spaces, 47 characters total in sentence

ifelse.cpp 

// ifelse.cpp -- using the if else statement
#include <iostream>
int main()
{
	char ch;

	std::cout << "Type, and I shall repeat.\n";
	std::cin.get(ch);
	while (ch != '

猜你喜欢

转载自blog.csdn.net/qq_43445867/article/details/129781008
今日推荐