• c中缀转后缀


    点击查看代码
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define maxsize 20
    #define true 1
    #define false 0
    
    typedef struct st2 {
    	int top;
    	char str[maxsize];
    }CharStackSize, *CharStack;
    
    //初始化
    void init(CharStack cstack)
    {
    	cstack->top = -1;
    	return;
    }
    //判空
    
    
    int CharisEmpty(CharStack cstack)
    {
    	if (cstack->top == -1) {
    		return true;
    	}
    	return false;
    }
    
    //判满
    
    int CharisFull(CharStack cstack)
    {
    	if (cstack->top == maxsize - 1) {
    		return true;
    	}
    	return false;
    }
    //出栈
    
    int CharPop(CharStack cstack, char * ch)
    {
    	if (CharisEmpty(cstack)) {
    		return false;
    	}
    	*ch = cstack->str[cstack->top--];
    	return true;
    }
    //进栈
    
    int CharPush(CharStack cstack, char ch)
    {
    	if (CharisFull(cstack)) {
    		return false;
    	}
    	cstack->str[++cstack->top] = ch;
    	return true;
    }
    
    int choose(char ch)
    {
    	if (ch == '+' || ch == '-') {
    		return 1;
    	}
    	else if (ch == '*' || ch == '/') {
    		return 2;
    	}
    	else if(ch  == ')'){
    		return 3;
    	}
    	else if (ch == '(') {
    		return 0;
    	}
    	else {
    		return 0;
    	}
    }
    
    int main(int argc, char *argv[])
    {
    	CharStack cstack = (CharStack)malloc(sizeof(CharStack));
    	init(cstack);
    
    	char biaodashi[50];
       scanf("%s",biaodashi);
    
    	for (int i = 0; i < strlen(biaodashi); i++) {
    		if ('0' <= biaodashi[i] && biaodashi[i] <= '9') {
    			printf("%c", biaodashi[i]);
    		}
    		//处理括号
    		else if(biaodashi[i] == '(' ) {
    			CharPush(cstack, biaodashi[i]);
    		}
    		else if (biaodashi[i] == ')') {
    			CharPush(cstack, biaodashi[i]);
    			while (cstack->top != -1) {
    				char ch;
    				CharPop(cstack, &ch);
    				if(ch != '(' && ch != ')')
    					printf("%c", ch);
    				if (ch == '(') {
    					break;
    				}
    			}
    		}
    		else {
    			if (choose(biaodashi[i]) <= choose(cstack->str[cstack->top])) {
    				while (cstack->top != -1) {
    					char ch;
    					CharPop(cstack, &ch);
    					if (ch != '(' && ch != ')')
    						printf("%c", ch);
    					if (ch == '(') {
    						break;
    					}
    					//9+(3-1)*3+6/2
    				}
    				CharPush(cstack, biaodashi[i]);
    				//printf("%c", biaodashi[i]);
    			}
    			else {
    				CharPush(cstack, biaodashi[i]);
    			}
    		}
    	}
    	while (cstack->top != -1) {
    		char ch;
    		CharPop(cstack, &ch);
    		printf("%c", ch);
    	}
    	return 0;
    }
    
    
    
  • 相关阅读:
    django目录
    django之form表单验证
    django操作数据库之查询F,Q操作 和 seach搜索功能
    django的序列化
    三目运算
    【转】做有生命力的接口测试
    【转】浅谈反应测试
    【转】jmeter 进行java request测试
    【转】探索式测试:基本概念
    【转】 测试职业思考:如何成为一名优秀的软件测试工程师
  • 原文地址:https://www.cnblogs.com/tqdlb/p/15676486.html
Copyright © 2020-2023  润新知