Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ONP
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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(); } } } 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; } }; // 2 + 2 * 2 // 2 2 2 * + // 1 [0] 1 [1] 1 [2] 2 [*] 2 [+] int main() { CalcMachine CM; CM.AddIntVar(2); CM.AddOperation(Operation_ID::Add); CM.AddIntVar(2); CM.AddOperation(Operation_ID::Multiple); CM.AddIntVar(2); std::cout<<CM.Run()<<'\n'; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.42 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0,56 sec
fork mode
|
history
|
discussion
0 0 2 6