Guest User

Untitled

a guest
Jun 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3. //答えラベルの初期状態は0
  4. myEqu.text = "0"
  5. // Do any additional setup after loading the view, typically from a nib.
  6. }
  7.  
  8. override func didReceiveMemoryWarning() {
  9. super.didReceiveMemoryWarning()
  10. // Dispose of any resources that can be recreated.
  11. }
  12. //-----------------------------------------------------------
  13. @IBOutlet weak var myEqu: UILabel!
  14. //値を補完するための変数を用意
  15. var x = 0
  16. var y = 0
  17. //calculationは初期状態は"none"
  18. var calculation = "none"
  19. //------------------------------------------------------------
  20. @IBAction func Btn1(_ sender: UIButton) {
  21. //元の数字に10かけて1を足して、答えに反映
  22. x = x * 10 + 1
  23. myEqu.text = String(x)
  24. }
  25. @IBAction func Btn2(_ sender: UIButton) {
  26. //元の数字に10かけて2を足して、答えに反映
  27. x = x * 10 + 2
  28. myEqu.text = String(x)
  29. }
  30. @IBAction func Btn3(_ sender: UIButton) {
  31. //元の数字に10かけて3を足して、答えに反映
  32. x = x * 10 + 3
  33. myEqu.text = String(x)
  34. }
  35. @IBAction func Btn4(_ sender: UIButton) {
  36. //元の数字に10かけて4を足して、答えに反映
  37. x = x * 10 + 4
  38. myEqu.text = String(x)
  39. }
  40. @IBAction func Btn5(_ sender: UIButton) {
  41. //元の数字に10かけて5を足して、答えに反映
  42. x = x * 10 + 5
  43. myEqu.text = String(x)
  44. }
  45. @IBAction func Btn6(_ sender: UIButton) {
  46. //元の数字に10かけて6を足して、答えに反映
  47. x = x * 10 + 6
  48. myEqu.text = String(x)
  49. }
  50. @IBAction func Btn7(_ sender: UIButton) {
  51. //元の数字に10かけて7を足して、答えに反映
  52. x = x * 10 + 7
  53. myEqu.text = String(x)
  54. }
  55. @IBAction func Btn8(_ sender: UIButton) {
  56. //元の数字に10かけて8を足して、答えに反映
  57. x = x * 10 + 8
  58. myEqu.text = String(x)
  59. }
  60. @IBAction func Btn9(_ sender: UIButton) {
  61. //元の数字に10かけて9を足して、答えに反映
  62. x = x * 10 + 9
  63. myEqu.text = String(x)
  64. }
  65. @IBAction func Btn0(_ sender: UIButton) {
  66. //元の数字に10かけて0を足して、答えに反映
  67. x = x * 10 + 0
  68. myEqu.text = String(x)
  69. }
  70. //------------------------------------------------------------
  71. @IBAction func add(_ sender: UIButton) {
  72. //calculationの状態をチェック
  73. if calculation == "add" {
  74. y = y + x
  75. x = 0
  76. }
  77. else if calculation == "sub" {
  78. y = y - x
  79. x = 0
  80. }
  81. else if calculation == "mul" {
  82. y = y * x
  83. x = 0
  84. }
  85. else if calculation == "div" {
  86. if x != 0 {
  87. y = y / x
  88. x = 0
  89. }
  90. }
  91. else if calculation == "none" {
  92. y = x
  93. }
  94. x = 0
  95. //計算結果をラベルに反映させる
  96. myEqu.text = String(y)
  97. calculation = "add"
  98. }
  99. @IBAction func sub(_ sender: UIButton) {
  100. //calculationの状態をチェック
  101. if calculation == "add" {
  102. y = y + x
  103. x = 0
  104. }
  105. else if calculation == "sub" {
  106. y = y - x
  107. x = 0
  108. }
  109. else if calculation == "mul" {
  110. y = y * x
  111. x = 0
  112. }
  113. else if calculation == "div" {
  114. if x != 0 {
  115. y = y / x
  116. x = 0
  117. }
  118. }
  119. else if calculation == "none" {
  120. y = x
  121. }
  122. x = 0
  123. //計算結果をラベルに反映させる
  124. myEqu.text = String(y)
  125. calculation = "sub"
  126. }
  127. @IBAction func mul(_ sender: UIButton) {
  128. //calculationの状態をチェック
  129. if calculation == "add" {
  130. y = y + x
  131. x = 0
  132. }
  133. else if calculation == "sub" {
  134. y = y - x
  135. x = 0
  136. }
  137. else if calculation == "mul" {
  138. y = y * x
  139. x = 0
  140. }
  141. else if calculation == "div" {
  142. if x != 0 {
  143. y = y / x
  144. x = 0
  145. }
  146. }
  147. else if calculation == "none" {
  148. y = x
  149. }
  150. x = 0
  151. //計算結果をラベルに反映させる
  152. myEqu.text = String(y)
  153. calculation = "mul"
  154. }
  155. @IBAction func div(_ sender: UIButton) {
  156. //calculationの状態をチェック
  157. if calculation == "add" {
  158. y = y + x
  159. x = 0
  160. }
  161. else if calculation == "sub" {
  162. y = y - x
  163. x = 0
  164. }
  165. else if calculation == "mul" {
  166. y = y * x
  167. x = 0
  168. }
  169. else if calculation == "div" {
  170. if x != 0 {
  171. y = y / x
  172. x = 0
  173. }
  174. }
  175. else if calculation == "none" {
  176. y = x
  177. }
  178. x = 0
  179. //計算結果をラベルに反映させる
  180. myEqu.text = String(y)
  181. calculation = "div"
  182. }
  183. //------------------------------------------------------------
  184. @IBAction func equ(_ sender: UIButton) {
  185. //calculationの状態をチェック
  186. if calculation == "add" {
  187. y = y + x
  188. x = 0
  189. }
  190. else if calculation == "sub" {
  191. y = y - x
  192. x = 0
  193. }
  194. else if calculation == "mul" {
  195. y = y * x
  196. x = 0
  197. }
  198. else if calculation == "div" {
  199. if x != 0 {
  200. y = y / x
  201. x = 0
  202. }
  203. }
  204. else if calculation == "none" {
  205. y = x
  206. }
  207. myEqu.text = String(y)
  208. calculation = "none"
  209. }
  210. //------------------------------------------------------------
  211. @IBAction func clear(_ sender: UIButton) {
  212. //変数・答えラベル・calculationの状態を初期に戻す
  213. x = 0
  214. y = 0
  215. myEqu.text = "0"
  216. calculation = "none"
  217. }
Add Comment
Please, Sign In to add comment