gpt4 book ai didi

c - 从结构中获取信息并在另一个函数中使用

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

我需要知道是否可以调用结构中的tire_Pressure中的信息并在另一个函数中使用它?基本上我不知道如何将其从一个调用到另一个。我必须在tireTest中声明该函数吗?这是我试图访问轮胎压力信息的函数。感谢您的帮助

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


struct tires
{
char Manufacturer[40];
int tire_pressure[0];
int pressure_change[0];
}typedef tires;
// Prototypes
void getTireInformation(tires*, int);
void tirePressure(tires*, int);
void tireTest(tires*, int);
int main()
{
tires tire[4];
tires* ptire = &tire[0];
srand(time(NULL));

getTireInformation(ptire, 4);
tirePressure(ptire, 4);
tireTest(ptire, 4);

return 0;
}// end main
//============================
void getTireInformation(tires* ptire, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("please enter Make for the tire: \n");
scanf("%s", &(ptire + i) ->Manufacturer);


}

printf("all tire make you entered ...just for verification:\n");
for(i = 0; i < size; i++)
printf("%s\n",(ptire +i) ->Manufacturer);

}//end getTireInformation
//=======================================================================

void tirePressure(tires* ptire, int size)
{
int i = 0;
int min = 18;
int max = 35;
for (i = 0; i < size; i++)
{
(ptire + i) ->tire_pressure[0] = rand() % (max - min + 1) + min;
printf("The Tire pressure is: ");
printf("%d\n",(ptire + i) -> tire_pressure[0]);
}// end for
}// end tirePressure
//==============================================================
void tireTest(tires* ptire, int size)
{
int i = 0;
int min = 2;
int max = 5;
int change[0] = {0};
i++;
for (i = 0; i < size; i++)
{
change[0] = rand() % (max - min + 1) + min;
//(ptire + i) -> pressure_change[0] = rand() % (max - min + 1) + min;
printf("Pressure change from test %d: %d\n",i + 1, change[0]);
//(ptire + i) -> pressure_change[0] = change[0] + tirePressure;
//printf("%d\n", (ptire +i) -> pressure_change);
}// end for

}

最佳答案

我认为您使问题变得更加困难 - 看看这种简化是否可以解决您的问题:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define PRESSURE_MIN 18
#define PRESSURE_MAX 35

struct tires
{
char Manufacturer[40];
int tire_pressure;
int pressure_change;
} typedef tires;

// Prototypes
void getTireInformation(tires *, int);
void tirePressure(tires *, int);
void tireTest(tires *, int);

//===============================================================

void getTireInformation(tires *ptire, int size)
{
for (int i = 0; i < size; i++)
{
printf("please enter Make for the tire: \n");
scanf("%s", ptire[i].Manufacturer);
}

printf("all the tire makes you entered ... just for verification:\n");

for (int i = 0; i < size; i++)
{
printf("%s\n", ptire[i].Manufacturer);
}

} // end getTireInformation

//===============================================================

void tirePressure(tires *ptire, int size)
{
for (int i = 0; i < size; i++)
{
ptire[i].tire_pressure = rand() % (PRESSURE_MAX - PRESSURE_MIN + 1) + PRESSURE_MIN;
printf("The Tire pressure is: ");
printf("%d\n", ptire[i].tire_pressure);
} // end for
} // end tirePressure

//==============================================================

void tireTest(tires *ptire, int size)
{
int min = 2;
int max = 5;

for (int i = 0; i < size; i++)
{
int change = rand() % (max - min + 1) + min;

printf("Pressure change from test %d: %d\n", i + 1, change);

ptire[i].pressure_change = change + ptire[i].tire_pressure;

printf("%d\n", ptire[i].pressure_change);
} // end for
}

//===============================================================

int main()
{
tires tire[4];

srand(time(NULL));

getTireInformation(tire, 4);
tirePressure(tire, 4);
tireTest(tire, 4);

return 0;
} // end main

关于c - 从结构中获取信息并在另一个函数中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36109888/

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