gpt4 book ai didi

c - 如何将for循环改为while循环

转载 作者:行者123 更新时间:2023-11-30 19:25:14 26 4
gpt4 key购买 nike

位于程序底部的 for(t = 0; t < tmax ...) 循环应该是一个 while 循环(例如 while (s.distance > 0)) 在高度大于 0 时运行。根据用户选择的推力,tmax 可能或可能不足以让着陆器着陆。 简而言之,我需要将for循环更改为while循环以加快燃料消耗。

#include "stdio.h"
#include <math.h>
#include "string.h"
#include "ctype.h"

struct _State //different to shorten variable names
{
double mass;
double thrust;
double accel;
double velo;
double distance;
double fuel;
char gameMode; // variable for keeping track of selected mode (A - 65 - Autopilot / M - 77 - Manual)
};
typedef struct _State State;

double getThrust(State* s) // Accepts user input for thrust and validates it
{
printf("Input Thrust bellow. \n--> ");
scanf("%lf", &s->thrust);

if (s->thrust < 0)
{
return 0;
}
else if (s->thrust > 45000)
{
return 45000;
}
else
return s->thrust;
}
void Update(State* s, double t, double stepSize)
{
const double gMoon = -1.6;
//double mass = 9000;
//double fuel = 1800;
s->thrust = getThrust(s);
s->mass = s->mass - (s->thrust / 3000);
s->accel = (s->thrust / s->mass) + gMoon;
s->velo += s->accel * stepSize;
s->distance += s->velo * stepSize;
s->fuel = s->fuel - (s->thrust / 3000);
}

void manualControl(State* s) //manual controll function
{
printf("\nManual mode: on\n");
printf("-----------------\n\n");

if (s->distance == 0) // Sets height value to the initial height value for calculations
{
s->distance = 15000;
}
if (s->velo == 0) // Sets velocity value to the initial velocity value for calculations
{
s->velo = -325;
}
}

void autopilotControl(State* s) // Autopilot controll function
{
printf("\nAutopilot: on\n");
printf("-----------------\n\n");
}

void modeHandler(State* s) // mode controller. Calls appropriate function depending on the mode selected
{
if (s->gameMode == 65)
{
autopilotControl(s);
}
else if (s->gameMode == 77)
{
manualControl(s);
}
}

void modeSelect(State* s) // modeSelect allows user to pick the game mode and validates their input.
{
printf("--> ");
scanf("%c", &s->gameMode);
s->gameMode = toupper(s->gameMode);

if (s->gameMode == 65 || s->gameMode == 77)
{
modeHandler(s);
}
else
{
printf("\nInvalid input. Please try again!\n");
scanf("%c", &s->gameMode);
s->gameMode = toupper(s->gameMode);
modeSelect(s);
}
}

int main(void)
{
State s = { 9000,0,0,-325,15000,1800 };
printf("Welcome to the Lunar Lander simulation!\n\n");
printf("Your goal is to land the vehicle safely on the moon.\n");
printf("You will be provided with information about the lander\nbased on which you will have to make a decision about\nhow much force should be applied to slow the vehicle down.\n");
printf("-----------------------------------------------------------\n\n");

printf("Choose your game mode.\n(Type \"A\" for autopilot) or (\"M\" for manual mode)\n");
modeSelect(&s);

double t = 0;
double tmax = 150;
const double stepSize = 1;
FILE* f = fopen("lander.csv", "wt");
if (!f)
{
printf("Unable to open file.\n");
system("pause");
return 1;
}
for (t = 0; t < tmax; t++)
{
while (t < tmax)
{
fprintf(f, "%.2f,%.2f,%.2f,%.2f%.2f,\n", t, s.accel, s.velo, s.distance, s.fuel);
Update(&s, t, stepSize);
printf("Current height: %.2f m\n", s.distance);
printf("Current velocity: %.2f m/s\n", s.velo);
printf("Fuel left : %.2f kg\n", s.fuel);
printf("---------------------------\n\n");


}
}
system("pause");
}

这里是输出:

Choose your game mode.
(Type "A" for autopilot) or ("M" for manual mode)
--> M

Manual mode: on
-----------------

Input Thrust bellow.
--> 20
Current height: 14673.40 m
Current velocity: -326.60 m/s
Fuel left : 1799.99 kg
---------------------------

Input Thrust bellow.
-->

最佳答案

似乎可以在 Update(...) 方法中定义燃料减量。那么,难道不应该定义类似以下建议的内容吗?

// ...
int t = 0;
int tmax = 150;
// ...
while ((s.fuel > 0.0) && (t < tmax))
{
fprintf(f, "%.2f,%.2f,%.2f,%.2f%.2f,\n", t, s.accel, s.velo, s.distance, s.fuel);
Update(&s, t, stepSize);
printf("Current height: %.2f m\n", s.distance);
printf("Current velocity: %.2f m/s\n", s.velo);
printf("Fuel left : %.2f kg\n", s.fuel);
printf("---------------------------\n\n");
t++;
}
// ...

关于c - 如何将for循环改为while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59165411/

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