L
luser droog
Trying to simplify some nested switch statements
over enum values. Expressions saves the day.
#include<stdio.h>
enum { X=1, Y=2 };
int pair(int a, int b){
return a*(Y+1)+b;
}
int switchpair(int p){
switch(p){
case 0:
printf("0\n"); break;
case Y:
printf("Y\n"); break;
case X*(Y+1):
printf("X\n"); break;
case X*(Y+1)+Y:
printf("XY\n"); break;
}
return 0;
}
int main(){
int x;
x = pair(X,Y);
switchpair(x);
x = pair(0,Y);
switchpair(x);
x = pair(X,0);
switchpair(x);
x = pair(0,0);
switchpair(x);
return 0;
}
Output:
XY
Y
X
0
over enum values. Expressions saves the day.
#include<stdio.h>
enum { X=1, Y=2 };
int pair(int a, int b){
return a*(Y+1)+b;
}
int switchpair(int p){
switch(p){
case 0:
printf("0\n"); break;
case Y:
printf("Y\n"); break;
case X*(Y+1):
printf("X\n"); break;
case X*(Y+1)+Y:
printf("XY\n"); break;
}
return 0;
}
int main(){
int x;
x = pair(X,Y);
switchpair(x);
x = pair(0,Y);
switchpair(x);
x = pair(X,0);
switchpair(x);
x = pair(0,0);
switchpair(x);
return 0;
}
Output:
XY
Y
X
0