不使用if语句检查循环有效性

时间:2017-10-04 00:46:40

标签: c++ c++11 if-statement while-loop

我的实验室教授希望我们使用while语句让抛物线向下弯曲(引力方程式),但如果没有c ++ 11中的if语句则不会消极。下面的代码工作正常,除了仍然使用一个负值的事实。有没有办法删除最后一个x / y值?

#include <iostream>
#include <cmath> 
using namespace std;  
int main() {
    const double gravAccel = 9.82;
    double initHeight;
    double itemAngle;
    double itemSpeed;
    double horzCoord = 0;
    double vertCoord;
    cout <<"What is the initial height of the projectile?";
    cin >> initHeight;
    cout <<"What angle is the projectile launched at?";
    cin >> itemAngle;
    cout <<"What is the projectile's speed?";
    cin >> itemSpeed;
    while (vertCoord >= 0)
    {
        vertCoord = initHeight + horzCoord*tan(itemAngle) - ((gravAccel*pow(horzCoord,2))/(2*(pow(itemSpeed*cos(itemAngle),2))));
        cout << horzCoord << "meters, " << vertCoord << "meters" << endl;
        horzCoord++;
    }

return 0; }

2 个答案:

答案 0 :(得分:0)

  

使用while语句使抛物线向下弯曲(重力方程式),但如果没有c ++ 11中的if语句则不会变为负数

不是很优雅,但您可以在while()测试中嵌入while ( (vertCoord = initHeight + horzCoord*tan(itemAngle) - ((gravAccel*pow(horzCoord,2))/(2*(pow(itemSpeed*cos(itemAngle),2))))) >= 0 ) { cout << horzCoord << "meters, " << vertCoord << "meters" << endl; horzCoord++; } 作业

gravAccel

关闭主题:据我所知,g(new)不是9.82(m / s ^ 2),而是9.807。

答案 1 :(得分:0)

  • 重力是约。 9.81米/(S ^ 2)。
  • 您似乎没有计算垂直和水平距离正确的距离..
  • sin + cos / tan in C ++使用弧度作为角度参数。
  • ++上的++运算符会将其增加1.0。
  • 如果你想让它永远不会低于零,那为什么不把它夹到它的时间?我的意思是计算弹丸击中地板所需的时间。然后在打印射弹的X和Y坐标的同时用一些步骤循环(time - currentTime)。这将确保垂直坐标始终> 0
#include <iostream>
#include <cmath>

using namespace std;

double calculateHorizontalCoordinate(double time, double initial_velocity, double initial_angle)
{
    double initial_velocity_x = initial_velocity * cos(initial_angle * M_PI / 180.0);
    return initial_velocity_x * time;
}

double calculateVerticalCoordinate(double time, double initial_velocity, double initial_angle)
{
    double initial_velocity_y = initial_velocity * sin(initial_angle * M_PI / 180.0);
    return (initial_velocity_y * time) - (0.5 * 9.81 * (time * time));
}

double calculateTime(double initial_velocity, double initial_angle)
{
    double initial_velocity_y = initial_velocity * sin(initial_angle * M_PI / 180.0);
    double time = (2 * initial_velocity_y) / 9.81;
    return time;
}

int main() {
    double initial_height = 0.0;
    double initial_angle = 0.0;
    double initial_velocity = 0.0;
    double horzCoord = 0.0;
    double vertCoord = 0.0;

    cout <<"What is the initial height of the projectile?";
    cin >> initial_height;
    cout <<"What angle is the projectile launched at?";
    cin >> initial_angle;
    cout <<"What is the projectile's speed?";
    cin >> initial_velocity;


    double currentTime = 0.0;
    double time = calculateTime(initial_velocity, initial_angle);
    double step = time / 10.0;

    while (time - currentTime >= numeric_limits<double>::min())
    {
        horzCoord = calculateHorizontalCoordinate(currentTime, initial_velocity, initial_angle);
        vertCoord = calculateVerticalCoordinate(currentTime, initial_velocity, initial_angle);

        cout << horzCoord << " meters, " << vertCoord << " meters" << endl;
        currentTime += step;
    }

    return 0;
}

输入:

What is the initial height of the projectile?  0
What angle is the projectile launched at?  45
What is the projectile's speed?  25
0 meters, 0 meters
6.37105 meters, 5.73394 meters
12.7421 meters, 10.1937 meters
19.1131 meters, 13.3792 meters
25.4842 meters, 15.2905 meters
31.8552 meters, 15.9276 meters
38.2263 meters, 15.2905 meters
44.5973 meters, 13.3792 meters
50.9684 meters, 10.1937 meters
57.3394 meters, 5.73394 meters
Program ended with exit code: 0