Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- enum token{
- plus, minus, mult, divi, squrt, expon, ln, empty, error, number
- };
- struct stackComponent{
- bool flag;
- double number;
- token operation;
- };
- stackComponent emptycomponent = {true, 0, number};
- stackComponent errorcomponent = {true, 0, error};
- bool compare(stackComponent a, stackComponent b){
- if(a.flag == b.flag && a.number == b.number && a.operation == b.operation) return true;
- else return false;
- }
- class stack{
- stackComponent stck[100];
- int tracker;
- public:
- stack();
- stackComponent pop();
- void push(stackComponent object);
- };
- stack::stack(){
- for(int i = 0; i < 100; i++) stck[i] = emptycomponent;
- tracker = 0;
- }
- stackComponent stack::pop(){
- if(!tracker) return errorcomponent;
- else{
- tracker--;
- stackComponent value = stck[tracker];
- stck[tracker] = emptycomponent;
- return value;
- }
- }
- void stack::push(stackComponent object){
- stck[tracker] = object;
- tracker++;
- }
- class calculator{
- stack equation;
- stack solution;
- void add();
- void subtract();
- void multiply();
- void divide();
- void squareroot();
- void exponent();
- void logarithm();
- public:
- double calculate();
- void loadeq();
- };
- void calculator::add(){
- stackComponent opA = solution.pop();
- stackComponent opB = solution.pop();
- if(!compare(opA, emptycomponent) || !compare(opA, errorcomponent)) return;
- if(!compare(opB, emptycomponent) || !compare(opB, errorcomponent)) return;
- solution.push({false, opA.number + opB.number, number});
- return;
- }
- int main(){
- stack s;
- s.push({false, 0, empty});
- s.push({false, 1, empty});
- s.push({false, 2, empty});
- s.push({false, 3, empty});
- printf("%f\n", s.pop().number);
- printf("%f\n", s.pop().number);
- printf("%f\n", s.pop().number);
- printf("%f\n", s.pop().number);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement