• 前后缀表达式求值顺序栈C语言


    #include
    #include
    #include
    #define MaxSize 50
    typedef int  ElemType;
    typedef struct {
    	ElemType data[MaxSize];
    	int top;
    }SqStack;
    void InitStack(SqStack& S) {
    	S.top = -1;
    }
    int StackEmpty(SqStack& S) {
    	if (S.top == -1)
    		return 1;
    	else
    		return 0;
    }
    void Push(SqStack& S, ElemType e) {
    	if (S.top == MaxSize - 1)
    		return;
    	S.data[++S.top] = e;
    
    }
    void Pop(SqStack& S, ElemType& e) {
    	if (S.top == -1)
    		return;
    	e = S.data[S.top--];
    }
    int GetTop(SqStack& S) {
    	int e = S.data[S.top];
    	return e;
    }
    int compulate(int x1,char f,int x2) {
    	int e1 = x1;
    	int e2 = x2;
    	int res=0;
    	switch (f) {
    	case '+':
    		res = e1 + e2;
    		break;
    	case '-':
    		res = e1 - e2;
    		break;
    	case '*':
    		res = e1 * e2;
    		break;
    	case '/':
    		res = e1 / e2;
    		break;
    	}
    	return res;
    	
    }
    int zhong(SqStack& S1,SqStack ) {
    
    }
    int qian(SqStack& S, char* s) {//前缀表达式求值
    	int length = strlen(s);
    	for (int i = length-1; i >= 0; i--) {
    		if (s[i] >= '0' && s[i] <= '9') {
    			Push(S, s[i] - '0');
    		}
    		else {
    			int e1, e2;
    			Pop(S, e1);
    			Pop(S, e2);
    			printf("%d%c%d=%d\n", e1, s[i], e2, compulate(e1, s[i], e2));
    			Push(S, compulate(e1, s[i], e2));
    		}
    	}
    	char e;
    	e = GetTop(S);
    	return e;
    }
    int hou(SqStack &S,char *s) {//后缀表达式求值
    	int length = strlen(s);
    	for (int i = 0; i<length; i++) {
    		if (s[i] >= '0' && s[i] <= '9') {
    			Push(S, s[i]-'0');
    		}
    		else {
    			int e1, e2;
    			Pop(S, e1);
    			Pop(S, e2);
    			printf("%d%c%d=%d\n", e2, s[i], e1, compulate(e2, s[i], e1));
    			Push(S, compulate(e2,s[i],e1));
    		}
    	}
    	char e;
    	e=GetTop(S);
    	return e ;
    }
    
    int main() {
    
    	char s[10];//用一个字符数组存储后缀表达式
    	gets_s(s);
    	SqStack S;
    	InitStack(S);
    	int x = qian(S,s);
    	printf("%d", x);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
  • 相关阅读:
    鲸探发布点评:9月22日发售《“京华号”盾构机》数字藏品
    django REST分页 PageNumberPagination
    Linux学习笔记(3)
    酒店订房退房管理系统(数组应用)
    js 字符串转数字
    Golang 结构化日志包 log/slog 详解(一):简单使用
    设计模式——外观模式
    EasyCVR视频汇聚平台无法自动播放视频的原因排查与解决
    Polygon zkEVM zkASM 与 以太坊虚拟机opcode 对应集合
    HTTP的本质理解
  • 原文地址:https://blog.csdn.net/qq_46659042/article/details/133048012