petervanhoef / calculator-brain Goto Github PK
View Code? Open in Web Editor NEWMy solutions for CS193P Winter 2017 Assignment II: Calculator Brain
License: MIT License
My solutions for CS193P Winter 2017 Assignment II: Calculator Brain
License: MIT License
Now that I've added the Memory display to the storyboard, I'm having trouble getting my Memory and Sequence displays to render the full width of the display (i.e. like I was able to in Assignment I). I figured out how to add the new Memory display, create a horizontal stack, add constraints, add a blank space in the text field of each (so they at least initially display in the storyboard), however they are bunched up on the left side of the screen, and then automatically resize depending on the text contained in each at a particular time. I've compared my code to yours to no avail, but obviously must be missing something somewhere. Any thoughts on where in my code I should look?
Re: @available, I think I understand what you did, but can you please point me somewhere to read more on deprecation? I don't understand the syntax of @available(*, deprecated, message: "Use evaluate instead"), and was trying to learn more about it - and the Xcode documentation has many "deprecation" entries, but all seemingly very case -specific and no general explanation. Thanks in advance.
Create a launch screen for your Calculator in LaunchScreen.storyboard. Remember that you will want to use some simple autolayout to make your launch screen look good on all platforms.
Add screen shots to README.md
Add two new buttons to your Calculator’s UI: →M and M. Don’t sacrifice any of the required operation buttons from Assignment 1 to add these (though you may add yet more operations buttons if you want). These two buttons will set and get (respectively) a variable in the CalculatorBrain
called M.
setOperand
.setOperand(variable: “M”)
in the brain and then show the result of calling evaluate in the display.Show the value of M (if set) in your UI somewhere.
Have your calculator report errors. For example, the square root of a negative number or divide by zero. There are a number of ways to go about “detecting” these errors (maybe add an associated value to the unary/binaryOperation cases which is a function that detects an error or perhaps have the function that is associated with a unary/binaryOperation return something that is either an error or a result or ???). How you report any discovered errors back to users of the CalculatorBrain API will require some API design on your part, but don’t force users of the CalculatorBrain API to deal with errors if they don’t want to (i.e. allow Controllers that want to display errors to do so, but let those that don’t just deal with NaN and +∞ appearing in their UI). In other words, don’t break any callers of the API described above (who don’t care about errors) to support this feature (i.e., add methods/ properties as needed instead). You are allowed to violate Required Task 11 to implement this Extra Credit item, but not Required Task 1 (you can enhance that data structure, but not switch to a new one).
Add the capability to your CalculatorBrain
to allow the input of variables. Do so by
implementing the following API in your CalculatorBrain
...
func setOperand(variable named: String)
This must do exactly what you would imagine it would: it inputs a “variable” as the operand (e.g. setOperand(variable: “x”)
would input a variable named x
). Setting the operand to x
and then performing the operation cos
would mean cos(x)
is in your CalculatorBrain
.
How to reproduce:
The 0 stays in front of the new digits.
Add app icons to your Assets.xcassets file (which we moved to Supporting Files in Lecture 1). The only trick here is to provide versions of your icon in all the right sizes.
We made the result
, description
and resultIsPending
vars non-private API in Assignment 1. That means we signed up to continue to support them even though we are now adding a new feature (variables) in this assignment which sort of makes them irrelevant. Really what we want to do is deprecate these (you’ll see all sorts of deprecated iOS API in Xcode), but for now we will keep the old result
, description
and resultIsPending
vars around and just implement each of them by calling evaluate
with the argument nil (i.e. they will give their answer assuming the value of any variables is zero). However, do not use any of these vars anywhere in your code in this assignment. Use evaluate
instead.
Add an Undo button to your Calculator. In Assignment 1’s Extra Credit, you might have added a “backspace” button. Here we’re talking about combining both backspace and actual undo into a single button. If the user is in the middle of entering a number, this Undo button should be backspace. When the user is not in the middle of entering a number, it should undo the last thing that was done in the CalculatorBrain
. Do not undo the storing of M’s value (but DO undo the setting of a variable as an operand).
Now that you allow variables to be entered as operands, add a method to evaluate the CalculatorBrain
(i.e. calculate its result) by substituting values for those variables found in a supplied Dictionary
...
func evaluate(using variables: Dictionary<String,Double>? = nil)
-> (result: Double?, isPending: Bool, description: String)
Note that this takes an Optional Dictionary
(with Strings
as keys and Doubles
as values) as its argument and that that argument defaults to nil if not supplied when this method is called. Also note that it returns a tuple (the first element of which is an Optional Double
). This method is not mutating and you are not allowed to make it so. If a variable that has been set as an operand is not found in the Dictionary
, assume its value is zero.
Make sure your C button from Assignment 1 works properly in this assignment. In addition, it should discard the Dictionary it was using for the M variable (it should not set M to zero or any other value, just stop using that Dictionary until →M is pressed again). This will allow you to test the case of an “unset” variable.
I met some problems in the process of Project two. Then I got some inspiration from your code and succeed in finishing it.
Thank you very much!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.