简单计算器制作 swift初步尝试
此计算器功能暂且不完善,有待后续改进
刚自学swift 写了一个小的计算器,功能暂不完善,算法有缺陷,但是可以供大家看下。
let array1:NSArray = ["1","2","3","+","4","5","6","-","7","8","9","x","AC","0","=","/"]
//用数组来用来定义按钮功能
//计算每个button的位置
for var i = 0;i < num ;i++ {
//动态生成每个个按钮
let buttonTempX:CGFloat = marginX + (buttonW + marginX)*CGFloat((i%4))
let buttonTempY:CGFloat = buttonOrigionY + (buttonH + marginX)*CGFloat((i/4))
let buttonTempW:CGFloat = buttonW
let buttonTempH:CGFloat = buttonH
let button = UIButton()
button.backgroundColor = UIColor.greenColor()
button.frame = CGRectMake(buttonTempX, buttonTempY, buttonTempW, buttonTempH)
//随机生成按钮背景颜色
let red = CGFloat(random())%255.0/255.0*1.0
let green = CGFloat(random())%255.0/255.0*1.0
let blue = CGFloat(random())%255.0/255.0*1.0
button.backgroundColor = UIColor.init(red:red , green: green, blue: blue, alpha: 0.5)
//设置数字按钮的数字
if ((i>=0 && i<=2)||(i>=4&&i<=6)||(i>=8&&i<=10) || (i==13))
{
index++
if(index == 10){
index=0
}
button.tag = index
button.setTitle("\(index)", forState: UIControlState.Normal)
}
//设置运算
if array1[i].isEqualToString("+"){
button.setTitle("+", forState: UIControlState.Normal)
}
if array1[i].isEqualToString("-"){
button.setTitle("-", forState: UIControlState.Normal)
}
if array1[i].isEqualToString("x"){
button.setTitle("x", forState: UIControlState.Normal)
}
if array1[i].isEqualToString("/"){
button.setTitle("/", forState: UIControlState.Normal)
}
if array1[i].isEqualToString("="){
button.setTitle("=", forState: UIControlState.Normal)
}
if array1[i].isEqualToString("AC"){
button.setTitle("AC", forState: UIControlState.Normal)
}
if !array1[i].isEqualToString("="){
//按“=”时执行的操作
button.addTarget(self, action: "run:", forControlEvents: UIControlEvents.TouchUpInside)
}
view.addSubview(button)
if array1[i].isEqualToString("="){
button.addTarget(self, action: "resultConsoles", forControlEvents: UIControlEvents.TouchUpInside)
}
if array1[i].isEqualToString("AC"){
//归零操作
button.addTarget(self, action: "clear", forControlEvents: UIControlEvents.TouchUpInside)
}
}
//显示屏显示内容
func run(button:UIButton)
{
blackBoardValue = blackBoardValue + button.titleForState(UIControlState.Normal)!
blackBoardLab.text = blackBoardValue
}
//清空显示屏
func clear()
{
blackBoardLab.text = "0"
blackBoardValue.removeAll()
mutableArray.removeAll()
}
//所有的按钮的运算都以字符串的形式展示在blackBoardValue上,接下来就是如何把显示屏上的内容计算出来,思路是一个数字一个运算符号碰到运算符号就截取前面的保存到一个数组,并且把当前运算符号也保存到一个数组,把运算符号之后的字符串重新赋值给blackBoardValue,再重复执行上述操作,直到把字符串数字和运算符号分开
func caculate(){
print(blackBoardValue)
// var tempArray: [String] = [""]
for c in blackBoardValue.characters
{
//1.判断当前的数据是否是数字
let isValueble:Int? = Int("\(c)")
//1.1如果isValueble有值 那么就是数字
if isValueble==nil {
//1.2 如果是字符。判断是什么字符,并截取至当前字符
//把符号左边的数据放进数组中
let characterArray = (blackBoardValue as NSString).substringToIndex(indexR)
mutableArray.append(characterArray)
//把符号本身放进数组中
mutableArray.append("\(c)")
//让原来的 blackBoardValue成为截取之后剩余的部分
blackBoardValue = (blackBoardValue as NSString).substringFromIndex(indexR+1)
//每次一重新生成的blackBoardValue下标都要重0开始
indexR = 0
}else{
//计算到最后一个数
if indexR+1 == blackBoardValue.lengthOfBytesUsingEncoding(NSUTF8StringEncoding){
mutableArray.append(blackBoardValue)
indexR = 0
break
}
indexR++
}
}
print(mutableArray)
}
//接下来就是如何把数组里边的值计算出来了 数字+符号+数字+符号+...+数字
func resultConsoles()
{
caculate()
// print(mutableArray)
var rs = Int(mutableArray[0])!
for var i = 0 ;i < mutableArray.count ; i++ {
//某一个函数的返回值
if i%2==1 {
let secondData = Int(mutableArray[i+1])
rs = judgeForTheCharacter(mutableArray[i], result:rs, secondData: secondData!)
if i == mutableArray.count-2{
break
}
}
}
blackBoardLab.text = "\(rs)"
}
func judgeForTheCharacter(string:NSString,result:Int ,secondData:Int)->(Int)! {
if string.isEqualToString("+"){
return (result + secondData)
}
else if string.isEqualToString("-"){
return (result - secondData)
}
else if string.isEqualToString("x"){
return (result * secondData)
}
else {
return (result / secondData)
}
}