#include "stdio.h"
int symbol[100]={0};
int num[100]={0};
int symbolIndex=0;
int numIndex=0;
int opPopData(void)
{
int ret;
if( numIndex < 0 )
return -1;
else
{
ret = num[numIndex];
numIndex--;
return ret;
}
}
int opPopSymbol(void)
{
int ret;
if( symbolIndex < 0 )
return -1;
else
{
ret = symbol[symbolIndex];
symbolIndex--;
return ret;
}
}
int opPushNum(int push)
{
//已经大于等于100个元素,不可以再写入
if( numIndex >= 99 )
{
return -1;
}
numIndex++;
num[numIndex] = push;
return 0;
}
int opPushSymbol(int push)
{
//已经大于等于100个元素,不可以再写入
if( symbolIndex >= 99 )
{
return -1;
}
symbolIndex++;
symbol[symbolIndex] = push;
return 0;
}
int opCal( int a, int b, int sym )
{
return 5;
}
int cal(const char *str)
{
int a=0,b=0;
int sym=0;
if( NULL == str )
return -1;
while( 0 != *str )
{
//是数字,压栈
if(( (*str) >= '0' )&&( (*str) <= "9" ))
{
opPushNum((*str)-'0');
}
//是+-,先压符号栈
else if(( '+' == (*str) )||( '-' == (*str) ))
{
opPushSymbol(*str);
}
//是*/
else if( ( '*' == (*str) )||( '/' == (*str) ) )
{
//后面是(,先将*/压栈
if( *(str+1) == '(' )
{
opPushSymbol(*str);
}
//后面跟着不是(,是数字,计算*/
else
{
a = opPopData();
if( -1 == a )
{
printf("syn error\n");
return -1;
}
b = opCal(a,(*(str+1)-'0'),*str);
//将得到的结果压栈
opPushNum(b);
str++;
}
}
//是左括号,压栈
else if( '(' == (*str) )
{
opPushSymbol(*str);
}
//是右括号,寻找左括号
else if( ')' == *str )
{
sym = opPopSymbol();
while( '(' != sym )
{
a = opPopData();
b = opPopData();
opPushNum(opCal(a,b,sym));
sym = opPopSymbol();
}
}
}
sym = opPopSymbol();
while( -1 != sym )
{
a = opPopData();
b = opPopData();
opPushNum(opCal(a,b,sym));
}
}
int main(void)
{
}
草稿
最新推荐文章于 2023-03-11 08:28:39 发布