muban caogao

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXSIZE 100
#define another 50
#define true 1
#define false 0
#define OVERFLOW -2
using namespace std;

typedef int status;
typedef char elemtype;
//stack
typedef struct
{
    elemtype *base;
    elemtype *top;
    int stacksize;
}Sqstack;

status isEmpty(Sqstack &S)
{
    if(S.top == S.base)
        return true;
    else
        return false;
}
status isFull(Sqstack &S)
{
    if(S.top - S.base >= MAXSIZE)
        return true;
    else
        return false;
}
void initSqstack(Sqstack &S)
{
    S.base = new elemtype[MAXSIZE];
    S.top = S.base;
    S.stacksize = MAXSIZE;
}
elemtype getTop(Sqstack &S)
{
    if(S.top == S.base)
        return false;
    else
        return *(S.top - 1);
}
void Push(Sqstack &S, elemtype e)
{
    if(S.top - S.base >= S.stacksize)
    {
        S.base = (elemtype *)realloc(S.base,
        (another + S.stacksize) *sizeof(elemtype));
        S.top = S.base + S.stacksize;
        S.stacksize += another;
    }
    *S.top++ = e;
}
int Pop(Sqstack &S, elemtype &e)
{
    if(S.top == S.base) return false;
    return e = * --S.top;
}
void clear(Sqstack &S)
{
    S.base = S.top;
}
//运算符优先级比较
int Cmp(char ch)
{
    int flag;
    if(ch =='+' || ch == '-')
        flag = 1;
    else if(ch == '*' || ch == '/')
        flag = 2;
    else if(ch == '(')
                flag = 3;
    else if(ch == ')')
        flag = 4;
    return flag;
}
//转换后缀狮
void change(Sqstack &S, char str[])
{
    char ch;
    for(int i = 0; str[i] != '#'; i++)
    {
        if(str[i] >= 'a' && str[i] <= 'z'){
            printf("%c", str[i]);
        }
        else
        {
            if(isEmpty(S)){
                Push(S, str[i]);
            }
            else if(Cmp(str[i]) > Cmp(getTop(S))){
                if(str[i] == ')'){
                    while(getTop(S) != '('){
                            printf("%c", getTop(S));
                            Pop(S, ch);
                          }
                          Pop(S, ch);
                }
                else
                    Push(S, str[i]);
            }
            else {
                if(getTop(S) != '('){
                    printf("%c", getTop(S));
                    Pop(S, ch);
                    Push(S, str[i]);
                   }
                   else
                    Push(S, str[i]);
            }
        }
    }
    while(!isEmpty(S)){
        char c;
        Pop(S, c);
        printf("%c", c);
    }
}
// 后缀狮求值
//遇到字母入栈,遇到运算符的时候将栈顶两个元素作运算,元素出栈,运算结果入栈。
// 次顶元素 与栈顶元素 作比较;
void Anser(Sqstack S, char str[])
{
    for(int i = 0; str[i] != '#'; i++){
        if(str[i] >= '0' && str[i] <= '9'){
            Push(S, str[i] - '0');
        }
        else if(str[i] == '*'){
            int a, b;
            Pop(S, a);
            Pop(S, b);
            Push(S, b*a);
        }
        else if(str[i] == '/'){
            int a, b;
            Pop(S, a);
            Pop(S, b);
            Push(S, b/a);
        }
        else if(str[i] == '+'){
            int a, b;
            Pop(S, a);
            Pop(S, b);
            Push(S, b+a);
        }
        else if(str[i] == '-'){
            int a, b;
            Pop(S, a);
            Pop(S, b);
            Push(S, b-a);
        }
    }
    printf("%d\n", getTop(S));
}
//括号匹配
int match(Sqstack &S, char str[])
{
    int flag;
    for(int i = 0; str[i] != '\0'; i++){
        if(str[i] == '(' || str[i] == '{' || str[i] == '['){
            flag = true;
            Push(S, str[i]);
           }
        else if(str[i] == ')'){
            if(isEmpty(S)){
                flag = false;
                break;
            }
            char c = getTop(S);
            if(getTop(S) == '('){
                Pop(S, c);
                flag = true;
               }
            else{
                flag = false;
                break;
            }
        }
        else if(str[i] == '}'){
            if(isEmpty(S)){
                flag = false;
                break;
            }
            char c = getTop(S);
            if(getTop(S) == '{'){
                Pop(S, c);
                flag = true;
               }
            else{
                flag = false;
                break;
            }
        }
        else if(str[i] == ']'){
            if(isEmpty(S)){
                flag = false;
                break;
            }
            char c = getTop(S);
            if(getTop(S) == '['){
                Pop(S, c);
                flag = true;
               }
            else{
                flag = false;
                break;
            }
        }
    }
    if(!isEmpty(S))
        flag = false;

    return flag;
} 
int main()
{
    char str[55];
    while(gets(str))
    {
        Sqstack S;
        initSqstack(S);
        int flag = match(S, str);
        if(flag == true)
            printf("yes\n");
        else
            printf("no\n");
    }
    Sqstack S;
    initSqstack(S);
    scanf("%s", str);
    change(S, str);
    if(n == 0) printf("0\n");
    else{
    while(n)
    {
        int cnt = n % r;
        Push(S,cnt);
        n /= r;
    }
    while(!isEmpty(S))
    {
        int cnt = Pop(S, cnt);
        printf("%d", cnt);
    }
    printf("\n");
    }
    //printf("\n");
    char str[100];
    Sqstack S;
    initSqstack(S);
    scanf("%s", str);
    Anser(S, str);*/
    char str[250];
    char ch;
     while(gets(str))
     {
         Sqstack S;
         initSqstack(S);
          for(int i = 0; i < strlen(str); i++){
            if(str[i] == '#'){
               if(!isEmpty(S)){
                    Pop(S, ch);
               }
            }
            else if(str[i] == '@'){
                clear(S);
            }
            else
                Push(S, str[i]);
        }
        int i = 0;
        while(!isEmpty(S)){
            ch = Pop(S, ch);
            str[i++] = ch;
        }
        for(int j = i-1; j >= 0; j--)
            printf("%c", str[j]);
        printf("\n");
	}
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值