gpt4 book ai didi

c - 代码的输出全为零

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

float tolGap(float Tol[], int size);
float meanGap(float Nom[], float Imp[], int size);
void parsePart(char input[], float *pNom, int *pImp, float *pTol, char* pFoV);
void parseGap(char input[], float *max, float *min);

int main()
{
FILE *pIN;
int i;
int Impact[10];
float Nominal[10], Tolerance[10];
char FoV[10];
char input_str[30];
float max, min, tol;
int Num_of_Parts;
float curr_max, curr_min, mean;

pIN=fopen("testingcode.txt", "r");

for(i=0; i<11; i++)
{
fgets(input_str,30, pIN);
if(input_str[0] == 'p')
{
parsePart(input_str, &Nominal[i], &Impact[i], &Tolerance[i], &FoV[i]);
}
else
{
parseGap(input_str, &min, &max);
Num_of_Parts = i;
break;
}
}

mean = meanGap(Nominal, Impact, Num_of_Parts);
tol = tolGap(Tolerance, Num_of_Parts);
curr_max = mean+tol;
curr_min = mean - tol;

if (fabs(max - curr_max) < 0.00001) //they are equal
{
printf("The Maximum Gap (%fin) is equal to specified (%fin)\n", max, curr_max);
}

if ((max - curr_max) > 0.00001)
{
printf("The Maximum Gap (%fin) is Greater than specified (%fin)\n", max, curr_max);
}
else
{
printf("The Maximum Gap (%fin) is within specified (%fin)\n", max, curr_max);
}

if (fabs(min - curr_min) < 0.00001) //they are equal
{
printf("The Minimum Gap (%fin) is equal to specified (%fin)\n", min, curr_min);
}

if ((min - curr_min) > 0.00001)
{
printf("The Minimum Gap (%fin) is Greater than specified (%fin)\n", min, curr_min);
}
else
{
printf("The Minimum Gap (%fin) is Less than specified (%fin)\n", min, curr_min);
}

return 0;
}

float tolGap(float Tol[], int size)
{
int i;
float sum=0;

for (i=0; i<size; i++)
{
sum+=Tol[i];
}
printf("Actual Gap Tolerance: %fin\n", sum);
return sum;
}

float meanGap(float Nom[], float Imp[], int size)
{
int i;
float sum=0;

for(i=0; i<size; i++)
{
sum+= Nom[i]*Imp[i];
}
printf("Actual Gap Mean: %fin\n", sum);
return sum;
}

void parsePart(char input[], float *pNom, int *pImp, float *pTol, char* pFoV)
{
int i;
char *elements[5];

elements[0]=strtok(input, ",");

for(i=1;i<5;i++)
{
elements[i] = strtok(NULL, ",");
}

*pNom = atof(elements[1]);
*pImp = atoi(elements[2]);
*pTol = atof(elements[3]);
*pFoV = *elements[4];

}

void parseGap(char input[], float *max, float *min)
{
int i;
char *elements[5];

elements[0]=strtok(input, ",");

for(i=1;i<5;i++)
{
elements[i] = strtok(NULL, ",");
}

*max = atof(elements[1]);
*min = atof(elements[2]);
}

我的文件中包含以下信息:

PART,2.000,-1,0.050,V

PART,0.975,-1,0.025,V

PART,3.000,+1,0.010,F

GAP,0.000,0.080

文件中没有空行,为了清楚起见,我添加了它们。问题是我的计算都没有完成。我的所有输出都是 0.000000。我想让它实际计算一下,但找不到问题。任何帮助将不胜感激!

最佳答案

您正在传递一个 int 数组

int Impact[10];

...

mean = meanGap(Nominal, Impact, Num_of_Parts);

到一个需要float数组的函数

float meanGap(float Nom[], float Imp[], int size)
<小时/>

您在数组范围之外写入:

int Impact[10];
float Nominal[10], Tolerance[10];
char FoV[10];
...

for(i=0; i<11; i++)

应该是

for(i=0; i<10; i++)
<小时/>

正如 @user2340048 所指出的,PART 是大写的,您正在 if(input_str[0] == 'p' 中检查 p )

关于c - 代码的输出全为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299378/

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