此容差分析代码编译并运行,但输出不正确,我认为这是对文件和文件中数据的错误处理。
文件中的数据是:
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
代码:
#include <stdlib.h>
#include <string.h>
#include <math.h>
void parsePart(char input[], float*pnom, int*pinp, float*ptol, char*pFV);
void parseGap(char input[], float *pmin, float *pmax);
float meanGap(float nom[], float imp[], int size);
float tolGap(float tol[], int size);
int main()
{
FILE *ptable;
int i, num_of_parts;
int impact[10];
float nominal[10], tolerance[10];
char FV[10];
char input_str[30];
float max, min, gap, tol;
float curr_max, curr_min;
ptable=fopen("D:\\Input.txt", "r");
for(i=0; i<11; i++);
{
fgets(input_str, 30, ptable);
if(input_str[0] == 'P')
{
parsePart(input_str, &nominal[i], &impact[i], &tolerance[i], &FV[i]);
}
else
{
parseGap(input_str, &min, &max);
num_of_parts = i;
}
}
gap = meanGap(nominal, impact, num_of_parts);
tol = tolGap(tolerance, num_of_parts);
curr_max = gap+tol;
curr_min = gap-tol;
printf("Gap mean is: %f inches\n", gap);
printf("Gap tolerance is: %f inches\n", tol);
if(fabs(max-curr_max)< 0.0001 && curr_max > (gap*2)) //they are equal
{
printf("The maximum gap is %f which is greater than the allowed %f\n", curr_max, gap*2);
}
else
{
printf("The maximum gap is %f which is within the allowed %f\n", curr_max, gap*2);
}
if(fabs(min+curr_min)<0.0001 && curr_min < gap-gap)
{
printf("The minimum gap is %f which is less than the allowed %f\n", curr_min, gap-gap);
}
else
{
printf("The minimum gap is %f which is within the allowed %f\n", curr_min, gap-gap);
}
return 0;
}
void parsePart(char input[], float*pnom, int*pinp, float*ptol, char*pFV)
{
int i;
char * field[5];
field[0]=strtok(input, ",");
for(i=1; i<5; i++);
{
field[i] = strtok(NULL, ",");
}
*pnom = atof(field[1]);
*pinp = atoi(field[2]);
*ptol = atof(field[3]);
*pFV = *field[4];
}
void parseGap(char input[], float *pmin, float *pmax)
{
char *field[2];
field[0] = strtok(input, ",");
field[1] = strtok(NULL, ",");
field[2] = strtok(NULL, ",");
*pmin = atof(field[1]);
*pmax = atof(field[2]);
}
float meanGap(float nom[], float imp[], int size)
{
int i;
float sum=0;
for(i=0; i<size; i++);
{
sum += nom[i]*imp[i];
}
return sum;
}
float tolGap(float tol[], int size)
{
int i;
float sum=0;
for(i=0; i<size; i++);
{
sum += tol[i];
}
return sum;
}
输出应该类似于:
Actual Gap Mean: 0.025”
Actual Gap Tolerance: 0.085”
The Maximum Gap (0.110”) is (Greater) than specified (0.080”)
The Minimum Gap (-0.060”) is (Less) than the specified (0.000”)
我得到0表示差距,而所有其他值都是非常大的数字。
关于我可能出错或需要改进的地方的任何和所有提示都很棒。谢谢。
答案 0 :(得分:0)
在第85行,char *field[2];
应为char *field[3];
。
答案 1 :(得分:0)
首先,数组影响 等的大小为10,所以
for(i=0; i<11; i++);
错误有两个原因:
1)循环应该从0到9
2) for 语句不应该有;在它之后,它终止了整个陈述。
答案 2 :(得分:-1)
typedef struct{char c[6];} String;
String field1,field2,… …;
String *pf1,*pf2,… …;
pf1=field1;
pf2=field2;
…
…
pf1=strtok(input,…
是否正确(大纲)代码 parseGap ,假设输入文件中的最长数据长度为5个字符,