C語言中棧和隊(duì)列實(shí)現(xiàn)表達(dá)式求值的實(shí)例
C語言中棧和隊(duì)列實(shí)現(xiàn)表達(dá)式求值的實(shí)例
實(shí)現(xiàn)代碼:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define STACK_SIZE 20
#define STACK_INCREMENT 10
#define QUEUE_SIZE 20
typedef int Status;
typedef char StackElemtype;
typedef struct Stack{
StackElemtype* base;
StackElemtype* top;
int stackSize;
}Stack;
Status StackInit(Stack* s){
s->base = (StackElemtype*)malloc(sizeof(StackElemtype) * STACK_SIZE);
if( !s->base )
return ERROR;
s->top = s->base;
s->stackSize = STACK_SIZE;
return OK;
}
Status Pop(Stack* s,StackElemtype* value){
if( s->base == s->top ){
printf("\nstack empty\n");
return ERROR;
}
*value = *(--(s->top));
return OK;
}
Status Push(Stack* s,StackElemtype value){
if( s->top - s->base == s->stackSize){
s->base = (StackElemtype*)realloc(s->base,sizeof(StackElemtype) * (STACK_INCREMENT + STACK_SIZE));
if( !s->base )
return ERROR;
s->top = s->base + STACK_SIZE;
s->stackSize = STACK_SIZE + STACK_INCREMENT;
}
*(s->top) = value;
s->top++;
return OK;
}
int StackLength(Stack s){
return s.top - s.base;
}
typedef double StackElemtype_ForValueExperssion;
typedef struct Stack_2{
StackElemtype_ForValueExperssion* base;
StackElemtype_ForValueExperssion* top;
int stackSize;
}Stack_2;
Status StackInit_2(Stack_2* s){
s->base = (StackElemtype_ForValueExperssion*)malloc(sizeof(StackElemtype_ForValueExperssion) * STACK_SIZE);
if( !s->base )
return ERROR;
s->top = s->base;
s->stackSize = STACK_SIZE;
return OK;
}
Status Pop_2(Stack_2* s,StackElemtype_ForValueExperssion* value){
if( s->base == s->top ){
printf("\nstack empty\n");
return ERROR;
}
*value = *(--(s->top));
return OK;
}
Status Push_2(Stack_2* s,StackElemtype_ForValueExperssion value){
if( s->top - s->base == s->stackSize){
s->base = (StackElemtype_ForValueExperssion*)realloc(s->base,sizeof(StackElemtype_ForValueExperssion) * (STACK_INCREMENT + STACK_SIZE));
if( !s->base )
return ERROR;
s->top = s->base + STACK_SIZE;
s->stackSize = STACK_SIZE + STACK_INCREMENT;
}
*(s->top) = value;
s->top++;
return OK;
}
typedef double QueueElemtype;
typedef char QueueOperatorValue;
typedef struct QueueNode{
QueueElemtype data;
QueueOperatorValue operator;
struct QueueNode* next;
int flag;
}QueueNode,*QueueNodePtr;
typedef struct Queue{
QueueNodePtr front;
QueueNodePtr rear;
}Queue;
Status QueueInit(Queue* q){
q->front = (QueueNodePtr)malloc(sizeof(QueueNode));
if( !q->front )
return ERROR;
q->rear = q->front;
q->rear->next = NULL;
return OK;
}
Status QueueInsert(Queue* q,QueueElemtype value){
QueueNodePtr new;
new = (QueueNodePtr)malloc(sizeof(QueueNode));
if( !new )
return ERROR;
new->data = value;
new->flag = 1;
new->next = NULL;
q->rear->next = new;
q->rear = new;
return OK;
}
Status QueueInsert_operatorValue(Queue* q,QueueOperatorValue value){
QueueNodePtr new;
new = (QueueNodePtr)malloc(sizeof(QueueNode));
if( !new )
return ERROR;
new->operator = value;
new->flag = 0;
new->next = NULL;
q->rear->next = new;
q->rear = new;
return OK;
}
Status QueueDelete(Queue* q,QueueElemtype* value,QueueOperatorValue *operator,int* symbol){
QueueNodePtr first;
if( q->front == q->rear )
return ERROR;
first = q->front->next;
if( first->flag == 1 ){
*value = first->data;
*symbol = 1;
}
else{
*operator = first->operator;
*symbol = 0;
}
q->front->next = first->next;
if( first == q->rear ){
q->rear = q->front;
}
return OK;
}
/* 利用棧將中綴表達(dá)式轉(zhuǎn)化為后綴表達(dá)式:
* ——————————————————————————————————————————————————————————————
* | 用戶的輸入 | 進(jìn)行的處理 |
* | 0~9: | 直接輸出到控制臺(tái) |
* | /,*,( | 直接Push |
* | +,- | 將棧中的元素Pop直到1.??栈蛘呤?.遇到( |
* | ) | 在遇到(之前將棧中的元素全部Pop |
* ——————————————————————————————————————————————————————————————
* */
Status Infix2Postfix(Queue* q){
//Queue q;
//QueueInit(&q);
Stack s;
StackInit(&s);
char c,e;
char bufferDigit[10];
int i = 0;
double longDigit;
printf(" Please Enter Infix Expression\n");
printf("------------NOTE: end of '#'--------------\n");
scanf("%c", &c);
while( '#' != c){
while( c <= '9' && c >= '0' || '.' == c ){
bufferDigit[i++] = c;
bufferDigit[i] = '\0';
scanf("%c", &c);
if(!((c <= '9' && c >= '0' ) || '.' == c )){
longDigit = atof(bufferDigit);
QueueInsert(q,longDigit);
i = 0;
}
}
if( '(' == c || '*' == c || '/' == c ){
Push(&s, c);
}
else if( '+' == c || '-' == c ){
if( !StackLength(s) )
Push(&s, c);
else{
Pop(&s, &e);
while( '(' != e ){
QueueInsert_operatorValue(q, e);
if( StackLength(s) == 0 ){
break;
}else
Pop(&s, &e);
}
if( '(' == e )
Push(&s, e);
Push(&s, c);
}
}else if( ')' == c ){
Pop(&s, &e);
while( '(' != e ){
QueueInsert_operatorValue(q, e);
Pop(&s, &e);
}
}else if( '#' == c){
break;
}else{
printf("input ERROR!\n");
return ERROR;
}
scanf("%c", &c);
}
while(StackLength(s)){
Pop(&s, &e);
QueueInsert_operatorValue(q, e);
}
QueueInsert_operatorValue(q,'#');
return OK;
}
Status ShowQueue(Queue q){
printf("The Reverse Polish Notation is:");
if(q.front == q.rear){
printf("Queue Empty");
return ERROR;
}
QueueNodePtr p = q.front->next;
while(p != q.rear){
if(p->flag)
printf("%g ", p->data);
else
printf("%c ", p->operator);
p = p->next;
}
printf("\n");
return OK;
}
/* 利用棧求解后綴表達(dá)式(逆波蘭表達(dá)式)的值。
* ——————————————————————————————————————————————————————————————————————
* | +,-,*,/, | 將棧頂?shù)膬蓚€(gè)元素彈出進(jìn)行計(jì)算,將結(jié)果壓入棧頂 |
* | 數(shù)字 | 將其壓入棧頂 |
* ———————————————————————————————————————————————————————————————————————
* */
Status ValueExpression(Queue q){
Stack_2 s;
StackInit_2(&s);
double o1;
double o2;
QueueElemtype number;
QueueOperatorValue operator;
int symbol;
QueueDelete(&q,&number,&operator,&symbol);
while( symbol == 1 || ( symbol == 0 && '#' != operator)){
if(symbol == 1){
Push_2(&s, number);
}
else if(symbol == 0){
switch(operator){
case '+':
Pop_2(&s,&o1);
Pop_2(&s,&o2);
Push_2(&s,o2 + o1);
break;
case '-':
Pop_2(&s,&o1);
Pop_2(&s,&o2);
Push_2(&s,o2 - o1);
break;
case '*':
Pop_2(&s,&o1);
Pop_2(&s,&o2);
Push_2(&s,o2 * o1);
break;
case '/':
Pop_2(&s,&o1);
Pop_2(&s,&o2);
Push_2(&s,o2 / o1);
break;
}
}
QueueDelete(&q,&number,&operator,&symbol);
}
Pop_2(&s,&o1);
printf("The Value of the Expression is %g\n",o1);
return OK;
}
int main(){
Queue q;
QueueInit(&q);
Infix2Postfix(&q);
ShowQueue(q);
/*
QueueElemtype number;
QueueOperatorValue operator;
int symbol;
QueueDelete(&q,&number,&operator,&symbol);
printf("%f,%c,%d\n",number,operator,symbol);
*/
ValueExpression(q);
//Stack
/*
Stack s;
StackInit(&s);
StackElemtype c;
Push(&s,'1');
Push(&s,'2');
Push(&s,'3');
Push(&s,'4');
Pop(&s,&c);
printf("%c ", c);
Pop(&s,&c);
printf("%c ", c);
Pop(&s,&c);
printf("%c ", c);
Pop(&s,&c);
printf("%c ", c);
*/
//Queue
/*
Queue q;
QueueElemtype c;
QueueInit(&q);
QueueInsert(&q,1);
QueueInsert(&q,2);
QueueInsert(&q,3);
QueueInsert(&q,4);
QueueDelete(&q,&c);
printf("%d ", c);
QueueDelete(&q,&c);
printf("%d ", c);
QueueDelete(&q,&c);
printf("%d ", c);
QueueDelete(&q,&c);
printf("%d ", c);
if(QueueDelete(&q,&c)){
printf("%d ",c);
}
*/
/*
Queue q;
QueueInit(&q);
QueueInsert(&q,2.1);
QueueInsert_operatorValue(&q,'+');
QueueInsert(&q,43.1);
QueueInsert_operatorValue(&q,'a');
QueueInsert_operatorValue(&q,'(');
int iswho;
double d;
char c;
QueueDelete(&q,&d,&c,&iswho);
if(iswho == 1)
printf("%f ",d);
else
printf("%c ", c);
QueueDelete(&q,&d,&c,&iswho);
if(iswho == 1)
printf("%f ",d);
else
printf("%c ", c);
QueueDelete(&q,&d,&c,&iswho);
if(iswho == 1)
printf("%f ",d);
else
printf("%c ", c);
QueueDelete(&q,&d,&c,&iswho);
if(iswho == 1)
printf("%f ",d);
else
printf("%c ", c);
*/
return 0;
}
以上就是C語言數(shù)據(jù)結(jié)構(gòu)中棧和隊(duì)列的應(yīng)用,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:LZ77壓縮算法原理的理解
欄 目:C語言
下一篇:C語言實(shí)現(xiàn)時(shí)區(qū)轉(zhuǎn)換函數(shù)的實(shí)例
本文標(biāo)題:C語言中棧和隊(duì)列實(shí)現(xiàn)表達(dá)式求值的實(shí)例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1259.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁面的局部加載


