Calculator with parentheses

Source: Internet
Author: User

Calculator with parentheses

// This calculator supports the brackets function. The format Example 3 + (3*3) # is followed by two # The calculation is complete, and features such as factorial and power are left to the students for improvement.
# Include <iostream> using namespace std; template <typename T> struct Node {// This is a data structure template. Both char and int can use T data; node <T> * next;}; template <typename T> class stack {Node <T> * top; public: stack (): top (NULL) {} void push (T n); T pop (); bool stackEmpty (); T getTop ();~ Stack () {} void print () ;}; template <typename T> void stack <T>: push (T n) {Node <T> * r = new Node <T>; r-> data = n; r-> next = top; top = r ;} template <typename T> T stack <T>: pop () {Node <T> * ptr = top; top => next; T t = ptr-> data; delete ptr; return t;} template <typename T> void stack <T>: print () {for (Node <T> * p = top; p; p = p-> next) cout <p-> data <"";} template <typename T> T stack <T >:: getTop () {return top-> data;} templa Te <typename T> bool stack <T>: stackEmpty () {if (top) return false; else return true;} bool In (char t) {// determine whether the character function if (t> = '0' & t <= '9') return false; else return true;} char Precede (char, char B) {operator priority function if (a = '(' & B = ')' | a = '#' & B = '#') return '= '; else if (a = '(' | a = '#' | B = '(' | (a = '+' & a =' -') & (B = '*' | B = '/') return '<'; else return '>';} int Operate (int first, char t, int second) {// operation function switch (T) {case '+': return first + second; case '-': return first-second; case '*': return first * second; case '/': return first/second;} return 0;} int EvaluateExpression () {char theta, x; int a, B; stack <char> OPTR; // operator stack <int> OPND; // number OPTR. push ('#'); string s; cin> s; for (int I = 0; I <s. length ()-1;) {if (! In (s [I]) {int sum = s [I]-'0'; I ++; (;! In (s [I]) & I <s. length ()-1; I ++) {int t = s [I]-'0'; sum = sum * 10 + t;} OPND. push (sum);} // If the ch is not an operator, it enters the OPND stack else switch (Precede (OPTR. getTop (), s [I]) {// compare the top element of the OPTR stack and the priority case of ch '<': // press the current character ch into the OPTR stack, read the next character ch OPTR. push (s [I]); I ++; break; case '>': // The operator at the top of the OPTR stack is displayed for calculation, and import the calculation result to the stack theta = OPTR. pop (); a = OPND. pop (); B = OPND. pop (); OPND. push (Operate (B, theta, a); break; case '=': x = OPTR. pop (); I ++; break;} // switch} // while return OPND. pop () ;}int main () {cout <"shuaicheng calculator supports brackets. Format: (expression ###) Example: 2 + 2 ##" <endl; for (;) {cout <EvaluateExpression (); cout <endl;} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.