Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

#include <iostream>
using namespace std;

const int otbl[7][7=
                {{
-2-2,  2,  2,  2-1,  0},//i+
                 {-2-2,  2,  2,  2-1,  0}, //-
                 {-2-2-2-2,  2-1,  0}, //*
                 {-2-2-2-2,  2-1,  0}, ///
                 { 2,  2,  2,  2,  2-1,  0}, //(
                 {-1-1-1-1-1-1,  0}, //)
                 { 2,  2,  2,  2,  2,  2-3}};//@
                
//o+   -   *   /   (   )   @

struct stack
{
    
char n[1001];
    
int top;
    stack()
    {
        memset(n, 
0sizeof n);
        top 
= 0;
    }
    
void push(char x)
    {
        n[
++top] = x;
    }
    
char pop()
    {
        
if(top > 0)
            
return n[top--];
        
return '!';
    }
    
bool empty()
    {
        
if(top > 0)
            
return false;
        
return true;
    }
}oprt;


bool isdigit(char x)
{
    
if(x != '@' && x != '+' && x != '-' && x != '*'
     
&&!= '/' && x != '(' && x != ')')
        
return true;
    
return false;
}

int ocmp(char opa, char opb)
{
    
int opax = 0;
    
switch(opa)
    {
        
case '+': opax = 0break;
        
case '-': opax = 1break;
        
case '*': opax = 2break;
        
case '/': opax = 3break;
        
case '(': opax = 4break;
        
case ')': opax = 5break;
        
case '@': opax = 6break;
    }

    
int opbx = 0;
    
switch(opb)
    {
        
case '+': opbx = 0break;
        
case '-': opbx = 1break;
        
case '*': opbx = 2break;
        
case '/': opbx = 3break;
        
case '(': opbx = 4break;
        
case ')': opbx = 5break;
        
case '@': opbx = 6break;
    }

    
return otbl[opbx][opax];
}

string tostring(int x)
{
    
string str = "";
    
int n[101], y = x;
    memset(n, 
0sizeof n);
    
for(int i = 1; y > 0; y /= 10)
    {
        n[i
++= y % 10;
        n[
0]++;
    }
    
for(int i = n[0]; i > 0; i--)
        str 
+= char(n[i] + '0');
    
return str;
}

int main()
{
    
string s, r = "";
    cin 
>> s;
    s 
+= '@';
    
int lng = s.length();
    oprt.push(
'@');
    
int x = 0;
    
for(int i = 0; i <= lng; i++)
    {
        
if(isdigit(s[i]))
        {
            r 
+= s[i];
            
continue;
        }
        
else
        {
            
int flg = ocmp(s[i], oprt.n[oprt.top]);
            
switch(flg)
            {
                
case 2: oprt.push(s[i]);
                        
break;
                
case -2while(ocmp(s[i], oprt.n[oprt.top]) == -2)
                             r 
+= oprt.pop();
                         oprt.push(s[i]);
                         
break;
                
case -1while(oprt.n[oprt.top] != '(')
                             r 
+= oprt.pop();
                         oprt.pop();
                         
break;
                
case 0while(oprt.n[oprt.top] != '@')
                             r 
+= oprt.pop();
                        
break;
                
case -3: x = 99999break;
            }
            
if(x == 99999)
                
break;
        }
    }
    cout 
<< r << endl;

    
return 0;
}

posted on 2009-09-15 21:26 mad4alcohol 阅读(68) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理