发新帖

表达式计算器

[复制链接]
11266 7
{:4_86:}最近在学习Android平台。 我的目标不仅要学习Android安全,开发也要学。没开发能力怎能称为猿?因为学过中缀后缀表达式相关的算法,以前用C写过,所以实现起来并不是很困难。


表达式求值类:
[mw_shl_code=java,true]
package cn.calc;

public class Expression {
        char opTable[] = { '+', '-', '*', '/', '(' };
        int leave[] = { 1, 1, 2, 2, 0 };

        public String postfix(String exp) {
                String hexp = "";
                EasyTextStack stack = new EasyTextStack();
                for (int i = 0; i < exp.length(); i++) {
                        char sub;
                        sub = exp.charAt(i);
                        if (Character.isDigit(sub)) {
                                hexp = hexp + sub;
                                continue;
                        }
                        if (sub == '(') {
                                stack.push("(");
                                continue;
                        }
                        if (sub == ')') {
                                while (stack.getTop().equals("(") == false) {
                                        hexp = hexp + " " + stack.pop();
                                }
                                stack.pop();
                                continue;
                        }
                        if (isOp(sub)) {
                                if (stack.isEmpty() == false) {
                                        while (getLeave(sub) <= getLeave(stack.getTop().charAt(0))) {
                                                hexp = hexp + " " + stack.pop();
                                                if (stack.isEmpty())
                                                        break;
                                        }
                                }
                                hexp = hexp + " ";
                                stack.push(Character.toString(sub));
                        }
                }
               
                while (stack.isEmpty() == false) {
                        hexp = hexp + " " + stack.pop();
                }
                return hexp;
        }
        public int postfix_value(String exp) {
                String strGroup[];
                int result=0;
                EasyTextStack stack = new EasyTextStack();
                strGroup = exp.split(" ");
                if(strGroup.length==0)
                {
                        return 0;
                }
                for (int i = 0; i < strGroup.length; i++) {
                        String string = strGroup;
                        if(isOp(string.charAt(0)))
                        {
                                int v1,v2;
                                if(stack.getCount()<2)
                                        return 0;
                                v1= Integer.parseInt(stack.pop());
                                v2 = Integer.parseInt(stack.pop());
                                if(string.equals("-"))
                                {
                                        result = v2-v1;
                                        stack.push(String.format("%d",result));
                                        continue;
                                }
                                if(string.equals("+"))
                                {
                                        result=v1+v2;
                                        stack.push(String.format("%d",result));
                                        continue;
                                }
                                if(string.equals("*"))
                                {
                                        result = v1*v2;
                                        stack.push(String.format("%d", result));
                                        continue;
                                }
                                if(string.equals("/"))
                                {
                                        result = v2/v1;
                                        stack.push(String.format("%d", result));
                                        continue;
                                }
                        }else
                        {
                                stack.push(string); // 数字入栈
                        }
                }
                if(stack.getCount()==1)
                        return Integer.parseInt(stack.pop());
                return 0;
        }

        private boolean isOp(char op) {
                for (int i = 0; i < opTable.length; i++)
                        if (opTable == op)
                                return true;
                return false;
        }
        private int getLeave(char op) {
                for (int i = 0; i < opTable.length; i++)
                        if (opTable == op)
                                return leave;
                return 0;
        }
}
[/mw_shl_code]

论坛怎么不能上传附件?
[sell=1,2]链接:http://pan.baidu.com/s/1jGq12ya 密码:umzn[/sell]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 使用道具

回复

精彩评论7

A00    发表于 2015-8-5 13:17:53 | 显示全部楼层
支持原创,沙发

举报 使用道具

回复 支持 反对
听鬼哥说故事    发表于 2015-8-6 09:41:42 | 显示全部楼层
赞楼主的分享精神,很给力~

举报 使用道具

回复 支持 反对
peterdocter    发表于 2015-8-6 09:46:04 | 显示全部楼层
多谢分享!楼主网盘有很多宝{:5_117:}

点评

的确,但是公开的好像没什么宝~  详情 回复 发表于 2015-8-6 13:50

举报 使用道具

回复 支持 反对
无名侠    发表于 2015-8-6 13:50:31 | 显示全部楼层
peterdocter 发表于 2015-8-6 09:46
多谢分享!楼主网盘有很多宝

{:4_87:}的确,但是公开的好像没什么宝~

举报 使用道具

回复 支持 反对
CRoot    发表于 2015-8-24 19:02:06 | 显示全部楼层
学习了 之前写过有个化学元素的计算器 没想到用栈的方式 会方便很多

举报 使用道具

回复 支持 反对
FindAllBlue    发表于 2015-8-27 20:04:41 | 显示全部楼层
美女!{:5_122:}{:5_122:}{:5_122:}{:5_122:}私聊吗

举报 使用道具

回复 支持 反对
mmpo789    发表于 2015-12-4 21:02:43 | 显示全部楼层
本付费内容需要支付 1NB 才能浏览

举报 使用道具

回复 支持 反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表