Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)

简介: Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:

注意1:末尾判断不是'\n',而是'\000'。

注意2:手动建栈。

AC 代码

inth,t;
ElementTypesk[10000];
voidfpop()
{
t--;
}
ElementTypeftop()
{
returnsk[t];
}
intfempty()
{
returnt==0?1:0;
}
voidfpush(ElementTypeval)
{
sk[++t]=val;
}
ElementTypeEvalPostfix( char*expr )
{
charc,op;
ElementTypenum=0,a,b;
intlen=1, dot=0, fu=1, hasNum=0, blank=0;
for(inti=0;;i++)
    {
c=expr[i];
if(c=='\000')
        {
if(hasNum&&!blank) returnnum*fu/len; // 测试点:单独一个数,直接输出elseif(hasNum&&blank) returnInfinity; // 末尾是操作数,而且不是整个表达式只有一个数num=ftop(), fpop();
if(!fempty()) returnInfinity; // 取完栈,发现还有操作数returnnum;
        }
elseif(c==' ')
        {
blank=1;
num=num*fu/len;
fpush(num);
fu=len=1, hasNum=dot=num=0;
        }
elseif(c>='0'&&c<='9')
        {
hasNum=1;
num=num*10+(c-'0');
if(dot) len*=10;
        }
elseif(c=='.')
        {
dot=1;
        }
elseif(c=='-'&&expr[i+1]!=' '&&expr[i+1]!='\000')
        {
fu=-1;
        }
elseif(c=='+'&&expr[i+1]!=' '&&expr[i+1]!='\000')
        {
        }
else        {
if(!fempty()) a=ftop(), fpop();
elsereturnInfinity;
if(!fempty()) b=ftop(), fpop();
elsereturnInfinity;
switch(c)
            {
case'+':
b+=a; fpush(b); break;
case'-':
b-=a; fpush(b); break;
case'*':
b*=a; fpush(b); break;
case'/':
if(a==0) returnInfinity;
b/=a; fpush(b); break;
default:
returnInfinity;
            }
if(b>=Infinity) returnInfinity; // 中间计算结果是否大于等于Infinityif(expr[i+1]==' ') i++;
        }
    }
}
目录
相关文章
|
机器学习/深度学习 算法
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
209 0
Data Structures and Algorithms (English) - 7-28 Review of Programming Contest Rules(30 分)
|
存储 容器
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
219 0
Data Structures and Algorithms (English) - 7-18 Hashing - Hard Version(30 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
109 0
Data Structures and Algorithms (English) - 7-8 File Transfer(25 分)
Data Structures and Algorithms (English) - 7-8 File Transfer(25 分)
110 0
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
Data Structures and Algorithms (English) - 6-15 Iterative Mergesort(25 分)
186 0
Data Structures and Algorithms (English) - 6-8 Percolate Up and Down(20 分)
Data Structures and Algorithms (English) - 6-8 Percolate Up and Down(20 分)
104 0
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
111 0
Data Structures and Algorithms (English) - 6-6 Level-order Traversal(25 分)
Data Structures and Algorithms (English) - 6-6 Level-order Traversal(25 分)
102 0
|
C++
Data Structures and Algorithms (English) - 6-9 Sort Three Distinct Keys(20 分)
Data Structures and Algorithms (English) - 6-9 Sort Three Distinct Keys(20 分)
113 0
Data Structures and Algorithms (English) - 6-10 Sort Three Distinct Keys(30 分)
Data Structures and Algorithms (English) - 6-10 Sort Three Distinct Keys(30 分)
104 0
下一篇
无影云桌面