gpt4 book ai didi

c - 我的开关盒出现错误

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

所以我试图创建一个输入函数,它接受用逗号分隔的内容并将每个内容放入它自己的数组中。我想我几乎已经弄清楚了这个开关,但它卡在了第二个语句中。我不知道为什么。

/*************************************************************************

3/25/2015
This program takes in a file of the format
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
does the tolerance analysis
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100
void input(char *gapPart, float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum);
void toleracningPt1(int size, char gapPart[], float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum);
int main(){
/**Decs**/
float nom[ARRAYSIZE]; //holds the nominal values (2.00, .975 ect)
float tollerance[ARRAYSIZE]; //holds the third value (.05, .025, ect)
int SIGNS[ARRAYSIZE]; // signifies if the value goes up or down
char gapPart[ARRAYSIZE];// holds the value if it's a gap or part
char V_F[ARRAYSIZE]; // F things cannot be changed, V things can be
int size=0;
float Spec_Minnimum=0, Spec_Maximum=0;
/**custom functions**/
input(gapPart, nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
toleracningPt1(size, gapPart,nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
return 0;
}
/***********************************************************************************************************/
void input(char *gapPart,float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum){

const char *delimiterCharacters = " ";
const char *delimiterCharacters2 = ",";
const char *filename = "tin.txt";
FILE *inputFile = fopen( filename, "r" );
char buffer[ BUFFER_SIZE ];
char *lastToken;
int i=1, step;

printf("File Data\n");
/* usual error check*/
if(inputFile == NULL ){
fprintf( stderr, "Unable to open file %s\n", filename );
}else{
/**Prints out contents of the file **/
while( fgets(buffer, BUFFER_SIZE,inputFile) != NULL ){// while there is stuff to do this with
lastToken = strtok( buffer, delimiterCharacters );
while( lastToken != NULL ){//same song..
printf( "%s\n", lastToken );
lastToken = strtok( NULL, delimiterCharacters );// clear out lastToken
}

}

rewind(inputFile);

while( fgets(buffer, BUFFER_SIZE,inputFile) != NULL ){// while there is stuff to do this with
lastToken = strtok( buffer, delimiterCharacters2 );
while( lastToken != NULL ){//same song..
//strtok into seperate arrays
while(i=1,i<5,++i){
switch(i){
case 1:
fscanf(inputFile,"%s\n", &gapPart[i]);
printf("debug1");
++i;
break;
case 2:
printf("debug2");
fscanf(inputFile,"%f\n", &nom[i]);
++i;
break;
case 3:
printf("debug3");
fscanf(inputFile,"%d\n", &SIGNS[i]);
++i;
break;
case 4:
printf("debug4");
fscanf(inputFile,"%f\n", &tollerance[i]);
break;
case 5:
printf("debug5");
fscanf(inputFile,"%c\n", &V_F[i]);
break;
default:
printf("Error");
}
}


}
lastToken = strtok( NULL, delimiterCharacters2 );// clear out lastToken
}
}



fclose(inputFile );

}


/*****************************************************************************************************************/
void toleracningPt1(int size, char gapPart[], float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum)
{
int x;
float Act_Gap, Act_Tollerance, Maximum_Gap = 0.0, Minnimum_Gap = 0.0;
for ( x=0, Act_Gap = 0; x<size; x++){ //does tolerance math
Act_Gap = Act_Gap + (nom[x]*SIGNS[x]);
}
for ( x=0, Act_Tollerance = 0; x<size; x++){
Act_Tollerance = Act_Tollerance + (tollerance[x]);
}
for (x= 0, Maximum_Gap = 0; x<size; x++){
Maximum_Gap = (nom[x]*SIGNS[x]+tollerance[x])+Maximum_Gap;
Minnimum_Gap = (nom[x]*SIGNS[x]-tollerance[x])+Minnimum_Gap;
}

printf("Actual Gap Mean: %.3f\"\n", Act_Gap); //printing
printf("Actual Gap Tolerance: %.3f\"\n", Act_Tollerance);
if (Maximum_Gap > Spec_Maximum){
printf("The maximum gap (%.3f\") is (Greater) than specified (%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Maximum_Gap < Spec_Maximum){
printf("The maximum gap (%.3f\") is (Less) than specified (%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Minnimum_Gap > Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Greater) than specified (%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
if (Minnimum_Gap < Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Less) than specified (%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
}

最佳答案

您的问题是由于使用

 while(i=1,i<5,++i)

而不是

 for(i=1;i<5;++i)

它的工作原理如下:

  1. for()循环中,初始化只进入一次 => i=1仅发生一次。
  2. 检查条件用于条件失败时退出循环 => i>=5表示退出循环;每次都会检查这一点。
  3. 增量发生在每次循环迭代结束时 => ++i在每次迭代执行循环体之后发生。

当你做 while(i=1,i<5,++i) 时, while() 内的所有 3 个操作在循环的每次迭代中按顺序发生,并且在开始时发生。

编辑:

移动线

lastToken = strtok(NULL, delimiterCharacters2);// clear out lastToken

当前位置上方有一个括号。

由于lastToken永远不会改变,因此while循环不会退出,因此会一次又一次地循环内容。

解决了代码中的错误并添加了以下代码。请使用像 winmerge 这样的 diff 实用程序来了解每个更改:

/*************************************************************************
3/25/2015
This program takes in a file of the format
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
does the tolerance analysis
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100

int input(char *gapPart, float *nom, float *tollerance, int *SIGNS, char *V_F, float *Spec_Minnimum, float *Spec_Maximum);
void toleracningPt1(int size, char gapPart[], float nom[], float tollerance[], int SIGNS[], char V_F[], float *Spec_Minnimum, float *Spec_Maximum);

char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;/* Count how many elements will be extracted. */
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;

while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}

/* Add space for trailing token.*/
count += last_comma < (a_str + strlen(a_str) - 1);

/* Add space for terminating null string so caller knows where the list of returned strings ends.*/
count++;

result = (char **)malloc(sizeof(char*)* count);

if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);

while (token)
{
assert(idx < count);
*(result + idx++) = _strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}

return result;
}

int main(){
float nom[ARRAYSIZE]; //holds the nominal values (2.00, .975 ect)
float tollerance[ARRAYSIZE]; //holds the third value (.05, .025, ect)
int SIGNS[ARRAYSIZE]; // signifies if the value goes up or down
char gapPart[ARRAYSIZE];// holds the value if it's a gap or part
char V_F[ARRAYSIZE]; // F things cannot be changed, V things can be
int size = 0;
float Spec_Minnimum = 0, Spec_Maximum = 0;

size = input(gapPart, nom, tollerance, SIGNS, V_F, &Spec_Minnimum, &Spec_Maximum);
toleracningPt1(size, gapPart, nom, tollerance, SIGNS, V_F, &Spec_Minnimum, &Spec_Maximum);
getchar();
return 0;
}

void printAllTokens(char** tokens){
int j;
for (j = 0; *(tokens + j); j++){
printf("token=[%s]\n", *(tokens + j));
}
}

void freeTokens(char** tokens){
int j;
for (j = 0; *(tokens + j); j++){
free(*(tokens + j));
}
}

int input(char *gapPart, float *nom, float *tollerance, int *SIGNS, char *V_F, float *Spec_Minnimum, float *Spec_Maximum)
{
char delimiter = ',';
const char *filename = "tin.txt";
char buffer[BUFFER_SIZE];
char** tokens;
int i = 0, j = 0;
FILE *inputFile = fopen(filename, "r");
printf("File Data\n");
/* usual error check*/
if (inputFile == NULL){
fprintf(stderr, "Unable to open file %s\n", filename);
}
else{
while (fgets(buffer, BUFFER_SIZE, inputFile) != NULL){// while there is stuff to do this with
strtok(buffer, "\n");
tokens = str_split(buffer, delimiter);

printAllTokens(tokens);
if (strstr(buffer, "GAP") != NULL) {
*Spec_Minnimum = strtof(*(tokens + 1),NULL);
*Spec_Maximum = strtof(*(tokens + 2), NULL);
}
else{
if (tokens){
for (j = 0; *(tokens + j); j++){
switch (j){
case 0: strcpy(*(tokens + j), &gapPart[i]); break;
case 1: nom[i] = strtof(*(tokens + j), NULL); break;
case 2: SIGNS[i] = atoi(*(tokens + j)); break;
case 3: tollerance[i] = strtof(*(tokens + j), NULL);break;
case 4: V_F[i] = (char)*(tokens + j); break;
default: printf("Error");
}
}
i++;
}
}
printf("\n");
freeTokens(tokens);
free(tokens);
}
}
fclose(inputFile);
return i;
}

void toleracningPt1(int size, char gapPart[], float nom[], float tollerance[], int SIGNS[], char V_F[], float *Spec_Minnimum, float *Spec_Maximum)
{
int x;
float Act_Gap = 0, Act_Tollerance = 0, Maximum_Gap = 0.0, Minnimum_Gap = 0.0;
for (x = 0, Act_Gap = 0; x<size; x++){
Act_Gap = Act_Gap + (nom[x] * SIGNS[x]);
Act_Tollerance = Act_Tollerance + (tollerance[x]);
Maximum_Gap = (nom[x] * SIGNS[x] + tollerance[x]) + Maximum_Gap;
Minnimum_Gap = (nom[x] * SIGNS[x] - tollerance[x]) + Minnimum_Gap;
}

printf("\nActual Gap Mean: %.3f\"\n", Act_Gap);
printf("Actual Gap Tolerance: %.3f\"\n", Act_Tollerance);

printf("The maximum gap (%.3f\") is (", Maximum_Gap);
printf(Maximum_Gap > *Spec_Maximum ? "Greater than" : (Maximum_Gap < *Spec_Maximum ? "Less than" : "Equal to"));
printf(") than specified (%.3f\")\n", *Spec_Maximum);

printf("The minimum gap (%.3f\") is (", Minnimum_Gap);
printf(Minnimum_Gap > *Spec_Minnimum ? "Greater than" : (Minnimum_Gap < *Spec_Minnimum ? "Less than" : "Equal to"));
printf(") than specified (%.3f\")\n", *Spec_Minnimum);
}

间隙计算的输出:

enter image description here

关于c - 我的开关盒出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337675/

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