链式栈 C语言练习

5 篇文章 0 订阅
#include <stdio.h>
#include <stdlib.h>

#define true 1
#define false 0
#define bool char

typedef char ElemType;


typedef struct node1
{
    ElemType data;
    struct node1 *next;
}LinkStackNode1,*LinkStack1;
//typedef LinkStackNode1 *LinkStack1;


typedef struct node2
{
    int data;
    struct node2 *next;
}LinkStackNode2,*LinkStack2;
//typedef LinkStackNode2 *LinkStack2;


void InitStack1(LinkStack1 *S)
{
    *S=(LinkStackNode1 *)malloc(sizeof(LinkStackNode1));
    (*S)->next=NULL;
}

void InitStack2(LinkStack2 *S)
{
    (*S) = (LinkStackNode2 *)malloc(sizeof(LinkStackNode2));
    (*S)->next=NULL;
}

bool Push1(LinkStack1 top,char e)
{
    LinkStackNode1 *tmp;
    tmp=(LinkStackNode1 *)malloc(sizeof(LinkStackNode1));
    if(tmp==NULL)
    {
        printf("申请空间失败");
        return false;
    }
    tmp->data=e;
    tmp->next=top->next;
    return true;
}

bool Push2(LinkStack2 top,int e)
{
    LinkStackNode2 *tmp;
    tmp=(LinkStackNode2 *)malloc(sizeof(LinkStackNode2));
    if(tmp==NULL)
    {
        printf("申请空间失败");
        return false;
    }
    tmp->data=e;
    tmp->next=top->next;
    return true;
}


bool Pop1(LinkStack1 top,char *e)
{
    LinkStackNode1 *tmp;
    tmp=top->next;
    if(tmp==NULL)
    {
        printf("栈空error");
        return false;
    }
    *e=tmp->data;
    free(tmp);
    return true;
}


bool Pop2(LinkStack2 top,int *e)
{
    LinkStackNode2 *tmp;
    tmp=top->next;
    if(tmp==NULL)
    {
        printf("栈空error");
        return false;
    }
    *e=tmp->data;
    free(tmp);
    return true;
}


bool In1(char ch,LinkStack1 OPSet)
{
    LinkStackNode1 *tmp;
    tmp=OPSet->next;
    while(tmp!=NULL)
    {
        if(tmp->data==ch)
        {
            return true;
        }
        tmp=tmp->next;
    }
    return true;
}

char GetTop1(LinkStack1 S)
{

    LinkStackNode1 *tmp;
    tmp=S->next;
    return tmp->data;
}

char Compare(char a,char b)
{
    if(b=='*' || b=='/')
    {
        if(a=='*' || a=='/')
        {
            return '=';
        }
        else if(a=='-' || a=='+')
        {
            return '<';
        }
    }
    else if(b=='+' || b=='-')
    {
        if(a=='+' || b=='-')
        {
            return '=';
        }
        else if(a=='*' || a=='/')
        {
            return '>';
        }
    }
}


int Execute(int a,char op,int b)
{
    printf("未完成");
    if(op=='+')
    {
        return a+b;
    }
    else if(op=='-')
    {
        return a-b;
    }
    else if(op=='*')
    {
        return a*b;
    }
    else if(op=='/')
    {
        return a/b;
    }
    else
    {
        printf("??");
    }
}

int ExpEvaluation()
{
    LinkStack1 OPTR,OPSet;
    LinkStack2 OVS;
    int n,v,a,b;
    char ch,op;


    //
    InitStack1(&OPSet);
    Push1(OPSet,'+');
    Push1(OPSet,'-');
    Push1(OPSet,'*');
    Push1(OPSet,'/');
    Push1(OPSet,'^');
    Push1(OPSet,'%');

    ch=GetTop1(OPSet);
    printf("haha");
    putchar(ch);

    InitStack1(&OPTR);
    InitStack2(&OVS);

    Push1(OPTR,'#');
    printf("Input(end with #): ");

    while(true)
    {
        scanf("%d",&n);
        Push2(OVS,n);

        ch=getchar();
        switch(Compare(ch,GetTop1(OPTR)))
        {
        case '>':
            Push1(OPTR,ch);
            break;
        case '=':
        case '<':
            Pop1(OPTR,&op);
            Pop2(OVS,&b);
            Pop2(OVS,&a);
            v=Execute(a,op,b);
            Push2(OVS,v);
        }

        if(ch=='#')
        {
            break;
        }
    }
    /*
    while(ch!='#' || GetTop(OPTR)!='#')
    {
        if(!In(ch,OPSet))
        {
            n=GetNumber(ch);
            push(&OVS,n);
            ch=getchar();
        }
        else
        {
            switch(Compare(ch,GetTop(OPTR)))
            {
            case '>':
                Push(&OPTR,ch);
                ch=getchar();
                break;
            case '=':
            case '<':
                Pop(&OPTR,&op);
                Pop(&OVS,&b);
                Pop(&OVS,&a);
                v=Execute(a,op,b);
                Push(&OVS,v);
                break;
            }
        }
    }
    */
    return true;
}






int main()
{


    if(ExpEvaluation())
    {
        printf("Yes\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值