4.1(几何:五边形面积)编写程序,提示用户输入从五边形的中心到边的距离r,计算五边形的面积。计算五边形面积的公式为:
,其中s为边长。边长可用来计算,其中r为从五边形中心到边的距离。精确到小数点后两位。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
const double PI = 3.1416;//定义PI为常量
//请求用户输入五边形中心到边的距离
cout << "Enter the length from the center to a vertex:";
double r = 0.0;
cin >> r;
double s = 2 * r * sin(PI / 5);//根据r计算五边形边长s
double area = 5 * pow(s, 2) / 4 / tan(PI / 5);//计算面积
cout << "The area of the pentagon is " <<fixed<<setprecision(2)<< area;//格式化输出,保留两位小数
return 0;
}
4.2(几何:大圆距离)大圆距离为一球体表面两个点之间的距离。让(x1,y1)与(x2,y2)为两点的地理纬度与经度。两点间的大圆距离可由下列公式计算:
编写程序,提示用户输入以度为单位的地球上的两个点的经度和维度,输出大圆距离。平均地球半径为6378.1km。公式中的维度和经度为北纬和西经。因此用负数代表南纬和东经。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
const double RADIUS = 6378.1;//平均地球半径
const double PI = 3.1416;//定义π
//输入第一个点的经纬度
cout << "Enter point 1(latitude and longitude) in degrees:" << endl;
double x1 = 0.0, y1 = 0.0;
cin >> x1 >> y1;
//输入第二个点的经纬度
cout << "Enter point 2(latitude and longitude) in degrees:" << endl;
double x2 = 0.0, y2 = 0.0;
cin >> x2 >> y2;
//计算两点的距离
double distance = RADIUS * acos(sin(x1 / 180 * PI) * sin(x2 / 180 * PI) + cos(x1 / 180 * PI) * cos(x2 / 180 * PI) * cos((y1 - y2) / 180 * PI));
cout << "The distance between the two points is " << fixed << distance;
return 0;
}
4.3(几何:估算面积)从www.gps-data-team.com/map/中为亚特兰大,佐治亚州;奥兰多,弗洛里达州;萨凡纳,佐治亚州;以及夏洛特,北卡罗来纳州找出GPS位置,计算4个城市范围之内的结算面积。(提示:使用程序设计练习4.2中的公式计算两个城市之间的距离。将多边形拆分为两个三角形,用程序设计练习2.19中的公式计算三角形的面积)。
亚特兰大:北纬33°46’西经84°25’;奥兰多:北纬28°32′18″, 西经82°37′15″;萨凡纳:北纬32°5′1″, 西经82°54′1″;夏洛特:北纬35°13′38″, 西经81°9′25″
4.4(几何:六边形面积)六边形(hexagon)面积可用下列公式计算(s为边长):。编写程序,提示用户输入六边形的边长,输出它的面积。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
const double PI = 3.1416;
cout << "Enter the side:";
double side = 0.0;
cin >> side;
double area = 0.0;
area = side * side * 6 / 4 / tan(PI / 6);
cout << "The area of the hexagon is " << fixed << setprecision(2) << area << endl;
return 0;
}
4.5(几何:正多边形的面积)正多边形为一个所有边长相同,所有角的大小相同的n条边的多边形(即多边形既为等边的又为等角的)。计算正多边形面积的公式为
,其中s为边长。编写程序,提示用户输入边数以及正多边形的边长,输出它的面积。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
const double PI = 3.1416;
//获取正多边形边的数量
cout << "Enter the number of sides:";
int sideNumber = 0;
cin >> sideNumber;
//获取边的长度
cout << "Enter the side:";
double side = 0;
cin >> side;
//计算面积
double area = 0.0;
area = sideNumber * side * side / 4 / tan(PI / sideNumber);
//格式化输出,保留两位小数
cout << "The area of the polygon is " << fixed << setprecision(2) << area << endl;
return 0;
}
4.6(圆上的随机点)编写程序,生成以(0,0)为圆心,半径为40的圆上的3个随机点,显示由这三个点形成的三角形的3个内角度数。(提示:随机生成一个弧度在0~2π之间的角度α,由这个角决定的点为(r*cos(α),r*sin(α)))。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <ctime>
using namespace std;
int main()
{
const double PI = 3.1416;
const double R = 40;
srand(time(0));
double x1 = 0.0, x2 = 0.0, x3 = 0.0, y1 = 0.0,
y2 = 0.0, y3 = 0.0, a1 = 0.0, a2 = 0.0, a3 = 0.0;
//随机生成一个弧度在0~2π之间的角度α,由这个角决定的点为(r*cos(α),r*sin(α)))
a1 = rand() * 1.0 / RAND_MAX * PI * 2;
a2 = rand() * 1.0 / RAND_MAX * PI * 2;
a3 = rand() * 1.0 / RAND_MAX * PI * 2;
//确定三个点的坐标
x1 = R * cos(a1), y1 = R * sin(a1);
x2 = R * cos(a2), y2 = R * sin(a2);
x3 = R * cos(a3), y3 = R * sin(a3);
double a, b, c;//定义三角形的三条边
//计算三条边的长度
a = pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5);
b = pow(pow(x1 - x3, 2) + pow(y1 - y3, 2), 0.5);
c = pow(pow(x2 - x3, 2) + pow(y2 - y3, 2), 0.5);
double innerAngle1, innerAngle2, innerAngle3;//定义三个内角
//计算三个内角值
innerAngle1 = acos((a * a - b * b - c * c) / (-2 * b * c));
innerAngle2 = acos((b * b - a * a - c * c) / (-2 * a * c));
innerAngle3 = acos((c * c - b * b - a * a) / (-2 * a * b));
cout << "三角形的三个内角度数为:" << innerAngle1/PI*180 << "° , "
<< innerAngle2/PI*180 << "° , " << innerAngle3/PI*180 <<"°"<< endl;
system("pause");
return 0;
}
4.7(拐角点坐标)假设一五边形以(0,0)为中心,一个点在0点位置。编写程序,提示用户输入五边形的外接圆的半径,输出该五边形的5个拐角点的坐标。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const double PI(3.1416);
//输入五边形外接圆的半径
cout << "Enter the radius of the bounding circle: ";
double boundingCircleRadius = 0.0;
cin >> boundingCircleRadius;
//输出坐标
cout << "The coordinates of five points on the pentagon are" << endl;
//输出第一个点的坐标
cout << "(0" << "," << boundingCircleRadius << ")" << endl;
//输出第二~五个点的坐标
cout << "(" << boundingCircleRadius * cos(PI / 2 + PI * 2 / 5) << "," << boundingCircleRadius * sin(PI / 2 + PI * 2 / 5) << ")" << endl;
cout << "(" << boundingCircleRadius * cos(PI / 2 + PI * 4 / 5) << "," << boundingCircleRadius * sin(PI / 2 + PI * 4 / 5) << ")" << endl;
cout << "(" << boundingCircleRadius * cos(PI / 2 + PI * 6 / 5) << "," << boundingCircleRadius * sin(PI / 2 + PI * 6 / 5) << ")" << endl;
cout << "(" << boundingCircleRadius * cos(PI / 2 + PI * 8 / 5) << "," << boundingCircleRadius * sin(PI / 2 + PI * 8 / 5) << ")" << endl;
return 0;
}
4.8(找出ASCII码对应的字符)编写程序,接收ASCII码(0~127之间的整数),输出它的字符。
#include <iostream>
using namespace std;
int main()
{
cout << "Enter an ASCII code:";
int number;
cin >> number;
if (number < 0 || number>127)
cout << "Error";
else
cout << "The character is "<<static_cast<char>(number) << endl;
return 0;
}
4.9(找出字符对应的ASCII码)编写程序,接收字符,输出它的ASCII码。
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a character : ";
char c;
cin >> c;
cout << "The ASCII code for the character is " << static_cast<int>(c) << endl;
return 0;
}
4.10(元音还是辅音?)假设字母A/a、E/e、I/i、O/o、U/u为元音。编写程序,提示用户输入字母,判断字母是元音还是辅音。
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a letter: ";
char letter;
cin >> letter;
if ('a' <= letter && 'z' >= letter || 'A' <= letter && 'Z' >= letter)//判断输出字符是否合法
{//输入确实是字母
switch (letter)
{//当输入字母为元音时,打印
case 'a':case 'e':case 'i':case 'o':case 'u':
case 'A':case 'E':case 'I':case 'O':case 'U':
cout << letter << " is a vowel" << endl;
break;
//其他非元音情况情况
default:cout << letter << " is a consonant" << endl;
break;
}
}
else//输入不是字母
cout << letter << " is an invalid input" << endl;
return 0;
}
4.11(将大写字母转换为小写字母)编写程序,提示用户输入1个大写字母,将其转换为小写字母。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter an uppercase letter: ";
char letter;
cin >> letter;
if (isupper(letter) && isalpha(letter))//判断输入字符确实是字母且确实是大写字母
cout << static_cast<char>(letter - 'A' + 'a') << endl;
else
cout << letter << " is an invalid input" << endl;
return 0;
}
4.12(将字母等级转换为数字)编写程序,提示用户输入字母A/a、B/b、C/c、D/d、F/f,输出其相应的数字值4/3/2/1/0。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
//输入字母
cout << "Enter a letter grade: ";
char letter;
cin >> letter;
int value = -1;//字母对应的数字值
if (isalpha(letter))//判断输入是否为字母
{
switch (letter)
{
case 'A':case 'a':value = 4; break;//字母为A、a时数字值为4
case 'B':case 'b':value = 3; break;
case 'C':case 'c':value = 2; break;
case 'D':case 'd':value = 1; break;
case 'F':case 'f':value = 0; break;
default:value = -1;break;//输入字母非法时,数字值为-1
}
if (value == -1)//如果数字值为-1,输出输入为非法值
cout << letter << " is an invalid input";
else//输入合法时输出相应等级
cout << "The numeric value for grade " << letter << " is " << value;
}
else//输入不是字母时,输出输入为非法值
cout << letter << " is an invalid input";
return 0;
}
4.13(十六进制到二进制)编写程序,提示用 户输入一个十六进制的数字,输出它对应的二进制数字。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
//提示用户输入一个十六进制整数
cout << "Enter a hex digit: ";
char digit;//因为输入有数字有字符所以用char类型
cin >> digit;
if (isalnum(digit))//判断输入是否为数字或字符,是则进行下列运算
{
switch (digit)
{
case '1':cout << "The binary value is " << 0001; break;
case '2':cout << "The binary value is " << 0010; break;
case '3':cout << "The binary value is " << 0011; break;
case '4':cout << "The binary value is " << 0100; break;
case '5':cout << "The binary value is " << 0101; break;
case '6':cout << "The binary value is " << 0110; break;
case '7':cout << "The binary value is " << 0111; break;
case '8':cout << "The binary value is " << 1000; break;
case '9':cout << "The binary value is " << 1001; break;
case 'A':case 'a':cout << "The binary value is " << 1010; break;
case 'B':case 'b':cout << "The binary value is " << 1011; break;
case 'C':case 'c':cout << "The binary value is " << 1100; break;
case 'D':case 'd':cout << "The binary value is " << 1101; break;
case 'E':case 'e':cout << "The binary value is " << 1110; break;
case 'F':case 'f':cout << "The binary value is " << 1111; break;
default:cout << digit << " is an invalid input"; break;
}
}
else//如果输入不是字符或数字,输出错误提示
cout << digit << " is an invalid input";
return 0;
}
4.14(将十进制转换为十六进制)编写程序,提示用户输入0~15之间的整数,输出相应的十六进制数。
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a decimal value (0 to 15): ";
int value;
cin >> value;
//判断输入是否在0-15范围内
if (value >= 0 && value <= 15)
{
cout << "The hex value is ";
if (value < 10)//如果输入小于10直接输出
cout << value;
else//如果输入大于10输出字母
cout << static_cast<char>(value - 10 + 'a');
}
else
cout << value << " is an invalid input";
return 0;
}
4.15(电话键面板)电话上匹配的国际标准字母/数字由下图所示:
编写程序,提示用户输入一个字母,输出它对应的数字。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter a letter : ";
char letter;
cin >> letter;
if (isalpha(letter))//判断输入是否为字母
{
switch (letter)
{
case 'a':case 'b':case 'c':case 'A':case 'B':case 'C':
cout << "The corresponding number is 2" << endl; break;
case 'd':case 'e':case 'f':case 'D':case 'E':case 'F':
cout << "The corresponding number is 3" << endl; break;
case 'g': case 'h':case 'i':case 'G':case 'H':case 'I':
cout << "The corresponding number is 4" << endl; break;
case 'j':case 'k':case 'l':case 'J':case 'K':case 'L':
cout << "The corresponding number is 5" << endl; break;
case 'm':case 'n':case 'o':case 'M':case 'N':case 'O':
cout << "The corresponding number is 6" << endl; break;
case 'p':case 'q':case 'r':case 's':case 'P':case 'Q':case 'R':case 'S':
cout << "The corresponding number is 7" << endl; break;
case 't':case 'u':case 'v':case 'T':case 'U':case 'V':
cout << "The corresponding number is 8" << endl; break;
case 'w':case 'x':case 'y':case 'z':case 'W':case 'X':case 'Y':case 'Z':
cout << "The corresponding number is 9" << endl; break;
default:cout << letter << " is an invalid input"; break;
}
}
else//输入为非字母,输出错误提示
cout << letter << " is an invalid input" << endl;
return 0;
}
4.16(处理一个字符串)编写程序,提示用户输入一个字符串,输出它的长度以及它的第一个字符。
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a string:";
string str;
getline(cin, str);
cout << "The length of the string is " << str.length() << endl;
if (str.length() != 0)//如果输入字符串不为空,输出第一个字符
cout << "The first of the string is " << str[0] << endl;
return 0;
}
4.17(商业:检验ISBN-10)重写程序设计练习3.35的程序,将ISBN数字输入为字符串。
#include <iostream>
#include <string>
using namespace std;
int main()
{
//输入ISBN的前九位
cout << "Enter the first 9 digits of an ISBN an integer: ";
string ISBN_9;
cin >> ISBN_9;
int d10 = 0;
//用循环进行公示中的加法运算
for (int i = 0; i < 9; i++)
d10 += (ISBN_9[i] - '0') * (i + 1);
d10 %= 11;//公式中的最后一步取余运算
//首先输出前九位
cout << "The ISBN-10 number is " << ISBN_9;
if (d10 == 10)//如果第十位是10则输出X
cout << 'X';
else//不是10,直接打印数字
cout << d10;
return 0;
}
4.18(随机字符串)编写程序,随机生成一个带有3个大写字母的字符串。
#include <iostream>
#include <string>
#include <ctime>
#include <cstdio>
using namespace std;
int main()
{
srand(time(0));//设置rand函数的种子
string str;
str += (char)('A' + rand() % 25);//生成第一个大写字母
str += (char)('A' + rand() % 25);//生成第二个大写字母
str += (char)('A' + rand() % 25);//生成第三个大写字母
cout << "The random string is " << str;
return 0;
}
4.19(排列三个城市)编写程序,提示用户输入3个城市,输出它们的升序排列。
#include <iostream>
#include <string>
using namespace std;
int main()
{
//输入第一个城市名称
cout << "Enter the first city:";
string firCity;
getline(cin, firCity);
//输入第二个城市名称
cout << "Enter the second city:";
string senCity;
getline(cin, senCity);
//输入第三个城市名称
cout << "Enter the third city:";
string thiCity;
getline(cin, thiCity);
//输出城市排列提示信息
cout << "The three cities in alphabetcal order are ";
if (firCity <= senCity)//升序排列
{
if (firCity <= thiCity)
{
if (senCity <= thiCity)
cout << firCity << " " << senCity << " " << thiCity;
else
cout << firCity << " " << thiCity << " " << senCity;
}
else
cout << thiCity << " " << firCity << " " << senCity;
}
else
{
if (senCity <= thiCity)
{
if (firCity <= thiCity)
cout << senCity << " " << firCity << " " << thiCity;
else
cout << senCity << " " << thiCity << " " << firCity;
}
else
cout << thiCity << " " << senCity << " " << firCity;
}
return 0;
}
4.20(一个月的天数)编写程序,提示用户输入年份以及月份名称的前3个字母(第一个字母大写),输出此月的天数。
#include <iostream>
#include <string>
using namespace std;
int main()
{
//提示用户输入年份
cout << "Enter a year:";
int year;
cin >> year;
//提示用户输入月份
cout << "Enter a month:";
string month;
cin >> month;
//1,3,5,7,8,10,12月为31天
if (month == "Jan" || month == "Mar" || month == "May" || month == "Jul" || month == "Aug" || month == "Oct" || month == "Dec")
cout << month << " " << year << " has " << 31 << " days";
//4,6,8,11月为30天
else if (month == "Apr" || month == "Jun" || month == "Sep" || month == "Nov")
cout << month << " " << year << " has " << 30 << " days";
//2月时对年份进行判断是闰年还是平年
else if (month == "Feb")
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
cout << month << " " << year << " has " << 29 << " days";
else
cout << month << " " << year << " has " << 28 << " days";
else//所以情况都不符合输出错误提示信息
cout << month << " is not a correct month name";
return 0;
}
4.21(学生专业和年纪)编写程序,提示用户输入两个字符,输出字符代表的专业以及年级。第一个字符代表专业,第二个为数字字符1/2/3/4,代表一个学生是大一、大二、大三还是大四。假设下列字符用来定义专业:
M:Mathematics(数学);C:Computer Science(计算机科学技术);I:Information Technology(信息技术)
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
//提示用户输入两个字符
cout << "Enter two characters:";
string str;
cin >> str;
//输出字符代表的专业
switch (str[0])
{
case 'M':cout << "Mathematics "; break;
case 'C':cout << "Computer Science "; break;
case 'I':cout << "Information Technology "; break;
default:cout << "Invalid major code"; break;
}
//输出字符代表的年级
switch (str[1])
{
case '1':cout << "Freshman"; break;
case '2':cout << "Sophomore"; break;
case '3':cout << "Junior"; break;
case '4':cout << "Senior"; break;
default:cout << "Invalid status code"; break;
}
return 0;
}
4.22(金融应用:工资单)编写程序,读取下列信息,输出工资表:
员工姓名(如Smith)
一周内的工作时间(如10)
小时工作率(如9.75)
联邦税收扣缴率(如9%)
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//输入员工姓名
cout << "Enter employee,s name:";
string name;
getline(cin, name);
//输入每周工作时长
cout << "Enter number of hours worked in a week:";
double hourWork;
cin >> hourWork;
//输入时薪
cout << "Enter hourly pay rate:";
double hourlyPayRate;
cin >> hourlyPayRate;
//输入联邦税率
cout << "Enter federal tax withholding rate:";
double federalTaxWithholdingRate;
cin >> federalTaxWithholdingRate;
//输入州税率
cout << "Enter state tax withholding rate:";
double stateTaxWithholdingRate;
cin >> stateTaxWithholdingRate;
double grossPay = hourlyPayRate * hourWork;//总收入
double federalWithholding = federalTaxWithholdingRate * grossPay;//联邦税收
double stateWithholding = stateTaxWithholdingRate * grossPay;//州税收
double totalDedutions = federalWithholding + stateWithholding;//总税收
double netPay = grossPay - totalDedutions;//实际收入
cout<< fixed << setprecision(2)//强输出小数点,结果保留两位小数
<< "Enployee Name: " << name << endl
<< "Hours Worked: " << hourWork << endl
<< "Pay Rate: $" << hourlyPayRate << endl
<< "Gross Pay: $" << grossPay << endl
<< "Deductions:" << endl
<< "\tFederal Withholding (" << federalTaxWithholdingRate * 100.0 << "%): $" << fixed << setprecision(2) << federalWithholding << endl
<< "\tState Withholding (" << stateTaxWithholdingRate * 100.0 << "%): $" << fixed << setprecision(2) << stateWithholding << endl
<< "\tTotal Deduction: $" << totalDedutions << endl
<< "Net Pay: $" << netPay;
return 0;
}
4.23(检验SSN)编写程序,提示用户输入ddd-dd-dddd形式的社会保障号码,其中d为一个数字。程序应该检查输入是否有效。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter a SSN:";
string ssn;
cin >> ssn;
if (isdigit(ssn[0]))
if (isdigit(ssn[1]))
if (isdigit(ssn[2]))
if (ssn[3] == '-')
if (isdigit(ssn[4]))
if (isdigit(ssn[5]))
if (ssn[6] == '-')
if (isdigit(ssn[7]))
if (isdigit(ssn[8]))
if (isdigit(ssn[9]))
cout << ssn << " is a valid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
else
cout << ssn << " is an invalid social security number";
return 0;
}