How to write the code of a simple calculator realized by iOS
This article mainly explains how to write the code for iOS to realize the simple calculator. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to write the code for iOS to implement a simple calculator".
Initial contact view, made a simple calculator, basically simple calculation is no problem, not very perfect, there may be some bug, continue to work hard.
/ ViewController.m// Calculator / Created by ma c on 15 Rhythm 8 Rhythm 25.za / Copyright (c) 2015 bjsxt. All rights reserved.//#import "ViewController.h" @ interface ViewController () @ property (weak, nonatomic) IBOutlet UITextField * ResultField;@property (nonatomic,assign) CGFloat temp;@property (nonatomic,assign) CGFloat num1;@property (nonatomic,assign) CGFloat num2;@property (nonatomic,strong) NSMutableString * string;@property (nonatomic,strong) NSArray * arr;@end@implementation ViewController- (IBAction) buttonClear: (UIButton *) sender {[_ string setString:@ "] / / restart the calculation, leave the text box empty self.ResultField.text = _ string;}-(IBAction) button7: (UIButton *) sender {[_ string appendString:@ "7"]; self.ResultField.text = _ string;}-(IBAction) button8: (UIButton *) sender {[_ string appendString:@ "8"]; self.ResultField.text = _ string }-(IBAction) button9: (UIButton *) sender {[_ string appendString:@ "9]; self.ResultField.text = _ string;}-(IBAction) button4: (UIButton *) sender {[_ string appendString:@" 4 "]; self.ResultField.text = _ string;}-(IBAction) button5: (UIButton *) sender {[_ string appendString:@" 5 "]; self.ResultField.text = _ string }-(IBAction) button6: (UIButton *) sender {[_ string appendString:@ "6]; self.ResultField.text = _ string;}-(IBAction) button1: (UIButton *) sender {[_ string appendString:@" 1]; self.ResultField.text = _ string;}-(IBAction) button3: (UIButton *) sender {[_ string appendString:@ "3"]; self.ResultField.text = _ string }-(IBAction) button2: (UIButton *) sender {[_ string appendString:@ "2"]; self.ResultField.text = _ string;}-(IBAction) button0: (UIButton *) sender {[_ string appendString:@ "0]; self.ResultField.text = _ string;}-(IBAction) buttonPoint: (UIButton *) sender {[_ string appendString:@". "]; self.ResultField.text = _ string } / / trigger arithmetic event-(IBAction) buttonDiv: (UIButton *) sender {[_ string appendString:@ "/"]; self.ResultField.text = _ string;}-(IBAction) buttonMul: (UIButton *) sender {[_ string appendString:@ "*]; self.ResultField.text = _ string;}-(IBAction) buttonSub: (UIButton *) sender {[_ string appendString:@"-"]; self.ResultField.text = _ string }-(IBAction) buttonAdd: (UIButton *) sender {[_ string appendString:@ "+]; self.ResultField.text = _ string;} / / do the result operation-(IBAction) buttonEqual: (UIButton *) sender {for (int item0; I)