📄 units.c
字号:
UnitsPrint(d);#endif if( d2==(DIMENSIONS *)NULL ) FatalError("In UnitsDivRep() : unit divider is a NULL pointer",(char *)NULL); if( d1==(DIMENSIONS *)NULL ) { printf("Warning: d1 is NULL, in UnitsDivRep()\n"); free((char *)d->units_name); free((char *)d); d = (DIMENSIONS *)NULL; return (DIMENSIONS *)NULL; } dp1 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS)); dp2 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS)); UnitsCopy(dp1,d1); UnitsCopy(dp2,d2); if(dp1->units_type == dp2->units_type) { d->units_type = dp1->units_type; } else { if( dp1->units_type==SI && dp2->units_type==US ) { d->units_type = SI; if( dp2->units_name != (char *)NULL ) UnitsTypeConvert( dp2, SI ); } else if( dp1->units_type==US && dp2->units_type==SI ) { d->units_type = SI; if( dp1->units_name != (char *)NULL ) UnitsTypeConvert( dp1, SI ); } else if(dp1->units_type == SI_US) d->units_type = dp2->units_type; else if(dp2->units_type == SI_US) d->units_type = dp1->units_type; } if(dp1->units_name != (char *)NULL) { if( FLAG == YES) { free((char *)d->units_name); length = UnitsLength(dp1->units_name,dp2->units_name,"STOP"); d->units_name = (char *)MyCalloc(length+6,sizeof(char)); d->units_name = strcpy(d->units_name, (char *)"("); d->units_name = strcat(d->units_name, dp1->units_name); d->units_name = strcat(d->units_name, (char *) ")"); if( dp2->units_name!=(char *)NULL ) { d->units_name = strcat(d->units_name, (char *) "/("); d->units_name = strcat(d->units_name, dp2->units_name); d->units_name = strcat(d->units_name, (char *) ")"); } } else { free((char *)d->units_name); length = UnitsLength(dp1->units_name,dp2->units_name,"STOP"); d->units_name = (char *)MyCalloc(length+2,sizeof(char)); d->units_name = strcpy(d->units_name, dp1->units_name); if( dp2->units_name!=(char *)NULL ) { d->units_name = strcat(d->units_name, (char *) "/"); d->units_name = strcat(d->units_name, dp2->units_name); } } } else if(dp2->units_name != (char *)NULL) { if( FLAG == YES) { free((char *)d->units_name); length = UnitsLength(dp2->units_name,"STOP"); d->units_name = (char *)MyCalloc(length+5,sizeof(char)); d->units_name = strcpy(d->units_name, (char *)"1/("); d->units_name = strcat(d->units_name, dp2->units_name); d->units_name = strcat(d->units_name, (char *) ")"); } else { free((char *)d->units_name); length = UnitsLength(dp2->units_name,"STOP"); d->units_name = (char *)MyCalloc(length+3,sizeof(char)); d->units_name = strcpy(d->units_name, (char *)"1/"); d->units_name = strcat(d->units_name, dp2->units_name); } } else { free((char *)d->units_name); d->units_name = (char *)NULL; }#ifdef DEBUG printf(" === In UnitsDivRep() ============= \n"); UnitsPrint(dp1); UnitsPrint(dp2); printf(" === In UnitsDivRep() ============= \n");#endif d->scale_factor = dp1->scale_factor/dp2->scale_factor; d->length_expnt = dp1->length_expnt - dp2->length_expnt; d->mass_expnt = dp1->mass_expnt - dp2->mass_expnt; d->time_expnt = dp1->time_expnt - dp2->time_expnt; d->temp_expnt = dp1->temp_expnt - dp2->temp_expnt; d->radian_expnt = dp1->radian_expnt - dp2->radian_expnt; free((char *)dp1->units_name); free((char *)dp1); free((char *)dp2->units_name); free((char *)dp2);#ifdef DEBUG printf(" At the end of UnitsDivRep() :\n"); UnitsPrint(d); printf(" Leaving UnitsDivRep() :\n");#endif return (d);}/* * ============================================================== * UnitsPower() : * * Input : DIMENSIONS *d1 : pointer to "units" * : double value : value of power "exponent" * : int FLAG : YES/NO flag for use of "()" * Output : DIMENSIONS * : pointer to "d1^value" units. * ============================================================== */#ifdef __STDC__DIMENSIONS *UnitsPower( DIMENSIONS *d1, double value, int FLAG )#elseDIMENSIONS *UnitsPower( d1, value, FLAG )DIMENSIONS *d1;double value;int FLAG;#endif{DIMENSIONS *d;char *cp;int length;#ifdef DEBUG printf(" Enter UnitsPower() :\n"); UnitsPrint(d1);#endif if( d1==(DIMENSIONS *)NULL ) { printf("Warning: d1 is NULL, in UnitsPower()\n"); return (DIMENSIONS *)NULL; } d = (DIMENSIONS *) MyCalloc(1,sizeof(DIMENSIONS)); if(d1->units_name != (char *)NULL && value != 0.0) { cp = (char *)calloc(20,sizeof(char)); sprintf(cp,"%g", value); length = UnitsLength(d1->units_name,cp,"STOP"); if( FLAG == YES ) { d->units_name = (char *)MyCalloc(length+4,sizeof(char)); d->units_name = strcpy(d->units_name, (char *) "("); d->units_name = strcat(d->units_name, d1->units_name); d->units_name = strcat(d->units_name, (char *) ")"); } else { d->units_name = (char *)MyCalloc(length+2,sizeof(char)); d->units_name = strcpy(d->units_name, d1->units_name); } d->units_name = strcat(d->units_name, (char *) "^"); d->units_name = strcat(d->units_name, cp); free((char *) cp); } else d->units_name = (char *)NULL; if(value > 0) { d->scale_factor = pow(d1->scale_factor, value); d->units_type = d1->units_type; d->length_expnt = d1->length_expnt * value; d->mass_expnt = d1->mass_expnt * value; d->time_expnt = d1->time_expnt * value; d->temp_expnt = d1->temp_expnt * value; d->radian_expnt = d1->radian_expnt * value; } else if(value == 0.0) { d->scale_factor = 1.0; d->units_type = d1->units_type; d->length_expnt = 0.0; d->mass_expnt = 0.0; d->time_expnt = 0.0; d->temp_expnt = 0.0; d->radian_expnt = 0.0; } else { d->scale_factor = 1.0/pow(d1->scale_factor, ABS(value)); d->units_type = d1->units_type; d->length_expnt = d1->length_expnt * value; d->mass_expnt = d1->mass_expnt * value; d->time_expnt = d1->time_expnt * value; d->temp_expnt = d1->temp_expnt * value; d->radian_expnt = d1->radian_expnt * value; } #ifdef DEBUG printf(" Leaving UnitsPower() :\n");#endif return (d);}#ifdef __STDC__DIMENSIONS *UnitsPowerRep( DIMENSIONS *d, DIMENSIONS *d1, double value, int FLAG )#elseDIMENSIONS *UnitsPowerRep( d, d1, value, FLAG )DIMENSIONS *d;DIMENSIONS *d1;double value;int FLAG;#endif{char *cp;DIMENSIONS *dp;int length;#ifdef DEBUG printf(" Enter UnitsPowerRep() :\n"); UnitsPrint(d);#endif if( d1==(DIMENSIONS *)NULL ) { printf("Warning: d1 is NULL, in UnitsPowerRep()\n"); free((char *)d->units_name); free((char *)d); d = (DIMENSIONS *)NULL; return (DIMENSIONS *)NULL; } dp = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS)); UnitsCopy(dp,d1); free((char *)d->units_name); if(dp->units_name != (char *)NULL && value != 0.0) { cp = (char *)calloc(20,sizeof(char)); sprintf(cp,"%g", value); length = UnitsLength(dp->units_name,cp,"STOP"); if( FLAG == YES ) { d->units_name = (char *)MyCalloc(length+4,sizeof(char)); d->units_name = strcpy(d->units_name, (char *) "("); d->units_name = strcat(d->units_name, dp->units_name); d->units_name = strcpy(d->units_name, (char *) ")"); } else { d->units_name = (char *)MyCalloc(length+2,sizeof(char)); d->units_name = strcpy(d->units_name, dp->units_name); } d->units_name = strcat(d->units_name, (char *) "^"); d->units_name = strcat(d->units_name, cp); free((char *) cp); } else d->units_name = (char *)NULL; if(value > 0) { d->scale_factor = pow(dp->scale_factor, value); d->units_type = dp->units_type; d->length_expnt = dp->length_expnt * value; d->mass_expnt = dp->mass_expnt * value; d->time_expnt = dp->time_expnt * value; d->temp_expnt = dp->temp_expnt * value; d->radian_expnt = dp->radian_expnt * value; } else if(value == 0.0) { d->scale_factor = 1.0; d->units_type = dp->units_type; d->length_expnt = 0.0; d->mass_expnt = 0.0; d->time_expnt = 0.0; d->temp_expnt = 0.0; d->radian_expnt = 0.0; } else { d->scale_factor = 1.0/pow(dp->scale_factor, ABS(value)); d->units_type = dp->units_type; d->length_expnt = dp->length_expnt * value; d->mass_expnt = dp->mass_expnt * value; d->time_expnt = dp->time_expnt * value; d->temp_expnt = dp->temp_expnt * value; d->radian_expnt = dp->radian_expnt * value; } free((char *)dp->units_name); free((char *)dp); #ifdef DEBUG printf(" Leaving UnitsPowerRep() :\n");#endif return (d);}/* * ============================ * Copy units from "d2" to "d1" * ============================ */#ifdef __STDC__DIMENSIONS *UnitsCopy(DIMENSIONS *d1, DIMENSIONS *d2)#elseDIMENSIONS *UnitsCopy(d1, d2)DIMENSIONS *d1, *d2;#endif{int length;#ifdef DEBUG printf(" Enter UnitsCopy() :\n");#endif /* [a] : Check for NULL pointer dimensions */ if( d2==(DIMENSIONS *) NULL ) { printf("Warning : d2 is NULL in UnitsCopy(), return NULL\n"); free((char *) d1->units_name); free((char *) d1); d1 = (DIMENSIONS *)NULL; return d1; } /* [b] : Copy units data structure */ if(d2->units_name != (char *)NULL) { free((char *)d1->units_name); d1->units_name = SaveString(d2->units_name); } else { free((char *)d1->units_name); d1->units_name = (char *)NULL; } d1->units_type = d2->units_type; d1->scale_factor = d2->scale_factor; d1->length_expnt = d2->length_expnt; d1->mass_expnt = d2->mass_expnt; d1->time_expnt = d2->time_expnt; d1->temp_expnt = d2->temp_expnt; d1->radian_expnt = d2->radian_expnt; #ifdef DEBUG printf(" Leaving UnitsCopy() :\n");#endif return (d1);}/* * ====================== * Zero out units in "d1" * ====================== */#ifdef __STDC__DIMENSIONS *ZeroUnits(DIMENSIONS *d1)#elseDIMENSIONS *ZeroUnits(d1)DIMENSIONS *d1;#endif{#ifdef DEBUG printf("Enter ZeroUnits()\n");#endif if( d1==(DIMENSIONS *)NULL ) { printf("Warning: d1 is NULL, in ZeroUnits(), return NULL\n"); return d1; } if( d1->units_name != (char *)NULL ) { free((char *)d1->units_name); d1->units_name = (char *)NULL; } d1->scale_factor = 1.0; d1->length_expnt = 0.0; d1->mass_expnt = 0.0; d1->time_expnt = 0.0; d1->temp_expnt = 0.0; d1->radian_expnt = 0.0; if(d1->units_type != SI && d1->units_type != US) d1->units_type = SI_US;#ifdef DEBUG printf("Leaving ZeroUnits()\n");#endif return (d1);}/* * =================================== * Get Default Units from Symbol Table * =================================== */#ifdef __STDC__DIMENSIONS *DefaultUnits(char *name)#elseDIMENSIONS *DefaultUnits(name)char *name;#endif{DIMENSIONS *dp, *d; d = (DIMENSIONS *)NULL; d = lookup(name)->u.q->dimen; if(d == (DIMENSIONS *)NULL){ printf("ERROR: \"%s\" is not found in hash_table \n", name); FatalError("In DefaultUnits()",(char *)NULL); } dp = (DIMENSIONS *) MyMalloc(sizeof(DIMENSIONS)); dp->units_name = SaveString(name); dp->scale_factor = d->scale_factor; dp->units_type = d->units_type; dp->length_expnt = d->length_expnt; dp->mass_expnt = d->mass_expnt; dp->time_expnt = d->time_expnt; dp->temp_expnt = d->temp_expnt; dp->radian_expnt = d->radian_expnt; return(dp);}/* * ================================================================= * UnitsSimplify() : Simplify Units Expression * * Input : DIMENSIONS * -- pointer to unsimplified units expression * Output : DIMENSIONS * -- pointer to simplified units expression * ================================================================= */#ifdef __STDC__DIMENSIONS *UnitsSimplify(DIMENSIONS *d)#elseDIMENSIONS *UnitsSimplify(d)DIMENSIONS *d;#endif{DIMENSIONS *d1, *d2, *d3, *d4, *dp;DIMENSIONS *dp1, *dp2, *dp3, *dp4;DIMENSIONS *dd1, *dd2;SYMBOL *hp;double dlength;#ifdef DEBUG printf(" Enter UnitsSimplify() \n"); UnitsPrint(d);#endif /* [a] : Check that units are defined */ if( d == (DIMENSIONS *) NULL ) { printf("Warning : d is NULL in UnitsSimplify()\n"); return (DIMENSIONS *) NULL; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -