3-06. Expression conversion (25)

Source: Internet
Author: User

题目链接:http://pat.zju.edu.cn/contests/ds/3-06


算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式说明:

输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。

输出格式说明:

在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

样例输入与输出:

序号 输入 输出
1
2+3*(7-4)+8/4
2 3 7 4 - * + 8 4 / +
2
((2+3)*4-(8+2))/5
2 3 + 4 * 8 2 + - 5 /
3
1314+25.5*12
1314 25.5 12 * +
4
-2*(+3)
-2 3 *
5
123
123


一直格式错误,求路过大牛点拨指教啊!




代码如下:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <stack>using namespace std;const int maxn = 520;stack <char>ss;char s[maxn];int isnum(char c){    if((c>='0' && c<='9') || c=='.')        return 1;    return 0;}int val_op(char c){    if(c == '(' || c == ')')        return 1;    else if(c == '+' || c == '-')        return 2;    else if(c == '*' || c == '/')        return 3;    return 0;}int main(){    scanf("%s",s);    int len = strlen(s);    int flag = 0, mark = 0;    for(int i = 0; i < len; i++)    {        if(i == 0)//为了防止类似:-2*(+3)的案例        {            if(s[i]=='-' || s[i] == '+')            {                printf("%c",s[0]);                continue;            }        }        //printf("size:%d",ss.size());        if(isnum(s[i]))//遇到运算数直接输出        {            if(!flag)            {                flag = 1;                printf("%c",s[i]);            }            else if(mark == 0)//为了防止类似:-2*(+3)的案例                printf("%c",s[i]);            else            {                printf(" %c",s[i]);                mark = 0;            }            continue;        }        if(s[i-1] == '(' && (s[i]=='+' || s[i]=='-'))        {            continue;        }        mark = 1;//为了防止类似:-2*(+3)的案例        if(s[i] == '(')//遇到左括号压入栈        {            ss.push(s[i]);        }        else if(s[i] == ')')//遇到右括号把栈内的运算符输出知道遇到左括号        {            while(ss.top()!='(')            {                printf(" %c",ss.top());                ss.pop();            }            // printf(" d%cd ",ss.top());            ss.pop();        }        else if(ss.size())        {            if(val_op(s[i]) <= val_op(ss.top()))//如果遇到的运算符的优先级小于等于栈顶运算符            {                while(ss.size())                {                    char tc = ss.top();                    if(val_op(tc) < val_op(s[i]))//直到当前运算符的优先级大于栈顶运算符                    {                        //ss.push(tc);                        break;                    }                    if(tc!='(' && tc != ')')                        printf(" %c",tc);                    ss.pop();                }            }        }        if(s[i]!='(' && s[i]!=')')            ss.push(s[i]);    }    if(ss.size())//输出栈内的运算符    {        int len = ss.size();        for(int i = 0; i < len; i++)        {            if(ss.top()!='(' && ss.top()!=')')                printf(" %c",ss.top());            ss.pop();        }    }    printf("\n");    return 0;}/*2*(9+6/3-5)+4-2*(+3)*/


3-06. 表达式转换(25)(我只是来寻求帮助的,一直PE求案例)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.