飞行的猪哼哼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Problem Description

对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。
Input

输入一个算术表达式,以‘#’字符作为结束标志。
Output

输出该表达式转换所得到的后缀式。
Sample Input

a*b+(c-d/e)*f#
Sample Output

abcde/-f+

#include <iostream>

using namespace std;

int main()
{
    char A[10000];
    int p=0;
    char c;
    while((cin>>c)!=NULL)
    {
      if(c=='*'||c=='/'||c=='(')
      {
          p++;
          A[p]=c;
      }
      else if(c=='+'||c=='-')
      {
          while(p)
          {
              if(A[p]=='(')
              {
                  break;
              }
              else
              {
                  cout<<A[p];
                  p--;
              }
          }
          p++;
          A[p]=c;
      }
      else if(c==')')
      {
          while(p)
          {
              if(A[p]=='(')
              {
                  p--;
                  break;
              }
              else
              {
                 cout<<A[p];
                 p--;
              }
          }
      }
      else  if(c=='#')
      {
          while(p)
          {
              cout<<A[p];
              p--;
          }
      }
      else
      {
          cout<<c;
      }

    }
    return 0;
}

详解:后缀表达式:1 如果读入是数字则直接输出
2如果读入是“#”将栈中所有元素输出
3如果读入是左括号或者是“*”或“/”,则直接入栈
4如果读入是“+”或“-”则出栈,直到遇到左括号,然后将该元素入栈
5如果读入右括号则出栈,直到遇到左括号。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stack>
using namespace std;

int main()
{
    char c;
    stack<char>mystack;
    while(scanf("%c",&c)!=EOF)
    {
        if(c=='('||c=='*'||c=='/')
        {
            mystack.push(c);
        }
        else if(c=='+'||c=='-')
        {
            while(!mystack.empty()&&mystack.top()!='(')
            {
                printf("%c", mystack.top());
                mystack.pop();
            }
            mystack.push(c);
        }
       else  if(c=='#')
        {
          while(!mystack.empty())
            {
                printf("%c", mystack.top());
                mystack.pop();
            }
        }
        else if(c==')')
        {
           while(mystack.top()!='(')
            {
                printf("%c", mystack.top());
                mystack.pop();
            }
            mystack.pop();
        }
        else
        {
            printf("%c",c);
        }
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main()
{
   char c;
   char  A[10000];
   int top=0;
  while(scanf("%c",&c)!=EOF)
  {
      if(c=='('||c=='*'||c=='/')
      {
          top++;
          A[top]=c;
      }
      else if(c=='+'||c=='-')
      {
          while(top)
          {
              if(A[top]=='(')
              {
                  break;
              }
              else
              {
                   printf("%c",A[top]);
                   top--;
              }

          }
          top++;
          A[top]=c;

      }
      else if(c=='#')
      {
          while(top)
          {
              printf("%c",A[top]);
              top--;
          }
      }
      else if(c==')')
      {
          while(top)
          {
              if(A[top]=='(')
              {
                  break;
              }
              else
              {
                   printf("%c",A[top]);
                   top--;
              }

          }
          top--;
      }
      else
      {
          printf("%c",c);
      }
  }
    return 0;
}

posted on 2018-10-04 17:09  飞行的猪哼哼  阅读(28)  评论(0)    收藏  举报