gpt4 book ai didi

进行公差分析的代码

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:50 26 4
gpt4 key购买 nike

因此,我正在尝试构建这段代码,但它在我的第二个自定义函数中的某处崩溃了。我还想打印出文件“tin.txt”的内容,但任何带有 EOF 的内容似乎都会使它崩溃。我正在用 C 编写代码。

似乎只有垃圾进入了我的阵列。无论如何我都需要使用 string tok ,有什么提示吗?编辑感谢您到目前为止的所有帮助。我已经输入了大部分建议的更改,并且我的编译器(代码块,以防万一)已经停止了痛苦的尖叫。现在我只是在努力使用 strtok 将信息放入数组中,我只是不知道如何让它工作。 (我也不确定此时我是应该问第二个问题还是只编辑这个问题,我刚刚编辑了这个问题。)

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

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(float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum);
void toleracningPt1(int size, 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 V_F[ARRAYSIZE]; // F things cannot be changed, V things can be
int size=0;

float Spec_Minnimum, Spec_Maximum;
/**custom functions**/
input( nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
toleracningPt1(size, nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
}
/***********************************************************************************************************/
void input(float *nom,float *tollerance,int *SIGNS,char *V_F,float Spec_Minnimum,float Spec_Maximum){

const char *delimiter_characters = " ";
const char *filename = "tin.txt";
FILE *input_file = fopen( filename, "r" );
char buffer[ BUFFER_SIZE ];
char *last_token;

printf("File Data\n");
/* usual error check*/
if( input_file == NULL ){
fprintf( stderr, "Unable to open file %s\n", filename );
}else{

while( fgets(buffer, BUFFER_SIZE, input_file) != NULL ){// while there is stuff to do this with
last_token = strtok( buffer, delimiter_characters );
while( last_token != NULL ){//same song..
printf( "%s\n", last_token );
last_token = strtok( NULL, delimiter_characters );// clear out last_token
}

}


fclose( input_file );

}


}
/*****************************************************************************************************************/
void toleracningPt1(int size, 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);
}
}

最佳答案

1) 我发现 input 函数有问题:

  • loop for (i=0; status != EOF; i++) 如果您的文件 tin.txt 超过 100,则可能导致段错误行(nom 和其他数组具有固定大小)。

  • 您将 float Spec_Minnimumfloat Spec_Maximum 作为参数发送给 input,但期望在函数完成后接收值...你不会收到。我必须更改这些参数和 scanf 的参数类型,例如:

    float input(float nom[],float tollerance[],int SIGNS[],char V_F[],float * p_Spec_Minnimum,float * p_Spec_Maximum)

    . . .

    fscanf(FTIN, "GAP,%f,%f\n", p_Spec_Minnimum, p_Spec_Maximum);

    所以将函数 input 称为 input( nom, tolerance, SIGNS, V_F, &Spec_Minnimum, &Spec_Maximum);

  • 您将 fsacnf 返回的值保存到 status 但您不检查第一个 fscanf 的状态...奇怪的编码风格

  • 函数 input 的类型为 float,但在错误情况下它返回 1(整数值),如果没有则返回 NOTHING文件打开错误

  • after function input finish main function 没有关于从文件中读取了多少行以及在数组中存储了多少元素的信息。

2) 在使用数据之前,最好在屏幕上看到这些数据,因此您需要打印从文件中读取的数据的功能。

所以我为input和数据output提出了以下函数:

// returns the number elements stored in arrays
int input(float nom[],float tollerance[],int SIGNS[],char V_F[],float* p_Spec_Minnimum,float* p_Spec_Maximum)
{
int status = 0, i,c;
FILE *FTIN;
FTIN = fopen ("tin.txt", "r");

if (FTIN == NULL){ //file empty/broken error
printf("ERROR\n");
return (-1);
}
/******/
else{
for (i=0; status != EOF && i < 100; ){ //reads until EoF, even though some guy on stackoverflow taught me it's bad
status = fscanf(FTIN,"PART,%f,%d,%f,%c\n", &nom[i], &SIGNS[i], &tollerance[i], &V_F[i]); //scans for a part
if(status == 4)
{
i++;
}
status = fscanf(FTIN, "GAP,%f,%f\n", p_Spec_Minnimum, p_Spec_Maximum); //scans for a gap
printf("Reading Info Into Arrays\n");
}
fclose(FTIN);
return i;
}
}
// output arrays in special format
void output(int size, float nom[],float tollerance[],int SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum)
{
int i;
for (i=0; i < size; i++)
{
printf("PART,%f,%d,%f,%c\n", nom[i], SIGNS[i], tollerance[i], V_F[i]);
}
printf("GAP,%f,%f\n", Spec_Minnimum, Spec_Maximum);
}

3) 您的数组大小必须在您的toleracningPt1 函数中已知,因此按如下方式进行:

float toleracningPt1(int size, 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);
}
}

现在你的主要功能是

int main(void){
/**Decs**/
float nom[100], tollerance[100];
int SIGNS[100];
char V_F[100];
//int status, i, x;
float Act_Gap, Act_Tollerance, Spec_Minnimum, Spec_Maximum;
/**custom functions**/
int size = 0;
size = input( nom, tollerance, SIGNS, V_F, &Spec_Minnimum, &Spec_Maximum);
output( size, nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
toleracningPt1(size, nom, tollerance, SIGNS, V_F, Spec_Minnimum, Spec_Maximum);
}

P.S.: 我不知道“公差分析”是什么,但在我修复后,对于您提供的示例文件,您的程序输出结果:

 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 specified (0.000")

关于进行公差分析的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29321743/

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