棧的應用舉例

2021-06-05 02:46:34 字數 1925 閱讀 3707

一、目的:

掌握棧的表示,實現及其針對棧的各種操作進行具體的應用。

二、要求:

1、建立乙個順序棧,實現括號配對,判斷乙個表示式中括號配對是否合法。

2、當使用者輸入乙個合法的表示式後,能夠返回正確的結果。能夠計算的運算子包括:加、減、乘、除、括號;能夠計算的數要求在實數範圍內。對於異常表示式給出錯誤提示。(要求使用靜態棧資料結構。)

三、實驗內容

1、設計程式。

2、除錯程式,並設計輸入資料。

四、實驗報告要求

寫出程式和實驗結果。

1.#include

#include

#include

#define initsize 10000

typedef struct

sqstack;

void initstack(sqstack *s);

void print(char expression);

void choose(int choice,char expression);

void inputform(char expression);

void judgeform(char expression);

int match(char x,char y);

void push(sqstack *s,char x);

void pop(sqstack *s);

int main(void)

;print(expression);

while(true)

return 0;

}void initstack(sqstack *s)

void print(char expression)

void choose(int choice,char expression)

}void inputform(char expression)

void judgeform(char expression)

else}}

if(flag)

else

}int match(char x,char y)

')return true;

else return false;

}void push(sqstack *s,char x)

void pop(sqstack *s)

2.#define n 50

#include

#include

#include "stdio.h"

#include "stdlib.h"

typedef structnumstack;//數字棧

typedef structopstack;//操作符棧

int cint(char mychar)

void pushnum(numstack *numstack,double num)

void popnum(numstack *numstack,double *num)

void pushop(opstack *opstack,char op)

void popop(opstack *opstack,char *op)

double calc(double a,double b,char c)

return result;

}char priority(char y,char x)

return priority;

}void process(numstack *numstack,opstack *opstack,char x)}}

else

switch(priority(opstack->array[opstack->top-1],x))}}

main()

棧的應用舉例

十進位制數n和其他d進製數的轉換是計算機實現計算的基本問題,其解決方法很多,其中乙個簡單演算法基於下列原理 n n div d d n mod d 其中 div為整除運算,mod為求餘運算 例如,2007 10 3727 8,其運算過程如下 可以看到上述過程是從低位到高位產生8進製的各個數字,然後從...

3 2棧的應用舉例

數制轉換 十進位制數n和其他d進製數的轉換是計算機實現計算的基本問題,其解決方法很多,簡單演算法基於下列原理 n n div d d n mod d 其中 div為整除運算,mod為求餘運算 例如 1348 10 2504 8 計算過程如下圖 這演算法很簡單,在此不做解釋 我們發現到,對於任意乙個非...

棧的應用舉例(三)

struct postype 全值變數 postype begin,end 迷宮的入口座標,出口座標 postype direc 4 移動方向依次為東南西北 define maxlength 25 設迷宮的最大行列為25 typedef int mazetype maxlength maxlengt...