gpt4 book ai didi

c++ - 如何使用 C++ 计算表中每年的更新平均速度?

转载 作者:行者123 更新时间:2023-11-27 23:48:12 25 4
gpt4 key购买 nike

In your program, you will have the user enter what percentage improvement in rocket speeds (up to but not exceeding light speed!) each year. Your program will then ask the user the maximum number of years that they are willing to wait on earth before they leave. Use while loops in this step to implement simple error checking by asking the user repeatedly until they give a valid input. Percentage must be somewhere between 0 and 100 and the years waiting must be a positive integer.

Next, your program will generate a table using a for loop. That table will have four columns with one row for leaving immediately followed by one row for each year the user is willing to wait. The first column will contain the departure year. The second column contains the rocket speed that rockets will be able to achieve that year. The new rocket speed each year is calculated with this equation: velocity = velocity + (lightspeed - velocity) * (improvement/100)

我能够在我试图制作的表格中正确地打印出每一年,但我无法弄清楚如何使用循环来使用循环找到每年火箭的速度。我很确定我应该使用嵌套的 for 循环,但是使用我现在拥有的代码,它陷入了无限循环。任何正确方向的指导都将不胜感激!

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main()
{

int percentIncrease = 0;
int maxYears = -1;
float speedLight = 299792;

while((percentIncrease <= 0) || (percentIncrease >= 100))
{
cout << "What percentage do rocket speeds increase by each year?" << endl;
cin >> percentIncrease;
}

while(maxYears < 0)
{
cout << "What is the maximum number of years you are willing to wait on
earth before you leave?" << endl;
cin >> maxYears;
}

cout << "Start year|\tAvg Speed|\tEarth ETA|\tYour ETA" << endl;

for(int i = 2018; i <= (maxYears + 2018); ++i)
{
cout << i << endl;
for(int j = 10000; i <= (maxYears + 2018); j = j + (speedLight - j) *
(percentIncrease/100))
{
cout << "\t" << j << endl;
}

}

return 0;
}

最佳答案

我认为考虑它的一个好方法是您必须逐行打印表格。所以您的第一个 for 循环似乎就是这样做的。

在每一行中,您必须首先打印年份(从当前年份开始)直到最大年份。让第一个 for 循环迭代多年是一个不错的选择(即让 i2018 一直到 maxYears + 2018)。其次,您必须在通过提供的公式计算改进后打印每年的速度。 (我假设在问题描述中给出了第一个速度是 10000?如果不是,起始值是多少?)因为你只会打印一个数字,所以你不需要第二个 for 循环。只需计算新速度并打印即可。至于第三和第四列,我不确定具体问的是什么,所以现在代码中是空白的。

我根据自己的意见修改了代码,加上一些与我对问题描述的理解、编码最佳实践和风格选择相关的修改(阅读下面的代码以了解更多信息)。

#include <iostream>
//--1
int main()
{
//--2
const float speedLight = 299792;
const int startingYear = 2018;
//--3
float percentIncrease = 0;
while ((percentIncrease <= 0) || (percentIncrease >= 100))
{
std::cout << "What percentage do rocket speeds increase by each year?" << std::endl;
std::cin >> percentIncrease;
}
//--4
int maxYears = -1;
while (maxYears < 1)
{
std::cout << "What is the maximum number of years you are willing to wait on earth before you leave? " << std::endl;
std::cin >> maxYears;
}

std::cout << "Year|\tAvg Speed|\tEarth ETA|\tYour ETA" << std::endl;
//--5
float currentSpeed = 10000;
for (int year = startingYear; year <= (maxYears + startingYear); ++year)
{
//--6
std::cout << year << "\t" << currentSpeed << std::endl;
currentSpeed = currentSpeed + (speedLight - currentSpeed) * (percentIncrease / 100);
}
//--7
system("pause");
return 0;
}
  • --1:我删除了未使用的库。 (您可能将它们用于其他部分程序的一部分,比如如果你想打印 float )。我也删除了 using namespace std; 因为这是一种不好的做法。你可以谷歌一下。

  • --2:这些数字看起来是不变的,所以最好让它们常量。

  • --3:也许percentIncrease不一定是整数。

  • --4:问题描述说年数是a正整数,因此不能为 0

  • --5:currentSpeed(以前的 j)应该在循环,因为它将在循环内更新。另外,它是一个 float 因为#3。

  • --6:速度要在年后打印。

  • --7:这是可选的,以防您希望程序窗口不关闭立即地。您也可以通过放置一个断点,或任何其他解决方案。

关于c++ - 如何使用 C++ 计算表中每年的更新平均速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48877558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com