-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Haoqiang Fan
committed
Jan 2, 2017
1 parent
9c9a989
commit 572cdc5
Showing
2 changed files
with
228 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
typedef struct Number{ | ||
enum{ | ||
NUM_INT, | ||
NUM_DOUBLE | ||
}type; // 0 -> int 1 -> double | ||
union{ | ||
double double_val; | ||
int int_val; | ||
}; | ||
} Number; | ||
Number addNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
c.type=c.NUM_INT; | ||
c.int_val=a.int_val+b.int_val; | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.int_val+b.double_val; | ||
return c; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.int_val=a.double_val+b.int_val; | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.double_val+b.double_val; | ||
return c; | ||
} | ||
} | ||
} | ||
Number subNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
c.type=c.NUM_INT; | ||
c.int_val=a.int_val-b.int_val; | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.int_val-b.double_val; | ||
return c; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.int_val=a.double_val-b.int_val; | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.double_val-b.double_val; | ||
return c; | ||
} | ||
} | ||
} | ||
Number mulNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
c.type=c.NUM_INT; | ||
c.int_val=a.int_val*b.int_val; | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.int_val*b.double_val; | ||
return c; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.int_val=a.double_val*b.int_val; | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.double_val*b.double_val; | ||
return c; | ||
} | ||
} | ||
} | ||
Number divNumber(Number a,Number b,const char ** msg){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
if (b.int_val==0){ | ||
msg[0]="divided by zero"; | ||
}else{ | ||
c.type=c.NUM_INT; | ||
c.int_val=a.int_val/b.int_val; | ||
} | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.int_val/b.double_val; | ||
return c; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
Number c; | ||
if (b.int_val==0){ | ||
msg[0]="divided by zero"; | ||
}else{ | ||
c.type=c.NUM_DOUBLE; | ||
c.int_val=a.double_val/b.int_val; | ||
} | ||
return c; | ||
}else{ //NUM_DOUBLE | ||
Number c; | ||
c.type=c.NUM_DOUBLE; | ||
c.double_val=a.double_val/b.double_val; | ||
return c; | ||
} | ||
} | ||
} | ||
Number modNumber(Number a,Number b,const char ** msg){ | ||
Number c; | ||
if (a.type!=a.NUM_INT || b.type!=b.NUM_INT){ | ||
msg[0]="cannot use non-integer value in mod"; | ||
}else{ | ||
c.type=c.NUM_INT; | ||
c.int_val=a.int_val%b.int_val; | ||
} | ||
return c; | ||
} | ||
bool leNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
return a.int_val<b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.int_val<b.double_val; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
return a.double_val<b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.double_val<b.double_val; | ||
} | ||
} | ||
} | ||
bool geNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
return a.int_val>b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.int_val>b.double_val; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
return a.double_val>b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.double_val>b.double_val; | ||
} | ||
} | ||
} | ||
bool nleNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
return a.int_val>=b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.int_val>=b.double_val; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
return a.double_val>=b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.double_val>=b.double_val; | ||
} | ||
} | ||
} | ||
bool ngeNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
return a.int_val<=b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.int_val<=b.double_val; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
return a.double_val<=b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.double_val<=b.double_val; | ||
} | ||
} | ||
} | ||
bool eqNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
return a.int_val==b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.int_val==b.double_val; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
return a.double_val==b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.double_val==b.double_val; | ||
} | ||
} | ||
} | ||
bool neqNumber(Number a,Number b){ | ||
if (a.type==a.NUM_INT){ | ||
if (b.type==b.NUM_INT){ | ||
return a.int_val!=b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.int_val!=b.double_val; | ||
} | ||
}else{ //NUM_DOUBLE | ||
if (b.type==b.NUM_INT){ | ||
return a.double_val!=b.int_val; | ||
}else{ //NUM_DOUBLE | ||
return a.double_val!=b.double_val; | ||
} | ||
} | ||
} |
Oops, something went wrong.