📄 units.c
字号:
/* * ============================================================================= * ALADDIN Version 1.0 : * units.c : Functions to Handle Engineering Units/Dimensions * * Copyright (C) 1995 by Mark Austin, Xiaoguang Chen, and Wane-Jang Lin * Institute for Systems Research, * University of Maryland, College Park, MD 20742 * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software for any on any computer system * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written by: M.A. Austin, Xiaoguang Chen, and Wane-Jang Lin December 1995 * ============================================================================= */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#ifdef __STDC__#include <stdarg.h>#else#include <varargs.h>#endif#include "defs.h"#include "miscellaneous.h"#include "units.h"#include "matrix.h"#include "fe_database.h"#include "symbol.h"static enum{UNITS_OFF, UNITS_ON} iFlag = UNITS_ON;/* #define DEBUG *//* * =========================================================== * Utility Functions for Units Operations * * SetUnitsOn() : Set flag for units "on" * SetUnitsOff() : Set flag for units "off" * CheckUnits() : Flag to see if units are "on" or "off" * CheckUnitsType() : Check type of units * =========================================================== */int SetUnitsOn(){ iFlag = UNITS_ON; }int SetUnitsOff(){ iFlag = UNITS_OFF; }int CheckUnits(){ return(iFlag);}int CheckUnitsType(){ return(UNITS_TYPE);}/* * ======================================== * Do two quantities have the same units ?? * ======================================== */#ifdef __STDC__int SameUnits(DIMENSIONS *d1, DIMENSIONS *d2)#elseint SameUnits(d1, d2)DIMENSIONS *d1, *d2;#endif{ if((d1->length_expnt == d2->length_expnt) && (d1->mass_expnt == d2->mass_expnt) && (d1->time_expnt == d2->time_expnt) && (d1->temp_expnt == d2->temp_expnt)) { return TRUE; } else { return FALSE; }}/* * ================================= * Functions for Operations on Units * ================================= */#ifdef __STDC__DIMENSIONS *UnitsMult(DIMENSIONS *d1, DIMENSIONS *d2)#elseDIMENSIONS *UnitsMult(d1, d2)DIMENSIONS *d1, *d2;#endif{DIMENSIONS *d;DIMENSIONS *dp1, *dp2;int length; if( d1==(DIMENSIONS *)NULL || d2==(DIMENSIONS *)NULL ) { printf("Warning: d1 or d2 is NULL, in UnitsMult()\n"); return (DIMENSIONS *)NULL; } d = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS)); 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 ) { 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 ) d->units_name = SaveString(dp2->units_name); else d->units_name = (char *)NULL; 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; free((char *)dp1->units_name); free((char *)dp1); free((char *)dp2->units_name); free((char *)dp2); return (d);}#ifdef __STDC__DIMENSIONS *UnitsMultRep(DIMENSIONS *d, DIMENSIONS *d1, DIMENSIONS *d2)#elseDIMENSIONS *UnitsMultRep(d, d1, d2)DIMENSIONS *d;DIMENSIONS *d1, *d2;#endif{DIMENSIONS *dp1, *dp2;int length; if( d1==(DIMENSIONS *)NULL || d2==(DIMENSIONS *)NULL ) { printf("Warning: d1 or d2 is NULL, in UnitsMultRep()\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 ) { 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 ) { free((char *)d->units_name); d->units_name = SaveString(dp2->units_name); } else { free((char *)d->units_name); d->units_name = (char *)NULL; } 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; free((char *)dp1->units_name); free((char *)dp1); free((char *)dp2->units_name); free((char *)dp2); return (d);}#ifdef __STDC__DIMENSIONS *UnitsDiv( DIMENSIONS *d1, DIMENSIONS *d2, int FLAG )#elseDIMENSIONS *UnitsDiv( d1, d2, FLAG )DIMENSIONS *d1, *d2;int FLAG; /* YES or NO flag : to determine wheather "()" is needed */#endif{DIMENSIONS *d;DIMENSIONS *dp1, *dp2;int length; if( d2==(DIMENSIONS *)NULL ) FatalError("In UnitsDiv() : unit divider is a NULL pointer",(char *)NULL); if( d1==(DIMENSIONS *)NULL ) { printf("Warning: d1 is NULL, in UnitsDiv()\n"); return (DIMENSIONS *)NULL; } d = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS)); 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) { 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 { 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) { 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 { 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 d->units_name = (char *)NULL; 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; free((char *)dp1->units_name); free((char *)dp1); free((char *)dp2->units_name); free((char *)dp2); return (d);}#ifdef __STDC__DIMENSIONS *UnitsDivRep( DIMENSIONS *d, DIMENSIONS *d1, DIMENSIONS *d2, int FLAG )#elseDIMENSIONS *UnitsDivRep( d, d1, d2, FLAG )DIMENSIONS *d;DIMENSIONS *d1, *d2;int FLAG; /* YES or NO flag : to determine wheather "()" is needed */#endif{DIMENSIONS *dp1, *dp2;int length;#ifdef DEBUG printf(" Enter UnitsDivRep() :\n");#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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -