【问题标题】:Unknown issue when calculating bouyancy in C++在 C++ 中计算浮力时的未知问题
【发布时间】:2014-02-12 06:17:22
【问题描述】:

我会切入正题:我用 C++ 编写了一个程序,用于计算球形物体是否适合某个类。但是,在我(根据我的想法)成功地在 Visual Studio 2013 中将程序提交到我需要的地方(Pearon 糟糕的 myProgrammingLab)之后,与 Pearon 相比,我得到了错误的输出。 (即:我的说它是浮动的,他们说它是下沉的,但不显示计算本身。)

这是我的代码:

// Bouyancy formula:
// Fb = V * y
// Where:
// Fb is the bouyant force
// V is the volume of the submerged object
// y is the specific weight of the fluid
// If Fb is greater than or equal to the weight of the object, then it will float, otherwise it will sink.

// Sphere volume formula:
// (4/3)pi(radius cubed)

#include "stdafx.h"
#include <iostream>
#define _USE_MATH_DEFINES // Used with math.h to provide access to M_PI (used in calculation of volume)
#include <math.h> // M_PI is the value of pie (3.14..)

using namespace std;

int main()
{
    float sphere_radius, sphere_weight; // Stores the value of the Sphere's radius and weight in feet and pounds respectively.
    double water_weight = 62.4; // Value set to 62.4lb /cubic feet, this value is the "y" value in the above formula.
    double bouyant_force, volume; // Defines Fb and V in the Bouyancy formula listed above.

    cout << "Enter the radius of the sphere, in feet: ";
    cin >> sphere_radius;
    cout << "\nEnter the weight of the sphere, in pounds: ";
    cin >> sphere_weight;
    cout << endl;

    volume = ((4.0 / 3.0) * M_PI * (pow(sphere_radius, 3))); // Calculates the volume of the sphere
    bouyant_force = (volume * water_weight);
    if (bouyant_force >= sphere_weight)
    {
        cout << "The sphere will float." << endl;
    }
    else if (bouyant_force < sphere_weight)
    {
        cout << "The sphere will sink." << endl;
    }
    else { cout << "Something went terribly, terribly, wrong.. Oh dear.."; }

    char x; 
    cin >> x; //Waits for user to press a key before closing the program.
    return 0;
}

谁能帮我理解为什么这是不正确的,或者为什么它没有被注册为正确的?提前致谢!

【问题讨论】:

    标签: c++ calculator


    【解决方案1】:

    从您的代码来看,错误似乎是您直接将您接受的重量与浮力进行比较。您应该在您使用的单位系统中将您接受的质量(磅是质量单位)乘以 g。这似乎解释了你得到它漂浮而另一方计算它下沉的原因。

    【讨论】:

    • 该网站为我们提供了一个特定的公式,该公式在计算中不包括重力。不过,我会试试的,谢谢编辑:同样的问题
    • @Scarecrow 在物理学中,重量与质量不同。参见例如Wikipedia。 (简化:重量是由重力来衡量的。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多