前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >算法之求String类型算数表达式的值 和 验证IP地址合法性

算法之求String类型算数表达式的值 和 验证IP地址合法性

原创
作者头像
CoffeeLand
修改2020-04-16 10:25:37
6180
修改2020-04-16 10:25:37
举报
文章被收录于专栏:CoffeeLandCoffeeLand

验证IP地址合法性

代码语言:javascript
复制
import org.apache.commons.validator.routines.InetAddressValidator;

public class IPValidator
{
    public static Boolean checkIpValid(String str)
    {
        // Get an InetAddressValidator
        InetAddressValidator validator = InetAddressValidator.getInstance();
        if (validator.isValidInet4Address(str) || validator.isValidInet6Address(str)) return true;
        return false;
    }

    public static void main(String[] args)
    {
        // an IPv4 address
          final String ipv4 = "10.8.9.28";

        // an IPv6 address
          final String ipv6 =
                "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
        // Get an InetAddressValidator
        Boolean res4 = checkIpValid(ipv4);
        System.out.println("res4 = " + res4);
        Boolean res6 = checkIpValid(ipv6);
        System.out.println("res6 = " + res6);

    }
}

String类型算数表达式的值

代码语言:javascript
复制
import java.util.Stack;

class Calculator {

    final static Stack<Character> opStack = new Stack<>();

    /**
     * calculate the value of the suffix expression
     * @param expr - the input string expression
     * @return - result of the expression
     */
    public static int calc(String expr){
        // transfer the string format from infix to suffix
        String suffix = transferToSuffix(expr);
        Stack<Integer> resStack = new Stack<>();
        char[] arr = suffix.toCharArray();
        for(char ch : arr)
        {
            if (Character.isDigit(ch)) resStack.push(Integer.valueOf(ch - '0'));
            else resStack.push(calResWithOperation(ch, resStack.pop(), resStack.pop()));
        }
        return resStack.pop();
    }

    /**
     * transfer the string format from infix to suffix
     * @param expr - String in the form of infix
     * @return String in the form of suffix
     */
    private static String transferToSuffix(String expr){
        char[] arr = expr.replaceAll(" ","").toCharArray();
        int len = arr.length;
        String suffix = "";

        for(int i = 0; i < len; i++)
        {
            char ch = arr[i];
            // output if the char is number
            if(Character.isDigit(ch))
            {
                suffix+=ch;
                continue;
            }

            // push to the stack directly if char is '('
            if(ch == '(') {
                opStack.push(ch);
                continue;
            }

            if(ch == '+' || ch == '-'){
                while(!opStack.empty() && (opStack.peek() != '('))
                    suffix+= opStack.pop();
                opStack.push(ch);
                continue;
            }

            if(ch == '*' || ch == '/'){
                while(!opStack.empty() && (opStack.peek() == '*' || opStack.peek() == '/'))
                    suffix+= opStack.pop();
                opStack.push(ch);
                continue;
            }

            // pop the stack
            if(ch == ')'){
                while(!opStack.empty() && opStack.peek() != '(')
                    suffix += opStack.pop();
                opStack.pop();
                continue;
            }
        }

        while(!opStack.empty()) suffix += opStack.pop();
        return suffix;
    }

    /**
     * calculate the two numbers with operation
     *
     * @param op the operation symbol
     * @param num1 the calculated number
     * @param num2 the calculated number
     * @return the calculate result
     */
    private static int calResWithOperation(char op, int num1, int num2){
        if (op == '+') return num2 + num1;
        else if (op == '-') return num2 - num1;
        else if (op == '*') return num2 * num1;
        else if (op == '/') return num2 / num1;
        else return 0;
    }

      public static void main(String[] args){
        String exp = "1+2* (3+4)";
        String postfix = transferToSuffix(exp);
        System.out.println("postfix = " + postfix); // postfix = 1234+*+
        int res = calc(postfix);
        System.out.println("res = " + res);  // res = 15
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 验证IP地址合法性
  • String类型算数表达式的值
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档