-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
156 lines (118 loc) · 3.92 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// ViewController.swift
// Calculatrix
//
// Created by Leo Neves on 4/10/15.
// Copyright (c) 2015 Leo Neves. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
@IBOutlet weak var historyLabel: UILabel!
var infixstring: String = ""
var posfix = true;
var userIsInTheMiddleOfTypingANumber = false;
@IBAction func appendDigit(sender: UIButton) {
var digit = sender.currentTitle!
let espace: String = ""
if digit == "." && display.text!.rangeOfString(".") != nil {
return;
}
if userIsInTheMiddleOfTypingANumber {
display.text = display.text! + espace
display.text = display.text! + digit
} else {
display.text = display.text! + espace
display.text = digit
userIsInTheMiddleOfTypingANumber = true
}
appendToHistoryLabel(digit)
}
@IBAction func operate(sender: UIButton!) {
var operation = sender.currentTitle!
if userIsInTheMiddleOfTypingANumber {
enter()
}
switch operation {
case "x": operation2 { $0 * $1 }
case "-": operation2 { $1 - $0 }
case "/": operation2 { $1 / $0 }
case "+": operation2 { $0 + $1 }
case "SQRT": operation1 { sqrt($0) }
case "SIN": operation1 { sin($0) }
case "COS": operation1 { cos($0) }
case "PI": operation1 { $0 * M_PI }
default: break
}
appendToHistoryLabel(" " + operation + " ")
}
func operation2(operation: (Double, Double) -> Double) {
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
func operation1(operation: Double -> Double) {
if operandStack.count >= 1 {
displayValue = operation(operandStack.removeLast())
enter()
}
}
func appendToHistoryLabel(text: String) {
historyLabel.text = historyLabel.text! + " " + text;
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMiddleOfTypingANumber = false
operandStack.append(displayValue)
}
@IBAction func clear(sender: UIButton) {
operandStack.removeAll(keepCapacity: false)
display.text = ""
historyLabel.text = ""
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set {
display.text = "\(newValue)"
userIsInTheMiddleOfTypingANumber = true
}
}
@IBAction func Fix(sender: UISwitch) {
posfix = !posfix
if (posfix) {
println("posfixa ");
}
else {
println("infixa ");
}
}
/*func Convert (){
var s = Array<Double>()
s = operandStack;
var inputplus = String()
var input : Array<String> = inputplus.componentsSeparatedByString("/")
var c : Int = 0
inputplus[1]
while( c < input.count) { // not end of postfix expression
if ( input[c]=="x" ||input[c]=="x" || input[c]=="-" || input[c]=="+") {
s.push(input[c])
}
else {
s.append(NSNumberFormatter().numberFromString(input[c])!.doubleValue)
}
if( e is an operand )
s.push( e );
else {
op2 = s.pop();
op1 = s.pop();
value = result of applying operator ‘e’ to op1 and op2;
s.push( value );
}
c++
}
finalresult = s.pop();
}*/
}