Git Product home page Git Product logo

calculator's Introduction

Calculator

My solutions of the required tasks and extra credits for CS193P Winter 2017 Assignment I: Calculator

Some notes:

Environment: Xcode Version 8.2.1 (8C1002) running on macOS Sierra 10.12.4 (16E195)

Screen shots

  • Calculator in portrait mode:

simulator screen shot 25 apr 2017 13 47 00

  • Calculator in landscape mode:

simulator screen shot 25 apr 2017 13 47 08

calculator's People

Contributors

petervanhoef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

calculator's Issues

Task 2: Floating point numbers support

Your Calculator already works with floating point numbers (e.g. if you touch 3 ÷ 4 =, it will properly show 0.75), however, there is no way for the user to enter a floating point number directly. Fix this by allowing legal floating point numbers to be entered (e.g. “192.168.0.1” is not a legal floating point number!). You will need to have a “.” button in your calculator. Don’t worry too much about precision or significant digits in this assignment (doing so is Extra Credit).

Task 3: Add operations buttons

Add some more operations buttons to your calculator such that it has at least a dozen operations total (it can have even more if you like). You can choose whatever operations appeal to you. The buttons must arrange themselves nicely in portrait and landscape modes on all iPhone 6’s and 7’s.

Hint 2

The floating point requirement can be implemented in a single line of code (a curly brace on a line by itself is not considered a “line of code”). Note that what you are reading right now is a Hint, not a Required Task.

Question on a Break in CalculatorBrain.swift

Is the break at line 63 necessary (in the mutating func performOperation)? I didn't think breaks were required when switching on Cases in Swift since they don't fall through? I commented it out on a copy that I made and it seemed to run fine, but wanted to check if there was a reason you had it in there.

With your help I feel like I now understand this first assignment, so now I am moving on to Assignment #2. Thanks again!

Extra Credit 3: Add RAND operation button

Makeoneofyouroperationbuttonsbe“generatearandomdouble-precisionfloating point number between 0 and 1”. This operation button is not a constant (since it changes each time you invoke it). Nor is it a unary operation (since it does not operate on anything). Probably the easiest way to generate a random number in iOS is the global Swift function arc4random() which generates a random number between 0 and the largest possible 32-bit integer (UInt32.max). You’ll have to get to double precision floating point number from there, of course.

Task 8: Add a C button

Add a C button that clears everything (your display, the new UILabel you added above, any pending binary operations, etc.). Ideally, this should leave your Calculator in the same state it was in when you launched it.

Extra Credit 1: "backspace" button

Implement a “backspace” button for the user to touch if they hit the wrong digit button. This is not intended to be “undo,” so if the user hits the wrong operation button, he or she is out of luck! It is up to you to decide how to handle the case where the user backspaces away the entire number they are in the middle of typing. You will probably find the Strings and Characters section of the Swift Reference Guide to be helpful here.

Constants.swift question

Hi Peter. Caveat - I'm a complete coding and GitHub newbie, so apologies if this is not the right way to post a question or comment, but here we go...

First of all - THANK YOU for posting this code. I was really struggling to comprehend some of the initial concepts (I'm following the course via iTunesU) and this really has helped me and inspired me to keep plugging away. I'm a 50 year old US expat currently living in Hong Kong, and just starting to attempt to learn Swift coding in my spare time. I'm a marketing/ops/finance guy by day...

I was trying to understand why you created a separate Constants.swift file for that one Constants struct? Since Foundation is imported into CalculatorBrain.swift and Constants.swift, why wouldn't you have simply written that struct in CalculatorBrain.swift? Does that somehow violate the principles of MVC?

Thanks in advance.

-Jim

Task 7: Visualise sequence of operands and operations

Implement a UILabel in your UI which shows the sequence of operands and operations that led to (or is leading to if resultIsPending) what is (or “will be” if resultIsPending) showing in the display. If resultIsPending is true, put . . . on the end of the UILabel, else put =. If the userIsInTheMiddleOfTyping, you can leave the UILabel showing whatever was there before the user started typing the number. Examples ...

  1. touching 7 + would show “7 + ...” (with 7 still in the display)
  2. 7 + 9 would show “7 + ...” (9 in the display)
  3. 7 + 9 = would show “7 + 9 =” (16 in the display)
  4. 7 + 9 = √ would show “√(7 + 9) =” (4 in the display)
  5. 7 + 9 = √ + 2 = would show “√(7 + 9) + 2 =” (6 in the display)
  6. 7 + 9 √ would show “7 + √(9) ...” (3 in the display)
  7. 7 + 9 √ = would show “7 + √(9) =“ (10 in the display)
  8. 7 + 9 = + 6 = + 3 = would show “7 + 9 + 6 + 3 =” (25 in the display)
  9. 7 + 9 = √ 6 + 3 = would show “6 + 3 =” (9 in the display)
  10. 5 + 6 = 7 3 would show “5 + 6 =” (73 in the display)
  11. 4 × π = would show “4 × π =“ (12.5663706143592 in the display)

Extra Credit 2: Only show 6 digits after the decimal point

Figure out from the documentation how to use the iOS struct NumberFormatter to format your display so that it only shows 6 digits after the decimal point (instead of showing all digits that can be represented in a Double). This will eliminate (or at least reduce) the need for Autoshrink in your display. While you’re at it, make it so that numbers that are integers don’t have an unnecessary “.0” attached to them (e.g. show “4” rather than “4.0” as the result of the square root of sixteen). You can do all this for your description in the CalculatorBrain as well.

Task 6: Add description property containing sequence of operands and operations

Add a String property to your CalculatorBrain called description which returns a description of the sequence of operands and operations that led to the value returned by result (or the result so far if resultIsPending). The character = (the equals sign) should never appear in this description, nor should ... (ellipses).

Hint 7

You might want to add a call to performPendingBinaryOperation() in your case binaryOperation too (that way 6 x 5 x 4 x 3 = will work).

Task 4: Add color to the UI

Use color to make your UI look nice. At the very least, your operations buttons must be a different color than your keypad buttons, but otherwise you can use color in whatever way you think looks nice.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.