C++primer Plus课本代码(第6章)

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;
		}
		else
			std::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:";
		else
			cout << 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";
	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";

	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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满城枪声皆无你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值