首页 > 试题广场 >

表达式求值

[编程题]表达式求值
  • 热度指数:3046 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

给出一个布尔表达式的字符串,比如:true or false and false,表达式只包含truefalseandor,现在要对这个表达式进行布尔求值,计算结果为真时输出true、为假时输出false,不合法的表达时输出error(比如:true true)。表达式求值是注意and 的优先级比 or 要高,比如:true or false and false,等价于 true or (false and false),计算结果是 true


输入描述:
输入第一行包含布尔表达式字符串s,s只包含true、false、and、or几个单词(不会出现其它的任何单词),且单词之间用空格分隔。 (1 ≤ |s| ≤ 103).


输出描述:
输出true、false或error,true表示布尔表达式计算为真,false表示布尔表达式计算为假,error表示一个不合法的表达式。
示例1

输入

and

输出

error
示例2

输入

true and false

输出

false
示例3

输入

true or false and false

输出

true
用操作数栈和操作符栈来解决
注意操作符的优先顺序,如果栈内操作符是and则先进行计算将结果存入操作数栈后再继续。
import java.util.*;

public class Main{
    public static String calcSub(String n1, String n2, String ops){
        if(ops.equals("and")){
            if(n1.equals("true") && n2.equals("true")){
                return "true";
            }else{
                return "false";
            }
        }else{
            if(n1.equals("false") && n2.equals("false")){
                return "false";
            }else{
                return "true";
            }
        }
    }
    public static void calc(Stack<String> op, Stack<String> num){
        String n1 = num.pop();
        String n2 = num.pop();
        String ops = op.pop();
        num.push(calcSub(n1, n2, ops));
    }

    //表达式求值
    public static String bds(String a){
        Stack<String> num = new Stack<>();
        Stack<String> op = new Stack<>();

        String[] s = a.split(" ");
        int flag = 0;   //数
        for(int i=0;i<s.length;i++){
            if(flag == 0) {
                if (!s[i].equals("true") && !s[i].equals("false")) {
                    return "error";
                }else{
                    num.push(s[i]);
                    flag = 1;
                }
            }else{
                if (!s[i].equals("and") && !s[i].equals("or")) {
                    return "error";
                }else{
                    //控制优先级
                    if(!op.empty() && "and".equals(op.peek())){
                        calc(op, num);
                    }
                    op.push(s[i]);
                    flag = 0;
                }
            }
        }
       //这里为了解决 “true and” 最后是操作符的情况
        if(flag == 0){
            return "error";
        }

        while(true){
            if(op.empty()){
                return num.pop();
            }
            calc(op, num);
        }
    }
    
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.println(bds(scanner.nextLine()));
    }
}


编辑于 2021-01-02 18:07:28 回复(0)
简简单单两小时,太菜了,害~

使用一个栈,只存“true”、“false”、“or”。
如果第一个字符串是true或false,就入栈,这样后面就不用频繁判断栈空了;否则返回error

循环判断字符串数组
遇到“#” 跳过本次循环
遇到T F
    如果栈顶是or,则入栈
    否则返回error
遇到or 
    如果stack不为空 并且栈顶不为or 则入栈; 
    否则error
遇到and
    如果stack为空或者i+1越界或者i+1不是T F,则返回error;
    否则弹出一个元素,与下一个元素and运算,将下一个结果标记为“#”。将结果压入栈

到达字符串数组末尾,弹出所有元素并进行or运算,将结果返回
import java.util.*;
public class Main{
    public static void  main(String[] args){
        Scanner sc = new Scanner(System.in);
        String[] s = sc.nextLine().split(" ");
        sc.close();
        String str = helper(s);
        System.out.println(str);
    }

    public static String helper(String[] s){
        Deque<String> stack = new LinkedList<>();
        String tmp = "";
        if (s[0].equals("true") || s[0].equals("false")) {
            stack.push(s[0]);
        }else {
            return "error";
        }

        for (int i = 1; i < s.length; i++) {
            String s1 = s[i];
            if(s1.equals("#")){
                continue;
            }
            if (stack.peek().equals("true") || stack.peek().equals("false")) {
                if (s1.equals("or") && i + 1 < s.length) {
                    stack.push(s1);
                }else if (s1.equals("and") && i + 1 < s.length) {
                    tmp = stack.pop();
                    boolean t1 = Boolean.parseBoolean(tmp) && Boolean.parseBoolean(s[i+1]);
                    stack.push(String.valueOf(t1));
                    s[i+1] = "#";
                }else {
                    return "error";
                }
            }else {
                //栈顶是or
                if (s1.equals("true") || s1.equals("false")) {
                    stack.push(s1);
                }else {
                    return "error";
                }
            }
        }

        if (!stack.isEmpty() || stack.peek().equals("true") || stack.peek().equals("false")){
            tmp = stack.pop();
            boolean res = Boolean.parseBoolean(tmp);
            while (!stack.isEmpty()){
                tmp = stack.pop();
                if (!tmp.equals("or")) {
                    res |= Boolean.parseBoolean(tmp);
                }
            }
            return String.valueOf(res);
        }else {
            return "error";
        }
    }
}
发表于 2020-10-10 20:19:49 回复(0)
import java.util.*; 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] exp = s.split(" ");
        System.out.println(method(exp));
    }

    private static String method(String[] s) {
        Stack<String> stack = new Stack<>();
        if ("or".equals(s[0]) ||"or".equals(s[s.length - 1]) || "and".equals(s[0]) ||"and".equals(s[s.length - 1])) {
            return "error";
        }
        stack.push(s[0]);
        for (int i = 1; i < s.length; i++) {
            if ("or".equals(s[i]) || "and".equals(s[i])) {
                if ("or".equals(s[i - 1]) || "and".equals(s[i - 1])) {
                    return "error";
                }
                if ("and".equals(s[i])) {
                    String w = stack.pop();
                    String p = s[i+1];
                    if (w.equals(p) && "true".equals(w)) {
                        stack.push("true");
                    } else {
                        stack.push("false");
                    }
                    i++;
                }
            } else {
                if ("true".equals(s[i - 1]) || "false".equals(s[i - 1])) {
                    return "error";
                }
                stack.push(s[i]);
            }
        }
        while (!stack.isEmpty()) {
            if ("true".equals(stack.pop())) {
                return "true";
            }
        }
        return "false";
    }
    
}

发表于 2020-03-20 10:56:56 回复(0)

写的一些解释,一个Ctrl + Z后退全没了,关键是不能前进。。。不讲了,直接上代码😑

import java.util.*;

public class Main{
    public static void main(String[] strs){
        String str = cal();
        System.out.println(str);
    }
    
    
    public static String cal(){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine().trim();
//        String str = "false&nbs***bsp;true and false";
        String[] words = str.split(" ");
        // System.out.println(Arrays.toString(words));
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < words.length; i++){
            if(words[i].equals("true")){
                stack.push(1);
            }else if(words[i].equals("false")){
                stack.push(0);
            }else if(words[i].equals("and")){
                if(!stack.isEmpty() && stack.peek() != 2 && i != words.length - 1
                        && (words[i + 1].equals("true") || words[i + 1].equals("false"))){
                    int val = words[i + 1].equals("true") ? 1 : 0;
                    stack.push(stack.pop() & val);
                    i++;
                }else return "error";
            }else if(words[i].equals("or")){
                stack.push(2);
            }else return "error";
        }
        int last = -1;
        while (!stack.isEmpty()){
            if(last == -1){
                last = stack.pop();
                if(last == 2) return "error";
            }else{
                int or = stack.pop();
                if(or != 2) return "error";
                int val = 0;
                if(!stack.isEmpty() && stack.peek() != 2) val = stack.pop();
                else return "error";
                last = last | val;
            }
        }
        return last == 1 ? "true" : "false";
    }
}



编辑于 2020-03-10 14:31:47 回复(0)