C ++使用浮点数,函数不计算有效数据

时间:2017-07-26 00:20:31

标签: c++ floating-point

为此赋值编写类矩形创建一个具有属性长度和宽度的Rectangle类,每个默认为1.提供计算矩形周长和面积的成员函数。另外,为length和width属性提供set和get函数。 set函数应验证长度和宽度是否都是大于0.0且小于20.0的浮点数。

代码执行,但输出不正确。这些功能不能正确调用。

输出示例

>The first objects information is
>the length is 1 
>the width is 1
>the perimeter is -1.07374e+08 the area is -1.07374e+08 
>The second objects information is   
>the length is -1.07374e+08
>the width is -1.07374e+08 
>the perimeter is -1.07374e+08 
>the area is -1.07374e+08

标头文件

#ifndef Rectangle_H
#define Rectangle_H
class Rectangle
{
public:
    Rectangle();
    Rectangle(float length);
    Rectangle(float length, float width);
    ~Rectangle();
    float setLengthAndWidth(float, float);
    float setLength(float Length);
    float setWidth(float Width);
    float calculatePerimeter();
    float calculateArea();
    void printInfo();
    float getLength();
    float getWidth();
private:
    float length;
    float width;
    float area;
    float perimeter;
};
#endif#pragma once

主档

#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;
int main()
{
    Rectangle objectOne;
    Rectangle objectTwo(7.1, 3.2);
    Rectangle objectThree(6.3);
    Rectangle objectFour(200, 300);
    Rectangle objectFive = objectTwo;
    cout << "The first objects information is\n  ";
    objectOne.printInfo();
    cout << "The second objects information is\n  ";
    objectTwo.printInfo();
    cout << "The third objects information is\n  ";
    objectThree.printInfo();
    cout << "The fourth objects information is\n  ";
    objectFour.printInfo();
    cout << "The fifth objects information is\n  ";
    objectFive.printInfo();
}

会员.cpp文件

#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle()
{
    length = width = 1.0;
}
Rectangle::Rectangle(float length)
{
    setLengthAndWidth(length, 1.0);
}
Rectangle::Rectangle(float length, float width)
{
    setLengthAndWidth(length, width);
}
float Rectangle::setLengthAndWidth(float Len, float Wid)
{
    setLength(Len);
    setWidth(Wid);
}
float Rectangle::setLength(float length)
{
    if (length >= 0 || length <= 20.0)
        length = length;
    else
        length = 1.0;
}
float Rectangle::setWidth(float width)
{
    if (width >= 0 || width <= 20.0)
        width = width;
    else
        width = 1.0;
}
float Rectangle::calculatePerimeter()
{
    perimeter = (length * 2) + (width * 2) ;
    return perimeter;
}
float Rectangle::calculateArea()
{
    area = length * width;
    return area;
}
float Rectangle::getLength()
{
    cout << "Please enter length" << endl;
    cin >> length;
        return length;
}
float Rectangle::getWidth()
{
    cout << "Please enter width" << endl;
    cin >> width;
    return width;
}

void Rectangle::printInfo()
{
    cout << "the length is " << length << endl << "the width is " << width << endl;
    cout << "the perimeter is " << perimeter << endl << "the area is " << area << endl;

}
Rectangle::~Rectangle()
{
    cout << "the object has gone out of scope. ";
}

1 个答案:

答案 0 :(得分:4)

您的setLength()成员未设置this->lengthsetWidth()也是如此。他们所做的只是设置参数,它没有永久效果。