Skip to content

Commit

Permalink
change the usage of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
dongfanghong committed Jan 4, 2017
1 parent 991e251 commit 59be557
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 181 deletions.
2 changes: 1 addition & 1 deletion intlist.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdlib.h>
typedef struct IntList{
int val;
IntList * next;
struct IntList * next;
int refcount;
}IntList;
IntList * allocateIntList(IntList * p){
Expand Down
108 changes: 55 additions & 53 deletions number.c
Original file line number Diff line number Diff line change
@@ -1,235 +1,237 @@
typedef struct Number{
enum{
#include <stdbool.h>
typedef enum NumType{
NUM_INT,
NUM_DOUBLE
}type; // 0 -> int 1 -> double
} NumType;
typedef struct Number{
NumType type; // 0 -> int 1 -> double
union{
double double_val;
int int_val;
};
} Number;
Number intNumber(int a){
Number r;
r.type=r.NUM_INT;
r.type=NUM_INT;
r.int_val=a;
return r;
}
Number doubleNumber(double a){
Number r;
r.type=r.NUM_DOUBLE;
r.type=NUM_DOUBLE;
r.double_val=a;
return r;
}
Number addNumber(Number a,Number b){
if (a.type==a.NUM_INT){
if (b.type==b.NUM_INT){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
Number c;
c.type=c.NUM_INT;
c.type=NUM_INT;
c.int_val=a.int_val+b.int_val;
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.double_val=a.int_val+b.double_val;
return c;
}
}else{ //NUM_DOUBLE
if (b.type==b.NUM_INT){
if (b.type==NUM_INT){
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.int_val=a.double_val+b.int_val;
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=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){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
Number c;
c.type=c.NUM_INT;
c.type=NUM_INT;
c.int_val=a.int_val-b.int_val;
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.double_val=a.int_val-b.double_val;
return c;
}
}else{ //NUM_DOUBLE
if (b.type==b.NUM_INT){
if (b.type==NUM_INT){
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.int_val=a.double_val-b.int_val;
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=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){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
Number c;
c.type=c.NUM_INT;
c.type=NUM_INT;
c.int_val=a.int_val*b.int_val;
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.double_val=a.int_val*b.double_val;
return c;
}
}else{ //NUM_DOUBLE
if (b.type==b.NUM_INT){
if (b.type==NUM_INT){
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.int_val=a.double_val*b.int_val;
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=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){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
Number c;
if (b.int_val==0){
msg[0]="divided by zero";
}else{
c.type=c.NUM_INT;
c.type=NUM_INT;
c.int_val=a.int_val/b.int_val;
}
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.double_val=a.int_val/b.double_val;
return c;
}
}else{ //NUM_DOUBLE
if (b.type==b.NUM_INT){
if (b.type==NUM_INT){
Number c;
if (b.int_val==0){
msg[0]="divided by zero";
}else{
c.type=c.NUM_DOUBLE;
c.type=NUM_DOUBLE;
c.int_val=a.double_val/b.int_val;
}
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=c.NUM_DOUBLE;
c.type=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){
if (a.type!=NUM_INT || b.type!=NUM_INT){
msg[0]="cannot use non-integer value in mod";
}else{
c.type=c.NUM_INT;
c.type=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){
if (a.type==NUM_INT){
if (b.type==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){
if (b.type==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){
if (a.type==NUM_INT){
if (b.type==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){
if (b.type==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){
if (a.type==NUM_INT){
if (b.type==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){
if (b.type==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){
if (a.type==NUM_INT){
if (b.type==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){
if (b.type==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){
if (a.type==NUM_INT){
if (b.type==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){
if (b.type==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){
if (a.type==NUM_INT){
if (b.type==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){
if (b.type==NUM_INT){
return a.double_val!=b.int_val;
}else{ //NUM_DOUBLE
return a.double_val!=b.double_val;
Expand Down
Loading

0 comments on commit 59be557

Please sign in to comment.