Sheffi66 2017-10-19 03:21 采纳率: 0%
浏览 1304
已结题

根据要求使用C语言写出逆波兰式

图片说明

  • 写回答

2条回答 默认 最新

  • super晓权 2017-10-19 04:46
    关注

    你这个大学里 离散数学实验不是有代码吗 具体代码 我不记得了 只好自己写一份了
    #include
    #include
    #include

    typedef struct Mystack *Stack;

    struct Mystack {
    int Capacity; /* 栈的容量 /
    int Top_of_stack; /
    栈顶下标 /
    int *Array; /
    存放栈中元素的数组 */
    };

    /* 栈的创建 */
    Stack CreateStack(int Max)
    {
    Stack S;
    S = malloc(sizeof(struct Mystack));
    if (S == NULL)
    printf("Create stack error!\n");

    S->Array = malloc(sizeof(char) * Max);
    if (S->Array == NULL)
        printf("Create stack error!\n");
    
    S->Capacity = Max;
    S->Top_of_stack = 0;
    return S;
    

    }

    /* 释放栈 */
    void DisposeStack(Stack S)
    {
    if (S != NULL)
    {

    free(S->Array);
    free(S);
    }

    }

    /* 判断一个栈是否是空栈 */
    int IsEmpty(Stack S)
    {
    return !S->Top_of_stack;
    }

    /* 判断一个栈是否满栈 */
    int IsFull(Stack S)
    {
    if (S->Top_of_stack == S->Capacity - 1)
    return 1;
    else
    return 0;
    }

    /* 数据入栈 */
    int Push(int x, Stack S)
    {
    if (IsFull(S))
    printf("The Stack is full!\n");
    else
    S->Array[S->Top_of_stack++] = x;
    }

    /* 数据出栈 */
    int Pop(Stack S)
    {
    if (IsEmpty(S))
    printf("The Stack is empty!\n");
    else
    S->Top_of_stack--;
    }

    /* 将栈顶返回 */
    int Top(Stack S)
    {
    if (!IsEmpty(S))
    return S->Array[S->Top_of_stack-1];
    printf("The Stack is empty!\n");
    return 0;
    }

    int main()
    {
    int i, len, result;
    char str[100];
    printf("Please input the postfix that you want to compute: \n");
    scanf("%s", str);
    len = strlen(str);
    /* 根据序列的长度来创建栈 */
    struct Mystack *my_stack = CreateStack(len+1);
    for (i = 0; i < len; i++)
    {
    if (str[i] >= '0' && str[i] <= '9')
    Push((int)str[i]-48, my_stack);
    else if (str[i] == '+')
    {
    int x = Top(my_stack);
    Pop(my_stack);
    int y =Top(my_stack);
    Pop(my_stack);
    Push(x+y, my_stack);
    //printf("%d\n", Top(my_stack));
    }
    else if (str[i] == '-')
    {
    int x = Top(my_stack);
    Pop(my_stack);
    int y = Top(my_stack);
    Pop(my_stack);
    Push(x-y, my_stack);
    //printf("%d\n", Top(my_stack));
    }

        else if (str[i] == '*')
        {
            int x = Top(my_stack);
            Pop(my_stack);
            int y = Top(my_stack);
            Pop(my_stack);
            Push(x*y, my_stack);
            //printf("%d\n", Top(my_stack));
        }
        else if (str[i] == '/')
        {
            int x = Top(my_stack);
            Pop(my_stack);
            int y = Top(my_stack);
            Pop(my_stack);
            Push(x/y, my_stack);
            //printf("%d\n", Top(my_stack));
        }
    }
    printf("The result is: %d\n", Top(my_stack));
    Pop(my_stack);
    /* A bug */
    

    // DisposeStack(my_stack);

    return 0;
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R