我需要有关此代码的帮助,我不知道该怎么做。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int input(float nom[],float toler[],int SIGN[],char V_F[],float *Spec_Min,float *Spec_Max);
float tolerances(int size, float nom[],float toler[],int SIGN[],char V_F[],float Spec_Min,float Spec_Max);
void adjust(int size, float nom[],float toler[],int SIGN[],char V_F[],float Spec_Min,float Spec_Max, int answer, float *dimension, float *impact, float *DgapMean, float *gaptol, float *newtol, float *newdim);
int main(void)
{
float nom[70], toler[70], Spec_Min=0, Spec_Max=0;
int SIGN[70];
char V_F[70];
int i, n;
n = input( nom, toler, SIGN, V_F, &Spec_Min, &Spec_Max);
for(i=0; i<n; ++i)
{
printf("Part: %f, %d, %f, %c\n\n", nom[i], SIGN[i], toler[i], V_F[i]);//printing off all parts and the dimensions tolerances and if the part is fixed or variable
}
printf("_______________________\n");
printf("Gap: %f, %f\n\n", Spec_Min, Spec_Max);//print the size of the gap
int size = 0;
size = input( nom, toler, SIGN, V_F, &Spec_Min, &Spec_Max);
tolerances(size, nom, toler, SIGN, V_F, Spec_Min, Spec_Max);
>line 43 is below this
adjust( size, nom, toler, SIGN, V_F, Spec_Min, Spec_Max, answer, &dimension, &impact, &DgapMean, &gaptol, &newtol, &newdim);
return 0;
}
int input(float nom[],float toler[],int SIGN[],char V_F[],float *Spec_Min,float *Spec_Max)
{
int status, i;
FILE *FTIN;
FTIN = fopen ("C:\\EGR107\\input.txt", "r");
if (FTIN == NULL)
{
printf("ERROR\n");//if the file didnt work show an error
return -1;
}
else
{
for(i=0;;)
{
if((status = fscanf(FTIN," PART,%f,%d,%f,%c", &nom[i], &SIGN[i], &toler[i], &V_F[i]))==4)//scans parts until it doesnt start with part
{
++i;
}
else if((status = fscanf(FTIN, " GAP,%f,%f", Spec_Min, Spec_Max))==0)//scans gap after part was scaned
{
fgetc(FTIN);
}
else if(status == EOF)
{
break;
}
}
}
fclose(FTIN);
return i;
}
float tolerances(int size, float nom[],float toler[],int SIGN[],char V_F[],float Spec_Min,float Spec_Max)
{
int x;
float Act_Gap, Act_Toler, Max_Gap = 0.0, Min_Gap = 0.0;
for ( x=0, Act_Gap = 0; x<size; x++) //does math for the tolerance
{
Act_Gap = Act_Gap + (nom[x]*SIGN[x]);
}
for ( x=0, Act_Toler = 0; x<size; x++)
{
Act_Toler = Act_Toler + (toler[x]);
}
for (x= 0, Max_Gap = 0; x<size; x++)
{
Max_Gap = (nom[x]*SIGN[x]+toler[x])+Max_Gap;
Min_Gap = (nom[x]*SIGN[x]-toler[x])+Min_Gap;
}
printf("_______________________\n");
printf("Actual Gap Mean: %.3f\"\n\n", Act_Gap); //printing actual gap and gap tolerances
printf("Actual Gap Tolerance: %.3f\"\n\n", Act_Toler);
if (Max_Gap > Spec_Max)
{
printf("The Maximum gap %.3f\" is Greater than specified %.3f\"\n\n", Max_Gap, Spec_Max);//printing whether or not the max gap is greater than or less than the specified
}
if (Max_Gap < Spec_Max)
{
printf("The Maximum gap %.3f\" is Less than specified %.3f\"\n\n", Max_Gap, Spec_Max);
}
if (Min_Gap > Spec_Min)
{
printf("The Minimum gap %.3f\" is Greater than specified %.3f\"\n\n", Min_Gap, Spec_Min);//printing whether or not the min gap is greater than or less than the specified
}
if (Min_Gap < Spec_Min)
{
printf("The Minimum gap %.3f\" is Less than specified %.3f\"\n\n", Min_Gap, Spec_Min);
}
return 0;
}
void adjust(int size, float nom[],float toler[],int SIGN[],char V_F[],float Spec_Min,float Spec_Max,int answer, float *dimension, float *impact, float *DgapMean, float *gaptol, float *newtol, float *newdim)
{
int i, x, status, change;
double gapmean, Atolerance, changeinNOM, changeinTOL;
double Part, TOL;
for(i=0; i<size; i++)
{
gapmean = gapmean+(dimension[i]*impact[i]); //gets actual gap of parts
printf("Gap Mean: %.3f\n", gapmean);
Atolerance = Atolerance + toler[i]; //gets actual tolerance of parts
printf("Actual Tolerance: %.3f\n",Atolerance);
}
changeinNOM = *DgapMean - gapmean; //finds change in the dimension
printf("Change in nominal value: %.3f\n",changeinNOM);
changeinTOL = Atolerance - *gaptol; //finds change in the tolerance
printf("Change in tolerance value: %.3f\n",changeinTOL);
printf("____________________________\n"); //return
printf("\n"); //return
for(i=0; i<size; i++)
{
if(V_F[i] == 'V') //looks for variable part
{
Part = dimension[i] - changeinNOM; //calculates new dimension of part
TOL = toler[i] - changeinTOL; //calculates new tolerance of part
if(TOL < 0)
{
printf("Part %d cannot be changed\n", i+1);
}
if(TOL >= 0)
{
printf("Part %d will be changed to %.3f\n",i+1,Part);
printf("Tolerance of Part %d will be changed to %.3f\n",i+1, TOL);
printf("Would you like to save the changes?\n");
printf("Please enter a number value 1 for yes or 0 or no.\n");
do
{
status = scanf("%d", &answer);
if(x != 1 && x != 0)
{
printf("Please enter a number value 1 or 0.\n");
fflush(stdin);
}
if(answer == 1)
{
change = 1;
}
else if(answer == 0)
{
change = 0;
}
}
while(answer != 1 && answer != 0 || status == 0);
if(change == 1)
{
newdim[i] = Part;
newtol[i] = TOL;
printf("New Part dimension: %.3f and new part tolerance: %.3f\n", newdim[i], newtol[i] );
printf("Your changes have been made.\n");
}
else if(change == 0)
{
printf("Your changes will not be saved.\n");
}
}
}
}
}
编译器输出:
||=== Build: Debug in Final_project_12_2_15 (compiler: GNU GCC Compiler) ===|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c||In function 'main':|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'answer' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|note: each undeclared identifier is reported only once for each function it appears in|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'dimension' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'impact' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'DgapMean' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'gaptol' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'newtol' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|43|error: 'newdim' undeclared (first use in this function)|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c||In function 'adjust':|
C:\EGR107\Code_Blocks\Final_project_12_2_15\main.c|179|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
答案 0 :(得分:1)
在所使用的函数的开头声明变量。在那之后留一个空行。也不要忘记初始化它们(在使用它们的值之前的某个地方),否则你只能得到垃圾。
关于警告:
您必须注意要使用运算符的优先级。此外,&&
和||
都是二元运算符,因此它们采用2个参数。这就是你必须加括号的原因。可能就像:
while(answer != 1 && (answer != 0 || status == 0))