欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言中棧和隊列實現表達式求值的實例

 更新時間:2017年08月14日 10:09:56   投稿:lqh  
這篇文章主要介紹了C語言中棧和隊列實現表達式求值的實例的相關資料,這里主要是對數據結構中棧和隊列的理解和應用,需要的朋友可以參考下

C語言中棧和隊列實現表達式求值的實例

實現代碼:

#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; 
} 
 
/* 利用棧將中綴表達式轉化為后綴表達式: 
 * —————————————————————————————————————————————————————————————— 
 * | 用戶的輸入  |      進行的處理      | 
 * |  0~9:   | 直接輸出到控制臺        | 
 * |  /,*,(  | 直接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; 
} 
 
/* 利用棧求解后綴表達式(逆波蘭表達式)的值。 
 * —————————————————————————————————————————————————————————————————————— 
 * |  +,-,*,/,  |   將棧頂的兩個元素彈出進行計算,將結果壓入棧頂 | 
 * | 數字      |   將其壓入棧頂                 | 
 * ——————————————————————————————————————————————————————————————————————— 
 * */ 
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語言數據結構中棧和隊列的應用,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • C語言實現兩個矩陣相乘

    C語言實現兩個矩陣相乘

    這篇文章主要為大家詳細介紹了C語言實現兩個矩陣相乘的程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 使用用C++做一顆會跳動的愛心實例代碼

    使用用C++做一顆會跳動的愛心實例代碼

    大家好,本篇文章主要講的是使用用C++做一顆會跳動的愛心實例代碼,感興趣的同學趕快來看一看吧,歡迎借鑒學習C++做一顆會跳動的愛心實例代碼
    2021-12-12
  • C語言詳細講解while語句的用法

    C語言詳細講解while語句的用法

    c語言while語句的使用語法如“while(condition)?{statement(s);}”,該語句可以是單個語句,也可以是一個語句塊,其條件可以是任意表達式,true是任意非零值,當條件為真時,循環(huán)進行迭代
    2022-05-05
  • OpenCV實現幀差法檢測運動目標

    OpenCV實現幀差法檢測運動目標

    這篇文章主要為大家詳細介紹了OpenCV實現幀差法檢測運動目標,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Visual Studio 如何創(chuàng)建C/C++項目問題

    Visual Studio 如何創(chuàng)建C/C++項目問題

    這篇文章主要介紹了Visual Studio 如何創(chuàng)建C/C++項目問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • C++重載的奧義之運算符重載詳解

    C++重載的奧義之運算符重載詳解

    函數的重載是指利用相同的函數名設計一系列功能相近,但是功能細節(jié)不一樣的函數接口;因此運算符重載也是指對于同一個運算符來說,它可以用于實現不同的功能。下面就一起來理解下運算符重載的應用吧
    2023-04-04
  • C到C++的升級關系及區(qū)別實例探究

    C到C++的升級關系及區(qū)別實例探究

    這篇文章主要為大家介紹了C到C++的升級關系及區(qū)別實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 淺析在C/C++中如何寫調試宏

    淺析在C/C++中如何寫調試宏

    這篇文章主要為大家詳細介紹了在C/C++中如何寫調試宏的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下
    2024-05-05
  • Qt實現定時器的兩種方法分享

    Qt實現定時器的兩種方法分享

    這篇文章主要為大家詳細介紹了Qt中實現定時器的兩種不同方法,文中的示例代碼講解詳細,對我們了解Qt有一定的幫助,感興趣的可以跟隨小編一起學習一下
    2022-11-11
  • c++實現圖像像素計算的示例詳解

    c++實現圖像像素計算的示例詳解

    我們知道每張圖像都能夠用矩陣來表示,矩陣中每個元素的值表示了圖像中每個像素值,像素值的大小就對應著圖像的亮暗,本文主要來和大家介紹一下C++進行圖像像素計算的相關知識,感興趣的可以了解下
    2023-12-12

最新評論