Info

Programmer by day, artist by night

calculator-assignment-1-stanford-university-ios-winter-2015Provides RPL (Reverse Polish Notation) calculator, where numbers are entered first then operations. For example, to add 6 and 4, you would:

6 Enter, 4 Enter, +
or 6 Enter, 4 +

This assignment solution covers all the required tasks including the ones listed under extra credit. Solution is Swift 2.0 and iOS 9.0 compatible.

Source code available at Github

Notes:

1. Overloaded the enter function for user input, because we want add to add it the history. The enter function is also called from other places in the code where we don’t want to add to history.

2. As mentioned in the assignment hints, floating point implementation was possible to add with a single-line of code:

if (digit != ".") || (digit == "." && display.text!.rangeOfString(".") == nil)

3. Pi implementation does not use the performOperation function but inline code since it’s easy to add:

case "π": displayValue = M_PI; enter()

Video Demo:

Comments

18 Comments

Post a comment
  1. WS #
    November 7, 2015

    Sup.

    1) In your floating point implementation, the `digit == “.”` is redundant.
    2) Also in your floating point implementation, if the user were to only hit the “.” then the return key, `display.text!` would be just “.”, and the `.numberFromString()` for the `displayValue` getter would crash the program.

    • November 7, 2015

      Thanks for the code comments, much appreciated.

      1. Removed the redundant logic check: digit == “.”
      2. Actually entering “.” and then the return key doesn’t crash the program, because displayValue returns an Optional Double. So numberFromString for “.” would just return a nil. Here are some test cases:

      3 return, . return, 1 +
      Displays 4.0

      . return, 3 return, 1 +
      Displays 4.0

      3 return, 1 return, . +
      Displays 4.0

      BTW, are you also following the Stanford U. course “Developing iOS 8 Apps with Swift”?

      • 6IX #
        December 18, 2015

        Hey man
        I sent you a message through youtube. Sorry for bugging but I really need help. So I managed to make all the buttons that were required on the assignment including the History label and the C button. However. I can’t set the constraints for my history label unless I have a number inside of it or else when I try to set the leading trails and all of that and then go “Update frame”, the label disappears. Any idea what I’m doing wrong? My history label is right above my display label. Please help me. You think I can send you my project so you can help me out? I would really appreciate it.
        Again sorry for bugging but I really want to advance through this course but I don’t want to unless I have everything 100% complete on the assignment.

      • December 18, 2015

        Hey no worries 🙂
        I am glad you found the post useful. Just put a space as the content in the history label and that should solve the issue of the label disappearing. The label is there like you said but it needs content, so I use a blank space (whereas you tried putting a number). Please see the image below.

        You can also check out my Storyboard by downloading the project here https://github.com/sanjibahmad/Calculator
        Otherwise please feel free to send me your project.

  2. 6IX #
    December 18, 2015

    Thanks a lot, that seems to do the trick! I’m glad I’m not the only one following along with this course. It’s very challenging even though I have over a year of Python experience and knowledge of OOP concepts, the new syntax and overall the new concepts are a little challenging.

    Any idea on how to push all the inputs of my history label to the left? You seem to have solved that problem. I’m hesitant on downloading your code and checking it out for myself so you think you can give me some sort of hint?

    Again thank you so much for your time and for a quick reply. I have your blog bookmarked so I’ll definitely be checking it out as I go through the rest of the course.

    • December 18, 2015

      Most welcome, it’s a pleasure to connect with others who are also taking the course through iTunes. I agree pursuing the course alone can be difficult, at least I had a lot of frustrations because some of the assignment requirements weren’t fully clear to me. I am still doing Assignment 4, hopefully will be able to complete the course soon…

      Ok, here is a hint to how I solved the problem, I have an equal sign (=) in the history label. So I use the method stringByReplacingOccurrencesOfString to make sure the inputs get pushed to the left of the equal sign.

      Good luck with the course! I find it full of gems. With the Professor’s long experience I think it’s worth taking it every year (as new versions get released through iTunes).

      • 6IX #
        December 19, 2015

        How would you use that method exactly? Do you use it in your enter function? Because I notice in your video you did it only after an answer was given back.

  3. 6IX #
    December 19, 2015

    Nevermind. Solved it but it looks like really bad code the way I solved it but it works 🙂 lol.

    • December 19, 2015

      Hey great stuff! Was just about to reply :p Yeah the implementation is a bit messy, the best I was able to do is wrap the method in a function called appendHistory, then call appendHistory on enter, operate, changeSign and backspace. A little spoiler alert, in Assignment 2, the appendHistory becomes more elegant as a better implementation (through an array of operands and operators called opStack) is taught in the subsequent lectures.

      • 6IX #
        December 19, 2015

        Thanks for the look-out! I’m getting a little frustrated because of my history label. So I’m able to input as much as I want but the “…” displays on the right and doesn’t let me view the new inputs. Did you have this issue? I can’t seem to find a work around it. I’m not sure if I’m concatenating the strings wrong when adding it to the label. As soon as I fix the label issue and add the +/- feature I’m going to move on to the next lectures, I’m so excited to move on haha

      • December 19, 2015

        I make the “…” appear on the left-side instead of the right-side, so that the latest inputs always show on the screen. This is how it looks like:

        Yeah, I think it’s best to move on as soon as you are satisfied. I cheated a little on Assignment 2, i.e. didn’t complete all the tasks according to the hints. But clarification would require asking questions either to the Teaching Assistants or the Professor, neither option feasible 🙂 So I did the best I could then moved on.

  4. 6IX #
    December 21, 2015

    Okay thanks! I just moved on. I finished everything on the Calculator but I just couldn’t get the “…” on the history label to go on the right side 😦 But thanks again for your help. I might be coming back to you if I hit dead ends in the rest of the course haha By the way. Do you read the reading assignments before watching the lectures?

    • 6IX #
      December 21, 2015

      Nevermind. I played around with the label settings and got it to work. Again, thanks for the help on this assignment. You’re a life-saver and a genius!

      • December 21, 2015

        Great! 🙂 Again, glad these posts were helpful to somebody.
        However I am just an apprentice :p though trying hard… 😀

    • December 21, 2015

      Sure please feel free to get in touch, it would be great for me also to collaborate on these learning projects. I find discussions always lead to “aha” moments, especially in programming.
      I read the “Swift Programming Language” book last year when Swift was first released. However need to revise again since substantial parts of the book got updated after Swift 2 was released.

  5. thedilab #
    April 12, 2016

    Hi

    Do you have all the assignment instructions ?

    I am viewing it from itunes, it does not provide assignment instructions.
    So I can not really follow to do the assignment.

    Thanks again for your efforts.

    Xu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS