013.复杂的boolean表达式

本文深入探讨了Ada语言中布尔运算符的使用,包括等号、不等号、大于等于、小于等于、大于和小于运算符,以及逻辑与、或、非、异或运算符的应用。通过具体实例展示了短路评估的原理。
with Ada.Text_IO,Ada.Integer_Text_IO;
use Ada.Text_IO,Ada.Integer_Text_IO;

procedure Compares is
   
   package Enum_IO is new Ada.Text_IO.Enumeration_IO(Boolean);
   use Enum_IO;
   
   Index,Count		:Integer :=12;
   Truth,Lies,Question  :Boolean;
   
begin
   Truth :=Index=Count; -- this is true
   Lies :=Index<Index; -- this is false
   
   -- example of all boolean operators
   
   Question:=Index=Count;	-- equality
   Question:=Index/=Count;	-- Inequality
   Question:=Index>=Count;	-- greater than or equal
   Question:=Index<=Count;	-- less than or equal
   Question:=Index>Count;	-- greater than
   Question:=Index<Count;	-- less than
                          -- example of all boolean operators
                          
   Question:=Index=12 and Count=12 and Truth and True;
   Question:=Index/=12 or False or Count>3 or Truth;
   Question:=(Truth or Lies) and (Truth and not Lies);
   Question:=True xor Lies;
   -- now for short circuit evaluation
   
   Question:=Index/=Count and then Index = 9/(Index-Count);
   Question:=Index=Count or else Index=9/(Index-Count);
   Question:=(Index=Count) or else (Index=9/(Index-Count));
   
   
   
   end Compares;
   
   
   
    
逆波兰表达式是一种后缀表达式,算符写在后面,而平常使用的算式是中缀表达式。解决LeetCode 150题逆波兰表达式求值问题,可使用栈来实现。 ### Python解法 整数除法只保留整数部分,给定的逆波兰表达式总是有效的,即表达式总会得出有效数值且不存在除数为 0 的情况。示例输入 `["2", "1", "+", "3", "*"]`,输出为 9,因为 `((2 + 1) * 3) = 9`。以下为Python伪代码思路: ```python def evalRPN(tokens): stack = [] for token in tokens: if token in ["+", "-", "*", "/"]: num2 = stack.pop() num1 = stack.pop() if token == "+": result = num1 + num2 elif token == "-": result = num1 - num2 elif token == "*": result = num1 * num2 else: result = int(num1 / num2) stack.append(result) else: stack.append(int(token)) return stack.pop() ``` ### C++解法 ```cpp /* * @lc app=leetcode.cn id=150 lang=cpp * * [150] 逆波兰表达式求值 */ // @lc code=start class Solution { public: int evalRPN(vector<string> &tokens) { stack<int> stk; for (const string &token : tokens) { if (token == "+") { int x = stk.top(); stk.pop(); int y = stk.top(); stk.pop(); stk.push(x + y); } else if (token == "-") { int x = stk.top(); stk.pop(); int y = stk.top(); stk.pop(); stk.push(y - x); } else if (token == "*") { int x = stk.top(); stk.pop(); int y = stk.top(); stk.pop(); stk.push(x * y); } else if (token == "/") { int x = stk.top(); stk.pop(); int y = stk.top(); stk.pop(); stk.push(y / x); } else { int num = stoi(token); stk.push(num); } } return stk.top(); } }; // @lc code=end ``` ### Java解法 ```java class Solution { public int evalRPN(String[] tokens) { Stack<Integer> stack=new Stack<>(); int num1,num2; for(int i=0;i<tokens.length;i++) { if(isNumber(tokens[i])) { stack.push(Integer.parseInt(tokens[i])); }else { num2=stack.pop(); num1=stack.pop(); switch(tokens[i]) { case "+": num1=num1+num2; break; case "-": num1=num1-num2; break; case "*": num1=num1*num2; break; case "/": num1=num1/num2; break; } stack.push(num1); } } return stack.pop(); } public boolean isNumber(String ch) { return !("+".equals(ch)||"-".equals(ch)||"*".equals(ch)||"/".equals(ch)); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值