Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ONP is working!
//g++ 4.9.3 #include <iostream> #include <sstream> #include <vector> #include <fstream> 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(); } } } else { while(Oprs.size()) { 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; } void Set(const std::string &Script) { std::string Text; std::stringstream ss; ss<<Script; while(ss>>Text) { std::stringstream ss2; if(IsNumber(Text)) { ss2<<Text; int Value; ss2>>Value; AddIntVar(Value); } else { if(Text=="+") AddOperation(Operation_ID::Add); else if(Text=="-") AddOperation(Operation_ID::Sub); else if(Text=="*") AddOperation(Operation_ID::Multiple); else if(Text=="/") AddOperation(Operation_ID::Divide); } } } bool IsNumber(std::string &Text) { const char *Itr=Text.c_str(); while(*Itr) { if(*Itr!='.'&&(*Itr<'0'||*Itr>'9')) return false; ++Itr; } return true; } }; // 2 + 2 * 2 // 2 2 2 * + // 1 [0] 1 [1] 1 [2] 2 [*] 2 [+] int main() { CalcMachine CM; std::string Script="2 + 4 / 2"; std::cout<<Script<<" = "; CM.Set(Script); std::cout<<CM.Run()<<'\n'; }
run
|
edit
|
history
|
help
0
Programa 3(Creo que ya esta)
chocolate Distribution Problem
sysFork3
bitmap with pairs
-fno-elide-constructors
HeapSort
Hello
lab17feb22x4B.cpp
count pairs with given sum hashing
ForwardListString