IOS基础之计算器的编写
# import "ViewController.h"
# import "Calculator.h"
@interface ViewController ( )
@property ( nonatomic, strong) IBOutlet UILabel * display;
- ( void ) processDigit: ( int ) digit;
- ( void ) processOp: ( char ) theOp;
- ( void ) storeFracPart;
- ( IBAction) clickDigit: ( UIButton* ) sender;
- ( IBAction) clickPlus;
- ( IBAction) clickMinus;
- ( IBAction) clickMultiply;
- ( IBAction) clickDivde;
- ( IBAction) clickOver;
- ( IBAction) clickEquals;
- ( IBAction) clickClear;
@end
@implementation ViewController
{
char op;
int currentNumber;
BOOL firstOperand, isNumerator;
Calculator * myCalculator;
NSMutableString * displayString;
}
- ( void ) viewDidLoad {
[ super viewDidLoad] ;
self . display. text= @"" ;
firstOperand = YES;
isNumerator = YES;
displayString = [ NSMutableString stringWithCapacity: 40 ] ;
myCalculator = [ [ Calculator alloc] init] ;
}
- ( void ) processDigit: ( int ) digit{
currentNumber = currentNumber * 10 + digit;
[ displayString appendString: [ NSString stringWithFormat: @"%i" , digit] ] ;
self . display. text = displayString;
}
- ( IBAction) clickDigit: ( UIButton * ) sender{
int digit = ( int ) sender. tag;
[ self processDigit: digit] ;
}
- ( void ) processOp: ( char ) theOp{
NSString * opStr;
op = theOp;
switch ( theOp) {
case '+' :
opStr