程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 用棧計算逆波蘭式

用棧計算逆波蘭式

編輯:C++入門知識

print?#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
 
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; 
 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;

}

 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved