首页 > 解决方案 > 如何使用函数调用失败或通过多个测试?

问题描述

我试图弄清楚如何调用一个函数。我必须能够输入负载量并查看它是否通过了三个测试。这些是给出的方程。

Buckling Load:
    Max Load = (0.3*E*area) /  ((length/width) * (length/width))
Compressive stress:
    Max Load = area * maximum compressive strength
Slenderness limits:
    Length/width <= 50

E 是弹性模量 (1,700,000 psi),面积是以平方英寸为单位的横截面积,最大抗压强度 = 445 psi (花旗松)。假设要使用的柱子是方形的,并且有 2 英寸的间隔(2x2、4x4、6x6 等)。我必须为每个人使用不同的功能!帮助 :/

我目前有:

{

    int strength, area, length, width, e, maxLoad, bucklingLoad, compressiveLoad, slenderness;
    float bucklingLoad;
    e = 1700000,
    maxLoad =

    cout << "Jessica's Engineering Company Analysis" ;
    cout << "\n*************************************";

    cout <<"\n\nPlease enter the expected load on the column in pounds: ";
    cin << maxLoad;

    cout << "\nPlease enter the length of the column in inches: ";
    cin << length;

    cout << "\n\n_Beam with wide of 2 inches - " ;
    cout << "\n_Beam with wide of 4 inches - " ;
    cout << "\n_Beam with wide of 6 inches - " ;
    cout << "\n_Beam with wide of 8 inches - " ;

}

标签: c++

解决方案


I'm not going to do the whole thing, and you should absolutely learn C++ properly from a book or other resource, because asking open-ended questions is going to be a really slow, frustrating and inefficient way to figure it out - but I can give you a hint to get started.

Buckling Load:
    Max Load = (0.3*E*area) /  ((length/width) * (length/width))

So we can translate this directly into a function, like

double calcBucklingLoad(int length, int width, int area)
{
    return (0.3*E*area) /  ((length/width) * (length/width));
}

(so long as we first define E somewhere, like

const double E = 1700000;

or something similar. It could be another function parameter if you want to reuse the code for different materials).

Then testing whether the max load is greater than the buckling load is just

bool isBucklingLoadOK(int maxload, int length, int width, int area)
{
    return maxload < calcBucklingLoad(length, width, area);
}

推荐阅读