在函数中使用方程式的麻烦

时间:2011-02-27 17:32:11

标签: c++ function equation

编写一个程序,用于确定当您将岩石扔出悬崖时岩石的行进时间和持续时间。单击此处将文件toss.txt复制到桌面(右键单击文件名并选择另存为)。该文件包含以米为单位的悬崖高度。

该程序将:

  1. 打开文件toss.txt并将悬崖高度读入双精度变量,然后使用适当的标签将悬崖高度的值打印到屏幕上。

  2. 询问用户投掷岩石的角度(90度是直线向上,0度是直线向前),以及投掷岩石的速度(以英里/小时为单位)。

  3. 检查以确保角度大于或等于0且小于或等于90.如果不是,程序将终止并向屏幕输出相应的错误消息。

    < / LI>
  4. 检查以确保速度小于或等于100英里/小时且大于或等于0英里/小时。如果不是,程序将终止并在屏幕上显示相应的错误消息。

  5. 如果角度和速度有效,程序将按如下方式完成计算:

    1. 将每小时英里数转换为每秒米数。

    2. 将角度转换为弧度。

    3. 使用以下等式计算行进时间:

      ,其中

    4. 使用以下方法计算水平方向的行进距离:

  6. 使用适当的标签将水平方向上行进的时间和距离输出到屏幕。

  7. 打印一条适当的消息,告诉用户水平方向行走的距离是否大于,小于或等于悬崖的高度。

    /* This program */
    
    using namespace std;
    
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<fstream>
    
    int readit ();
    int calcit (double, double, double);
    
    int main()
    {
        readit ();
    
        system ("pause");
    
        return 0;
    }
    
    int readit ()
    {
        double hite, angl, v;
    
        ifstream datain ( "toss.txt" );
    
        datain >> hite;
    
        cout << "The cliff height is " << hite << " meters"<< endl;
    
        cout << "Enter the angle in degrees (from horizontal) the rock is thrown: "
             << endl;
    
        cin >> angl;
    
        if (angl>=0 && angl<=90)
        {
           cout << endl << "The angle you have entered is "<<angl<< endl <<endl;
        } 
           else
        {
           cout << "The angle you have entered is not acceptable" << endl;
    
           return 0;
        }
    
        cout << "Enter the velocity in mph the rock is thrown: " << endl;
    
        cin >> v;
    
        if (v>=0 && v<=100)
        {
           cout << endl << "The velocity at which the rock is thrown is "<<v<<
                 " mph" << endl << endl;
        } 
           else
        {
           cout << "The velocity you have entered is not acceptable" << endl;
    
           return 0;
        }
    
        calcit (hite, angl, v);
    }
    
    int calcit (double hite, double angl, double v)
    {
        double tyme, dist;
    
        v = v * (1609.344/3600);
    
        angl = angl*(M_PI/180);
    
        tyme = -v*sin(angl) + (sqrt((v*sin(angl)*v*sin(angl)) + 2*9.8*hite)/9.8) + (2*(v*sin(angl))/9.8);
    
        dist = (tyme * v) * cos(angl);
    
        cout << tyme << " " << dist <<endl;
    
    
    
    
    }
    
  8. 我正试图在岩石撞击地面之前获得正确的时间,但我一直得到错误的答案。我不确定我是否正在改变方程式,以确定摇滚将在空中影响c ++语言的时间。任何有任何想法???我真的需要完成这个该死的项目。

4 个答案:

答案 0 :(得分:2)

从我们所拥有的岩石的y(高于0的高度)的等式开始

y = h + v*sin(a)*t - g/2*t^2

转换为

g/2 T^2 - v*sin(a)*T - h == 0

当我们解决最终条件y(T)=0时。

这会产生

T = v*sin(a)/g + sqrt(v*sin(a)*v*sin(a) + 2*g*h)/g

我无法弄清楚等式中第一部分-v*sin(angl)的来源。其他一切看起来都很好。所以似乎不是你的代码,而是你开始的等式。

答案 1 :(得分:0)

我会建议一些事情来“清理”代码:

  • 如果函数返回int,请确保它们确实返回了某些内容。 (主要不需要,但其他功能)。

  • 计算v * sin(ang1)一次,然后在公式中使用它。不仅效率更高,而且会使您的代码更清晰。

  • 就像你给Pi一个“常数”一样,用你正在使用的其他数字(如9.8)(引力?)

答案 2 :(得分:0)

你想要的等式是:

s =ut + 1/2 at^2

s = Total distance traveled.   (Height of the cliff)
u = Starting velocity          (In your case negative as you are throwing
                                away from the target. And take into account
                                that not all the starting velocity is away
                                from the target (eg angle 0 mean u = 0))
a = acceleration               (9.81 m/s2)
t = time                       (The value you want to calculate).

重新排列公式以解决t

找到t的解决方案,其中s = 0 ...
这个公式是基本的二次方:

y = a.x^2 + b.x + c

Where:
    x/y     are variables.
    a/b/c   are constants.

y为0的二次方程的解是:

x = [ -b ± sqrt(b^2 - 4ac) ] / 2a

Notice the ± symbol. There are actually two solutions to the problem.
You should be able to deduce which one is correct for you as the other
is probably negative.

In your particular case the map is:

   x ==> t
   y ==> 0

   a ==> 1/2.g
   b ==> u
   c ==> -s

答案 3 :(得分:-1)

如果代码中有一个令人困惑的公式,只需引入更多变量名称,直到含义变得明显。只要你不为同一个变量重新分配不同的值,就不会让程序混淆。

int calcit (double hite_meters, double angl_deg, double v_mph)
{
    double const gravity = 9.8;
    double v_ms = v_mph * (1609.344/3600);
    double angl_rad = angl_deg * (M_PI/180);
    double v_vertical = v_ms * sin( angl_rad );
    double time_up = v_vertical / gravity; // [m/s] / [m/s^2] = [s]
    double time_down_over_cliff = time_up;
    // use quadratic formula t = ( -v - ( v^2 - 4gd )^1/2 ) / 2g:
    double time_under_cliff = ( - v_vertical
         - sqrt( ( v_vertical * v_vertical )
               - ( 4 * - gravity * hite_meters ) ) // negative gravity = down
        ) / ( 2 * - gravity ); // ( [m/s] + ([m/s]^2 - [m/s^2]*[m])^1/2 ) / [m/s^2]
                             // = [m/s] / [m/s^2] = [s]
    double time_total = time_up + time_down_over_cliff + time_under_cliff;
    double v_horizontal = v_ms * cos( angl_rad );
    double dist_horizontal = v_ms * time_total;

    cout << time_total << " " << dist_horizontal <<endl;
}

每行代码都会产生一条新的相关信息。转换为新单元时,我引入了一个带有新名称的新变量。涉及多个单位的公式获得评论中解释的单位类型。这应该有助于解决单位转换错误,否则我无法帮助你抓住。

编写这种类型的代码需要更多的输入,但是节省头脑和寻求帮助的时间远远超过它。

程序本身效率不高。更重要的是,它可能很容易修改,所以在经过一些修改后它不会变成低效的混乱。