online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <stdio.h> #include <cstdlib> #include<iostream> #include<cctype> #include<cstring> #include<stack> #include<cmath> #include<sstream> #include <vector> using namespace std; class DividedByZero: public exception {}; bool is_balanced(const string& parenthesis) { stack<char> store; char check; for(string::size_type i = 0; i < parenthesis.length(); ++i) { check = parenthesis[i]; if(check == '(' || check == '{' || check == '[') { store.push(check); } if(!store.empty()) { if(check == ')') { if(store.top() == '(') { store.pop(); continue; } else { break; } } if(check == '}') { if(store.top() == '{') { store.pop(); continue; } else { break; } } if(check == ']') { if(store.top() == '[') { store.pop(); continue; } else { break; } } } else break; } return(store.empty()); } double precedence(char op) { if(op == '+' || op == '-') return 1; else if(op == '*' || op == '/') return 2; return 0; } void parse_stack_top(stack<double>& operand, stack<char>& operation) { double number1, number2; number2 = operand.top(); operand.pop(); number1 = operand.top(); operand.pop(); switch(operation.top()) { case '+': operand.push(number1 + number2); break; case '-': operand.push(number1 - number2); break; case '*': operand.push(number1 * number2); break; case '/': if(number2 == 0) { cout << "\nError!: Divide by zero."; throw DividedByZero(); } else operand.push(number1 / number2); } operand.pop(); } double evaluate_stack(const string& token) { stack<double> operand; stack<char> operation; for(string::size_type i = 0; i < token.size(); i++) { if(isdigit(token[i]) || token[i] == '.') { double val; while(i < token.size() && isdigit(token[i])) { val = (val*10) + (token[i]-'0'); i++; } i--; operand.push(val); } else if(token[i] == '+' || token[i] == '-' || token[i] == '*' || token[i] == '/') { while(!operation.empty() && precedence(token[i]) < precedence(operation.top())) { parse_stack_top(operand, operation); } operation.push(token[i]); } else if(token[i] == '(' || token[i] == '{' || token[i] == '[') { operation.push('('); } else if(token[i] == ')' || token[i] == '}' || token[i] == ']') { while(!operation.empty() && operation.top() != '(' || operation.top() != '{' || operation.top() != '[') { parse_stack_top(operand, operation); } operation.pop(); } } while(!operation.empty()) { parse_stack_top(operand, operation); } return operand.top(); } int main(int argc, char** argv) { string token; cout << "\nYour Expression: "; getline(cin, token); if(is_balanced(token)) { answer = evaluate_stack(token); cout << "\nResult: " << answer; } else cout << "\nError!: parentheses is not balanced!"; return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue