After completing the simple program ‘Hello World’, it is time to start coding something useful. After a quick decision (well actually lets be honest with a new language one of the first programs to write is the obligatory calculator application). However I decided to add a small twist to the Calculator, making it effectively the same as the one you most probably have on your desk right now. Not only would the calculator do simple operations like + – * / =, but it would also have a working single float memory – with the ability to set clear and recall that memory, and a clear button for the whole display.
First of all I needed to create a new Cocoa Application project in XCode, then I got to play with the lovely Interface Builder – adding buttons and text fields to work as the input and output, and playing with the inspector properties of each section until I was happywith the desired look.
Then we need to create a new class account for the input/output buttons on the .xib file, through the File -> New File menu, and choosing the Objective-C Class template. By convention the start of the filename should be an identifier to the program (SC for Simple Calculator here), with the secondary name of Buttons, as this class will control all input/output from the GUI.
To enable the calculator to perform simple operations I used a simple integer flag system (calcOp) to determine the operator, and NSMutableStrings to allow both the main display and the memory information to be continually updated with additional button presses. As the string is Mutable it is possible to append additional characters to the string through the command on line 1, and then to convert the display string into a float value with line 3:
[SCDisplay appendString:@"3"];
...
number1 = [SCDisplay floatValue];
Where SCDisplay is a NSMutableString and number1 is a float value. The Interface Builder creates the code for the @interface SCButtons once the GUI has been created and inputs and outputs linked to the buttons.
//
// SCButtons.h
//
// Created by Delphi on 21/03/2010.
// Copyright 2010. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface SCButtons : NSObject {
IBOutlet id textOutput;
IBOutlet id textMemory;
}
- (IBAction)button0:(id)sender;
- (IBAction)button1:(id)sender;
- (IBAction)button2:(id)sender;
- (IBAction)button3:(id)sender;
- (IBAction)button4:(id)sender;
- (IBAction)button5:(id)sender;
- (IBAction)button6:(id)sender;
- (IBAction)button7:(id)sender;
- (IBAction)button8:(id)sender;
- (IBAction)button9:(id)sender;
- (IBAction)buttonDecimal:(id)sender;
- (IBAction)buttonDivide:(id)sender;
- (IBAction)buttonEquals:(id)sender;
- (IBAction)buttonMinus:(id)sender;
- (IBAction)buttonPlus:(id)sender;
- (IBAction)buttonTimes:(id)sender;
@end
Working within the SCButtons.m file the string was appended with the integer or decimal information as pressed within the GUI, only once an operator is pressed does the string information change into a float value, and the string be set back to an empty string.
[SCDisplay setString:@""];
Only once the final operator (the equals) is pressed, does the second string get changed into a float and passed along with the operator into the simpleCalculation function. This uses a simple series of if statements to perform the correct operation and return a float.
/ / Continuation of SCButtons.h
float simpleCalculation( float n1, float n2, int op )
{
float simpleAnswer = 0;
if ( op == 1 ) // Divide
{
simpleAnswer = n1 / n2;
}
if ( op == 2 ) // Minus
{
simpleAnswer = n1 - n2;
}
if ( op == 3 ) // Plus
{
simpleAnswer = n1 + n2;
}
if ( op == 4 ) // Times
{
simpleAnswer = n1 * n2;
}
return simpleAnswer;
}
One of the hardest problems I came across coding this simple application was changing the float back into a string so it could be displayed on the output. In the end I had to AppendFormat on the NSMutableString which is equivalent to writing the float back to the string in character form. This is then displayed in the output window (textOutput).
//
// SCButtons.m
//
// Created by Delphi on 21/03/2010.
// Copyright 2010. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SCButtons.h"
@implementation SCButtons
NSMutableString *SCDisplay = nil;
NSMutableString *displayMemory = nil;
int calcOp;
float number1, number2, answer, floatMemory;
+ (void)initialize
{
SCDisplay = [[NSMutableString alloc] initWithString:@""];
displayMemory = [[NSMutableString alloc] initWithString:@""];
calcOp = 0;
}
- (IBAction)button0:(id)sender
{
[SCDisplay appendString:@"0"];
[textOutput setStringValue:SCDisplay];
}
// Same for buttons1-9
}
- (IBAction)buttonDecimal:(id)sender {
[SCDisplay appendString:@"."];
[textOutput setStringValue:SCDisplay];
}
- (IBAction)buttonDivide:(id)sender {
number1 = [SCDisplay floatValue];
[SCDisplay setString:@""];
calcOp = 1; // Division Operator
[textOutput setStringValue:SCDisplay];
}
- (IBAction)buttonMinus:(id)sender {
number1 = [SCDisplay floatValue];
[SCDisplay setString:@""];
calcOp = 2; // Subtraction Operator
[textOutput setStringValue:SCDisplay];
}
- (IBAction)buttonPlus:(id)sender {
number1 = [SCDisplay floatValue];
[SCDisplay setString:@""];
calcOp = 3; // Addition Operator
[textOutput setStringValue:SCDisplay];
}
- (IBAction)buttonTimes:(id)sender {
number1 = [SCDisplay floatValue];
[SCDisplay setString:@""];
calcOp = 4; // Multiplication Operator
[textOutput setStringValue:SCDisplay];
}
- (IBAction)buttonEquals:(id)sender {
number2 = [SCDisplay floatValue];
[SCDisplay setString:@""];
answer = simpleCalculation( number1, number2, calcOp );
[SCDisplay appendFormat:@"%2.3f",answer];
[textOutput setStringValue:SCDisplay];
}
If you have never coded before then I am sure that this looks like a huge mess, which is very hard to follow and understand, when in actual fact this is some very simple code, just following a logical pathway to the end function.
SocialVibe