[ create a new paste ] login | about

Link: http://codepad.org/Yh50TaxB    [ raw code | output | fork ]

C++, pasted on Oct 16:
#include <iostream>
#include <stdio.h>
#define MAX 100
using namespace std;

struct Stack{
    int top;
    char data[MAX];
};
void KhoiTao(Stack &s){
    s.top = 0;
}
bool isFull(Stack s){
    return (s.top == MAX);
}
bool isEmpty(Stack s){
    return (s.top == 0);
}
void Push(Stack &s, char x){
    if (isFull(s))
        cout << "Overflow.";
    else{
        s.top++;
        s.data[s.top] = x;
    }
}
void Pop(Stack &s){
    if (isEmpty(s))
        cout << "Underflow.";
    else{
        cout << s.data[s.top];
        s.top--;
    }
}
int precedence(char op){
    if(op == '+'||op == '-')
    return 1;
    if(op == '*'||op == '/')
    return 2;
    return 0;
}
int applyOp(int a, int b, char op){
    switch(op){
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return a / b;
    }
}
int evaluate(string tokens){
    int i;
     
    // stack to store integer values.
    Stack <int> values;
     
    // stack to store operators.
    Stack <char> ops;
     
    for(i = 0; i < tokens.length(); i++){
         
        // Current token is a whitespace,
        // skip it.
        if(tokens[i] == ' ')
            continue;
         
        // Current token is an opening
        // brace, push it to 'ops'
        else if(tokens[i] == '('){
            ops.push(tokens[i]);
        }
         
        // Current token is a number, push
        // it to stack for numbers.
        else if(isdigit(tokens[i])){
            int val = 0;
             
            // There may be more than one
            // digits in number.
            while(i < tokens.length() &&
                        isdigit(tokens[i]))
            {
                val = (val*10) + (tokens[i]-'0');
                i++;
            }
             
            values.push(val);
             
            // right now the i points to
            // the character next to the digit,
            // since the for loop also increases
            // the i, we would skip one
            //  token position; we need to
            // decrease the value of i by 1 to
            // correct the offset.
              i--;
        }
         
        // Closing brace encountered, solve
        // entire brace.
        else if(tokens[i] == ')')
        {
            while(!ops.empty() && ops.top() != '(')
            {
                int val2 = values.top();
                values.pop();
                 
                int val1 = values.top();
                values.pop();
                 
                char op = ops.top();
                ops.pop();
                 
                values.push(applyOp(val1, val2, op));
            }
             
            // pop opening brace.
            if(!ops.empty())
               ops.pop();
        }
         
        // Current token is an operator.
        else
        {
            // While top of 'ops' has same or greater
            // precedence to current token, which
            // is an operator. Apply operator on top
            // of 'ops' to top two elements in values stack.
            while(!ops.empty() && precedence(ops.top())
                                >= precedence(tokens[i])){
                int val2 = values.top();
                values.pop();
                 
                int val1 = values.top();
                values.pop();
                 
                char op = ops.top();
                ops.pop();
                 
                values.push(applyOp(val1, val2, op));
            }
             
            // Push current token to 'ops'.
            ops.push(tokens[i]);
        }
    }
     
    // Entire expression has been parsed at this
    // point, apply remaining ops to remaining
    // values.
    while(!ops.empty()){
        int val2 = values.top();
        values.pop();
                 
        int val1 = values.top();
        values.pop();
                 
        char op = ops.top();
        ops.pop();
                 
        values.push(applyOp(val1, val2, op));
    }
     
    // Top of 'values' contains result, return it.
    return values.top();
}
int main()
{
    Stack s;
    KhoiTao(s);
    char name[10] = "alo";
    int n = strlen(name);
    for (int i = 0; i < n; i++)
    {Push(s,name[i]);}
    while (s.top != 0) { cout <<" "; Pop(s);} 
    return 0;
}


Output:
1
2
3
In function 'int evaluate(std::string)':
Line 54: error: 'Stack' is not a template
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: