运算符的优先级(Operator Precedence)总结(表)

概念

  在一个表达式中可能包含多个有不同运算符连接起来的、具有不同数据类型的数据对象;由于表达式有多种运算,不同的结合顺序可能得出不同结果甚至出现错误运算错误,因为当表达式中含多种运算时,必须按一定顺序进行结合,才能保证运算的合理性和结果的正确性、唯一性。
  优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。表达式的结合次序取决于表达式中各种运算符的优先级。优先级高的运算符先结合,优先级低的运算符后结合,同一行中的运算符的优先级相同。

优先级

  优先级与求值顺序无关。如a+b && bc,虽然优先级最高,但这个表达式求值顺序是从左到右。
优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。相同优先级中,按结合性进行结合。大多数运算符结合性是从左到右,只有三个优先级是从右至左结合的,它们是单目运算符、条件运算符、赋值运算符。

优先级需要记住:

指针最优,单目运算优于双目运算。如正负号。
先算术运算,后移位运算,最后位运算。

请特别注意:1 << 3 + 2 & 7等价于 (1 << (3 + 2))&7.
逻辑运算最后结合。

C语言优先级

在这里插入图片描述

C++运算符优先级

在这里插入图片描述

说明

  同一优先级的运算符,结合次序由结合方向所决定。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建顺序栈的结构、出栈、入栈、取栈顶元素算法: ```python class Stack: def __init__(self): self.stack = [] def push(self, item): self.stack.append(item) def pop(self): return self.stack.pop() def peek(self): return self.stack[-1] def is_empty(self): return len(self.stack) == 0 ``` 其中,`push` 方法用于入栈,`pop` 方法用于出栈,`peek` 方法用于取栈顶元素,`is_empty` 方法用于判断栈是否为空。 2. 创建判断达式括号匹配、判断运算符运算符优先级和两数运算的函数: ```python def is_matching_parentheses(expr): stack = Stack() for char in expr: if char in '({[': stack.push(char) elif char in ')}]': if stack.is_empty(): return False elif char == ')' and stack.peek() == '(': stack.pop() elif char == '}' and stack.peek() == '{': stack.pop() elif char == ']' and stack.peek() == '[': stack.pop() else: return False return stack.is_empty() def is_operator(char): return char in '+-*/' def get_operator_precedence(char): if char in '+-': return 1 elif char in '*/': return 2 else: return -1 def evaluate(expr): operand_stack = Stack() operator_stack = Stack() i = 0 while i < len(expr): if expr[i].isdigit(): j = i while j < len(expr) and expr[j].isdigit(): j += 1 operand = int(expr[i:j]) operand_stack.push(operand) i = j elif is_operator(expr[i]): while not operator_stack.is_empty() and get_operator_precedence(operator_stack.peek()) >= get_operator_precedence(expr[i]): operator = operator_stack.pop() operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = evaluate_operation(operand1, operand2, operator) operand_stack.push(result) operator_stack.push(expr[i]) i += 1 else: i += 1 while not operator_stack.is_empty(): operator = operator_stack.pop() operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = evaluate_operation(operand1, operand2, operator) operand_stack.push(result) return operand_stack.pop() def evaluate_operation(operand1, operand2, operator): if operator == '+': return operand1 + operand2 elif operator == '-': return operand1 - operand2 elif operator == '*': return operand1 * operand2 elif operator == '/': return operand1 / operand2 else: raise ValueError('Unknown operator: ' + operator) ``` 其中,`is_matching_parentheses` 函数用于判断达式中的括号是否匹配,`is_operator` 函数用于判断一个字符是否为运算符,`get_operator_precedence` 函数用于获取一个运算符优先级,`evaluate` 函数用于计算达式的值,`evaluate_operation` 函数用于对两个操作数进行指定的运算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值