nyoj 257 蛋疼的c小加

就是把中缀写为后缀。1+2写成12+。
就是栈的应用。难度不大,但是很蛋疼。。。因为调试了好久才过。。
具体思路就是碰到数字直接输出,碰到新符号之后先和栈顶符号比较,优先级大就入栈,小的话就出栈,并且输出,再比较下一个。知道优先级比新符号小,新符号入栈。
涉及到很多细节问题,需要思维比较细= =。比如左右括号问题。左括号不管三七二十一直接入栈,然后其他符号也可以在左括号后压栈。碰到右括号,就一直出栈(不能只出一个,(1-2*3)+4这种),直到将匹配的左括号也出来。也就是说,左括号优先级为任意数,右括号优先级最低。
#include
#include
#include
typedef struct snode
{
char data;
struct snode *next;
}snode;
typedef struct linkstack
{
snode *top;
}linkstack;
void creatstack(linkstack *s)
{
s->top=NULL;
}
int isempty(linkstack *s)
{
return s->top == NULL;
}
void push(linkstack *s,char x)
{
snode *p=(snode *)malloc(sizeof(snode));
p->data=x;
p->next=s->top;
s->top=p;
}
char pop(linkstack *s)
{
char t;
snode *p=s->top;
if(isempty(s))
{
printf("empty\n");
}
else
{
    t=p->data;
    s->top=p->next;
    free(p);
}
return t;
}

char gettop(linkstack *s)
{
    char t;
      if(isempty(s))
        printf("ERROR\n");
else
    t = s -> top -> data;
    return t;

}

void print(linkstack *s)
{
snode *p=s->top;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
}
int match(char b)
{
    int x;
    if(b == '+' || b == '-')
        x = 1;
    else if(b == '*' || b == '/')
        x = 2;
    else if(b == ')' || b == '(')
        x = 0;
    return x;
}

int main()
{
    int n, i;
    linkstack *s = (linkstack *)malloc(sizeof(linkstack));
    creatstack(s);
    char a[1010], t, x;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%s", a);
        for(i = 0 ; a[i] != '\0' ; i++)
        {
            if(a[i] <= '9' && a[i] >= '0')
                printf("%c", a[i]);
            else
            {
                if(isempty(s))
                    push(s, a[i]);
                else
                {
                    if(a[i] == ')')
                    {
                        //t = pop(s);                                             //这个地方这两句一定不能有!否则他就告诉我输出了大于10M的数。。。。T_T
                        //printf("%c", t);
                        while(gettop(s) != '(' )
                            {
                                t = pop(s);
                                printf("%c", t);
                            }
                        x = pop(s);
                    }
                    else if((match(gettop(s)) < match(a[i])) || a[i] == '(')
                        {
                            push(s, a[i]);
                          // printf("%c   ",a[i]);
                        }
                    else
                    {
                      while(!isempty(s) && match(gettop(s)) >= match(a[i]))
                      {
                          t = pop(s);
                          printf("%c", t);
                      }
                      if(a[i] != ')')
                      push(s, a[i]);
                    }
                }
            }
        }
        while(!isempty(s))
        {
            t = pop(s);
            if(t != '(')
              printf("%c", t);
        }
        printf("\n");

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值