2.1(将摄氏温度值转换为华氏温度值)编写程序,读入一个double型的摄氏温度值,将其转换为华氏温度值并显示结果。
#include <iostream> using namespace std; int main() { double celsius =
0.0;//摄氏温度值 cout << "请输入摄氏温度值:"; cin >> celsius; double fahrenheit =
0.0;//华氏温度值 fahrenheit = (9.0 / 5)*celsius + 32;//转换公式 cout << celsius <<
"摄氏度是" << fahrenheit << "华氏度"; return 0; }

2.2(计算一个圆柱体的体积)编写一个程序,读入一个圆柱体的半径和长度,用如下公式计算其面积和体积:
area = radius * radius * π;volume = area * length
#include <iostream> #include <cmath> using namespace std; int main() { const
double PI = 3.141592;//定义常量π double radius = 0.0, length = 0.0; cout << "Enter
the radius and length of a cylinder:"; cin >> radius >> length; double area =
0.0, volume = 0.0; area = pow(radius, 2)*PI;//计算圆柱面积公式 volume = area *
length;//计算圆柱体积公式 cout << "The area is:" << area << endl; cout << "The volume
is:" << volume << endl; return 0; }

2.3(将英尺转换成米)编写一个程序,读入一个以英尺为单位的长度值,将其转换为以米为单位的值,输出结果。1英尺等于0.305米。
#include <iostream> using namespace std; int main() { const double rate =
0.305; double feet = 0.0, meter = 0.0; cout << "Enter a value for feet:"; cin
>> feet; meter = rate * feet; cout << feet << " feet is " << meter << "
meters"; return 0; }

2.4(将磅转换为千克)编写程序,读入一个以磅为单位的重量值,转换为以千克为单位的值,输出结果。1磅等于0.454千克。
#include <iostream> using namespace std; int main() { const double rate =
0.454; double pound = 0.0, kilogram = 0.0; cout << "Enter a number in pounds:";
cin >> pound; kilogram = pound * rate; cout << pound << " pounds is " <<
kilogram << " kilograms"; return 0; }


2.5(金融应用:计算消费)编写程序,读入消费小计和小费费率,计算小费金额和消费总计。例如,用户输入消费小计为10,小费费率为15%,程序应输出小费金额为$1.5以及消费总计为$11.5。
#include <iostream> using namespace std; int main() { double subtotal = 0.0,
gratuityRate = 0.0; cout << "Enter the subtotal and a gratuity rate:"; cin >>
subtotal >> gratuityRate; double gratuity = 0.0, total = 0.0; gratuity =
gratuityRate / 100 * subtotal; total = gratuity + subtotal; cout << "The
gratuity is $" << gratuity << " and total is $" << total; return 0; }

2.6(讲一个整数中的所有数字相加)编写一个程序,读入一个0~1000范围内的整数,将此整数中的所有数字相加。例如,如果整数为932,则所有数字和为14。
#include <iostream> using namespace std; int main() { int number = 0,
sumOfDigits = 0; cout << "Enter a number between 0 and 1000:"; cin >> number;
sumOfDigits += number % 10;//提取个位数字 sumOfDigits += (number / 10) % 10;//提取十位数字
sumOfDigits += (number / 100) % 10;//提取百位数字 cout << "The sum of the digits is "
<< sumOfDigits; return 0; }

2.7(找出年数)编写程序,提示用户输入分钟数(如,10亿),输出所对应的年数和天数。为了简明,假定一年有365天。
#include <iostream> using namespace std; int main() { int numberOfMinutes = 0;
int numberOfDays = 0; int numberOfYears = 0; cout << "Enter the number of
minutes:"; cin >> numberOfMinutes; numberOfDays = numberOfMinutes / 60 /
24;//计算总共有多少天 numberOfYears = numberOfDays / 365;//用天数计算有多少年 numberOfDays %=
365;//总天数对年取余就是剩余的天数 cout << numberOfMinutes << " minutes is approximately " <<
numberOfYears << " years and " << numberOfDays << " days"; return 0; }


2.8(当前时间)程序清单2-9,ShowCurrentTime.cpp,给出一个程序,显示当前的格林尼治时间。修改程序使它提示用户输入与格林尼治时间相差的时区,输出待定时区的时间。
/*本题在跨天时会出现bug,如GMT为1:0:0,差-5个时区 则当前时间为-4:0:0,但根据题意和所学难度,此程序为 该时期的正确答案*/
#include <iostream> #include <ctime> using namespace std; int main() { int
totalSecond = time(0); int currentSecond = totalSecond % 60; int currentMinute
= (totalSecond / 60) % 60; int currentHour = (totalSecond / 60 / 60) % 24; int
timeZone = 0;//一个时区相处一小时 cout << "Enter the time zone offset to GMT:"; cin >>
timeZone; cout << "The current time is " << currentHour+timeZone << ":" <<
currentMinute << ":" << currentSecond; return 0; }

2.9(物理:加速度)平均加速度被定义为速率的变化除以时间,如下列公式所示:a =
(v1-v0)/t,编写程序,提示用户输入以米/秒为单位的初速度v0,以米/秒为单位的末速度v1,以秒为单位花费时间t,输出平均加速度。
#include <iostream> using namespace std; int main() { double v0 = 0, v1 = 0, t
= 0; cout << "Enter v0,v1 and t:"; cin >> v0 >> v1 >> t; double a = (v1 - v0) /
t; cout << "The average acceleration is " << a; return 0; }

2.10(科学技术:计算能量)编写程序,计算将水从初始温度加热到末温度所需的能量。程序提示用户输入以千克为单位的水量、初始水温以及末温度。计算公式为:Q
= M * (finalTemperature - initialTemperature)*
4184其中M为单位的水的质量,温度则以摄氏度为单位,能量Q的单位是焦耳。
#include <iostream> using namespace std; int main() { const double RATE =
4184; double amountOfWater = 0; double initialTemperature = 0, finalTemperature
= 0; double energy = 0; cout << "Enter the amount of water in kilograms:"; cin
>> amountOfWater; cout << "Enter the initial temperature:"; cin >>
initialTemperature; cout << "Enter the final temperature:"; cin >>
finalTemperature; energy = amountOfWater * (finalTemperature -
initialTemperature)*RATE; cout << "The energy needed is " << energy << "J";
return 0; }

2.11(人口估算)重新编写程序设计练习1.11的程序,提示用户输入年数,输出这么多年后的人口数。使用程序设计练习1.11中的提示。
#include <iostream> using namespace std; int main() { int numberOfYears = 0;
cout << "Enter the number of years:"; cin >> numberOfYears; int totalSecond =
numberOfYears * 365 * 24 * 60 * 60; cout << "The population in " <<
numberOfYears << " years is " << 312032486 + totalSecond / 7 + totalSecond / 45
- totalSecond / 13; return 0; }

2.12(物理:计算跑道长度)给出飞机的加速度a以及起飞速度v,可以用下面的公式计算出飞机起飞需要的最短跑道长度:length =
/2a,编写程序,用户输入以米/秒(m/s)为单位的速度v以及以米/秒的平方(m/)为单位的加速度a,输出最短跑道长度。
#include <iostream> #include <cmath> using namespace std; int main() { double
v = 0,a = 0, length = 0; cout << "Enter speed and acceleration:"; cin >> v >>
a; length = pow(v, 2) / 2 / a; cout << "The minimum runway length for this
airplance is " << length<<"m"; return 0; }

2.13(金融应用:计算零存整数价值)假定用户每月向一个储蓄账号中存100美元,年利率为5%。那么,月利率为0.05 / 12 =
0.00417。第一个月后,账面金额变为
100 * (1 + 0.00417) = 100.417
第二个月后,账面金额变为
(100 + 100.417) * (1 + 0.00417) = 201.252
第三个月后,账面金额变为
(100 + 201.252) * (1 + 0.00417) = 302.507
以此类推。
编写程序,提示用户输入每月储蓄的金额,输出6个月后的账面金额。(在程序设计练习5.32中,会使用循环来简化本题的代码,并扩展为计算机任意个月后的账面金额。)
#include <iostream> using namespace std; int main() { const double YEARRATE =
0.05; double monthRate = YEARRATE / 12; double monthlySavingAmount = 0; cout <<
"Enter the monthly saving amount:"; cin >> monthlySavingAmount; double
accountValue = 0; accountValue = monthlySavingAmount * (1 + monthRate);//first
month accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);//第二个月 accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);//第三个月 accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);//第四个月 accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);//第五个月 accountValue = (monthlySavingAmount + accountValue)*(1 +
monthRate);//第六个月 cout << "After the sixth month, the account value is $" <<
accountValue; return 0; }


2.14(健康应用:BMI)身体质量指数(BMI)在体重方面衡量健康。用以千克为单位的重量除以以米为单位的身高的平方来计算。编写程序,提示用户输入以磅为单位的体重以及以英尺为单位的身高,输出BMI。注意,1磅等于0.45359237千克,1英尺为0.0254米。
#include <iostream> using namespace std; int main() { const double
KILOGRAMRATE = 0.45359237, METERRATE = 0.0254; double pound = 0, inche = 0;
cout << "Enter weight in pounds:"; cin >> pound; cout << "Enter height in
inches:"; cin >> inche; double BMI = 0.0; BMI = pound * KILOGRAMRATE /
(inche*METERRATE) / (inche*METERRATE); cout << "BMI is " << BMI; return 0; }

2.15(几何学:两点之间的距离)编写程序,提示用户输入两个点(x1,y1),(x2,y2),输出他们之间的距离。计算距离的公式为
注意可以使用pow(a,0.5)来计算.
#include <iostream> #include <cmath> using namespace std; int main() { double
x1 = 0, x2 = 0, y1 = 0, y2 = 0; cout << "Enter x1 and y1:"; cin >> x1 >> y1;
cout << "Enter x2 and y2:"; cin >> x2 >> y2; double distance = 0; distance =
pow(pow(x2 - x1, 2) + pow(y2 - y1, 2), 0.5); cout << "The distance between the
two points is " << distance; return 0; }

2.16(几何:六边形的面积)编写程序,提示用户输入六边形的边,输出它的面积。计算六边形面积的公式为Area = 。其中s是边的长度。
#include <iostream> #include <cmath> using namespace std; int main() { double
side = 0; cout << "Enter the side:"; cin >> side; double area =
1.5*sqrt(3.0)*side*side; cout << "The atea of the hexagon is " << area; return
0; }


2.17(科学技术:风冷温度)外面到底有多冷?单单的温度不足够提供答案。其他因素包括风速、相对湿度以及光照在决定户外寒冷程度上都起了重要作用。2001年,国际气候协会(NWS)履行了新的风冷温度,使用温度和风速来测量寒冷程度,公式为:

其中是以华氏度为单位的户外温度,v是以英里每小时(mph)为单位的速度。
为风冷温度。公式在风速低于2mph或者温度低于-58华氏度或高于41华氏度时不能使用。
编写程序,提示用户输入在-58华氏度及41华氏度之间的温度,大于或等于2mph的风速,输出风冷温度。使用pow(a,b)来计算。
#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; 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; }

2.18(输出一个表)编写程序,输出下面的列表:

abpow(a,b)
121
238
3481
451024
5615625 #include<iostream> #include <cmath> using namespace std; int main() {
int a = 1, b = 2; cout << "a" << " " << "b" << " " << "pow(a,b)" << endl; cout
<< a << " " << b << " " << pow(a, b) << endl; a++, b++; cout << a << " " << b
<< " " << pow(a, b) << endl; a++, b++; cout << a << " " << b << " " << pow(a,
b) << endl; a++, b++; cout << a << " " << b << " " << pow(a, b) << endl; a++,
b++; cout << a << " " << b << " " << pow(a, b) << endl; return 0; }
2.19(几何:三角形的面积)编写程序,提示用户输入三角形的三个顶点(x1,y1),(x2,y2),(x3,y3),输出它的面积。计算三角形的公式为

s = (side1 + side3 + side3)/2

area = 
#include <iostream> #include <cmath> using namespace std; int main() { double
x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0; cout << "Enter three points for
a triangle;"; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; double side1 =
pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5); double side2 = pow(pow(x1 - x3, 2)
+ pow(y1 - y3, 2), 0.5); double side3 = pow(pow(x3 - x2, 2) + pow(y3 - y2, 2),
0.5); double s = (side1 + side2 + side3) / 2; double area = pow(s * (s - side1)
* (s - side2) * (s - side3), 0.5); cout << "The area of the triangle is " <<
area; return 0; }
2.20(线的斜率)编写程序,提示用户输入两个点的坐标(x1,y1)和(x2,y2),输出连接两点的线的斜率。斜率公式为(y2-y1)/(x2-x1)。
#include <iostream> using namespace std; int main() { double x1 = 0, x2 = 0,
y1 = 0, y2 = 0; cout << "Enter the coordinates for two points:"; cin >> x1 >>
y1 >> x2 >> y2; double k = (y2 - y1) / (x2 - x1); cout << "The slope for the
line that connects two points " << "(" << x1 << "," << y1 << ") and (" << x2 <<
"," << y2 << ") is " << k; return 0; }
2.21(驾车费用)编写程序,提示用户输入驾驶距离、以公里每加仑为单位的汽车耗油效率、每加仑的价格,输出这趟路程的花费。
#include <iostream> using namespace std; int main() { double distance = 0,
milesPerGallon = 0, pricePerGallon = 0; cout << "Eter the driving distance:";
cin >> distance; cout << "Enter miles per gallon:"; cin >> milesPerGallon; cout
<< "Enter price per gallon:"; cin >> pricePerGallon; double costOfDriving =
distance / milesPerGallon * pricePerGallon; costOfDriving =
((int)(costOfDriving * 100)) / 100.0;//使结果保留两位小数,即精确到美分 cout << "The cost of
driving is $" << costOfDriving; return 0; }
2.22(金融应用:计算利息)已知余款和年利率,可计算出下个月的支付利息,用下面的公式:interest = balance *
(annualIntersRate / 1200)。编写程序,读余额及年利率,输出下个月的利息。
#include <iostream> using namespace std; int main() { double interest = 0,
balance = 0, annualInterestRate = 0; cout << "Enter balance and interest rate
(e.g.,3 for 3%):"; cin >> balance >> annualInterestRate; interest = balance *
(annualInterestRate / 1200); cout << "The interest is " << interest; return 0; }
2.23(金融应用:未来投资价值)编写程序,读入投资数额、年利率、年数,输出未来投资价值,使用下面的公式:

#include <iostream> #include <cmath> using namespace std; int main() { double
investmentAmount = 0, monthlyInterestRate = 0, numberOfYears = 0; double
annualInterestRate = 0; cout << "Enter investment amount:"; cin >>
investmentAmount; cout << "Enter annual interest rate in percentage:"; cin >>
annualInterestRate; cout << "Enter number of years:"; cin >> numberOfYears;
monthlyInterestRate = annualInterestRate / 1200;//计算月利率 double
futureInvestmentValue = 0; futureInvestmentValue = investmentAmount * pow(1 +
monthlyInterestRate, numberOfYears * 12); cout << "Accumulated value is $" <<
futureInvestmentValue; return 0; }

2.24(金融应用:货币单元)重新编写程序清单2-12,ComputeChange.cpp,修复将一个float值转换为int值时可能产生的精度丢失。输入后两个数字代表美分的整数。例如输入1156代表11美元和56美分。
#include <iostream> using namespace std; int main() { unsigned amount = 0;
cout << "Enter an amount in int,for example 1156 is 11 dollars and 56
pennies:"; cin >> amount; unsigned remainingAmount = amount; unsigned
numberOfOneDollars = remainingAmount / 100;//一美元数量 remainingAmount =
remainingAmount % 100; unsigned numberOfQuarters = remainingAmount /
25;//二角五分数量 remainingAmount = remainingAmount % 25; unsigned numberOfDimes =
remainingAmount / 10;//一角数量 remainingAmount %= 10; unsigned numberOfNickels =
remainingAmount / 5;//五美分数量 remainingAmount %= 5; unsigned numberOfPennies =
remainingAmount;//一美分数量 cout << "Your amount " << amount/100 <<" dollars and
"<<amount%100<<" pennies"<< " consists of " << endl << " " <<
numberOfOneDollars << " dollars" << endl << " " << numberOfQuarters << "
quarters" << endl << " " << numberOfDimes << " dimes" << endl << " " <<
numberOfNickels << " nickels" << endl << " " << numberOfPennies << " pennies"
<< endl; return 0; }
 

技术
友情链接
码工具
Toolsou
API参考文档
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信