Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidLoad() {
- super.viewDidLoad()
- //答えラベルの初期状態は0
- myEqu.text = "0"
- // Do any additional setup after loading the view, typically from a nib.
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- //-----------------------------------------------------------
- @IBOutlet weak var myEqu: UILabel!
- //値を補完するための変数を用意
- var x = 0
- var y = 0
- //calculationは初期状態は"none"
- var calculation = "none"
- //------------------------------------------------------------
- @IBAction func Btn1(_ sender: UIButton) {
- //元の数字に10かけて1を足して、答えに反映
- x = x * 10 + 1
- myEqu.text = String(x)
- }
- @IBAction func Btn2(_ sender: UIButton) {
- //元の数字に10かけて2を足して、答えに反映
- x = x * 10 + 2
- myEqu.text = String(x)
- }
- @IBAction func Btn3(_ sender: UIButton) {
- //元の数字に10かけて3を足して、答えに反映
- x = x * 10 + 3
- myEqu.text = String(x)
- }
- @IBAction func Btn4(_ sender: UIButton) {
- //元の数字に10かけて4を足して、答えに反映
- x = x * 10 + 4
- myEqu.text = String(x)
- }
- @IBAction func Btn5(_ sender: UIButton) {
- //元の数字に10かけて5を足して、答えに反映
- x = x * 10 + 5
- myEqu.text = String(x)
- }
- @IBAction func Btn6(_ sender: UIButton) {
- //元の数字に10かけて6を足して、答えに反映
- x = x * 10 + 6
- myEqu.text = String(x)
- }
- @IBAction func Btn7(_ sender: UIButton) {
- //元の数字に10かけて7を足して、答えに反映
- x = x * 10 + 7
- myEqu.text = String(x)
- }
- @IBAction func Btn8(_ sender: UIButton) {
- //元の数字に10かけて8を足して、答えに反映
- x = x * 10 + 8
- myEqu.text = String(x)
- }
- @IBAction func Btn9(_ sender: UIButton) {
- //元の数字に10かけて9を足して、答えに反映
- x = x * 10 + 9
- myEqu.text = String(x)
- }
- @IBAction func Btn0(_ sender: UIButton) {
- //元の数字に10かけて0を足して、答えに反映
- x = x * 10 + 0
- myEqu.text = String(x)
- }
- //------------------------------------------------------------
- @IBAction func add(_ sender: UIButton) {
- //calculationの状態をチェック
- if calculation == "add" {
- y = y + x
- x = 0
- }
- else if calculation == "sub" {
- y = y - x
- x = 0
- }
- else if calculation == "mul" {
- y = y * x
- x = 0
- }
- else if calculation == "div" {
- if x != 0 {
- y = y / x
- x = 0
- }
- }
- else if calculation == "none" {
- y = x
- }
- x = 0
- //計算結果をラベルに反映させる
- myEqu.text = String(y)
- calculation = "add"
- }
- @IBAction func sub(_ sender: UIButton) {
- //calculationの状態をチェック
- if calculation == "add" {
- y = y + x
- x = 0
- }
- else if calculation == "sub" {
- y = y - x
- x = 0
- }
- else if calculation == "mul" {
- y = y * x
- x = 0
- }
- else if calculation == "div" {
- if x != 0 {
- y = y / x
- x = 0
- }
- }
- else if calculation == "none" {
- y = x
- }
- x = 0
- //計算結果をラベルに反映させる
- myEqu.text = String(y)
- calculation = "sub"
- }
- @IBAction func mul(_ sender: UIButton) {
- //calculationの状態をチェック
- if calculation == "add" {
- y = y + x
- x = 0
- }
- else if calculation == "sub" {
- y = y - x
- x = 0
- }
- else if calculation == "mul" {
- y = y * x
- x = 0
- }
- else if calculation == "div" {
- if x != 0 {
- y = y / x
- x = 0
- }
- }
- else if calculation == "none" {
- y = x
- }
- x = 0
- //計算結果をラベルに反映させる
- myEqu.text = String(y)
- calculation = "mul"
- }
- @IBAction func div(_ sender: UIButton) {
- //calculationの状態をチェック
- if calculation == "add" {
- y = y + x
- x = 0
- }
- else if calculation == "sub" {
- y = y - x
- x = 0
- }
- else if calculation == "mul" {
- y = y * x
- x = 0
- }
- else if calculation == "div" {
- if x != 0 {
- y = y / x
- x = 0
- }
- }
- else if calculation == "none" {
- y = x
- }
- x = 0
- //計算結果をラベルに反映させる
- myEqu.text = String(y)
- calculation = "div"
- }
- //------------------------------------------------------------
- @IBAction func equ(_ sender: UIButton) {
- //calculationの状態をチェック
- if calculation == "add" {
- y = y + x
- x = 0
- }
- else if calculation == "sub" {
- y = y - x
- x = 0
- }
- else if calculation == "mul" {
- y = y * x
- x = 0
- }
- else if calculation == "div" {
- if x != 0 {
- y = y / x
- x = 0
- }
- }
- else if calculation == "none" {
- y = x
- }
- myEqu.text = String(y)
- calculation = "none"
- }
- //------------------------------------------------------------
- @IBAction func clear(_ sender: UIButton) {
- //変数・答えラベル・calculationの状態を初期に戻す
- x = 0
- y = 0
- myEqu.text = "0"
- calculation = "none"
- }
Add Comment
Please, Sign In to add comment