C++ Primer Plus课后复习题及编程练习第六章-分支语句和逻辑运算符

6.10 复习题

1.Version1:对于空格需要一次判断,但对于换行符需要两次判断

Version2:对于空格需要一次判断,对于换行符仅需要一次判断

2.用ch+1代替++ch,输出会变成Ascii码所代表的十进制再加1

3.输出:

if条件句中,一个“ = ”为赋值,会改变ch的值,所以每打一个字符,ct1和ct2都会进行++操作,注意回车和空格也算输入,最后可得ct1和ct2的值均为9.

4.

a.weight >= 115 && weight < 125

b.ch == ' q ' || ch == ' Q '

c.x % 2 == 0 && x != 26

d.x % 2 == 0 && x % 26 == 0

e.( donation >= 1000 && donation <= 2000 ) || guest == 1

f.( ch >= ' a ' && ch <= ' z ' ) || ( ch >= ' A ' && ch <= ' Z  ' )

5.当x为布尔类型,“!!x”就等于x本身;若x不为布尔类型,”!!x“会被看作”!(!x)“,先进行一次布尔运算,将x转换成布尔类型值,然后再对其进行逻辑非运算。

6.x = (x > 0) ? x : -x;

7.

switch (ch)
{
case'A':a_grade++;
	break;
case'B':b_grade++;
	break;
case'C':c_grade++;
	break;
case'D':d_grade++;
	break;
default:f_grade++;
	break;
}

8.如果使用数字数据类型,输入字符型变量可能直接导致程序出错;但如果使用字符型变量,输入的整型也能转换成字符变量。一定程度提高程序健壮性。

9.

int line = 0;
char ch;
while (cin.get(ch)&&ch!='Q') {
	if (ch == '\n') {
		line++;
	}
}

6.11 编程练习

1.

#include<iostream>
#include<cctype>
using namespace std;

int main() {

	//1.
	char ch;
	char A_ch,a_ch;
	while (cin.get(ch)) {
		if (ch == '@')
			break;
		if (ch >= 'a' && ch <= 'z') {
			cout << char(toupper(ch));
		}
		else if (ch >= 'A' && ch <= 'Z') {
			cout << char(tolower(ch));
		}
		else {
			continue;
		}
	}
	cout << endl;

	system("pause");
	return 0;
}

2.

#include<iostream>
#include<array>
using namespace std;

int main() {

	//2.
	array<double, 10> donation;
	int flag = 0;
	double sum = 0;
	double average = 0;
	double input;
	int big_ave = 0;
	cout << "PLease enter the No.1 number: ";
	cin >> input;

	for (int i = 0; ; i++) {
		donation[flag] = input;
		sum += donation[flag];

		if (++flag >= 10 || input == 0)
			break;

		cout << "PLease enter the No." << flag + 1 << " number: ";
		cin >> input;
	}
	/*while (input != 0 && flag < 10) {
		donation[flag] = input;
		sum += donation[flag++];
		cout << "PLease enter the No." << flag + 1 << " number: ";
		cin >> input;
	}*/
	cout << endl;
	cout << "the sum = " << sum << endl;

	average = sum / flag;
	cout << "the average = " << average << endl;

	for (int i = 0; i < 10; i++) {
		if (donation[i] > average)
			big_ave++;
	}
	cout << "how many bigger than average: " << big_ave << endl;

	system("pause");
	return 0;
}

3.

#include<iostream>
using namespace std;

int main() {

    //3.
	cout << "Please enter one of the following choices: " << endl;
	cout << "c) carnivore  p) pianist" << endl
		<< "t) tree  g)game" << endl;

	char ch;
	cin >> ch;

	while(ch)
	{
		switch (ch)
		{
		case'c':cout << "A maple is a carnivore." << endl;
			break;
		case'p':cout << "A maple is a pianist." << endl;
			break;
		case't':cout << "A maple is a tree." << endl;
			break;
		case'g':cout << "A maple is a game." << endl;
			break;
		default:
			break;
		}
		
		cout << "Please enter a c, p, t, or g: ";
		cin >> ch;
	}



	system("pause");
	return 0;
}

4.

#include<iostream>
using namespace std;

const int strsize = 20;

struct bop {
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};

bop Bop[5] = {
		{"Wimp Macho","Programmer","MIPS",0},
		{"Raki Rhods","Junior Programmer","",1},
		{"Celia","","MIPS",2},
		{"Hoppy Hipman","Analyst Trainee","",1},
		{"Pat Hand","","LOOPY",2}
};

void print_preference() {
	for (int i = 0; i < 5; i++) {
		switch (Bop[i].preference)
		{
		case 0:cout << Bop[i].fullname << endl; break;
		case 1:cout << Bop[i].title << endl; break;
		case 2:cout << Bop[i].bopname << endl; break;
		default:
			break;
		}
	}
	cout << "Next choice: ";
}

int main() {

    //4.
	cout << "Benevolent Order of Programmers Report" << endl;
	cout << "a) display by name  b) display by title" << endl
		<< "c) display by bopname  d)display by preference" << endl
		<< "q) quit" << endl;

	char choice;
	cout << "Enter your choice: ";
	cin.get(choice);
	
	while (choice != 'q')
	{
		switch (choice)
		{
		case'a':
			for (int i = 0; i < 5; i++) {
				cout << Bop[i].fullname << endl;
			}
			cout << "Next choice: ";
			break;
		case'b':
			for (int i = 0; i < 5; i++) {
				cout << Bop[i].title << endl;
			}
			cout << "Next choice: ";
			break;
		case'c':
			for (int i = 0; i < 5; i++) {
				cout << Bop[i].bopname << endl;
			}
			cout << "Next choice: ";
			break;
		case'd':
			print_preference();
			break;
		default:
			break;
		}

		cin.get(choice);
		if (choice == 'q') {
			cout << "Bye! " << endl;
			break;
		}
	}

	system("pause");
	return 0;
}

5.

#include<iostream>
using namespace std;

int main() {

    //5.
	double tvarps, tax;
	cout << "Please enter your tvarps: ";
	cin >> tvarps;

	while(tvarps > 0)
	{
		cout << "Your taxes paid is: ";
		
		if (tvarps <= 5000) {
			tax = 0;
			cout << tax << " tvarps." << endl;
		}
		else if (tvarps <= 15000) {
			tax = (tvarps - 5000) * 0.1;
			cout << tax << " tvarps." << endl;
		}
		else if (tvarps <= 35000) {
			tax = (tvarps - 15000) * 0.15 + 10000 * 0.1;
			cout << tax << " tvarps." << endl;
		}
		else if (tvarps > 35000) {
			tax = (tvarps - 35000) * 0.2 + 10000 * 0.1 + 20000 * 0.15;
			cout << tax << " tvarps." << endl;
		}
		else {
			return false;
		}
		
		cout << "Please enter your tvarps: ";
		cin >> tvarps;
	}

	system("pause");
	return 0;
}

6.

#include<iostream>
using namespace std;

int main() {

    //6.
	double tvarps, tax;
	cout << "Please enter your tvarps: ";
	cin >> tvarps;

	while (tvarps > 0)
	{
		cout << "Your taxes paid is: ";

		if (tvarps <= 5000) {
			tax = 0;
			cout << tax << " tvarps." << endl;
		}
		else if (tvarps <= 15000) {
			tax = (tvarps - 5000) * 0.1;
			cout << tax << " tvarps." << endl;
		}
		else if (tvarps <= 35000) {
			tax = (tvarps - 15000) * 0.15 + 10000 * 0.1;
			cout << tax << " tvarps." << endl;
		}
		else if (tvarps > 35000) {
			tax = (tvarps - 35000) * 0.2 + 10000 * 0.1 + 20000 * 0.15;
			cout << tax << " tvarps." << endl;
		}
		else {
			return false;
		}

		cout << "Please enter your tvarps: ";
		cin >> tvarps;
	}

	system("pause");
	return 0;
}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值