做一个iOS的简单计算器(二)

2016-09-08  本文已影响138人  水田夏木

接上一篇,我已经完成了多个小数点,除零,格式异常等处理,直接上代码,使用swift写的。
首先是一个处理栈等基本功能的类。

//定义一个处理基本操作的类
class CalculateTool: NSObject {
    var opndStack:Array<String>?    //运算数栈
    var optrStack:Array<String>?    //运算符栈
    
    //初始化运算数栈
    func initOpndStack()
    {
        opndStack = Array<String>()
    }
    //初始化运算符栈
    func initOptrStack(){
        optrStack = Array<String>()
    }
    //压入运算数
    func pushOperand(operand:String)
    {
        opndStack?.append(operand)
    }
    //压入运算符
    func pushOperator(optr:String)
    {
        optrStack?.append(optr)
    }
    //弹出一个运算数,返回弹出数:小数点也是数
    func popOperand() -> Float
    {
        if opndStack?.count > 0
        {
             let last = opndStack?.last
            opndStack?.removeLast()
            let lastString = last! as NSString
            return lastString.floatValue
        }
        return 0.0
    }
    //弹出一个运算符
    func popOperator() -> String
    {
        if optrStack?.count>0
        {
            let last = optrStack?.last
            optrStack?.removeLast()
            return last!
        }
        return ""
    }
    //取栈顶运算数
    func getTopOperand() -> Float
    {
        if opndStack?.count > 0
        {
            let last = opndStack?.last
            let lastString = last! as NSString
            return lastString.floatValue
        }
        return 0.0
    }
    //取栈顶运算符
    func getTopOperator() -> String
    {
        if optrStack?.count>0
        {
            let last = optrStack?.last
            return last!
        }
        return ""
    }
    //判断字符串是否为运算符
    func isOperator(c:String)->Bool
    {
        if c=="+" || c=="÷" || c=="×" || c=="-" || c=="=" || c=="(" || c==")"
        {
            return true
        }
        else
        {
            return false
        }
    }
    
    //判断运算符的优先级:栈内和栈外的区别
    func precedeInStack(s:String) -> Int
    {
        switch s {
        case "-":
            fallthrough
        case "+":
            return 2
        case "×":
            fallthrough
        case "÷":
            return 4
        case "(":
            return 0
        case ")":
            return 7
        default:
            return -1
        }
    }
    func precedeOutStack(s:String) -> Int
    {
        switch s {
        case "-":
            fallthrough
        case "+":
            return 1
        case "×":
            fallthrough
        case "÷":
            return 3
        case "(":
            return 7
        case ")":
            return 0
        default:
            return -1
        }
    }
    
    //进行单独的运算
    func operate(x:Float,opr:String,y:Float) -> Float
    {
        var result:Float?
        switch opr
        {
        case "+":
            result = x+y
            break
        case "-":
            result = x-y
            break
        case "×":
            result = x*y
            break
        case "÷":
            if y != 0
            {
                result = x/y
            }
            else
            {
                return PI
            }
            break
        default:
            result = 0
            break
        }
        return result!
    }
}

然后是一个处理运算表达式的类。


class CalculateResult: NSObject {
    var calculateTool:CalculateTool!
    
    override init() {
        super.init()
        calculateTool = CalculateTool()
        calculateTool.initOptrStack()
        calculateTool.initOpndStack()
    }
    //如何排除异常的表达式,包括小数点,除以0,其他格式
    func calculateWithString(s:String) -> Float
    {
        var calculateString = stringToArray(s)
        var result:Float = 0.0//初始化一个结果
        while calculateString.count>0
        {
            let firstElement = calculateString.first
            if firstElement?.componentsSeparatedByString(".").count>2
            {
                return PI
            }
            else if Float(firstElement!)==nil//如果是运算符,则比较优先级;多个小数点的情况
            {
                let topOptr = calculateTool.getTopOperator()
                //如果栈内运算符优先级较低,则运算符入栈
                if calculateTool.precedeInStack(topOptr)<calculateTool.precedeOutStack(firstElement!)
                {
                    calculateTool.pushOperator(firstElement!)
                    calculateString.removeAtIndex(0)
                    continue
                }
                //从符号栈弹出一个运算符,从对象栈弹出2个操作数,运算结果压入对象栈
                else if calculateTool.precedeInStack(topOptr)>calculateTool.precedeOutStack(firstElement!)
                {
                    let popOptr = calculateTool.popOperator()
                    let secondNumber = calculateTool.popOperand()
                    let firstNumber = calculateTool.popOperand()
                    let tempResult = calculateTool.operate(firstNumber, opr: popOptr, y: secondNumber)
                    if tempResult==PI//如果除以0
                    {
                        calculateTool.initOptrStack()
                        calculateTool.initOpndStack()
                        return PI
                    }
                    
                    calculateTool.pushOperand(String(tempResult))
                    continue
                }
                else//两个运算符相等时,从运算栈抵消
                {
                    let a = calculateTool.popOperator()
                    print("弹出的运算符为\(a)")
                    calculateString.removeAtIndex(0)
                    continue
                }
            }
            else//如果运算数则直接入栈,如果有两个小数点需要报错
            {
                calculateTool.pushOperand(firstElement!)
                calculateString.removeAtIndex(0)
                continue
            }
        }
        //上述步骤结束后那么剩下的运算符必然有序排列:从运算符栈弹出一个运算符,从运算数栈弹出两个运算数,将结果压入运算数
        while calculateTool.getTopOperator() != ""
        {
            let popOptr = calculateTool.popOperator()
            let secondNumber = calculateTool.popOperand()
            let firstNumber = calculateTool.popOperand()
            let tempResult = calculateTool.operate(firstNumber, opr: popOptr, y: secondNumber)
            calculateTool.pushOperand(String(tempResult))
        }
        result = calculateTool.getTopOperand()
        calculateTool.initOptrStack()
        calculateTool.initOpndStack()
        return result
    }
    
    
    //将输入的字符串输出为数组
    func stringToArray(s:String) -> Array<String>
    {
        var stringArray = Array<String>()
        var mutableStr = s
        while mutableStr.characters.count>0
        {
            let firstCharacter = mutableStr.substringToIndex(mutableStr.startIndex.advancedBy(1))
            if Int(firstCharacter)>=0 &&  Int(firstCharacter)<10//如果是数字
            {
                let length = mutableStr.characters.count
                
                //从前向后扫描
                for i in 0...length-1
                {
                    let character = mutableStr.substringWithRange(Range(start:mutableStr.startIndex.advancedBy(i),end:mutableStr.startIndex.advancedBy(i+1)))
                    print("该字符的整数值为\(Int(character))")
                    if Int(character)==nil && character != "."
                    {
                        let numberStr = mutableStr.substringToIndex(mutableStr.startIndex.advancedBy(i))
                        stringArray.append(numberStr)
                        mutableStr = mutableStr.substringFromIndex(mutableStr.startIndex.advancedBy(i))
                        break//跳出当次循环
                    }
                }
            }
            else//如果是运算符
            {
                stringArray.append(firstCharacter)
                mutableStr = mutableStr.substringFromIndex(mutableStr.startIndex.advancedBy(1))
            }
        }
        
        return stringArray
    }
  
}

界面用的UICollectionView,比较简单的。
用了几年的Object-c,思维习惯上还是转变不过来,代码里始终有着OC的影子,还在努力更正.

最近稍稍研究了一下JS在iOS中的应用,有些意思的。过一段时间再发。

上一篇 下一篇

猜你喜欢

热点阅读