第三章 分支语句 程序设计练习

3.1(代数:解二次方程)二次方程ax^2+bx+c=0的两个根可由下列公式得出:r_1=\frac{-b+\sqrt{b^2-4ac}}{2a}r_2=\frac{-b-\sqrt{b^2-4ac}}{2a},其中b^2-4ac称为二次方程的判别式。如果它为正,则方程会有两个实根。如果它为零,则方程有一个根。如果它为负,则方程无实根。编写程序,提示用户输入a、b、c的值,输出基于判别式的结果。如果判别式为正,则输出两个根。如果判别式为0,输出一个根。否则,输出"The equation has on real roots."注意:可以使用pow(x,0.5)来计算\sqrt{x}

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

int main()
{
	double a = 0, b = 0, c = 0;
	cout << "Enter a,b,c:";
	cin >> a >> b >> c;

	int flag = b * b - 4 * a * c;
	if (flag > 0)
	{
		double r1 = (0 - b + pow(flag, 0.5)) / 2 / a;
		double r2 = (0 - b - pow(flag, 0.5)) / 2 / a;
		cout << "The roots are " << r1 << " and " << r2;
	}
	else if (flag == 0)
	{
		double r = (0 - b + pow(flag, 0.5)) / 2 / a;
		cout << "The root is " << r;
	}
	else
		cout << "The equation has no real roots.";

	return 0;
}

3.2(检验数字)编写程序,提示用户输入两个整数,检验第一个数是否能由第二个数整除。

#include <iostream>
using namespace std;

int main()
{
	int a = 0, b = 0;
	cout << "Enter two integers:";
	cin >> a >> b;

	if (b == 0)cout << "Error!";//除数不能为0
	else if (a % b == 0)
		cout << a << " is divisible by " << b;
	else
		cout << a << " is not divisible by " << b;


	return 0;
}

3.3(代数:解2*2线性方程组)可以使用克莱姆法则解下列2*2方程组:

\\ax+by=e\\ cx+dy=f         x=\frac{ed-bf}{ad-bc}      y=\frac{af-ec}{ad-bc}

编写程序,提示用户输入a、b、c、d、e和f,输出结果。如果ad-bc为0,则输出“The equation has no solution”。

#include <iostream>
using namespace std;

int main()
{
	double a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
	cout << "Enter a,b,c,d,e,f:";
	cin >> a >> b >> c >> d >> e >> f;

	if (a * d - b * c == 0)
		cout << "The equation has no solution";
	else
		cout << "x is " << (e * d - b * f) / (a * d - b * c) << " and y is " << (a * f - e * c) / (a * d - b * c);

	return 0;
}

3.4(检测温度)编写程序,提示用户输入温度。如果温度低于30,则输出too cold;如果温度高于100,则输出too hot;否则,输出just right。

#include <iostream>
using namespace std;

int main()
{
	int temperature;
	cout << "Enter the temperature:";
	cin >> temperature;

	if (temperature < 30)
		cout << "too cold";
	else if (temperature > 100)
		cout << "too hot";
	else
		cout << "just right";

	return 0;
}

3.5(找出未来日期)编写程序,提示用户输入今天是星期几的整数(星期日为0,星期一为1,……,星期六为6)。接下来,提示用户输入未来某天距今天共几天,输出未来这天为星期几。

#include <iostream>
using namespace std;

int main()
{
	int today, futureDay,theNumberOfDays;
	cout << "Enter today's day:";
	cin >> today;
	cout << "Enter the number of days elapsed since today:";
	cin >> theNumberOfDays;
	futureDay = (today + theNumberOfDays) % 7;//计算未来是星期几
	cout << "Today is ";
	switch (today)
	{
	case 0:cout << "Sunday"; break;
	case 1:cout << "Monday"; break;
	case 2:cout << "Tuesday"; break;
	case 3:cout << "Wednesday"; break;
	case 4:cout << "Thursday"; break;
	case 5:cout << "Friday"; break;
	case 6:cout << "Saturday"; break;
	default:cout << "Error!"; break;
	}
	cout << " and the future day is ";
	switch (futureDay)
	{
	case 0:cout << "Sunday"; break;
	case 1:cout << "Monday"; break;
	case 2:cout << "Tuesday"; break;
	case 3:cout << "Wednesday"; break;
	case 4:cout << "Thursday"; break;
	case 5:cout << "Friday"; break;
	case 6:cout << "Saturday"; break;
	default:cout << "Error!"; break;
	}

	return 0;
}

3.6(健康应用:BMI)重写程序清单3-2,ComputeAndInterpretBMI.cpp,使用户输入体重,脚的尺寸以及身高。例如,一个人的脚的尺寸为5,身高为10,则feet输入为5,inches输入为10。

#include <iostream>
using namespace std;

int main()
{
	const double KILOGRAMS_PER_POUND = 0.45359237;
	const double METERS_PER_INCH = 0.0254;

	cout << "Enter weight in pounds:";
	double weight;
	cin >> weight;

	cout << "Enter feet:";
	double feet;
	cin >> feet;

	cout << "Enter inches:";
	double inches;
	cin >> inches;

	double weightInKilograms = weight * KILOGRAMS_PER_POUND;
	double heightInMeters = inches * METERS_PER_INCH;
	double bmi = weightInKilograms / (heightInMeters * heightInMeters);

	cout << "BMI is " << bmi << endl;
	if (bmi < 18.5)
		cout << "Underweight" << endl;
	else if (bmi < 25)
		cout << "Normal" << endl;
	else if (bmi < 30)
		cout << "Overweight" << endl;
	else
		cout << "Obese" << endl;

	return 0;
}

3.7(对3个整数进行排序)编写程序,提示用户输入3个整数,输出非递减顺序的3个整数的排列。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter three integers:";
	int a, b, c;
	cin >> a >> b >> c;
	if (a > b)
		if (a > c)
			if (b > c)
				cout << c << "	" << b << "	" << a;
			else
				cout << b << "	" << c << "	" << a;
		else
			cout << b << "	" << a << "	" << c;
	else
		if (a < c)
			if (b > c)
				cout << a << "	" << c << "	" << b;
			else
				cout << a << "	" << b << "	" << c;
		else
			cout << c << "	" << a << "	" << b;

	return 0;
}

3.8(金融应用:计算货币单位数量)改写程序清单2-12,ComputeChange.cpp,只显示非零的货币单位数量,对于单个货币显示单词的单数形式,如1 dollar、1 penny,对于多个货币显示复数形式,如2 dollars、3 pennies。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter an amount in double,for example 11.56: ";
	double amount;
	cin >> amount;

	int remainingAmount = static_cast<int>(amount * 100);
	int numberOfDollars, numberOfQuarters, numberOfDimes, numberOfNickels, numberOfPennies;

	numberOfDollars = remainingAmount / 100;//计算一美元硬币数量
	remainingAmount %= 100;

	numberOfQuarters = remainingAmount / 25;//计算两角五分硬币数量
	remainingAmount %= 25;

	numberOfDimes = remainingAmount / 10;//计算一角硬币数量
	remainingAmount %= 10;

	numberOfNickels = remainingAmount / 5;//计算五美分硬币数量
	remainingAmount %= 5;

	numberOfPennies = remainingAmount;//最后剩余的是一美分硬币数量

	//判断一美元硬币数量是否大于1,大于1输出复数形式,等于1输出单数,小于1不做处理。
	if (numberOfDollars > 1)cout << numberOfDollars << "	" << "dollars" << endl;
	else if (numberOfDollars == 1)cout << numberOfDollars << "	" << "dollars" << endl;
	
	if (numberOfQuarters > 1)cout << numberOfQuarters << "	" << "quarters" << endl;
	else if (numberOfQuarters == 1)cout << numberOfQuarters << "	" << "quarter" << endl;
	
	if (numberOfDimes > 1)cout << numberOfDimes << "	" << "dimes" << endl;
	else if (numberOfDimes == 1)cout << numberOfDimes << "	" << "dime" << endl;
	
	if (numberOfNickels > 1)cout << numberOfNickels << "	" << "nickels" << endl;
	else if (numberOfNickels == 1)cout << numberOfNickels << "	" << "nickel" << endl;
	
	if (numberOfPennies > 1)cout << numberOfPennies << "	" << "pennies" << endl;
	else if (numberOfPennies == 1)cout << numberOfPennies << "	" << "penny" << endl;

	return 0;
}

3.9(求一个月的天数)编写一个程序,提示用户输入月份和年份,输出该月的天数。例如,如果用户输入月份为2,年份为2012,程序应该显示2012年2月有29天。如果用户输入月份为3,年份为2015,程序应输出2015年3月31天。

#include <iostream>
using namespace std;

int main()
{
	cout << "请输入月份和年份:" << endl;
	int month = 0, year = 0;
	cin >> month >> year;

	cout << year << "年" << month << "月有";
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:cout << 31; break;
	case 4:
	case 6:
	case 9:
	case 11:cout << 30; break;
	case 2:if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)cout << 29;//判断是否为闰年
		  else cout << 28;break;
	default:
		break;
	}
	cout << "天" << endl;

	return 0;
}

3.10(游戏:加法学习工具)程序清单3-4,SubtractionQuiz.cpp,随机生成一个减法题。修改程序,使之能随机生成一个加法题,两个运算整数均在100以内。

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
	srand(time(0));
	int number1 = rand() % 100;
	int number2 = rand() % 100;

	cout << "What is " << number1 << " + " << number2 << " ?";
	int answer;
	cin >> answer;

	if (number1 + number2 == answer)
		cout << "You are correct";
	else
		cout << "Your answer is wrong. " << number1 << " + " << number2 <<
		" should be " << (number1 + number2) << endl;

	return 0;
}

3.11(运输的价格)运输公司使用下列函数以包裹的重量(以磅为单位)为基础来计算运输的价格(以美元为单位)c(w)=\left\{\begin{matrix} 3.5&0<w\leqslant 1\\ 5.5&1<w\leqslant 3 \\ 8.5&3<w\leqslant 10 \\ 10.5& 10<w\leqslant 20 \end{matrix}\right.

编写程序,提示用户输入包裹的重量,输出运输的价格。如果重量超过50,则输出信息“the package cannot be shipped”。

//觉得此题有问题
#include <iostream>
using namespace std;

int main()
{
	double weight = 0;
	cout << "Ehter the weight:";
	cin >> weight;

	if (weight <= 1 && weight > 0)
		cout << "The price is " << 3.5;
	else if (weight <= 3)
		cout << "The price is " << 5.5;
	else if (weight <= 10)
		cout << "The price is " << 8.5;
	else if (weight <= 20)
		cout << "The price is " << 10.5;
	else if (weight > 50)
		cout << "the package cannot be shipped";
	return 0;
}

3.12(游戏:正面朝上还是反面朝上)编写程序让用户猜测扔硬币的结果是正面朝上还是背面朝上。程序随机生成一个整数0或1,分别代表正面与背面。程序提示用户输入猜测,然后告知用户是否猜对。

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
	srand(time(0));
	int number = rand() % 2;//生成一个0或1的随机数

	int answer;
	cout << "请输入您的猜测,0代表正面,1代表反面:";
	cin >> answer;

	if (answer == number)
		cout << "恭喜你,猜对了!" << endl;
	else
		cout << "很遗憾,猜错了!" << endl;

	return 0;
}

3.13(金融应用:计算税款)程序清单3-3,ComputeTax.cpp,给出了计算单身纳税人税款的源代码。补充程序清单3-3给出完整的源代码。

#include <iostream>
using namespace std;

int main()
{
	//Prompt the user to enter filing status
	cout << "(0-single filer,1-marring jointly,"
		<< "or qualifying widow(er)," << endl
		<< "2-married separately, 3-head of household)" << endl
		<< "Enter the filing status: ";

	int status;
	cin >> status;

	//Prompt the user to enter taxable income
	cout << "Enter the taxable income:";
	double income;
	cin >> income;

	//Compute tax
	double tax = 0;

	if (status == 0)//Compute tax for single filers
	{
		if (income <= 8350)
			tax = income * 0.10;
		else if (income <= 33950)
			tax = 8350 * 0.1 + (income - 8350) * 0.15;
		else if (income <= 82250)
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
		else if (income <= 171550)
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
		else if (income <= 372950)
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;
		else
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
	}
	else if (status == 1)//Compute tax for married filing jointly or qualifying widow(er)
	{
		if (income <= 16700)
			tax = income * 0.1;
		else if (income <= 67900)
			tax = 16700 * 0.1 + (income - 16700) * 0.15;
		else if (income <= 137050)
			tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
		else if (income <= 208850)
			tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
		else if (income <= 372950)
			tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
		else
			tax = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
	}
	else if (status == 2)//Compute tax for married filing seprately
	{
		if (income <= 8350)
			tax = income * 0.10;
		else if (income <= 33950)
			tax = 8350 * 0.1 + (income - 8350) * 0.15;
		else if (income <= 68525)
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
		else if (income <= 104425)
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
		else if (income <= 186475)
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
		else
			tax = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186425 - 104425) * 0.33 + (income - 186425) * 0.35;
	}
	else if (status == 3)//Compute tax for married filing separately
	{
		if (income <= 11950)
			tax = income * 0.10;
		else if (income <= 45500)
			tax = 11950 * 0.1 + (income - 11950) * 0.15;
		else if (income <= 117450)
			tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
		else if (income <= 190200)
			tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
		else if (income <= 372950)
			tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
		else
			tax = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
	}
	else {//Display wrong status
		cout << "Error:invalid status";
		return 0;
	}
	//Display the result
	cout << "Tax is " << static_cast<int>(tax * 100) / 100.0 << endl;

	return 0;
}

3.14(游戏:彩票)重写程序清单3-7,Lottery.cpp,来生成一个3个数的彩票。程序提示用户输入一个3位的数,根据下列规则来确定用户是否中奖:

如果用户输入同彩票上的号码完全相符,则奖励$10000.

如果用户输入的数字同彩票上的号码相同,则奖励$3000.

如果用户输入的数字中有一个与彩票上的号码相同,则奖励$1000.

#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;

int main()
{
	//Generate a lottery
	srand(time(0));
	int lottery = rand() % 1000;

	//Prompt the user to enter a guess
	cout << "Enter your lottery pick (three digits): ";
	int guess;
	cin >> guess;

	//Get digits form lottery
	int lotteryDigit1 = lottery / 100;
	int lotteryDigit2 = (lottery / 10) % 10;
	int lotteryDigit3 = (lottery % 100) % 10;

	//Get digits from guess
	int guessDigit1 = guess / 100;
	int guessDigit2 = (guess / 10) % 10;
	int guessDigit3 = (guess % 100) % 10;

	//Check the guess
	if (guess == lottery)
		cout << "Exact match:you win $10000" << endl;
	else if ((guessDigit1 == lotteryDigit1 && guessDigit3 == lotteryDigit2 && guessDigit2 == lotteryDigit3) ||
		(guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2 && guessDigit3 == lotteryDigit3) ||
		(guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2 && guessDigit1 == lotteryDigit3) ||
		(guessDigit3 == lotteryDigit1 && guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit3) ||
		(guessDigit3 == lotteryDigit1 && guessDigit2 == lotteryDigit2 && guessDigit1 == lotteryDigit3))
		cout << "Match all digits:you win $3000" << endl;
	else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit3
		|| guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3
		|| guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3)
		cout << "Macth one digit:you win $1000" << endl;
	else
		cout << "Sorry,no match" << endl;
	
		return 0;
}

3.15(游戏:剪刀、石头、布)编写程序进行剪刀石头布这个游戏。(剪刀可以剪布,石头可以敲击剪刀,布可以包裹石头。)程序随机生成0、1或者2,它们分别代表剪刀石头和布。程序提示用户输入0、1或2,然后输出信息来告知用户或者计算机赢、输或者平。

#include<iostream>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
	srand(time(0));
	int computerInput = rand() % 3;//电脑随机得到一个0,1,2的数

	//提示用户输入
	cout << "scissor(0),rock(1),paper(2):";
	int userInput;
	cin >> userInput;

	//输出电脑是剪刀石头还是布
	cout << "The computer is ";
	switch (computerInput)
	{
	case 0:cout << "scissor"; break;
	case 1:cout << "rock"; break;
	case 2:cout << "paper"; break;
	};
	//输出用户的剪刀石头还是布
	cout << ". You are ";
	switch (userInput)
	{
	case 0:cout << "scissor"; break;
	case 1:cout << "rock"; break;
	case 2:cout << "paper"; break;
	default:cout << "inputting is error"; return 0;
	};

	if (computerInput == userInput)//相等则平局
		cout << " too. It is a draw";
	else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0 || userInput == 2 && computerInput == 1)
		cout << ". You won";
	else
		cout << ". You loss";

	return 0;
}

3.16(计算三角形周长)编写程序,读入一个三角形的三条边,如果输入是合法的,计算三角形的周长。否则,显示输入非法的信息。输入合法的条件是任意两条边之和大于第三边。

#include<iostream>
using namespace std;

int main()
{
	cout << "请输入三角形的三条边:";
	int a, b, c;
	cin >> a >> b >> c;

	if (a + b <= c || a + c <= b || b + c <= a)
	{
		cout << "输入的三角形非法" << endl;
		return 0;
	}

	cout << "三角形周长为:" << a + b + c << endl;

	return 0;
}

3.17(科学技术:风冷温度)程序设计练习2.17给出了计算机风冷温度的公式。公式对在-58F与41F之间的温度以及大于或等于2的风速有效。编写程序,提示用户输入温度与风速。如果输入有效,则输出风冷温度;否则,输出信息告知温度或风速是无效的。

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

int main()
{
	double twc = 0, ta = 0, v = 0;
	cout << "Enter the temperature in Fahrenheit:";
	cin >> ta;
	cout << "Enter the wind speed in miles per hour:";
	cin >> v;

	if (ta < -58 || ta>41 || v < 2)
	{
		cout << "The temperature or wind speed is invalid!";
		return 0;
	}

	twc = 35.74 + 0.6215 * ta - 35.75 * pow(v, 0.16) + 0.4275 * ta * pow(v, 0.16);
	cout << "The wind chill index is " << twc;

	return 0;
}

3.18(游戏:三个数相加的学习工具)程序清单3-4,SubtractionQuiz.cpp中,随机生成一个减法题。修改它,使之能随机生成一个加法题,三个运算数均在100以内。

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
	srand(time(0));
	int number1 = rand() % 100;
	int number2 = rand() % 100;
	int number3 = rand() % 100;

	cout << "What is " << number1 << " + " << number2 << " + " << number3 << " ?";
	int answer;
	cin >> answer;

	if (number1 + number2 + number3 == answer)
		cout << "You are correct";
	else
		cout << "Your answer is wrong. " << number1 << " + " << number2 <<"+"<<number3<<"+ "
		<<" should be " << (number1 + number2) << endl;

	return 0;
}

3.19(几何:点是否在圆内?)编写程序,提示用户输入点(x,y),检验点是否在以(0,0)为圆心,半径为10的圆内。例如,(4,5)在圆内,而(9,9)在圆外。(提示:如果点距(0,0)的距离低于或等于10,则点在圆内)

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

int main()
{
	cout << "Enter the point with two coordinate:";
	double x, y;
	cin >> x >> y;

	double distance = pow(x * x + y * y, 0.5);
	if (distance <= 10)
		cout << "Point (" << x << ", " << y << ") is in the circle" << endl;
	else
		cout<< "Point (" << x << ", " << y << ") is not in the circle" << endl;

	return 0;
}

3.20(几何:点是否在矩形内)编写程序,提示用户输入一个点(x,y),检测点是否在以点(0,0)为中心,宽10,高5的矩形内。例如,(2,2)在矩形内,而(6,4)不在矩形内。(提示:如果点距(0,0)的水平距离小于或等于10/2,并且它距点(0,0)的垂直距离小于或等于5/2,则点在矩形内。对所有情况测试程序。)

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

int main()
{
	cout << "Enter the point with two coordinate:";
	double x, y;
	cin >> x >> y;

	if (abs(x) <= 5 && abs(y) <= 2.5)
		cout << "Point (" << x << "," << y << " ) is in the rectangle" << endl;
	else
		cout << "Point (" << x << "," << y << " ) is not in the rectangle" << endl;

	return 0;
}

3.21(游戏:抽取卡牌)编写程序,模仿从有52张牌的整副牌中抽取卡片。程序应该输出卡牌的排名(Ace,2,3,4,5,6,7,8,9,10,Jack,Queen,King)与花色(Clubs,Diamonds,Heart,Spades)。

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
	srand(time(0));
	int rankingOfCard = 1 + rand() % 13;//卡牌的排名
	int colorOfCard = 1 + rand() % 4;//卡牌的花色

	cout << "The card you picked is ";
	switch (rankingOfCard)//输出卡牌的排名
	{
	case 1:cout << "Ace"; break;
	case 2:cout << 2; break;
	case 3:cout << 3; break;
	case 4:cout << 4; break;
	case 5:cout << 5; break;
	case 6:cout << 6; break;
	case 7:cout << 7; break;
	case 8:cout << 8; break;
	case 9:cout << 9; break;
	case 10:cout << 10; break;
	case 11:cout << "Jack"; break;
	case 12:cout << "Queen"; break;
	case 13:cout << "King"; break;
	default:break;
	}

	cout << " of ";

	switch(colorOfCard)//输出卡牌的花色
	{
	case 1:cout << "Clubs"; break;
	case 2:cout << "Diamonds"; break;
	case 3:cout << "Heart"; break;
	case 4:cout << "Spades"; break;
	default:break;
	}

	return 0;
}

3.22(几何:交点)给出线1上的两点(x1,y1)与(x2,y2),线2上的两点(x3,y3)与(x4,y4)。两条线的交点可通过下列线性方程得出:

\\(y1-y2)x-(x1-x2)y=(y1-y2)x1-(x1-x2)y1 \\(y3-y4)x-(x3-x4)y=(y3-y4)x3-(x3-x4)y3

线性方程可用克莱姆法则解出(见程序练习3.3)。如果方程无解,则两条线是平行的。编写程序,提示用户输入4个点,输出交点。

#include<iostream>
using namespace std;

int main()
{
	cout << "Enter x1, y1, x2, y2, x3, y3, x4, y4:";
	double x1, x2, x3, x4, y1, y2, y3, y4;
	cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;

	double a, b, c, d, e, f;
	a = y1 - y2; b = x2 - x1;
	c = y3 - y4; d = x4 - x3;
	e = (y1 - y2) * x1 - (x1 - x2) * y1;
	f = (y3 - y4) * x3 - (x3 - x4) * y3;

	double x = (e * d - b * f) / (a * d - b * c);
	double y = (a * f - e * c) / (a * d - b * c);

	if (a * d - b * c == 0)
		cout << "The two lines are parallel" << endl;
	else
		cout << "The intersecting point is at (" << x << "," << y << ")" << endl;

	return 0;
}

3.23(几何:点是否在三角形内?)假设一个正三角形放置在平面上。直角点放置在(0,0),其他两点放置在(200,0)与(0,100)。编写程序,提示用户输入带有x,y坐标的点,确定此点是否在三角形内部。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter the point's x- and y-coordinates:";
	double x, y;
	cin >> x >> y;

	/*点P在三角形ABC内部,可以通过以下三个条件判断:
		点P和点C在直线AB同侧
		点P和点B在直线AC同侧
		点P和点A在直线BC同侧
	如果以上三个条件同时满足,则点P在三角形ABC内部。*/
	//利用程序3-29可以判断点在直线的那一侧
	//顺时针遍历三个顶点时,第三个点一定位于剩余两点组成有向线段的右侧
	//所以,顺时针时,点P一直要处于有向线段的右侧,点P才在三角形内部

	double x0, x1, y0, y1;
	//1.点(0.0)指向点(0,100)的有向线段
	x0 = 0, y0 = 0, x1 = 0, y1 = 100;
	bool flag1 = (x1 - x0) * (y - y0) - (x - x0) * (y1 - y0) < 0;//小于0则位于线段右侧
	//2.点(0,100)指向点(200,0)的有向线段
	x0 = 0, y0 = 100, x1 = 200, y1 = 0;
	bool flag2 = (x1 - x0) * (y - y0) - (x - x0) * (y1 - y0) < 0;
	//3.点(200,0)指向点(0,0)的有向线段
	x0 = 200, y0 = 0, x1 = 0, y1 = 0;
	bool flag3 = (x1 - x0) * (y - y0) - (x - x0) * (y1 - y0) < 0;

	if (flag3 && flag2 && flag1)
		cout << "The point is in the triangle" << endl;
	else
		cout << "The point is not in the triangle" << endl;

	return 0;
}

3.24(使用&&和||运算符)编写一个程序,提示用户输入一个整数,检查它是否能同时被5和6整除,能否被5或6整除,是否被5、6之一且只被其一整除。

#include<iostream>
using namespace std;

int main()
{
	cout << "Enter an integer:" << endl;
	int integer;
	cin >> integer;

	//是否能同时被5和6整除
	cout << "Is " << integer << " divisible by 5 and 6? ";
	if ((integer % 5 == 0) && (integer % 6 == 0))
		cout << "true" << endl;
	else
		cout << "false" << endl;

	//能否被5或6整除
	cout << "Is " << integer << " divisible by 5 or 6? ";
	if ((integer % 5 == 0) || (integer % 6 == 0))
		cout << "true" << endl;
	else
		cout << "false" << endl;

	//是否被5、6之一且只被其一整除
	cout << "Is " << integer << " divisible by 5 or 6, but not both? ";
	if ((integer % 5 == 0) && (integer % 6 != 0) || (integer % 5 != 0) && (integer % 6 == 0))
		cout << "true" << endl;
	else
		cout << "false" << endl;


	return 0;
}

3.25(几何:两个矩形)编写程序,提示用户输入两个矩形的中心x、y、宽度与高度,检查第二个矩形是否在第一个矩形内或是否与第一个有重叠。

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

int main()
{
	cout << "Enter r1's center x-, y-coordinates, width, and height:";
	double center1_X, center1_Y, width1, height1;
	cin >> center1_X >> center1_Y >> width1 >> height1;

	cout << "Enter r2's center x-, y-coordinates, width, and height:";
	double center2_X, center2_Y, width2, height2;
	cin >> center2_X >> center2_Y >> width2 >> height2;

	if (abs(center1_X - center2_X) + width2 / 2.0 <= width1 / 2.0 && abs(center1_Y - center2_Y) + height2 / 2.0 <= height1 / 2.0)
		cout << "r2 is inside r1" << endl;
	else if (abs(center1_X - center2_X) < (width1 + width2) / 2.0 && abs(center1_Y - center2_Y) < (height2 + height1) / 2.0)
		cout << "r2 overlaps r1" << endl;
	else
		cout << "r2 does not overlap r1" << endl;

	return 0;
}

3.26(几何:两个圆)编写程序,提示用户输入圆心坐标以及两个圆的半径,检查第二个圆是否在第一个圆内或是否与第一个圆有重叠。(提示:如果两个圆心之间的距离<=|r1-r2|,则圆2在圆1内;如果两个圆之间的距离<=r1+r2,则圆2与圆1有重叠。 )

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

int main()
{
	cout << "Enter circle1's center x-, y-coordinates, and radius:";
	double x1, y1, r1;
	cin >> x1 >> y1 >> r1;

	cout << "Enter circle2's center x-, y-coordinates, and radius:";
	double x2, y2, r2;
	cin >> x2 >> y2 >> r2;

	double distance = pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5);
	if (distance <= r1 - r2)
		cout << "circle2 is inside circle1";
	else if (distance <= r1 + r2)
		cout << "circle2 overlaps circle1";
	else
		cout << "circle2 does not overlap circle1";

	return 0;
}

3.27(当前时间)重写程序设计练习2.8,输出十二小时制的时间。

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
	int timeZone = 0;//一个时区相差一小时
	cout << "Enter the time zone offset to GMT:";
	cin >> timeZone;

	int totalSecond = time(0);
	int currentSecond = totalSecond % 60;
	int currentMinute = (totalSecond / 60) % 60;
	int gmtHour = (totalSecond / 60 / 60) % 24;//GMT小时

	int currentHour = gmtHour + timeZone;//当前时区的小时

	if (currentHour < 0)//如果小于0,则证明是前一天,加上24小时,显示正确的时间
		currentHour += 24;
	else //如果大于等于24,则证明是后一天,减去24.显示正确时间
		currentHour %= 24;

	if (currentHour <= 12)
		cout << "The current time is " << currentHour << ":" << currentMinute << ":" << currentSecond << " AM" << endl;
	else
		cout << "The current time is " << currentHour - 12 << ":" << currentMinute << ":" << currentSecond << " PM" << endl;

	return 0;
}

3.28(金融应用:货币兑换)编写程序,提示用户输入货币由美元到人民币的兑换率。提示用户输入0表示由美元到人民币的转换,1表示由人民币到美元的转换。提示用户输入要兑换的美元或人民币的数量,分别抓换为人民币或美元。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter the exchange rate from dollars to RMB:";
	double exchangeRateDollarsToRmb;
	cin >> exchangeRateDollarsToRmb;

	cout << "Enter 0 to convert dollars to RMB and 1 vice versa:";
	int wayOfExchange;
	cin >> wayOfExchange;

	if (wayOfExchange == 0)
	{
		cout << "Enter the dollar amount:";
		double dollar;
		cin >> dollar;
		cout << "$" << dollar << " is " << static_cast<int>(dollar * exchangeRateDollarsToRmb*100)/100.0 << " yuan" << endl;
	}
	else if (wayOfExchange == 1)
	{
		cout << "Enter the RMB amount:";
		double rnb;
		cin >> rnb;
		cout << rnb << " yuan is $" << static_cast<int>(rnb / exchangeRateDollarsToRmb*100)/100.0 << endl;
	}
	else
		cout << "Incorrect input" << endl;

	return 0;
}

3.29(几何:点的位置)给出从点p0(x0,y0)到p1(x1,y1)的有向线,可以使用下列情况来检测点p2(x2,y2)在线的左侧、右侧,还是在线上:

(x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)\left\{\begin{matrix} >0 &p2& is& on& the&left\\ =0&p2& is& on& the&line\\ <0& p2& is& on& the&right\end{matrix}\right.

编写程序,提示用户输入三个点p0、p1、p2,输出p2是否在从p0到p1的有向线段的左边、右边,或恰好在线上。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter three points for p0,p1,and p2:";
	double x0, x1, x2, y0, y1, y2;
	cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;

	double flag = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
	if (flag > 0)
		cout << "p2 is on the left side of the line" << endl;
	else if (flag == 0)
		cout << "p2 is on the same line" << endl;
	else
		cout << "p2 is on the right side of the line" << endl;

	return 0;
}

3.30(金融应用:比较开销)假设我们买了两袋不同的大米。编写一个程序来比较开销。程序提示用户输入重量以及每一袋的价格,输出价钱更好的一袋。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter weight and price for packge 1:";
	double weight1, price1;
	cin >> weight1 >> price1;

	cout << "Enter weight and price for packge 2:";
	double weight2, price2;
	cin >> weight2 >> price2;

	double packge1 = price1 / weight1;
	double packge2 = price2 / weight2;

	if (packge1 < packge2)
		cout << "Packge 1 has a better price." << endl;
	else if (packge1 == packge2)
		cout << "Two packages have the same price." << endl;
	else
		cout << "Packge 2 has a better price." << endl;

	return 0;
}

3.31(几何:点在线段上)程序设计练习3.29显示了怎样测试一个点是否在一条无界线上。重写程序设计3.29,使其测试一个点是否在一条线段上。编写程序,提示用户输入三个点p0、p1、p2以及p2是否在从p0到p1的线段上。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter three points for p0, p1, and p2:";
	double x0, x1, x2, y0, y1, y2;
	cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2;

	double flag = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
	if (flag == 0)
		if (x0 < x1)
			if (x0 < x2 && x2 < x1)
				cout << "("<<x2<<", "<<y2<<") is on the line segment from ("<<x0<<", "<<y0<<") to ("<<x1<<", "<<y1<<")" << endl;
			else
				cout << "(" << x2 << ", " << y2 << ") is not on the line segment from (" << x0 << ", " << y0 << ") to (" << x1 << ", " << y1 << ")" << endl;
		else
			if (x1 < x2 && x2 < x0)
				cout << "(" << x2 << ", " << y2 << ") is on the line segment from (" << x0 << ", " << y0 << ") to (" << x1 << ", " << y1 << ")" << endl;
			else
				cout << "(" << x2 << ", " << y2 << ") is not on the line segment from (" << x0 << ", " << y0 << ") to (" << x1 << ", " << y1 << ")" << endl;
	else
		cout << "("<<x2<<", "<<y2<<") is not on the line segment from (" << x0 << ", " << y0 << ") to (" << x1 << ", " << y1 << ")" << endl;

	return 0;
}

3.32(代数:斜率截距式)编写程序,提示用户输入两个点的坐标(x1,y1)与(x2,y2),输出线性方程的斜率截距式,即y=mx+b

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter the coordinates for two points:";
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;

	double m = (y2 - y1) / (x2 - x1);
	double b = y1 - m * x1;

	cout << "The line equation for two points (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is y = ";
	if (m == 1)
		cout << "x";
	else
		cout << m << "x";
	if (b > 0)
		cout << " + "<<b;
	if (b < 0)
		cout << " - " << -b;

	return 0;
}

3.33(科学技术:星期)蔡勒公式是由扎卡里亚斯发明的用来计算星期的算法。公式为

h=\left ( q+\frac{26(m+1)}{10} +k+\frac{k}{4}+\frac{j}{4}+5j\right )\%7

其中

·h为星期(0:星期六,1:星期日,2:星期一,3:星期二,4:星期三,5:星期四,6:星期五)。

·q是其中一个月的日期。

·m为月份(3:三月,4:四月,...,12:十二月)。一月和二月被认为是上一年的13和14月。

·j为世纪(即\frac{year}{100})。

·k为世纪中的年(即year%100)。

注意,公式中的除法执行的均为整数除法。编写程序,提示用户输入年、月以及月中的日期,输出这一天为星期几。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter year: (e.g., 2012):";
	int year;
	cin >> year;

	cout << "Enter month: 1-12:";
	int month;
	cin >> month;

	cout << "Enter the day of the month: 1-31:";
	int day;
	cin >> day;
	//一月和二月在公式中换算为13和14,将年转换为上一年
	if (month == 1 || month == 2)
	{
		month += 12;
		year -= 1;
	}

	int j = year / 100;//j为世纪
	int k = year % 100;//k为世纪中的年
	//泰勒公式
	int h = (day + 26 * (month + 1) / 10 + k + k / 4 + j / 4 + j * 5) % 7;
	//输出结果
	cout << "Day of the week is ";
	switch (h)
	{
	case 0:cout << "Saturday" << endl; break;
	case 1:cout << "Sunday" << endl; break;
	case 2:cout << "Monday" << endl; break;
	case 3:cout << "Tuesday" << endl; break;
	case 4:cout << "Wednesday" << endl; break;
	case 5:cout << "Thursday" << endl; break;
	case 6:cout << "Friday" << endl; break;
	}

	return 0;
}

3.34(随机点)编写程序,输出一矩形中的随机坐标。矩形以(0,0)为中心,宽为100,高为200.

#include <iostream>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
	srand(time(0));
	int x = rand() % 101 - 50;
	int y = rand() % 201 - 100;

	cout << "矩形中的随机坐标为:(" << x << ", " << y << ")" << endl;

	return 0;
}

3.35(商业:检查ISBN-10)一个ISBN-10(国际标准书号)包括10个数字:d1d2d3d4d5d6d7d8d9d10。最后一位数字d10为校验和,是由其他9个数字通过以下公式计算出来的:

(d_1*1+d_2*2+d_3*3+d_4*4+d_5*5+d_6*6+d_7*7+d_8*8+d_9*9)\%11

如果校验和为10,根据ISBN-10规定最后一位定义为X。编写程序,提示用户输入前9个数字,输出十个数字包括前导0 。程序需要将输入整体读入。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter the first 9 digits of an ISBN as integer:";
	int isbn_10;
	cin >> isbn_10;

	int digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8, digit9, digit10;
	digit9 = isbn_10 % 10;
	digit8 = (isbn_10 % 100) / 10;
	digit7 = (isbn_10 % 1000) / 100;
	digit6 = (isbn_10 % 10000) / 1000;
	digit5 = (isbn_10) % 100000 / 10000;
	digit4 = (isbn_10) % 1000000 / 100000;
	digit3 = (isbn_10) % 10000000 / 1000000;
	digit2 = (isbn_10) % 100000000 / 10000000;
	digit1 = (isbn_10) % 1000000000 / 100000000;

	digit10 = (digit1 + digit2 * 2 + digit3 * 3 + digit4 * 4 + digit5 * 5 + digit6 * 6 + digit7 * 7 + digit8 * 8 + digit9 * 9) % 11;
	cout << "The ISBN-10 number is ";
	if (digit1 == 0)//如果第一位为0,输出补位0
		cout << digit1 << isbn_10;
	else
		cout << isbn_10;
	if (digit10 == 10)//如果校验和为10,输出为X
		cout << "X" << endl;
	else
		cout << digit10 << endl;

	return 0;
}

3.36(回文数)编写程序,提示用户输入一个三位的整数,判断他是否是回文数。如果一个数从右往左与从左往右读都是一样的,则这个数叫做回文数。

#include <iostream>
using namespace std;

int main()
{
	cout << "Enter a three-digit integer:";
	int number;
	cin >> number;

	int digit1 = number % 10;//取出个位数
	int digit2 = number / 100;//取出百位数

	if (digit1 == digit2)//如果百位和个位相同则为回文数
		cout << number << " is a palindrome" << endl;
	else
		cout << number << " is not a palindrome" << endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40836442/article/details/106814688