【发布时间】: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