zoukankan      html  css  js  c++  java
  • 中缀表达式转后缀表达式

    思路:从左到右遍历中缀表达式的每个数字和符号,如果是数字直接输出,如果是符号判断其与栈顶符号的优先级,是右括号或者优先级低于栈顶符号,则栈顶元素依次出栈并输出

      1 //中缀表达式转后缀表达式
      2 #include<stdio.h>
      3 #include<stdlib.h>
      4 #include<math.h>
      5 #include<ctype.h>//isdigit(c)函数的头文件,用于判断传入的字符c是否为"0-9"的数字字符 
      6 
      7 #define STACK_INIT_SIZE 20 //栈的初始空间大小 
      8 #define STACKINCREMENT 10    //增量,用于追加额外空间 
      9 
     10 typedef char ElemType;
     11 
     12 typedef struct{
     13     ElemType *base;//栈底指针 
     14     ElemType *top;//栈顶指针 
     15     int stackSize;
     16 }sqStack;
     17 
     18 //初始化栈 
     19 void InitStack(sqStack &s){
     20     s.base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
     21     if( !s.base )
     22         exit(0);
     23     s.top = s.base;
     24     s.stackSize = STACK_INIT_SIZE;
     25 }
     26 
     27 //元素入栈 
     28 void Push(sqStack &s, ElemType e){
     29     if( s.top-s.base >= s.stackSize ){
     30         s.base = (ElemType *)realloc(s.base, (s.stackSize+STACKINCREMENT)*sizeof(ElemType));
     31         if( !s.base )
     32             exit(0);
     33     }
     34     *s.top = e;
     35     s.top++;
     36 }
     37 
     38 //元素出栈 
     39 void Pop(sqStack &s, ElemType &e){
     40     if( s.top == s.base )
     41         return ;
     42     e = *--s.top;
     43 }
     44 
     45 //返回栈的当前容量 
     46 int StackLen(sqStack s){
     47     return (s.top-s.base);
     48 } 
     49 
     50 int main()
     51 {
     52     sqStack s;
     53     InitStack(s);
     54     char c,e;
     55     printf("请输入中缀表达式,以‘#’作为结束标志:");
     56     scanf("%c",&c);
     57     while( c != '#' )
     58     {
     59         while( c >= '0' && c <= '9' )
     60         {//处理连续数字 
     61             printf("%c",c);
     62             scanf("%c",&c);
     63             if( c < '0'|| c > '9')
     64             {
     65                 printf(" ");
     66             }
     67         }    
     68         if( c == ')' )
     69         {
     70             Pop(s,e);
     71             while( e != '(')
     72             {
     73                 printf("%c ",e);
     74                 Pop(s,e);
     75             }        
     76         }
     77         else if( c == '+' || c == '-' )
     78         {
     79             if( !StackLen(s) )
     80                 Push(s,c);
     81             else{
     82                 do
     83                 {
     84                     Pop(s,e);
     85                     if( e == '(' )
     86                         Push(s,e);
     87                     else
     88                         printf("%c ",e);
     89                 }while(StackLen(s) && e!='(');
     90                 Push(s,c);
     91             }
     92         }
     93         else if(c == '*' || c == '/' || c == '(')
     94         {
     95             Push(s,c);
     96         }
     97         else if( c == '#' )
     98         {
     99             break ;
    100         } 
    101         else
    102         {
    103             printf("
    出错:输入格式错误!");
    104             return -1;
    105         }
    106         scanf("%c ",&c);    
    107     }
    108     while( StackLen(s) )
    109     {
    110         Pop(s,e);
    111         printf("%c ",e);
    112     }
    113     return 0; 
    114 }

     

  • 相关阅读:
    day5
    \_\_setitem\_\_和\_\_getitem和\_\_delitem__
    描述符(\_\_get\_\_和\_\_set\_\_和\_\_delete\_\_)
    \_\_getattribute\_\_
    面向对象进阶小结
    property装饰器
    super()方法详解
    菱形继承问题
    类的派生
    类的继承
  • 原文地址:https://www.cnblogs.com/geziyu/p/9937716.html
Copyright © 2011-2022 走看看