#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。我希望它实际计算它但无法找到问题。任何帮助将不胜感激!
答案 0 :(得分:1)
您正在传递int
s
int Impact[10];
...
mean = meanGap(Nominal, Impact, Num_of_Parts);
到期望float
s
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
是大写的,您正在检查p
中的if(input_str[0] == 'p')
答案 1 :(得分:0)
以下行: if(input_str [0] ==&#39; p&#39;)应替换为: if(input_str [0] ==&#39; P&#39;)因为在数据文件中,&#39; PART&#39;是大写的,但你正在检查小写&#39; p&#39;。