表达式求值(C++,代码)

#include<iostream>
#include<stack>
#include<sstream>
using namespace std;
int In(char ch)//判断是否为运算符
{
	if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')'||ch=='#')
		return 1;
	else
		return 0;
}
char Precede(char ch1, char ch2)//判断优先级,ch1栈顶元素
{
	char flag='>';
	if (ch1 == '+')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '#':
		case ')':
			flag = '>';
			break;
		case '*':
		case '/':
		case '(':
		
			flag = '<';
			break;
		}
	}
	else if (ch1 == '-')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '#':
			flag = '>';
			break;
		case '*':
		case '/':
		case '(':
		case ')':
			flag = '<';
			break;
		}
	}
	else if (ch1 == '*')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '#':
		case '*':
		case '/':
		case ')':
			flag = '>';
			break;
		case '(':
			flag = '<';
			break;
		}
	}
	else if (ch1 == '/')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '#':
		case '*':
		case '/':
		case ')':
			flag = '>';
			break;
		case '(':
			flag = '<';
			break;
		}
	}
	else if (ch1 == '(')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '*':
		case '/':
		case '(':
			flag = '<';
			break;
		case ')':
			flag = '=';
			break;
		}
	}
	else if (ch1 == ')')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '*':
		case '/':
		case ')':
		case '#':
			flag = '>';
			break;
		}
	}
	else if (ch1 == '#')
	{
		switch (ch2)
		{
		case '+':
		case '-':
		case '*':
		case '/':
		case '(':
			flag = '<';
			break;
		case '#':
			flag = '=';
			break;
		}
	}
	return flag;
}
int Operate(int a, char b,int c)//运算
{
	if (b == '+')
		return a + c;
	else if (b == '-')
		return a - c;
	else if (b == '*')
		return a * c;
	else  
		return a / c;
}
int main()
{
	stack<char> OPTR;//运算符栈
	stack<int> OPND;//操作数栈
	OPTR.push('#');
	char ch;
	cin >> ch;
	while (ch != '#' || OPTR.top() != '#')
	{
		if (!In(ch))//不为运算符,进入操作数栈
		{
			int a;
			stringstream stream;
			stream << ch; stream >> a;
			OPND.push(a);
			cin >> ch;
		}
		else
		{
			switch (Precede(OPTR.top(), ch))//运算符栈栈顶元素与ch比较
			{
			case '<'://优先级低于,ch压入运算符栈
				OPTR.push(ch);
				cin >> ch;
				break;
			case '>'://优先级高于,栈顶元素弹出,弹出操作数栈两个数,进行运算
				char theta;
				int A, B;
				theta=OPTR.top();
				OPTR.pop();
				A = OPND.top(), OPND.pop();
				B = OPND.top(), OPND.pop();
				OPND.push(Operate(B,theta, A));
				break;
			case '='://括号相等
				OPTR.pop();
				cin >> ch;
				break;
			}
		}
	}
	cout << OPND.top();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我可以回答这个问题。中缀表达式求值可以通过将中缀表达式转换为后缀表达式,然后再用栈来计算后缀表达式的值。以下是一个示例 C 代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 typedef struct { int top; int data[MAX_SIZE]; } Stack; void push(Stack *s, int x) { if (s->top == MAX_SIZE - 1) { printf("Stack overflow\n"); exit(1); } s->data[++s->top] = x; } int pop(Stack *s) { if (s->top == -1) { printf("Stack underflow\n"); exit(1); } return s->data[s->top--]; } int is_operator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } int precedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } else { return ; } } int evaluate(char *expression) { Stack s; s.top = -1; int i, j, len, a, b, result; char c, postfix[MAX_SIZE]; len = strlen(expression); j = ; for (i = ; i < len; i++) { c = expression[i]; if (c >= '' && c <= '9') { postfix[j++] = c; } else if (is_operator(c)) { while (s.top != -1 && is_operator(s.data[s.top]) && precedence(s.data[s.top]) >= precedence(c)) { postfix[j++] = pop(&s); } push(&s, c); } else if (c == '(') { push(&s, c); } else if (c == ')') { while (s.top != -1 && s.data[s.top] != '(') { postfix[j++] = pop(&s); } if (s.top == -1) { printf("Unmatched parentheses\n"); exit(1); } pop(&s); } else { printf("Invalid character: %c\n", c); exit(1); } } while (s.top != -1) { if (s.data[s.top] == '(') { printf("Unmatched parentheses\n"); exit(1); } postfix[j++] = pop(&s); } postfix[j] = '\'; len = strlen(postfix); s.top = -1; for (i = ; i < len; i++) { c = postfix[i]; if (c >= '' && c <= '9') { push(&s, c - ''); } else if (is_operator(c)) { b = pop(&s); a = pop(&s); switch (c) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; } push(&s, result); } else { printf("Invalid character: %c\n", c); exit(1); } } if (s.top != ) { printf("Invalid expression\n"); exit(1); } return pop(&s); } int main() { char expression[MAX_SIZE]; printf("Enter an infix expression: "); scanf("%s", expression); int result = evaluate(expression); printf("Result: %d\n", result); return ; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值