gpt4 book ai didi

c - 为什么我的变量不断重置自己?

转载 作者:行者123 更新时间:2023-12-03 23:34:39 24 4
gpt4 key购买 nike

在我调用 updateStats 后,我的 Stats 结构中包含的所有变量都会不断地自行重置。我没有正确引用或传递我的 Stats 变量吗?不确定要提供哪些其他信息,因为这是我提出的第一个问题,但显然我需要输入更多信息。如果它有帮助,这个程序应该代表了月球着陆器模拟游戏的一个非常基本的版本。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct _stats
{
double altitude, velocity, mass, fuel, acceleration;
};
typedef struct _stats Stats;

void printStats(Stats a, double thrust)
{
printf("Thrust: %f Altitude: %f Velocity: %f Mass: %f Fuel: %f\n", thrust, a.altitude, a.velocity, a.mass, a.fuel);
}
void updateStats(Stats a, double thrust, int time)
{
a.acceleration += (thrust / a.mass -1.6) * time;
a.mass -= thrust / 3000.0;
printf("aaaaa%f", a.mass);
a.fuel -= thrust / 3000.0;
a.velocity += a.acceleration * time;
a.altitude += a.velocity * time;
}
double thrustAllowed(Stats a, double thrust)
{
if (thrust/1000 <= 45 && thrust <= (3000 * a.fuel))
{
return thrust;
}
else
return (3000*a.fuel)/1000;
}

int main(void)
{
FILE* inputFile = fopen("simulation.csv", "wt");
if (!inputFile)
{
printf("Unable to open file.\n");
return 1;
}

int time = 0;
double thrust = 0;
Stats rocket;
rocket.altitude = 150000;
rocket.velocity = -325;
rocket.mass = 9000;
rocket.fuel = 1800;
rocket.acceleration = 0;
while (time < 150)
{
time++;
fprintf(inputFile, "Enter a Thrust in kN: ");
scanf("%lf", &thrust);
fprintf(inputFile, "%f", thrust);
thrust = thrust * 1000;
if (thrustAllowed(rocket, thrust) == thrust)
{
updateStats(rocket, thrust, time);
fprintf(inputFile, "\nThrust: %.1f Altitude : %.1f Velocity : %.1f Mass : %.1f Fuel : %.1f\n", thrust, rocket.altitude, rocket.velocity, rocket.mass, rocket.fuel);
if (rocket.altitude <= 0)
{
if (rocket.velocity <= 0 && rocket.velocity >= -1)
{
fprintf(inputFile, "Soft Landing!!!");
break;
}
else
{
fprintf(inputFile, "You broked it");
break;
}
}
}
else
fprintf(inputFile, "Thrust must be <= %.1f\n", thrustAllowed(rocket, thrust));
}
fclose(inputFile);
}'''

最佳答案

您需要对要更改的变量使用指针:

void updateStats(Stats* a, double thrust, int time) {
a->acceleration += (thrust / a->mass -1.6) * time;
a->mass -= thrust / 3000.0;
printf("aaaaa%f", a->mass);
a->fuel -= thrust / 3000.0;
a->velocity += a->acceleration * time;
a->altitude += a->velocity * time;

}

然后像 updateStats(&rocket,thrust,time); 一样调用它。

关于c - 为什么我的变量不断重置自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622427/

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