2023-07-19:布尔表达式 是计算结果不是 true 就是 false 的表达式

有效的表达式需遵循以下约定:

't',运算结果为 true

'f',运算结果为 false

'!(subExpr)',运算过程为对内部表达式 subExpr 进行 逻辑非(NOT)运算

'&(subExpr1, subExpr2, ..., subExprn)'

运算过程为对 2 个或以上内部表达式

subExpr1, subExpr2, ..., subExprn 进行 逻辑与(AND)运算

'|(subExpr1, subExpr2, ..., subExprn)'

运算过程为对 2 个或以上内部表达式

subExpr1, subExpr2, ..., subExprn 进行 逻辑或(OR)运算

给你一个以字符串形式表述的 布尔表达式 expression,返回该式的运算结果。

题目测试用例所给出的表达式均为有效的布尔表达式,遵循上述约定。

输入:expression = "&(|(f))"。

输出:false。

答案2023-07-19:

大体过程如下:

1.主函数main中定义了一个布尔表达式expression为"&(|(f))",该表达式需要计算结果。

2.调用parseBoolExpr函数,并将布尔表达式作为参数传递给它。

3.parseBoolExpr函数中定义了一个内部递归函数f,接收两个参数:表达式字符串exp和当前字符索引index

4.函数f中首先获取当前索引处的字符judge,判断其类型。

5.如果judge为't',返回结果为true,索引保持不变。

6.如果judge为'f',返回结果为false,索引保持不变。

7.如果judge为其他字符,进行进一步判断。

8.如果judge为'!',则递归调用f函数,并将索引加1作为参数,获取递归调用的结果next,对该结果执行逻辑非运算,返回结果为!next.ans,索引更新为next.end + 1

9.如果judge为'&'或'|',则设置布尔变量ans为相应的值(true或false),并在循环中处理多个子表达式。

10.在循环中,当当前字符不是')'时,执行以下操作:

- 如果当前字符是',',则将索引加1,跳过逗号字符。

- 否则,递归调用`f`函数,并将当前索引作为参数获取递归结果`next`。

- 根据父表达式的运算符进行相应的逻辑运算,更新布尔变量`ans`的值。

- 更新索引为`next.end + 1`。

11.循环结束后,返回结果为Info{ans, index},其中ans为布尔表达式的计算结果,index为当前索引。

12.返回到parseBoolExpr函数,获取f函数的结果Info,返回Info.ans作为布尔表达式的最终计算结果。

13.输出最终结果。根据给定的表达式"&(|(f))",计算结果为false,打印结果false。

时间复杂度:假设表达式字符串的长度为n,递归过程涉及到遍历字符串中的每个字符,因此时间复杂度为O(n)。

空间复杂度:递归调用过程中会使用额外的栈空间来保存递归的状态,最坏情况下递归的深度可以达到n,因此空间复杂度为O(n)。

go完整代码如下:

package main

import (
"fmt"
) type Info struct {
ans bool
end int
} func parseBoolExpr(expression string) bool {
return f([]rune(expression), 0).ans
} func f(exp []rune, index int) Info {
judge := exp[index]
if judge == 'f' {
return Info{false, index}
} else if judge == 't' {
return Info{true, index}
} else {
var ans bool
index += 2
if judge == '!' {
next := f(exp, index)
ans = !next.ans
index = next.end + 1
} else {
ans = judge == '&'
for exp[index] != ')' {
if exp[index] == ',' {
index++
} else {
next := f(exp, index)
if judge == '&' {
if !next.ans {
ans = false
}
} else {
if next.ans {
ans = true
}
}
index = next.end + 1
}
}
}
return Info{ans, index}
}
} func main() {
expression := "&(|(f))"
result := parseBoolExpr(expression)
fmt.Println(result)
}

rust代码如下:

fn main() {
let expression = "&(|(f))";
let result = parse_bool_expr(expression.to_string());
println!("{}", result);
} fn parse_bool_expr(expression: String) -> bool {
let exp: Vec<char> = expression.chars().collect();
let info = f(&exp, 0);
info.ans
} struct Info {
ans: bool,
end: usize,
} fn f(exp: &[char], index: usize) -> Info {
let judge = exp[index];
if judge == 'f' {
return Info {
ans: false,
end: index,
};
} else if judge == 't' {
return Info {
ans: true,
end: index,
};
} else {
let mut ans: bool;
let mut index = index + 2;
if judge == '!' {
let next = f(exp, index);
ans = !next.ans;
index = next.end + 1;
} else {
ans = judge == '&';
while exp[index] != ')' {
if exp[index] == ',' {
index += 1;
} else {
let next = f(exp, index);
if judge == '&' {
if !next.ans {
ans = false;
}
} else {
if next.ans {
ans = true;
}
}
index = next.end + 1;
}
}
}
Info { ans, end: index }
}
}

c++完整代码如下:

#include <iostream>
#include <string> using namespace std; struct Info {
bool ans;
// 结束下标!
int end; Info(bool a, int e) {
ans = a;
end = e;
}
}; Info f(const string& exp, int index) {
char judge = exp[index];
if (judge == 'f') {
return Info(false, index);
}
else if (judge == 't') {
return Info(true, index);
}
else {
// !
// &
// |
// 再说! bool ans; // ! ( ?
// i i+1 i+2
// & ( ?
// i i+1 i+2
// | ( ?
// i i+1 i+2
index += 2;
if (judge == '!') {
// ! ( ?...... )
// i i+1 i+2
Info next = f(exp, index);
ans = !next.ans;
index = next.end + 1;
}
else {
// &
// i
// judge == '&' 或者 judge == '|'
ans = judge == '&';
while (exp[index] != ')') {
if (exp[index] == ',') {
index++;
}
else {
Info next = f(exp, index);
if (judge == '&') {
if (!next.ans) {
ans = false;
}
}
else {
if (next.ans) {
ans = true;
}
}
index = next.end + 1;
}
}
}
return Info(ans, index);
}
} bool parseBoolExpr(const string& expression) {
return f(expression, 0).ans;
} int main() {
string expression = "&(|(f))";
cout << boolalpha << parseBoolExpr(expression) << endl;
return 0;
}

c完整代码如下:

#include <stdbool.h>
#include<stdio.h> typedef struct Info {
bool ans;
int end;
} Info; Info f(char* exp, int index); bool parseBoolExpr(char* expression) {
return f(expression, 0).ans;
} Info f(char* exp, int index) {
char judge = exp[index];
Info info; if (judge == 'f') {
info.ans = false;
info.end = index;
}
else if (judge == 't') {
info.ans = true;
info.end = index;
}
else {
bool ans;
index += 2; if (judge == '!') {
Info next = f(exp, index);
ans = !next.ans;
index = next.end + 1;
}
else {
ans = judge == '&'; while (exp[index] != ')') {
if (exp[index] == ',') {
index++;
}
else {
Info next = f(exp, index); if (judge == '&') {
if (!next.ans) {
ans = false;
}
}
else {
if (next.ans) {
ans = true;
}
} index = next.end + 1;
}
}
} info.ans = ans;
info.end = index;
} return info;
} int main() {
char* expression = "&(|(f))";
bool result = parseBoolExpr(expression); printf("%d\n", result); return 0;
}

2023-07-19:布尔表达式 是计算结果不是 true 就是 false 的表达式 有效的表达式需遵循以下约定: ‘t‘,运算结果为 true ‘f‘,运算结果为 false ‘!(subExpr的更多相关文章

  1. AI Summit(2018.07.19)

    AI Summit 时间:2018.07.19地点:北京丽都皇冠假日酒店

  2. 2021.07.19 P2294 狡猾的商人(差分约束)

    2021.07.19 P2294 狡猾的商人(差分约束) [P2294 HNOI2005]狡猾的商人 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.差分约束最长路与最短 ...

  3. 2021.07.19 P2624 明明的烦恼(prufer序列,为什么杨辉三角我没搞出来?)

    2021.07.19 P2624 明明的烦恼(prufer序列,为什么杨辉三角我没搞出来?) [P2624 HNOI2008]明明的烦恼 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn ...

  4. 2021.07.19 BZOJ2654 tree(生成树)

    2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...

  5. 2023 01 19 HW

    2023 01 19 HW Okay, then let's start.  Okay. Maybe Karina, we start with the C2 design freeze. Yeah, ...

  6. Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现

    1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  7. c语言,中缀表达式转后缀表达式并计算

    //c语言中缀表达式计算 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  8. Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算

    中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...

  9. JavaScript实现计算后缀表达式(逆波兰表达式)以及将中缀表达式转为后缀表达式

    逆波兰表达式,它的语法规定,表达式必须以逆波兰表达式的方式给出.逆波兰表达式又叫做后缀表达式.这个知识点在数据结构和编译原理这两门课程中都有介绍,下面是一些例子: 正常的表达式 逆波兰表达式 a+b ...

  10. Murano Weekly Meeting 2016.07.19

    Meeting time: 2016.July.19 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

随机推荐

  1. 企业应用可观测性利器!华为云CodeArts APM发布

    摘要:近日,华为云全链路应用性能管理服务CodeArts APM全新上线,提供端到端的全链路性能管理服务,涵盖前端监控.应用性能监控,全面拥抱开源生态. 本文分享自华为云社区<企业应用可观测性利 ...

  2. docker的安装(linux、centos)

    环境:centos7 1.先确定linux是否是centos7 cat /etc/redhat-release 2.如果自己的linux上之前有安装docker,先卸载.如果没有,则直接跳过这一步. ...

  3. vue中获取所有路由

    在router实例上有options属性:

  4. ES6必会重点汇总

    当下的前端开发已经成为一项非常流行的技能.在这个领域中,ES6是一个重要的主题.ES6是ECMAScript 2015的缩写,是JavaScript语言的下一个版本,引入了很多新的语言特性和API,让 ...

  5. Gusfield算法学习

    算法详解 等价流树正如其名,树上两点间的路径上的边权最小值为图上两点间的最小割. Gusfield算法就是建等价流树的一种算法.设当前正在处理的集合为 \(S(|S|\ge 2)\),从 \(S\) ...

  6. MacOS 环境下 VSCode 的 C++ 环境搭建

    编译器安装 编译器可以选择 Clang 或者 GCC,在 MacOS 上 Clang 的安装更为简单一些. Clang(推荐) 打开终端输入命令, clang -v 查看是否已经安装. 如果已经安装, ...

  7. 【Docker】容器管理

    一.容器生命周期及启动过程 1.容器生命周期 2.容器启动过程 二.容器管理命令 Usage: docker [OPTIONS] COMMAND A self-sufficient runtime f ...

  8. 2步打通ModelArts和Astro,实现AI应用快速落地

    摘要:本文以 ModelArts 的"找云宝"自动学习 AI 应用为例,结合低代码平台 Astro 轻应用快速实现一个"找云宝"小应用. 本文分享自华为云社区& ...

  9. Win10系统Anaconda下tensorflow的GPU环境搭建

    我的环境:Win10 + Anaconda + tensorflow-gpu1.14 + CUDA10.0 + cuDNN7.6 + python3.6 注意:tensorflow版本.CUDA版本. ...

  10. 【从0开始编写webserver·基础篇#01】为什么需要线程池?写一个线程池吧

    线程池 参考: 1.游双Linux高性能服务器编程 2.TinyWebServer 注:虽然是"从0开始",但最好对(多)线程.线程同步等知识点有所了解再看,不然可能有些地方会理解 ...