📄 units.c
字号:
UnitsCopy(d3,dp3); } else d3 = UnitsPower(dp3,d->time_expnt, NO); dd1 = UnitsMult(d1,d2); UnitsMultRep(d,dd1,d3); free((char *)d1->units_name); free((char *)d1); free((char *)d2->units_name); free((char *)d2); free((char *)d3->units_name); free((char *)d3); free((char *)dp1->units_name); free((char *)dp1); free((char *)dp2->units_name); free((char *)dp2); free((char *)dp3->units_name); free((char *)dp3); free((char *)dd1->units_name); free((char *)dd1); } /* Append radian components to units name */ if(d->radian_expnt != 0) { RadUnitsNameExtension( d ); } return(d); } /* [l] : Express units in compact form */ if(d->units_type == SI) { dp1 = DefaultUnits("m"); dp2 = DefaultUnits("kg"); dp3 = DefaultUnits("sec"); dp4 = DefaultUnits("deg_C"); } else { dp1 = DefaultUnits("in"); dp2 = DefaultUnits("lb"); dp3 = DefaultUnits("sec"); dp4 = DefaultUnits("deg_F"); } /* Units of Mass */ if( d->mass_expnt!=0 ) { if( d->mass_expnt==1 ) { d2 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); UnitsCopy(d2,dp2); } else d2 = UnitsPower(dp2,d->mass_expnt, NO); } else { d2 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); ZeroUnits(d2); d2->units_type = SI_US; } /* Units of Length */ if( d->length_expnt!=0 ) { if( d->length_expnt==1 ) { d1 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); UnitsCopy(d1,dp1); } else d1 = UnitsPower(dp1,d->length_expnt, NO); } else { d1 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); ZeroUnits(d1); d1->units_type = SI_US; } /* Units of Time */ if( d->time_expnt!=0 ) { if( d->time_expnt==1 ) { d3 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); UnitsCopy(d3,dp3); } else d3 = UnitsPower(dp3,d->time_expnt, NO); } else { d3 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); ZeroUnits(d3); d3->units_type = SI_US; } if( d->temp_expnt!=0 ) { if( d->temp_expnt==1 ) { d4 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); UnitsCopy(d4,dp4); } else d4 = UnitsPower(dp4,d->temp_expnt, NO); } else { d4 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS)); ZeroUnits(d4); d4->units_type = SI_US; } dd1 = UnitsMult(d1,d2); dd2 = UnitsMult(d3,d4); UnitsMultRep(d,dd1,dd2); free((char *)d1->units_name); free((char *)d1); free((char *)d2->units_name); free((char *)d2); free((char *)d3->units_name); free((char *)d3); free((char *)d4->units_name); free((char *)d4); free((char *)dp1->units_name); free((char *)dp1); free((char *)dp2->units_name); free((char *)dp2); free((char *)dp3->units_name); free((char *)dp3); free((char *)dp4->units_name); free((char *)dp4); free((char *)dd1->units_name); free((char *)dd1); free((char *)dd2->units_name); free((char *)dd2);#ifdef DEBUG printf(" Leaving UnitsSimplify() \n");#endif return(d);}/* * ============================================================ * RadUnitsNameExtension() : Extend "radians" to units name. * * Input : DIMENSIONS *d : Pointer to dimensions data structure * Ouput : DIMENSIONS *d : Pointer to "simplified" dimensions * ============================================================ */#ifdef __STDC__RadUnitsNameExtension( DIMENSIONS *d )#elseRadUnitsNameExtension ( d )DIMENSIONS *d;#endif{#ifdef DEBUG printf("Enter RadUnitsNameExtension() \n");#endif /* [a] : Check for simple extension to name */ switch ((int) d->radian_expnt) { case -1: d->units_name = strcat(d->units_name, (char *) "/rad"); break; case 1: d->units_name = strcat(d->units_name, (char *) ".rad"); break; default: break; } /* [b] : Energy due to rotation */ if( d->mass_expnt == 1 && d->length_expnt == 2 && d->time_expnt == -2 && d->radian_expnt == 1 ) { free ( (char *) d->units_name ); d->units_name = (char *) SaveString ("Joule"); }#ifdef DEBUG printf("Leave RadUnitsNameExtension() \n");#endif}/* * ============================================================ * RadUnitsSimplify() : Simplify Radian Units * * Input : DIMENSIONS *d : Pointer to dimensions data structure * Ouput : DIMENSIONS *d : Pointer to "simplified" dimensions * ============================================================ */#ifdef __STDC__DIMENSIONS *RadUnitsSimplify( DIMENSIONS *d )#elseDIMENSIONS *RadUnitsSimplify( d )DIMENSIONS *d;#endif{SYMBOL *hp;#ifdef DEBUG printf(" Enter RadUnitsSimplify() \n");#endif /* [a] : Are unit names are in the symbol table */ if( d==(DIMENSIONS *)NULL ) { printf("Warning: d is NULL, in RadUnitsSimplify()\n"); return (DIMENSIONS *)NULL; } hp = (SYMBOL *)NULL; if(d->units_name != NULL) hp = lookup(d->units_name); if(hp != NULL) return(d); /* [b] : Check for radian units */ if(d->length_expnt == 0 && d->time_expnt == 0 && d->mass_expnt == 0 && d->temp_expnt == 0 ) { free((char *)d->units_name); d->units_name = SaveString( (char *) "rad" ); d->scale_factor = 1.0; d->units_type = SI_US; return(d); }#ifdef DEBUG printf(" Leaving RadUnitsSimplify() \n");#endif return(d);}/* * ========================== * UnitsPrint() : Print Units * ========================== */#ifdef __STDC__void UnitsPrint(DIMENSIONS *d)#elsevoid UnitsPrint(d)DIMENSIONS *d;#endif{#ifdef DEBUG printf("\n");#endif if( d==(DIMENSIONS *)NULL ) { printf("Unit d is NULL, in UnitsPrint()\n"); return; } if( d->units_name != (char *)NULL ) printf("\n units_name = %s\n", d->units_name); else printf("\n units_name = %s\n", (char *)"NULL"); printf(" d->units_type = %d \n",d->units_type); printf(" d->scale_factor = %lg\n",d->scale_factor); printf(" d->mass_expnt = %g \n",d->mass_expnt ); printf(" d->length_expnt = %g \n",d->length_expnt); printf(" d->time_expnt = %g \n",d->time_expnt ); printf(" d->temp_expnt = %g \n",d->temp_expnt ); printf(" d->radian_expnt = %g \n",d->radian_expnt );#ifdef DEBUG printf("\n");#endif}/* * ================================================================== * Usage : * int : L ; char * name1, * name2, ... * L = UnitsLength(name1, "STOP"); * or L = UnitsLength(name1, name2, ..., "STOP"); * ================================================================== */ #ifdef __STDC__int UnitsLength( char *first_name, ... )#elseint UnitsLength(va_alist)va_dcl#endif{char *name;int length;#ifdef __STDC__va_list arg_ptr;#elseva_list arg_ptr;char *first_name;#endif#ifdef DEBUG printf(" Enter UnitsLength() \n");#endif#ifdef __STDC__ va_start(arg_ptr, first_name);#else va_start(arg_ptr); first_name = va_arg(arg_ptr, char *);#endif for (name = first_name, length = (int) 0; ; name = va_arg(arg_ptr, char * ) ) { if(name != (char *)NULL && !strcmp(name, "STOP") ) break; if (name != (char *)NULL) { length += strlen(name); } else length += 0; } va_end(arg_ptr);#ifdef DEBUG printf(" Leaving UnitsLength() length = %d\n", length);#endif return(length);}/* * ================================================== * BufferPrint() : Print contents of matrix buffer * ================================================== */ #ifdef __STDC__void BufferPrint(char *name, DIMENSIONS *array, int no_col)#elsevoid BufferPrint(name, array, no_col)char *name;DIMENSIONS *array;int no_col;#endif{int i;#ifdef DEBUG printf("\n Enter BufferPrint()\n");#endif printf("\n In BufferPrint() no_of col = %d \n", no_col); printf(" \n buffer_name = %s\n", name); if(array != (DIMENSIONS *)NULL) { for (i = 1; i <= no_col; i++) { printf("\n col/row[%d] \n", i); UnitsPrint(&array[i-1]); } } else printf("\n Buffer = NULL \n");#ifdef DEBUG printf("\n Leaving BufferPrint()\n");#endif }/* * ================================================== * BufferInit() : Allocate and zero Matrix Buffer * * Input : int no_col : number of elements in buffer * Output : pointer to array of DIMENSIONS. * ================================================== */ #ifdef __STDC__DIMENSIONS *BufferInit(int no_col)#elseDIMENSIONS *BufferInit(no_col)int no_col;#endif{DIMENSIONS *array;int i;#ifdef DEBUG printf("\n Enter BufferInit()\n");#endif array = (DIMENSIONS *) MyCalloc(no_col, sizeof(DIMENSIONS)); for( i = 1; i <= no_col; i++) ZeroUnits(&array[i-1]);#ifdef DEBUG BufferPrint(" no name ", array, no_col); printf("\n Leaving BufferInit()\n");#endif return (array);}/* * ======================================================================== * ConvertTempUnits() : Convert temperature units * * Input : * Output : * ======================================================================== */#ifdef __STDC__double ConvertTempUnits(char *name, double value,int units_type)#elsedouble ConvertTempUnits(name, value, units_type)char *name;double value;int units_type;#endif{#ifdef DEBUG printf("**** Enter ConvertTempUnits \n"); printf(" : name = %s \n", name); printf(" : value= %g \n", value); printf(" : type = %d \n", units_type);#endif switch(units_type) { case SI: /* convert to SI */ if(name != (char *)NULL) { if( !strcmp(name, "deg_C") || !strcmp(name, "deg_F") ) value = 5.0*(value - 32.0)/9.0; else { printf("*** In ConvertTempUnits(): units_name = %s", name); FatalError("Try to convert a non temperature units", (char *) NULL); } } break; case US: /* convert to US */ if(name != (char *)NULL) { if( !strcmp(name, "deg_C") || !strcmp(name, "deg_F") ) value = 9.0*value/5.0 + 32.0; else { printf("*** In ConvertTempUnits(): units_name = %s", name); FatalError("Try to convert a non temperature units", (char *) NULL); } } break; } return(value);}/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -