zoukankan      html  css  js  c++  java
  • [栈和队列]从中缀向后缀转换表达式

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #define INIT_STACK_SIZE 100
     5 typedef struct
     6 {
     7     char * chOperator;
     8     int dwtop;
     9 }OPND;
    10 
    11 void InitStack(OPND *);
    12 char Pop(OPND *);
    13 void Push(OPND *,char );
    14 char GetTop(OPND);
    15 
    16 char ComparePri(char op1,char op2);
    17 void printStack(OPND);
    18 
    19 char stringBuffer[128] = {''};
    20 char stringValue[128] = {''};
    21 int main()
    22 {
    23     OPND opnd;
    24     int N = 0;
    25     char ch ,*strValue,*strBuffer;
    26     scanf("%d",&N);
    27     getchar();
    28     while(N--){
    29 
    30         strValue = stringValue;
    31         strBuffer = stringBuffer;
    32 
    33         memset(stringValue,0,128*sizeof(char));
    34         memset(stringBuffer,0,128*sizeof(char));
    35 
    36         gets(stringBuffer);
    37         InitStack(&opnd);
    38         Push(&opnd,'#');
    39 
    40         ch = *strBuffer++;
    41         while(ch != '#' || GetTop(opnd) != '#'){
    42             if((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z')) {
    43                 *strValue = ch;
    44                 strValue++;
    45                 ch = *(strBuffer++);
    46             }
    47             else
    48             switch(ComparePri(GetTop(opnd),ch)){
    49                 case '<' : Push(&opnd,ch); ch = *(strBuffer++); break;
    50                 case '=' : Pop(&opnd);     ch = *(strBuffer++); break;
    51                 case '>' : *strValue = Pop(&opnd); strValue++; break;
    52             }
    53         }
    54         puts(stringValue);
    55     }
    56     return 0;
    57 }
    58 void InitStack(OPND *S)
    59 {
    60     S->chOperator = (char *)malloc(INIT_STACK_SIZE * sizeof(char));
    61     if(!S->chOperator) exit(1);
    62     S->dwtop = 0;
    63 }
    64 void Push(OPND *S,char ch)
    65 {
    66     *(S->chOperator + S->dwtop) = ch;
    67     S->dwtop++;
    68 }
    69 char Pop(OPND *S)
    70 {
    71     S->dwtop--;
    72     return *(S->chOperator + S->dwtop);
    73 }
    74 char ComparePri(char op1,char op2)
    75 {
    76     if((op1 == '+' || op1 == '-')&&(op2 == '*' || op2 == '/'))        return '<';
    77     else if((op1 == '(' && op2 == ')')||(op1 == '#' && op2 == '#')) return '=';
    78     else if(op1 == '(' || op1 == '#' || op2 == '(')                 return '<';
    79     else return '>';
    80 }
    81 void printStack(OPND opnd)
    82 {
    83     while(opnd.dwtop){
    84         opnd.dwtop--;
    85         printf("%c",*(opnd.chOperator + opnd.dwtop));
    86     }
    87 }
    88 char GetTop(OPND opnd)
    89 {
    90     return *(opnd.chOperator + opnd.dwtop -1);
    91 }

     

     

  • 相关阅读:
    Android入门第六篇之ListView (一)
    STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
    fastjson 之常见的数据类型与json的相互转换
    Jetty:配置安全
    xml文件格式例如以下
    《编程珠玑》---笔记。浏览此文,一窥此书。
    【Github教程】史上最全github用法:github入门到精通
    SSL连接建立过程分析(1)
    ant 安装
    GMM的EM算法实现
  • 原文地址:https://www.cnblogs.com/Karma-wjc/p/4023071.html
Copyright © 2011-2022 走看看