编程计算图形的面积。程序可计算圆形、长方形、正方形的面积,运行时先提示用户选择图形的类型,然后,对圆形要求用户输入半径值,对长方形要求用户输入长和宽的值,对正方形要求用户输入边长的值,计算出面积的值后将其显示出来。C++的两种方式
时间: 2024-03-22 10:39:35 浏览: 67
采用面向对象的程序设计方法编写一个计算图形面积的类,程序应当能够计算并输出圆、矩形和三角形的面积。
5星 · 资源好评率100%
第一种方式:使用C++编写一个控制台程序,实现计算图形面积的功能。代码如下:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int choice;
float radius, length, width, side, area;
cout << "请选择图形的类型:" << endl;
cout << "1. 圆形" << endl;
cout << "2. 长方形" << endl;
cout << "3. 正方形" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "请输入圆的半径:";
cin >> radius;
area = M_PI * radius * radius;
cout << "圆的面积为:" << area << endl;
break;
case 2:
cout << "请输入长方形的长度:";
cin >> length;
cout << "请输入长方形的宽度:";
cin >> width;
area = length * width;
cout << "长方形的面积为:" << area << endl;
break;
case 3:
cout << "请输入正方形的边长:";
cin >> side;
area = side * side;
cout << "正方形的面积为:" << area << endl;
break;
default:
cout << "输入有误,请重新选择" << endl;
break;
}
return 0;
}
```
第二种方式:使用C++和Qt编写一个图形界面程序,实现计算图形面积的功能。代码如下:
```c++
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <cmath>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("图形面积计算器");
QGridLayout *layout = new QGridLayout(&window);
QLabel *shape_label = new QLabel("请选择图形的类型:", &window);
layout->addWidget(shape_label, 0, 0, 1, 2, Qt::AlignLeft | Qt::AlignTop);
QRadioButton *circle_radiobutton = new QRadioButton("圆形", &window);
layout->addWidget(circle_radiobutton, 1, 0);
QRadioButton *rectangle_radiobutton = new QRadioButton("长方形", &window);
layout->addWidget(rectangle_radiobutton, 2, 0);
QRadioButton *square_radiobutton = new QRadioButton("正方形", &window);
layout->addWidget(square_radiobutton, 3, 0);
QLabel *radius_label = new QLabel("请输入圆的半径:", &window);
layout->addWidget(radius_label, 4, 0);
QLineEdit *radius_edit = new QLineEdit(&window);
layout->addWidget(radius_edit, 4, 1);
QLabel *length_label = new QLabel("请输入长方形的长度:", &window);
layout->addWidget(length_label, 5, 0);
QLineEdit *length_edit = new QLineEdit(&window);
layout->addWidget(length_edit, 5, 1);
QLabel *width_label = new QLabel("请输入长方形的宽度:", &window);
layout->addWidget(width_label, 6, 0);
QLineEdit *width_edit = new QLineEdit(&window);
layout->addWidget(width_edit, 6, 1);
QLabel *side_label = new QLabel("请输入正方形的边长:", &window);
layout->addWidget(side_label, 7, 0);
QLineEdit *side_edit = new QLineEdit(&window);
layout->addWidget(side_edit, 7, 1);
QPushButton *calculate_button = new QPushButton("计算面积", &window);
layout->addWidget(calculate_button, 8, 0, 1, 2, Qt::AlignCenter);
QLabel *result_label = new QLabel("", &window);
layout->addWidget(result_label, 9, 0, 1, 2, Qt::AlignCenter);
QObject::connect(calculate_button, &QPushButton::clicked, [&](){
if (circle_radiobutton->isChecked())
{
float radius = radius_edit->text().toFloat();
float area = M_PI * radius * radius;
result_label->setText(QString("圆的面积为:%1").arg(area));
}
else if (rectangle_radiobutton->isChecked())
{
float length = length_edit->text().toFloat();
float width = width_edit->text().toFloat();
float area = length * width;
result_label->setText(QString("长方形的面积为:%1").arg(area));
}
else if (square_radiobutton->isChecked())
{
float side = side_edit->text().toFloat();
float area = side * side;
result_label->setText(QString("正方形的面积为:%1").arg(area));
}
else
{
result_label->setText("请选择图形的类型");
}
});
window.show();
return app.exec();
}
```
阅读全文