计算图形面积和周长的界面设计c++
时间: 2023-07-30 13:06:26 浏览: 101
以下是一个简单的计算图形面积和周长的界面设计的C++代码示例:
```
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int choice;
double radius, length, width, base, height, side, area, perimeter;
cout << "1. Calculate the area and perimeter of a circle" << endl;
cout << "2. Calculate the area and perimeter of a rectangle" << endl;
cout << "3. Calculate the area and perimeter of a triangle" << endl;
cout << "Enter your choice (1-3): ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the radius of the circle: ";
cin >> radius;
area = M_PI * pow(radius, 2);
perimeter = 2 * M_PI * radius;
cout << "Area of the circle = " << area << endl;
cout << "Perimeter of the circle = " << perimeter << endl;
break;
case 2:
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
area = length * width;
perimeter = 2 * (length + width);
cout << "Area of the rectangle = " << area << endl;
cout << "Perimeter of the rectangle = " << perimeter << endl;
break;
case 3:
cout << "Enter the base and height of the triangle: ";
cin >> base >> height;
area = 0.5 * base * height;
cout << "Area of the triangle = " << area << endl;
cout << "Enter the length of the side of the triangle: ";
cin >> side;
perimeter = base + 2 * side;
cout << "Perimeter of the triangle = " << perimeter << endl;
break;
default:
cout << "Invalid choice!" << endl;
break;
}
return 0;
}
```
该代码使用了一个简单的菜单系统,允许用户选择要计算的图形类型。根据用户的选择,程序将要求用户输入必要的参数(例如,圆的半径,矩形的长度和宽度,三角形的底和高以及侧面长度),计算图形的面积和周长,并将结果输出到屏幕上。
阅读全文