代码拉取完成,页面将自动刷新
#include <iostream>
#include <cmath>
#include <string>
double calculateArea(const std::string& shapeType = "rectangle", double param1 = 1.0, double param2 = 1.0, double param3 = 1.0);
double calculateCircleArea(double radius);
double calculateRectangleArea(double length, double width);
double calculateTriangleArea(double a, double b, double c);
int main() {
int choice;
std::cout << "请输入要计算的图形类型(1-圆形,2-矩形,3-三角形): ";
std::cin >> choice;
std::string shapeType;
double param1, param2, param3;
switch (choice) {
case 1:
shapeType = "circle";
std::cout << "请输入圆形的半径:";
std::cin >> param1;
param2 = 1.0;
param3 = 1.0;
break;
case 2:
shapeType = "rectangle";
std::cout << "请输入矩形的长和宽:";
std::cin >> param1 >> param2;
param3 = 1.0;
break;
case 3:
shapeType = "triangle";
std::cout << "请输入三角形的边长:";
std::cin >> param1 >> param2 >> param3;
break;
default:
std::cerr << "无效的选择!" << std::endl;
return 1;
}
double area = calculateArea(shapeType, param1, param2, param3);
std::cout << "该图形为" << shapeType << ",其面积为:" << area << std::endl;
return 0;
}
double calculateArea(const std::string& shapeType, double param1, double param2, double param3) {
if (shapeType == "circle") {
return calculateCircleArea(param1);
} else if (shapeType == "rectangle") {
return calculateRectangleArea(param1, param2);
} else if (shapeType == "triangle") {
return calculateTriangleArea(param1, param2, param3);
} else {
std::cerr << "未知的形状类型!" << std::endl;
return -1.0;
}
}
double calculateCircleArea(double radius) {
return 3.14159265358979323846 * radius * radius;
}
double calculateRectangleArea(double length, double width) {
return length * width;
}
double calculateTriangleArea(double a, double b, double c) {
double s = (a + b + c) / 2;
return std::sqrt(s * (s - a) * (s - b) * (s - c));
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。