Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ONP
//g++ 4.9.3 #include <iostream> #include <sstream> #include <vector> struct Operation_ID { constexpr static int Add=0, Sub=1, Multiple=2, Divide=3; }; struct Option { int Type=0; Option(){;} }; struct IntVar : Option { int Value; IntVar(int NewValue) { Type=1; Value=NewValue; } }; struct Operation : Option { int ID; Operation(int newID) { Type=2; ID=newID; } }; struct CalcMachine { std::vector<Option*> Sketch; std::vector<float> Stack; std::vector<int> Oprs; CalcMachine() { /* Sketch.push_back(new IntVar(2)); Sketch.push_back(new IntVar(2)); Sketch.push_back(new IntVar(2)); Sketch.push_back(new Operation(Operation_ID::Multiple)); Sketch.push_back(new Operation(Operation_ID::Add)); */ } void AddOperation(int ID) { if(ID!=-1) { if(!Oprs.size()||Oprs.back()/2<ID/2) { Oprs.push_back(ID); } else { while(Oprs.size() && Oprs.back()>ID) { Sketch.push_back(new Operation(Oprs.back())); Oprs.pop_back(); } } } //Sketch.push_back(new Operation(ID)); } void AddIntVar(int Value) { Sketch.push_back(new IntVar(Value)); } float Run() { AddOperation(-1); for(size_t i=0; i<Sketch.size(); ++i) { Option *Obj=Sketch[i]; switch(Obj->Type) { case 1: Stack.push_back(static_cast<IntVar*>(Obj)->Value); break; case 2: int ID=static_cast<Operation*>(Obj)->ID; if(ID==Operation_ID::Add) { float Value=Stack.back(); Stack.pop_back(); Stack.back()+=Value; } else if(ID==Operation_ID::Sub) { float Value=Stack.back(); Stack.pop_back(); Stack.back()-=Value; } else if(ID==Operation_ID::Multiple) { float Value=Stack.back(); Stack.pop_back(); Stack.back()*=Value; } else if(ID==Operation_ID::Divide) { float Value=Stack.back(); Stack.pop_back(); Stack.back()/=Value; } break; } } if(Stack.size()) { float Ret=Stack.back(); Stack.pop_back(); return Ret; } else return 0; } }; // 2 + 2 * 2 // 2 2 2 * + // 1 [0] 1 [1] 1 [2] 2 [*] 2 [+] int main() { CalcMachine CM; CM.AddIntVar(2); CM.AddIntVar(Operation_ID::Add); CM.AddIntVar(2); CM.AddIntVar(Operation_ID::Multiple); CM.AddIntVar(2); std::cout<<CM.Run()<<'\n'; }
run
|
edit
|
history
|
help
0
Riemann's prime number formula
substr
Weighted Average
HeapSort
Operation on Set Container
NonparaSign
FAK MEN
SOS DP
point to a rvalue
Listas enlazadas - dividir lista en dos reutilizando nodos