写的还不是很完善,只能按照标准的表达式输入,例如 :
(1+2)*3-4/2=
输入的时候乘号不能省略,等于号不能省略。
#include <iostream>
using namespace std;
#define STACK_INIT_SIZE 100
int Decide(char* Iput,int ChLen);
int GetLen(char* Iput);
int GetNum(char* ChNum,int* len);
int logic(int a,int b,char ch);
int ChDecide(char Chstr,char Chtop);
bool isope(char s);
class Stack
{
private:
int date[100];
int top;
int base;
public:
Stack();
InitStack();
ClearStack();
void Push(char Iput);
void Pop();
int Length();
Gettop(int* Chtop);
void pri();
};
Stack::Gettop(int* Chtop)
{
*Chtop = date[top];
//return date[top];
}
/
Stack::Stack()
{
InitStack();
}
//
Stack::InitStack()
{
top = 0;
base = 0;
for (int i=1;i<100;i++)
{
date[i] = 0;
}
date[0] = '\0';
}
/
Stack::ClearStack()
{
top = 0;
base = top;
}
/
void Stack::Push(char Iput)
{
// if (base == top)
// {
// date[top]= Iput;
// }
// else
// {
if (top-base == STACK_INIT_SIZE )
{
cout << "This Stack is filled" << endl;
return;
}
else if (top-base < STACK_INIT_SIZE)
{
top++;
date[top] = Iput;
}
// }
}
/
void Stack::Pop()
{
if (top == 0)
{
cout << "This Stack is empty" << endl;
return;
}
else if(top >= base)
{
date[top] = 0;
top--;
}
}
/
int Stack::Length()
{
int len = top;
return len;
}
/
void Stack::pri()
{
for (int i=0;i<Length();i++)
{
cout << date[i] <<" ";
}
}
//
int Decide(char* Iput,int ChLen)
{
Stack str;
Stack number;
int Chtop;
int Numlen = 0;
for (int i = 0;i < ChLen; i++)
{
bool r = isope(Iput[i]);
if (r)
{
str.Gettop(&Chtop);
int result = ChDecide(Iput[i],Chtop);
int numleft;
int numright;
int Lresult;
switch (result)
{
case 0:
if(Iput[i] == '='&& Chtop == '\0')
{
i = ChLen;
break;
}
else
{
str.Gettop(&Chtop);
str.Pop();
number.Gettop(&numright);
number.Pop();
number.Gettop(&numleft);
number.Pop();
Lresult = logic(numleft,numright,Chtop);
number.Push(Lresult);
i--;
break;
}
case 1:
str.Push(Iput[i]);
break;
case 2:
str.Pop();
break;
case 3:
cout << "Error" << endl;
return 0;
break;
}
}
else
{
int num = GetNum(&(Iput[i]),&Numlen);
i = i + Numlen -1;
number.Push(num);
}
}
int top;
number.Gettop(&top);
return top;
}
/
bool isope(char s)
{
switch (s)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '=':
return true;
break;
default:
return false;
break;
}
}
/
int ChDecide(char Chstr,char Chtop)
{
switch (Chstr)
{
case '+':
{
switch (Chtop)
{
case '\0':
return 1;
break;
case '+':
return 0; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 0;
break;
case '*':
return 0;
break;
case '/':
return 0;
break;
case '(':
return 1;
break;
case ')':
return 0;
break;
case '=':
return 3;
break;
}
break;
}
case '-':
{
switch (Chtop)
{
case '\0':
return 1;
break;
case '+':
return 0; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 0;
break;
case '*':
return 0;
break;
case '/':
return 0;
break;
case '(':
return 1;
break;
case ')':
return 0;
break;
case '=':
return 3;
break;
}
break;
}
case '*':
{
switch (Chtop)
{
case '\0':
return 1;
break;
case '+':
return 1; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 1;
break;
case '*':
return 0;
break;
case '/':
return 0;
break;
case '(':
return 1;
break;
case ')':
return 0;
break;
case '=':
return 3;
break;
}
break;
}
case '/':
{
switch (Chtop)
{
case '\0':
return 1;
break;
case '+':
return 1; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 1;
break;
case '*':
return 0;
break;
case '/':
return 0;
break;
case '(':
return 1;
break;
case ')':
return 0;
break;
case '=':
return 3;
break;
}
break;
}
case '(':
{
switch (Chtop)
{
case '\0':
return 1;
break;
case '+':
return 1; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 1;
break;
case '*':
return 1;
break;
case '/':
return 1;
break;
case '(':
return 1;
break;
case ')':
return 3;
break;
case '=':
return 3;
break;
}
break;
}
case ')':
{
switch (Chtop)
{
case '\0':
return 1;
break;
case '+':
return 0; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 0;
break;
case '*':
return 0;
break;
case '/':
return 0;
break;
case '(':
return 2;
break;
case ')':
return 0;
break;
case '=':
return 3;
break;
}
break;
}
case '=':
{
switch (Chtop)
{
case '\0':
return 0;
break;
case '+':
return 0; // Pop 0 Push 1 break 2 error 3
break;
case '-':
return 0;
break;
case '*':
return 0;
break;
case '/':
return 0;
break;
case '(':
return 3;
break;
case ')':
return 0;
break;
case '=':
return 3;
break;
}
break;
}
}
return 3;
}
/
int logic(int a,int b,char ch) //numberleft ,numberright,Chtop
{
int result;
switch (ch)
{
case '+':
result = a + b;
return result;
break;
case '-':
result = a - b;
return result;
break;
case '*':
result = a * b;
return result;
break;
case '/':
result = a / b;
return result;
break;
default:
cout << "Error" << endl;
break;
}
return 0;
}
/
int GetLen(char* Iput)
{
int ChLen;
for (int j = 0;j<STACK_INIT_SIZE;j++)
{
if (*Iput == '\0')
{
break;
}
Iput++;
}
ChLen = j;
return ChLen;
}
/
int GetNum(char* ChNum,int* len)
{
int i = 0;
int j = 0;
while (*ChNum >='0'&&*ChNum<='9')
{
i = i*10+(*ChNum-'0');
j++;
ChNum++;
}
*len=j;
return i;
}
//
void main()
{
while(1)
{
system("cls");
char Iput[100] = {'\0'};
cout << "please input : ";
cin >> Iput;
cout << "result:"<<Decide(Iput,GetLen(Iput)) << endl;
system("pause");
}
}