📄 units.c
字号:
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;
}
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);
#ifdef DEBUG
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 )
#else
DIMENSIONS *UnitsPower( d1, value, FLAG )
DIMENSIONS *d1;
double value;
int FLAG;
#endif
{
DIMENSIONS *d;
char *cp;
int length;
#ifdef DEBUG
printf(" Enter UnitsPower() :\n");
printf("d1->units_name = %s \n", d1->units_name);
printf("value = %g \n", value);
#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;
}
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;
}
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;
}
#ifdef DEBUG
printf(" Leaving UnitsPower() :\n");
#endif
return (d);
}
#ifdef __STDC__
DIMENSIONS *UnitsPowerRep( DIMENSIONS *d, DIMENSIONS *d1, double value, int FLAG )
#else
DIMENSIONS *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");
printf("d1->units_name = %s \n", d1->units_name);
printf("value = %g \n", value);
#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;
}
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;
}
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;
}
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)
#else
DIMENSIONS *UnitsCopy(d1, d2)
DIMENSIONS *d1, *d2;
#endif
{
int length;
#ifdef DEBUG
printf(" Enter UnitsCopy() :\n");
#endif
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;
}
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;
#ifdef DEBUG
printf(" Leaving UnitsCopy() :\n");
#endif
return (d1);
}
/*
* ======================
* Zero out units in "d1"
* ======================
*/
#ifdef __STDC__
DIMENSIONS *ZeroUnits(DIMENSIONS *d1)
#else
DIMENSIONS *ZeroUnits(d1)
DIMENSIONS *d1;
#endif
{
#ifdef DEBUG
printf("\n 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;
if(d1->units_type != SI && d1->units_type != US)
d1->units_type = SI_US;
#ifdef DEBUG
printf("\n Leaving ZeroUnits()\n");
#endif
return (d1);
}
/*
* ===================================
* Get Default Units from Symbol Table
* ===================================
*/
#ifdef __STDC__
DIMENSIONS *DefaultUnits(char *name)
#else
DIMENSIONS *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;
return(dp);
}
/*
* =================================================================
* Simplify Units Expression
*
* Input : DIMENSIONS * -- pointer to unsimplified units expression
* Output : DIMENSIONS * -- pointer to simplified units expression
* =================================================================
*/
#ifdef __STDC__
DIMENSIONS *UnitsSimplify(DIMENSIONS *d)
#else
DIMENSIONS *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");
#endif
/* [0] Check waether unit names are in the symbol table */
if( d==(DIMENSIONS *)NULL ) {
printf("Warning: d is NULL, in UnitsSimplify()\n");
#ifdef DEBUG
printf(" Leaving UnitsSimplify() \n");
#endif
return (DIMENSIONS *)NULL;
}
if(d->units_type==SI_US) {
#ifdef DEBUG
printf(" Leaving UnitsSimplify() \n");
#endif
return(d);
}
hp = NULL;
if(d->units_name != (char *)NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -