1

编写一个程序,确定当您将岩石从悬崖上扔下时,它会移动多远和移动多长时间。单击此处将文件 toss.txt 复制到您的桌面(右键单击文件名并选择另存为)。该文件包含以米为单位的悬崖高度。

然后程序将:

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

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

  3. 检查以确保角度大于或等于 0 且小于或等于 90。如果不是,则程序终止并在屏幕上打印适当的错误消息。

  4. 检查以确保速度小于或等于 100 mph 且大于或等于 0 mph。如果不是,则程序终止并将适当的错误消息打印到屏幕上。

  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;
    
    
    
    
    }
    

我试图在岩石撞到地面之前获得正确的移动时间,但我一直得到错误的答案。我不确定我是否正在转动等式来计算出岩石在空气中停留的时间,直到影响到 c++ 语言为止。有什么想法吗???我真的需要完成这个该死的项目。

4

4 回答 4

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)等式中的第一部分来自哪里。其他一切看起来都很好。因此,它似乎与您的代码无关,而是与您开始的方程式有关。

于 2011-02-27T17:51:26.097 回答
0

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

  • 如果函数返回 int 确保它们确实返回了一些东西。(main 不需要,但其他函数可以)。

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

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

于 2011-02-27T17:58:01.180 回答
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
于 2011-02-27T17:58:46.560 回答
-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;
}

每一行代码都会产生一条新的、相关的信息。转换为新单位时,我会引入一个新变量,并使用新名称。涉及多个单位的公式会在注释中解释单位类型。这应该有助于发现单位转换错误,否则我无法帮助您发现。

编写这种代码需要更多的打字,但节省的时间和寻求帮助所节省的时间远远超过了它。

该程序本身的效率并没有降低。更重要的是,它可能很容易修改,所以不会在几次修改后变成低效的烂摊子。

于 2011-02-27T18:26:05.360 回答