常见图形,圆形、长方形和正方形面积的计算

编程计算图形的面积:
程序可计算圆形、长方形、正方形的面积,运行时先提示用户选择图形的类型,
然后,对圆形要求用户输入半径值,对长方形要求用户输入长和宽的值,
对正方形要求用户输入边长的值,计算出面积的值后将其显示出来。

#include<iostream>
using namespace std;

//定义符号常量
const float PI = 3.14;
//float const PI = 3.14;

//主函数
int main()
{
    //定义变量
    int iType;
    float a,b,radius,area;
    //输出提示信息
    cout << "please select a type(0-circular 1-rectangle 2-sequare):";
    //输入变量值
    cin >> iType;
    //利用switch语句进行判断和计算
    switch (iType)
    {
    case 0:
    cout << "please input the radius of circular :";
    cin >> radius; 
    area = PI * radius * radius;
    cout << "PI is " << PI <<endl;
    cout << "The area of circular is " << area <<"!" << endl;
    break ;

    case 1:
    cout << "please input the length and width of rectangle :" ;
    cin >>a >> b;
    area = a * b;
    cout << "The area of rectangle is " << area <<"!" <<endl;
    break;

    case 2:
    cout << "please input the width of sequare:";
    cin >> a;
    area = a*a;
    cout << "The area of sequare is " << area << "!" << endl;
    break;
    default:
        cout << "please input again!";
        break;
    }
    
    return 0;
}

在计算圆形面积的时候,会出现如下图中问题:

图中在左侧的变量监控中radius会显示出一长串小数点,而在控制台的结果输出中没有,并且这在长方形和正方形的面积计算中并没有出现。且在输入的半径值为小数比如1.3时,在监控栏中会看到1.29999995.
目前,我并不知道原因,如有看到此文的读者知道的请在评论区为我解惑,本人不胜感激。

原文地址:https://www.cnblogs.com/ghbuff/p/12742121.html