zoukankan      html  css  js  c++  java
  • 四则运算----封装

    我的结对伙伴是:陈思明   http://www.cnblogs.com/chensiming/

    package size;
    import java.util.Stack;
    public class opt
    {
        public int math(String str)
        { 
            String[] operater =new String[20]; 
            String[] number = new String[20]; 
            Stack countStack1 = new Stack(); 
            Stack countStack2 = new Stack(); 
            int result =0; 
            number = str.split("\/|\*|\+|\-"); 
            operater= str.split("\d+"); 
            for(int i = 0; i<number.length;i++)
            { 
                countStack1.push(number[i]); 
                if(i!=number.length-1)
                {
                    countStack1.push(operater[i+1]); 
                } 
            } 
            while(!countStack1.isEmpty())countStack2.push(countStack1.pop()); 
            String op;
            while(!countStack2.isEmpty())
            { 
                result=0; 
                op = countStack2.pop().toString(); 
                if(op.equals("*"))
                { 
                result=Integer.parseInt(countStack1.pop().toString())*Integer.parseInt(countStack2.pop().toString()); 
                countStack1.push(result); 
                continue; 
                }
                if(op.equals("/"))
                { 
                result=Integer.parseInt(countStack1.pop().toString())/Integer.parseInt(countStack2.pop().toString()); 
                countStack1.push(result); 
                continue; 
                } 
                countStack1.push(op); 
            }
            while(!countStack1.isEmpty())countStack2.push(countStack1.pop()); 
            while(!countStack2.isEmpty())
            {
                result=0; 
                op = countStack2.pop().toString(); 
                if(op.equals("+"))
                { 
                    result=Integer.parseInt(countStack1.pop().toString())+Integer.parseInt(countStack2.pop().toString()); 
                    countStack1.push(result); 
                    continue; 
                } 
                if(op.equals("-"))
                { 
                    result=Integer.parseInt(countStack1.pop().toString())-Integer.parseInt(countStack2.pop().toString()); 
                    countStack1.push(result); 
                    continue; 
                } 
                countStack1.push(op); 
            }
            return result;
        } 
    }

    上一次我已经将计算的模块独立出来了,这次将他丢到另一个CLASS里封装了起来,方便测试以及其他用途

  • 相关阅读:
    nginx并发数设置_Nginx Ingress 高并发实践
    推荐一个国人开源的推荐系统
    异步并发利器:实际项目中使用CompletionService提升系统性能
    JDK中CompletableFuture类
    mysql日志redo log、undo log、binlog
    <a>标签下载文件 重命名失败 download 无效
    nginx geo黑名单
    夜莺微信报警-V3
    分布式事务的学习
    php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)
  • 原文地址:https://www.cnblogs.com/SiPine/p/4469070.html
Copyright © 2011-2022 走看看