Skip to content

eunjios/ios-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

23 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ฑ iOS Calculator

Hits GitHub all releases GitHub last commit

์•„์ง ๋ฏธ์™„์„ฑ์ธ ํ”„๋กœ์ ํŠธ ์ž…๋‹ˆ๋‹ค.

ios-calculator-simulator

๐Ÿ“Œ Introduction

iPhone์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ๊ณ„์‚ฐ๊ธฐ์™€ ๊ธฐ๋Šฅ ๋ฐ ๋””์ž์ธ์„ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“  iOS ์•ฑ ์ž…๋‹ˆ๋‹ค.


๐Ÿ“ Functionality

1. ๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ ๊ธฐ๋Šฅ

๊ธฐ๋ณธ์ ์ธ ์—ฐ์‚ฐ (+, โˆ’, ร—, รท) ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.



2. ๊ฒฐ๊ณผ ์ถœ๋ ฅ

  • ์ถœ๋ ฅ๊ฐ’์„ Int type ์œผ๋กœ ๋ณ€ํ™˜ํ•ด๋„ ์ƒ๊ด€ ์—†๋Š” ๊ฒฝ์šฐ, Int type ์œผ๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” Double type ์œผ๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • +, โˆ’ ์—ฐ์‚ฐ์€ ์ „์ฒด ์‹์˜ result ๊ฐ’์„ ํ™”๋ฉด์— ์ถœ๋ ฅ

  • ร—, รท ์—ฐ์‚ฐ์€ ํ•ด๋‹น ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ’์ธ temp ๋งŒ ์ถœ๋ ฅ



๐Ÿ”จ Trouble Shooting

1. ๊ณฑํ•˜๊ธฐ ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์ด ์ œ๋Œ€๋กœ ์•ˆ๋˜๋Š” ์˜ค๋ฅ˜

๋ฌธ์ œ ์ธ์‹

  • ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ์˜ˆ์ƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ Int type ์œผ๋กœ ๋ณ€ํ™˜์ด ์•ˆ๋˜๊ณ  0.2000... ๊ณผ ๊ฐ™์ด ๋„ˆ๋ฌด ๊ธธ๊ฒŒ ์ถœ๋ ฅ

  • Double type ์„ ์ž…๋ ฅ ๋ฐ›์€ ํ›„ ๊ณฑํ•˜๊ธฐ, ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ์„ ํ•˜๋ฉด, ๊ฒฐ๊ณผ๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ์ƒํ™ฉ ๋ฐœ์ƒ

  • Swift ์ž์ฒด์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ํ‹€๋ฆผ

    print(3.3 * 3)  
    // 9.89999999... ์ถœ๋ ฅ

Solution

  • round() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ์ˆ˜์  ์•„๋ž˜ N๋ฒˆ์งธ ์ˆซ์ž์—์„œ ๋ฐ˜์˜ฌ๋ฆผ ํ•˜๋„๋ก ์ฝ”๋“œ ์ž‘์„ฑ

    print(round(3.3 * 3 * 1000) / 1000)
    // 9.9 ์ถœ๋ ฅ
  • ํ•ด๋‹น function ์ˆ˜์ •

    func intOrDouble(_ value: Double) -> String {
         let roundedValue = round(value * 1000000000) / 1000000000
         if roundedValue - Double(Int(roundedValue)) == 0 {
              return String(Int(roundedValue))
         } else {
              return String(roundedValue)
         }
    }

2. ๊ณฑ์…ˆ/๋‚˜๋ˆ—์…ˆ์—์„œ ๋ง์…ˆ/๋บ„์…ˆ ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ํ™”๋ฉด์—์„œ 0์ด ์ถœ๋ ฅ๋˜๋Š” ์˜ค๋ฅ˜ ๋ฐœ์ƒ

๋ฌธ์ œ ์ƒํ™ฉ

  • ์•ฑ์„ ์‹œ์ž‘ํ•˜๊ณ  ์ฒซ ์—ฐ์‚ฐ์ด ๊ณฑ์…ˆ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ดํ›„ ๋ง์…ˆ์„ ํ•  ๊ฒฝ์šฐ ๊ฒฐ๊ณผ๊ฐ€ 0์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ์˜ค๋ฅ˜ ๋ฐœ์ƒ
  • ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด์ž.
10 * 10 * 10 + 10 ์„ ๊ณ„์‚ฐํ•˜๋ฉด + ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ„๊ณผ ๋™์‹œ์— ํ™”๋ฉด์— 0 ์ด ์ถœ๋ ฅ๋จ

๋ฌธ์ œ ์ธ์‹

  • + ๋˜๋Š” - ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ„๊ณผ ๋™์‹œ์— ์ด์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜๋ฏ€๋กœ ํ•ด๋‹น @IBAction func ์— ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ์„ ์ธ์‹
  • ์ฒซ ์—ฐ์‚ฐ์ด ๊ณฑ์…ˆ/๋‚˜๋ˆ—์…ˆ์ธ ๊ฒฝ์šฐ์—๋งŒ ์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์ „ ์—ฐ์‚ฐ์ด ์—†๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ์„ ์ธ์‹

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๊ธฐ์กด @IBAction func sumButton ์˜ ์ผ๋ถ€์ด๋‹ค.

...
// ๊ณผ๊ฑฐ ์ƒํƒœ๊ฐ€ ๊ณฑํ•˜๊ธฐ๋‚˜ ๋‚˜๋ˆ„๊ธฐ๋ฉด, result์— temp ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋บŒ
// ๊ณผ๊ฑฐ ์ƒํƒœ๊ฐ€ ๋”ํ•˜๊ธฐ๋‚˜ ๋นผ๊ธฐ๋ฉด, result์— inputNum ๊ฐ’์„ ์—ฐ์‚ฐ
if pastState == "multi" || pastState == "div" {
    if beforeMultiState == "" {
        result = calculateTwoNumbers(state: pastState, num1: result, num2: inputNum)  // error
    } else {
        result = calculateTwoNumbers(state: beforeMultiState, num1: result, num2: temp)
    }
} else {        // sum or sub
    result = calculateTwoNumbers(state: pastState, num1: result, num2: inputNum)
}
...
  • temp ๋ž€? ์—ฐ์‚ฐ์˜ ์ž„์‹œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ = ์„ ๋ˆ„๋ฅด๊ธฐ ์ „๊นŒ์ง€ ํ™”๋ฉด์— ๋ณด์ด๋Š” ๊ฐ’์„ ์˜๋ฏธ
  • result ๋ž€? ํ˜„์žฌ์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ํ˜„์žฌ ์ƒํƒœ๊ฐ€ sum ์ด๋‚˜ sub ์ผ ๋•Œ๋งŒ ๊ณ„์‚ฐํ•˜์—ฌ ๊ฐ’์„ ๋ฐ”๊ฟˆ
  • beforeMultiState ๋ž€? ๊ณฑ์…ˆ/๋‚˜๋ˆ—์…ˆ ์ „ ์ƒํƒœ๊ฐ€ ๋ง์…ˆ์ด์—ˆ๋Š”์ง€ ๋บ„์…ˆ์ด์—ˆ๋Š”์ง€ ์•Œ๊ธฐ ์œ„ํ•ด ๋งŒ๋“  ๋ณ€์ˆ˜๋กœ ์ดˆ๊ธฐ๊ฐ’์€ "" ์ด๋ฉฐ, result ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ

๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„: result(=0) ์™€ inputNum ์„ pastState(๊ณฑ์…‰/๋‚˜๋ˆ—์…ˆ) ์œผ๋กœ ๊ณ„์‚ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0 ์ด ๋‚˜์˜ฌ ์ˆ˜๋ฐ–์— ์—†์Œ

Solution

  • ํ•ด๋‹น function ์ˆ˜์ •
...
if pastState == "multi" || pastState == "div" {
    if beforeMultiState == "" {
        result = temp   // temp = calculateTwoNumbers(state: pastState, num1: temp, num2: inputNum)
    } else {
        result = calculateTwoNumbers(state: beforeMultiState, num1: result, num2: temp)
    }
} else {                // sum or sub
    result = calculateTwoNumbers(state: pastState, num1: result, num2: inputNum)
}
...



โš™๏ธ Development Environment

  • Xcode 12.3
  • Swift 5

About

๐Ÿ“ฑios calculator clone coding

Topics

Resources

Stars

Watchers

Forks

Languages