#includestdio.h #includestring.h //char stackchar stack[25]; int top = -1; void push(char item) {
编程学习网为您整理以下代码实例,主要实现:C语言数据结构表达解析使用堆栈,希望可以帮到各位朋友。
#include<stdio.h> 
#include<string.h> 
//char stack
char stack[25]; 
int top = -1; 
voID push(char item) {
   stack[++top] = item; 
} 
char pop() {
   return stack[top--]; 
} 
//returns precedence of operators
int precedence(char symbol) {
   switch(symbol) {
      case '+': 
      case '-':
         return 2; 
         break; 
      case '*': 
      case '/':
         return 3; 
         break; 
      case '^': 
         return 4; 
         break; 
      case '(': 
      case ')': 
      case '#':
         return 1; 
         break; 
   } 
} 
//check whether the symbol is operator?
int isOperator(char symbol) {
   switch(symbol) {
      case '+': 
      case '-': 
      case '*': 
      case '/': 
      case '^': 
      case '(': 
      case ')':
         return 1; 
      break; 
         default:
         return 0; 
   } 
} 
//converts infix Expression to postfix
voID convert(char infix[],char postfix[]) {
   int i,symbol,j = 0; 
   stack[++top] = '#'; 
   for(i = 0;i<strlen(infix);i++) {
      symbol = infix[i]; 
      if(isOperator(symbol) == 0) {
         postfix[j] = symbol; 
         j++; 
      } else {
         if(symbol == '(') {
            push(symbol); 
         } else {
            if(symbol == ')') {
               while(stack[top] != '(') {
                  postfix[j] = pop(); 
                  j++; 
               } 
               pop();   //pop out (. 
            } else {
               if(precedence(symbol)>precedence(stack[top])) {
                  push(symbol); 
               } else {
                  while(precedence(symbol)<=precedence(stack[top])) {
                     postfix[j] = pop(); 
                     j++; 
                  } 
                  push(symbol); 
               }
            }
         }
      }
   }
   while(stack[top] != '#') {
      postfix[j] = pop(); 
      j++; 
   } 
   postfix[j]='\0'; //null terminate string. 
} 
//int stack
int stack_int[25]; 
int top_int = -1; 
voID push_int(int item) {
   stack_int[++top_int] = item; 
} 
char pop_int() {
   return stack_int[top_int--]; 
} 
//evaluates postfix Expression
int evaluate(char *postfix){
   char ch;
   int i = 0,operand1,operand2;
   while( (ch = postfix[i++]) != '\0') {
      if(isdigit(ch)) {
         push_int(ch-'0'); // Push the operand 
      } else {
         //Operator,pop two  operands 
         operand2 = pop_int();
         operand1 = pop_int();
         switch(ch) {
            case '+':
               push_int(operand1+operand2);
               break;
            case '-':
               push_int(operand1-operand2);
               break;
            case '*':
               push_int(operand1*operand2);
               break;
            case '/':
               push_int(operand1/operand2);
               break;
         }
      }
   }
   return stack_int[top_int];
}
voID main() { 
   char infix[25] = "1*(2+3)",postfix[25]; 
   convert(infix,postfix); 
   printf("Infix Expression is: %s\n" , infix);
   printf("Postfix Expression is: %s\n" , postfix);
   printf("Evaluated Expression is: %d\n" , evaluate(postfix));
}
				 沃梦达教程
				
			本文标题为:C语言数据结构表达解析使用堆栈
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
 - 明确指定任何或所有枚举数的整数值 1970-01-01
 - 迭代std :: bitset中真实位的有效方法? 2022-10-18
 - 总计将在节日礼物上花多少钱 1970-01-01
 - C语言3个整数的数组 1970-01-01
 - C++多态 1970-01-01
 - 向量<unique_ptr<A>>使用初始化列表 2022-10-23
 - C语言数组 1970-01-01
 - 用指数格式表示浮点数 1970-01-01
 - 对 STL 容器的安全并行只读访问 2022-10-25
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				