Improved Stack Calculator

Source: Internet
Author: User

#include <iostream>
#include <stack>
#include <string>
#include <cmath>
#include <iomanip>
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;
}


int Judge (string s) {

if (s[0]== '-') {
cout<< "Because the formula starts with a minus sign, in order to facilitate the calculation of the calculator, so the entire equation before adding a 0" <<endl;
s= ' 0 ' +s;
}
int len = S.length ();
int c=0;
for (int i=0;i<len;i++) {
if (s[i]== ' (') C + +;
if (s[i]== ') ') c--;

if (s[i]== '. ') {
cout<< "This calculator does not support the operation of decimal points" <<endl;
return 1;
}

if (! ( (s[i]>= ' 0 ' &&s[i]<= ' 9 ') | | s[i]== ' + ' | | s[i]== '-' | | s[i]== ' * ' | | s[i]== '/' | | s[i]== ' # ' | | s[i]== ' (' | | | s[i]== ')) {
cout<< "I'm sorry, you partially input characters do not meet the requirements" <<endl;
return 2;
}
if (s[i]== ' 0 ' &&s[i-1]== '/') {
cout<< "Divisor is 0, operation Error" <<endl;
return 0;
}



}
if (c!=0) {
cout<< "The parenthesis you entered does not match" <<endl;
return 3;
}

return-1;
}


void Run () {
string S;
Getline (cin,s, ' \ n ');

while (Judge (s) ==1| | Judge (s) ==2| | Judge (s) ==3| | Judge (s) ==0) {
cout<< "Please re-enter:";
Getline (cin,s, ' \ n ');
}



Stack<int> OPND;
Stack<char> optr;
Char op[1000];
int num[1000];
Optr.push (' # ');
int optop=0;
int numtop=0;
int step = 0;
BOOL FAC = FALSE;

cout<< "------------------------------------------------------------------------------------------------- ------------------"<<endl;
cout<< "Step optr stack OPND stack input character main operation" <& Lt;endl;
cout<< "------------------------------------------------------------------------------------------------- ------------------"<<endl;

String mainoprt = "";
op[optop++]= ' # ';
while (s!= "") {
int e=0;
int flag=0;
COUT&LT;&LT;LEFT&LT;&LT;SETW (+) << ++step;
for (int i=0;i<optop;i++) {
cout<<op[i];
}
COUT&LT;&LT;LEFT&LT;&LT;SETW (15-optop) << "";
for (int i=0;i<numtop;i++) {

cout<<num[i]<< "'";

int t = num[i];
if (t<0) {
t = -1*t;
flag++;
}
while (T&GT;10) {
flag++;
T=T/10;
}

}
COUT&LT;&LT;SETW (20-2*numtop-flag) << "";
COUT&LT;&LT;RIGHT&LT;&LT;SETW (<<s<<endl;)





if (s[0]< ' 0 ' | | S[0]> ' 9 ') {
Char ts = GETOPRA (s);
op[optop++] = ts;
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 ();
Numtop = numtop-2;

cout<< "a==" <<a<< "" << "b==" <<b<<endl;
Opnd.pop ();
if (optr.top () = = ' * ') Opnd.push (a*b);
if (optr.top () = = '/') {
if (a==0) cout<< "program has a divisor of 0, the divisor will be replaced by a random number, this operation is invalid" <<endl;
FAC = TRUE;
A = 1;
Opnd.push (b/a);
}
if (optr.top () = = ' + ') Opnd.push (a+b);
if (optr.top () = = '-') Opnd.push (b-a);
cout<< "opnd.top () = =" <<opnd.top () <<endl;
optop--;
OP[OPTOP-1] = ts;
num[numtop++] = Opnd.top ();
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 ();
Optop = optop-2;
ts = ';
cout<< "optr.top () = =" <<optr.top () <<endl;
}
if (Level (Ts,optr.top ()) ==0) {

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

else{

int a = Getnum (s);
Opnd.push (a);
Mainoprt + = "PUSH (OPND, ')";
num[numtop++] = 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 ();
Numtop = numtop-2;
if (optr.top () = = ' * ') Opnd.push (a*b);
if (optr.top () = = '/') {
if (a==0) cout<< "program has a divisor of 0, the divisor will be replaced by a random number, this operation is invalid" <<endl;
FAC = TRUE;
A = 1;
Opnd.push (b/a);
}
if (optr.top () = = ' + ') Opnd.push (a+b);
if (optr.top () = = '-') Opnd.push (b-a);
optop--;
num[numtop++] = Opnd.top ();
Optr.pop ();


int e=0;
int flag=0;
COUT&LT;&LT;LEFT&LT;&LT;SETW (+) << ++step;
for (int i=0;i<optop;i++) {
cout<<op[i];
}
COUT&LT;&LT;LEFT&LT;&LT;SETW (15-optop) << "";
for (int i=0;i<=numtop;i++) {

cout<<num[i]<< "'";

int t = num[i];
if (t<0) {
t = -1*t;
flag++;
}
while (T&GT;10) {
flag++;
T=T/10;
}

}
COUT&LT;&LT;SETW (26-2*numtop-flag) << "";
COUT&LT;&LT;RIGHT&LT;&LT;SETW (<<s<<endl;)


}

COUT&LT;&LT;LEFT&LT;&LT;SETW (&LT;&LT;STEP+1&LT;&LT;LEFT&LT;&LT;SETW) (15-optop) << "#" <<left< &LT;SETW (15-optop) <<opnd.top () &LT;&LT;RIGHT&LT;&LT;SETW (+) << "#" <<endl;
cout<<endl;
if (FAC = = False) cout<< "The result of this equation =" <<opnd.top () <<endl;
else cout<< "The result of this formula does not exist" <<endl;

}

int main () {
while (1) {
Run ();
}

return 0;
}

Improved Stack Calculator

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.