顯示廣告
隱藏 ✕
看板 TL
作者 TL (踢欸樓)
標題 [筆記] Re: y資料結構問題
時間 2013年01月27日 Sun. PM 06:25:09


發信人: tucson.bbs@bbs.ee.nsysu.edu.tw (愛睡了), 看板: C_and_CPP
標  題: Re: y資料結構問題
發信站: 漢神小站 (Fri Apr 16 02:39:59 1999)
轉信站: Ptt!CSZoneNews!Handxon

※ 引述《imlex.bbs@ptt.csie.ntu.edu.tw (不知名最珍貴)》之銘言:
: 用後序寫一個運算式的程式
: 計算結果


       下面的程式看一下吧

#include <stdlib.h>

struct stack_node                 /* 堆疊的結構宣告     */
{
   int data;                      /* 堆疊資料           */
   struct stack_node *next;       /* 指向下一節點       */
};
typedef struct stack_node stack_list; /* 串列新型態     */
typedef stack_list *link;         /* 串列指標新型態     */

link operand  = NULL;             /* 運算元堆疊指標     */

/* ---------------------------------------- */
/*  堆疊資料的存入                          */
/* ---------------------------------------- */
link push(link stack,int value)
{
   link new_node;                 /* 新節點指標         */

   /* 配置節點記憶體 */
   new_node = ( link ) malloc(sizeof(stack_list));
   if ( !new_node )
   {
      printf("記憶體配置失敗! \n");
      return NULL;                /* 存入失敗           */
   }
   new_node->data = value;        /* 建立節點內容       */
   new_node->next = stack;        /* 新節點指向原開始   */
   stack = new_node;              /* 新節點成為堆疊開始 */
   return stack;
}

/* ---------------------------------------- */
/*  堆疊資料的取出                          */
/* ---------------------------------------- */
link pop(link stack,int *value)
{
   link top;                      /* 指向堆疊頂端       */

   if ( stack != NULL )
   {
      top = stack;                /* 指向堆疊頂端       */
      stack = stack->next;        /* 堆疊指標指向下節點 */
      *value = top->data;         /* 取出資料           */
      free(top);                  /* 釋回節點記憶體     */
      return stack;               /* 傳回堆疊指標       */
   }
   else
      *value = -1;
}

/* ---------------------------------------- */
/*  檢查堆疊是否是空的                      */
/* ---------------------------------------- */
int empty(link stack)
{
   if ( stack == NULL )           /* 是否是空           */
      return 1;                   /* 空的               */
   else
      return 0;                   /* 不是空的           */
}

/* ---------------------------------------- */
/*  是否是運算子                            */
/* ---------------------------------------- */
int isoperator(char op)
{
   switch ( op )
   {
      case '+':
      case '-':
      case '*':
      case '/': return 1;         /* 是運算子           */
      default:  return 0;         /* 不是運算子         */
   }
}

/* ---------------------------------------- */
/*  計算二元運算式的值                      */
/* ---------------------------------------- */
int get_value(int op,int operand1,int operand2)
{
   switch ( (char) op )
   {
      case '*': return ( operand2 * operand1 );
      case '/': return ( operand2 / operand1 );
      case '+': return ( operand2 + operand1 );
      case '-': return ( operand2 - operand1 );
   }
}

/* ---------------------------------------- */
/*  主程式: 輸入後序運算式後, 計算其值.     */
/* ---------------------------------------- */
void main()
{
   char exp[100];                 /* 運算式字串變數     */
   int operand1 = 0;              /* 前運算元變數       */
   int operand2 = 0;              /* 後運算元變數       */
   int result = 0;                /* 計算結果變數       */
   int pos = 0;                   /* 目前運算式位置     */

   printf("請輸入後序運算式 ==> ");
   gets(exp);                     /* 讀取運算式         */
   printf("後序運算式[%s]的結果是 ",exp);
   /* 剖析運算式字串迴路 */
   while ( exp[pos] != '\0' && exp[pos] != '\n' )
   {
      if ( isoperator(exp[pos]) ) /* 是不是運算子       */
      {
         /* 從堆疊取出一運算子和兩運算元 */
         operand = pop(operand,&operand1);
         operand = pop(operand,&operand2);
         /* 計算取出運算子和元的值後, 存入堆疊 */
         operand = push(operand,
                        get_value(exp[pos],operand1,operand2));
      }
      else
         /* 是運算元, 存入運算元堆疊 */
         operand = push(operand,exp[pos]-48);
      pos++;                      /* 下一字串位置       */
   }
   operand = pop(operand,&result); /* 取出結果          */
   printf(" %d\n",result);        /* 印出結果           */
}
--
   ※發信站: 漢神小站 <bbs.ee.nsysu.edu.tw>
    ◆ From: 140.117.184.35

--
Shaken, Not Stirred.
--
※ 作者: TL 時間: 2013-01-27 18:25:09
※ 看板: TL 文章推薦值: 0 目前人氣: 0 累積人氣: 33 
分享網址: 複製 已複製
guest
x)推文 r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇