#include<iostream>
#include<math.h>
using namespace std;
struct Node
{
int Data;
Node *Next;
};typedef Node * Stack;
Node *TaoNut(int X)
{
Node *p;p=new Node;
p->Data=X;
return p;
}
void CreateStack(Stack &S)
{
S=NULL;
}
bool isEmpty(Stack S)
{
return (S==NULL);
}
void Push(Stack &S,int X)
{
Node *p;
p=TaoNut(X);
p->Next=S;
S=p;
}
int Top(Stack S)
{
if(S!=NULL)
return S->Data;
}
void Pop(Stack &S)
{
Node *p;
if(S!=NULL)
{
p=S;S=S->Next;
delete(p);
}
}
bool istoanhang(char c)
{
return (c!='+' && c!='-' && c!='*' &&c!='/'&&c!='^');
}
bool istoantu(char c)
{
return (c=='+' || c=='-' || c=='*' ||c=='/'||c=='^');
}
int TinhToan(int a, int b, char c)
{
switch (c) {
case '+': return b+a;
case '-': return b-a;
case '/':
if (a ==0) cout<<"Chia cho 0";
else return b/a;
case '*': return b*a;
case '^': return (int)pow(b,a);
}
}
void in_S(Stack S)
{
Node *p;
for(p=S;p!=NULL;p=p->Next)
cout<<p->Data;
}
void in_P(int t, char P[])
{
for(int k=t;k<strlen(P);k++)
cout<<P[k];
}
int dinhgia(char* str)
{
int a, b;
Stack S;
CreateStack(S);
int i = 0;
while (i < strlen(str))
{
if (istoanhang(str[i])) Push(S, int(str[i]-'0'));
else if (istoantu(str[i])) {
a = Top(S);
Pop(S);
b = Top(S);
Pop(S);
Push(S, TinhToan(a, b, str[i]));
}
in_S(S);cout<<"\t"; in_P(i,str);cout<<"\n";
i++;
}
return Top(S);
}
int main()
{
cout<<" ket qua la :"<<dinhgia("521-+2^53+-")<<"\n";
//cout<<2^2;
system("pause");
}
» Tin mới nhất:
» Các tin khác: