Calculator stack with brackets 2016 10 21

Source: Internet
Author: User

#include <iostream>
#include <stack>
#include <string>
#include <cmath>
using namespace Std;

int Getnum (string s) {
string SS;
SS = S;
int len = Ss.length ();
int sum = 0;
int count=0;
for (int i=0;i<len;i++) {
if (ss[i]>= ' 0 ' && ss[i]<= ' 9 ') {
sum = (ss[i]-' 0 ') + sum*10;
count++;
}
else if (count!=0) break;
}
return sum;
}

String Cutnum (string s) {
 string ss,s1;
 ss = s;
 s1 = "";
    int t;
 int len = ss.length ();
    for (int i=0;i<len;i++) {
     if (ss[i]< ' 0 ' | | Ss[i]> ' 9 ') {
        t=i;
     break; 
  }
 }
 
 if (t==0) return S1;
&NBSP
 for (int j=t;j<len;j++) {
  s1+=ss[j];
 }
 
 return s1; 
}

String Cutopra (string s) {
String ss,s1;
SS = S;
S1= "";
int t=0;
int len = Ss.length ();

if (len==1) return S1;

for (int i=0;i<len;i++) {
if (ss[i]< ' 0 ' | | ss[i]> ' 9 ') {
t=i;
Break
}
}

for (int j=t+1;j<len;j++) {
S1+=SS[J];
}

return s1;
}

Char Getopra (string s) {
String ss,s1;
SS = S;
int t;
int len = Ss.length ();
for (int i=0;i<len;i++) {
if (ss[i]< ' 0 ' | | Ss[i]> ' 9 ') {
t=i;
Break
}
}
char ts;
ts = ss[t];
return TS;
}

A stack top element
int level (char A,char b) {//Priority comparison greater than return1 equals return0 less than return-1

if (a== ' + ' && (b== ' * ' | | b== '/' | | b== ' (')) return 1;
else if (a== ' + ' && (b== ' + ' | | b== '-' | | b== ' # ' | | b== ')) return-1;

else if (a== '-' && (b== ' * ' | | b== '/' | | b== ' (')) return 1;
else if (a== '-' && (b== ' + ' | | b== '-' | | b== ' # ' | | b== ')) return-1;

else if (a== ' * ' && b== ' (') return 1;
else if (a== ' * ' && (b== ' * ' | | b== '/' | | b== ' + ' | | b== '-' | | b== ' # ' | | b== ')) return-1;

else if (a== '/' && (b== ') return 1;
else if (a== '/' && (b== ' * ' | | b== '/' | | b== ' + ' | | b== '-' | | b== ' # ' | | b== ')) return-1;

 else if (a== ' && (b== ' + ' | | b== '-' | | b== ' * ' | | b== '/' | | b== ' ('))                              return 1;
 else if (a== ' (' && b== ') ')                                                                 return 2;
 
 else if (a== ') ' && (b== ' + ' | | b== '-' | | b== ' * ' | | b== '/' | | b== ') ' | | b== ' # ')                      return 1;

else if (a== ' # ' && (b== ' + ' | | b== '-' | | b== ' * ' | | b== '/' | | b== ' (')) return 1;
else if (a== ' # ' &&b== ' # ') return 0;

else return 100000;
}

void Run () {
string S;
Getline (cin,s, ' \ n ');
Stack<int> OPND;
Stack<char> optr;
Optr.push (' # ');

while (s!= "") {
cout<<s<<endl;
cout<<s[0]<<endl;
if (s[0]< ' 0 ' | | S[0]> ' 9 ') {
Char ts = GETOPRA (s);
cout<< "ts=" <<ts<<endl;
S=cutopra (s);
cout<< "Cutopra (s) = =" <<s<<endl;
cout<< "optr.top () =" <<optr.top () <<endl;
while (Level (Optr.top (), TS) ==-1) {
int A, B;
A = Opnd.top ();
Opnd.pop ();
b = Opnd.top ();
cout<< "a==" <<a<< "" << "b==" <<b<<endl;
Opnd.pop ();
if (optr.top () = = ' * ') Opnd.push (a*b);
if (optr.top () = = '/') Opnd.push (b/a);
if (optr.top () = = ' + ') Opnd.push (a+b);
if (optr.top () = = '-') Opnd.push (b-a);
cout<< "opnd.top () = =" <<opnd.top () <<endl;
Optr.pop ();
cout<< "optr.top () = =" <<optr.top () <<endl;

}
while (Level (Optr.top (), TS) ==1) {
Optr.push (TS);
Break
}
if (Level (Optr.top (), TS) ==2) {
Optr.pop ();
ts = ';
cout<< "optr.top () = =" <<optr.top () <<endl;
}
if (Level (Ts,optr.top ()) ==0) {

cout<< "zhangding=" <<optr.top () <<endl;
Optr.pop ();
Break
}
}

else{

int a = Getnum (s);
Opnd.push (a);
S=cutnum (s);
}
}




while (! Optr.empty ()) {

cout<< "zhangding=" <<optr.top () <<endl;
Optr.pop ();

int A, B;
A = Opnd.top (); cout<< "A=" <<a<<endl;
Opnd.pop ();
b = Opnd.top (); cout<< "b=" <<b<<endl;
Opnd.pop ();
if (optr.top () = = ' * ') Opnd.push (a*b);
if (optr.top () = = '/') Opnd.push (b/a);
if (optr.top () = = ' + ') Opnd.push (a+b);
if (optr.top () = = '-') Opnd.push (b-a);
Optr.pop ();
}

cout<< "opnd.top () =" <<opnd.top () <<endl;

}

int main () {
Run ();
return 0;
}

Calculator stack with brackets 2016 10 21

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.