如何在计算答案到达变量之前停止循环

  • 本文关键字:循环 变量 计算 答案 c++
  • 更新时间 :
  • 英文 :


这是我的代码,我正在做一个程序,根据用户输入的距离,将坠落物体的距离、时间和速度制成表格。我希望循环在达到最大值后停止,然后计算最终距离、时间和速度

#include<iostream>
#include<iomanip>
using namespace std;
#define magnitute 9.81
int main(void)
{
double initial_velocity = 0;
int time = -1;
double final_time;
double distance;
distance = 0;
char direction;
double velocity = 0;
double total_distance = 0;
double final_distance;
double final_velocity;
final_velocity = 0;
cout << "Enter whether the object is thrown upwards or downwards" << endl;
cout << "Enter U for upwards and D for downwards" << endl;
cin >> direction;
cout << "Enter the distance of the object and the ground" << endl;
cin >> distance;
cout << "------------------------------------------------------------------" << endl;
cout << "|" << setw(15) << "distance" << "|" << setw(15) << "time" << "|" << setw(15) << "Velocity" << "|" << endl;
cout << "------------------------------------------------------------------" << endl;
while (direction == 'D' || direction == 'd' && total_distance <= distance )
{
if (total_distance <= distance) 
{
initial_velocity += magnitute;
velocity = initial_velocity + (magnitute * time);
time = time + 1;
total_distance = (initial_velocity * time) + (1 / 2) * magnitute * time * time;
cout << setw(10) << total_distance << setw(10) << time << setw(15) << velocity << endl;
}
else
{
final_distance = velocity * time + 1 / 2 * magnitute * time * time;
final_time = final_distance / 2 / (0 + final_velocity);
final_velocity = velocity + magnitute * time;
cout << setw(10) << velocity << setw(10) << final_time << setw(15) << final_velocity;
}
}
return 0;
}

您的线路:

while (direction == 'D' || direction == 'd' && total_distance <= distance )

添加所有括号类似于:

while ((direction == 'D') || ((direction == 'd') && (total_distance <= distance )))

这不是你想要的,因为如果你输入"D",你永远不会停止循环,你想要这样的东西:

while ((direction == 'D' || direction == 'd') && (total_distance <= distance))`

甚至最好在循环之前测试方向,因为它永远不会改变。

现在有了正确的测试来进行内部循环:

if (total_distance <= distance) 
{
...
}
else
{
...
}

没有意义,因为你知道if (total_distance <= distance)是真的,所以如果你想做其他部分,你必须把它放在循环后面,比如:

if (direction == 'D' || direction == 'd') {
while (total_distance <= distance )
{
initial_velocity += magnitute;
velocity = initial_velocity + (magnitute * time);
time = time + 1;
total_distance = (initial_velocity * time) + (1 / 2) * magnitute * time * time;
cout << setw(10) << total_distance << setw(10) << time << setw(15) << velocity << endl;
}
final_distance = velocity * time + 1 / 2 * magnitute * time * time;
final_time = final_distance / 2 / (0 + final_velocity);
final_velocity = velocity + magnitute * time;
cout << setw(10) << velocity << setw(10) << final_time << setw(15) << final_velocity;
}

请注意,您在循环中写入的值可能是错误的,因为total_distance可能大于distance,最后为什么要进行循环?

您使用的宽度和分隔符在页眉和后面的页眉之间不一致,最好相同。

例如:

#include<iostream>
#include<iomanip>
using namespace std;
#define magnitute 9.81
int main(void)
{
double initial_velocity = 0;
int time = -1;
double final_time;
double distance;
distance = 0;
char direction;
double velocity = 0;
double total_distance = 0;
double final_distance;
double final_velocity;
final_velocity = 0;
cout << "Enter whether the object is thrown upwards or downwards" << endl;
cout << "Enter U for upwards and D for downwards" << endl;
cin >> direction;
cout << "Enter the distance of the object and the ground" << endl;
cin >> distance;
cout << "------------------------------------------------------------------" << endl;
cout << "|" << setw(15) << "distance" << "|" << setw(15) << "time" << "|" << setw(15) << "Velocity" << "|" << endl;
cout << "------------------------------------------------------------------" << endl;
if (direction == 'D' || direction == 'd') {
while (total_distance <= distance )
{
initial_velocity += magnitute;
velocity = initial_velocity + (magnitute * time);
time = time + 1;
total_distance = (initial_velocity * time) + (1 / 2) * magnitute * time * time;
cout << "|" << setw(15) << total_distance << "|" << setw(15) << time << "|" << setw(15) << velocity << "|" << endl;
}

final_distance = velocity * time + 1 / 2 * magnitute * time * time;
final_time = final_distance / 2 / (0 + final_velocity);
final_velocity = velocity + magnitute * time;
cout << "|" << setw(15) << velocity << "|" << setw(15) << final_time << "|" << setw(15) << final_velocity << "|" << endl;
} 

return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
Enter whether the object is thrown upwards or downwards
Enter U for upwards and D for downwards
D
Enter the distance of the object and the ground
10
------------------------------------------------------------------
|       distance|           time|       Velocity|
------------------------------------------------------------------
|              0|              0|              0
|          19.62|              1|          19.62
|          19.62|            inf|          29.43
pi@raspberrypi:/tmp $ ./a.out
Enter whether the object is thrown upwards or downwards
Enter U for upwards and D for downwards
d
Enter the distance of the object and the ground
10
------------------------------------------------------------------
|       distance|           time|       Velocity|
------------------------------------------------------------------
|              0|              0|              0|
|          19.62|              1|          19.62|
|          19.62|            inf|          29.43|
pi@raspberrypi:/tmp $ 

正如你所看到的,最后的时间是错误的,因为你除以0,因为这些线:

final_time = final_distance / 2 / (0 + final_velocity);
final_velocity = velocity + magnitute * time;

必须交换才能拥有

final_velocity = velocity + magnitute * time;
final_time = final_distance / 2 / (0 + final_velocity);

但为什么要添加速度而不是final_velocity = magnitute * time;

之后(使用final_velocity = velocity + magnitute * time;(:

pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
Enter whether the object is thrown upwards or downwards
Enter U for upwards and D for downwards
D
Enter the distance of the object and the ground
10
------------------------------------------------------------------
|       distance|           time|       Velocity|
------------------------------------------------------------------
|              0|              0|              0|
|          19.62|              1|          19.62|
|          19.62|       0.333333|          29.43|
pi@raspberrypi:/tmp $ 

正如我所说,你不需要一个循环,为了得到确切的结果(仍然没有摩擦(,做time = sqrt(2 * distance / magnitute)final_velocity = magnitute * time;

你只管理向下的情况,你也必须管理向上的情况。

最新更新