Created
August 3, 2015 19:40
-
-
Save loufranco/44d2e68464b59279dd03 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
using namespace std; | |
struct Const { | |
Const(int c) : c(c) {} | |
int c; | |
}; | |
struct One : Const { | |
One() : Const(1) {} | |
}; | |
struct Zero : Const { | |
Zero() : Const(0) {} | |
}; | |
struct Var { | |
Var(string v) : v(v) {} | |
string v; | |
}; | |
Zero Add(Zero c1, Zero c2) { | |
return Zero(); | |
} | |
template<class T> T Add(T t1, Zero c2) { | |
return t1; | |
} | |
template <class T> Const Add(Zero c1, T t2) { | |
return t2; | |
} | |
Const Add(Const c1, Const c2) { | |
return Const(c1.c + c2.c); | |
} | |
template<class T> T Mult(One c1, T t2) { | |
return t2; | |
} | |
template<class T> T Mult(T t1, One c2) { | |
return t1; | |
} | |
template<class T> Zero Mult(Zero c1, T t2) { | |
return Zero(); | |
} | |
template<class T> Zero Mult(T t1, Zero c2) { | |
return Zero(); | |
} | |
Const Mult(Const c1, Const c2) { | |
return Const(c1.c * c2.c); | |
} | |
Var Mult(Const c1, Var v2) { | |
return Var(v2.v + " * "); | |
} | |
Var Mult(Var v1, Const c2) { | |
return Var(v1.v + " * "); | |
} | |
Var Mult(Var v1, Var v2) { | |
return Var(v1.v + " * " + v2.v); | |
} | |
Var Add(Var v1, Var v2) { | |
return Var(v1.v + " + " + v2.v); | |
} | |
Var Add(Const c1, Var v2) { | |
return Var(v2.v + " + "); | |
} | |
Var Add(Var v1, Const c2) { | |
return Var(v1.v + " + "); | |
} | |
ostream& operator <<(ostream& o, const Const& c) { | |
return (o << c.c); | |
} | |
ostream& operator <<(ostream& o, const Var& v) { | |
return (o << v.v); | |
} | |
int main() { | |
// (1+0×x)×3+12 | |
cout << Add(Mult(Add(One(), Mult(Zero(), Var("x"))), 3), 12) << endl; | |
// (1+0×(x*y))×3+12 | |
cout << Add(Mult(Add(One(), Mult(Zero(), Mult(Var("y"), Var("x")))), 3), 12) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment