尝试使用内部简单方程式创建函数

时间:2018-10-02 14:55:31

标签: c++ function math

我是C ++编码的新手,正在做作业,但是我被困住了,似乎无法弄清楚自己做错了什么。作业要求我们使用函数来执行一些涉及“右圆柱”的方程。现在,我能够编写出应做的代码,但不必使用函数来计算答案。我当前的代码如下:

#include <iostream>
#include <cmath>

using namespace std;
int main() {
    double  r = 0,
            h = 0,
            TSurfaceArea = 0,
            LSurfaceArea = 0,
            Volume = 0;

    const double    PI = 3.cylinder   
    cout << "Welcome to the right-circular clyinder area and volume calculator, this program will calculate three things: "
        "\nThe first, is the Total Surface Area."
        "\nThen we will see the Lateral Surface Area."
        "\nAnd Finally we will see the volume." << endl;

    cout << "\nPlease enter a value for the radius: ";
    cin >> r;

    cout << "\nThank you! \n\nNow please enter a value for the height: ";
    cin >> h;

    TSurfaceArea = 2 * PI*r*(r + h);
    LSurfaceArea = 2 * PI*r*h;
    Volume = PI * r*h;

    cout << "Thank you for your input! \n\nSo here are your results based on a radius of " << r << " and a height of " << h << ":" << endl;
    cout << "\nTotal Surface Area is " << TSurfaceArea << "\nLateral Surface Area is " << LSurfaceArea << "\nVolume is " << Volume << endl;

    system("pause");
}

因此,就像我提到的那样,这是可行的,但是首先必须将值分配为“ 0”,然后等式必须在代码中向下计算解,直到用户输入“ r”和“ h” '。我确信有更好的方法可以做到这一点,然后我正在研究如何将方程式转化为以后可以调用的函数,而不必每次我想使用新值进行计算时都必须添加完整的方程式,这就是教练正在寻找,但是我在创建函数时一定做错了,因为我似乎找不到有效的方法。我尝试这样做无济于事:

#include <iostream>
#include <cmath>

using namespace std;
int main() {
    const double    PI = 3.14159;

    double  r = 0,
            h = 0,

            TSurfaceArea() {
                double tsa = 2 * PI*r*(r + h);
                return tsa;
            }, 
            LSurfaceArea() {
                double lsa = 2 * PI*r*h;
                return lsa;
            },
                Volume() {
                double v = PI * r*h;
                return v;
            };

    cout << "Welcome to the right-circular clyinder area and volume calculator, this program will calculate three things: "
        "\nThe first, is the Total Surface Area."
        "\nThen we will see the Lateral Surface Area."
        "\nAnd Finally we will see the volume." << endl;

    cout << "\nPlease enter a value for the radius: ";
    cin >> r;

    cout << "\nThank you! \n\nNow please enter a value for the height: ";
    cin >> h;

    cout << "Thank you for your input! \n\nSo here are your results based on a radius of " << r << " and a height of " << h << ":" << endl;
    cout << "\nTotal Surface Area is " << TSurfaceArea << "\nLateral Surface Area is " << LSurfaceArea << "\nVolume is " << Volume << endl;

    system("pause");
}

现在,我知道这应该比较容易解决,但是我觉得我只是缺少一些东西,希望有人可以帮助我!预先感谢!

2 个答案:

答案 0 :(得分:0)

我这里看不到任何物体。您将C ++用作“更好的C”。

距离我编写C或C ++已经很长时间了,但是我认为您想要更多类似的东西。我已从主要内容中剔除肉来强调我的观点:

#include <iostream>
#include <cmath>

using namespace std;

static const double    PI = 3.14159;  // surely you can do better than six digits

double topSurfaceArea(double r, double h) { return 2.0*PI*r*(r+h); } 
double lateralSurfaceArea(double r, double h) { return 2.0*PI*r*h; }
double volume(double r, double h) { return PI*r*r*h; }  // your formula was clearly wrong.

int main() { // removed body for simplicity }

答案 1 :(得分:0)

lambda也许是您想要的最接近的,例如对于矩形

double width,height;
auto area = [&](){ return width*height; };
width = 5;
height = 10;
std::cout << area(); 

将打印50。但是,您可能最好先了解函数...

double area(double width,double height) {
    return width * height;
}

int main() {
    std::cout << area(5.0,10.0);
}

请注意,该函数声明一个返回类型(double),它接受一些参数(doubledouble),并且既没有,也没有{ {1}}。