A simple computer program written with Object-c, which mainly learns the action action of the button.
The following is the main interface:
The following code is where the interface buttons and ViewController.h are connected:
- (ibaction) Button_0: (UIButton*) sender;-(ibaction) Button_dian: (UIButton*) sender;-(ibaction) Button_dengyu: (UIButton*) sender;-(ibaction) Button_1: (UIButton*) sender;-(ibaction) Button_2: (UIButton*) sender;-(ibaction) Button_3: (UIButton*) sender;-(ibaction) Button_4: (UIButton*) sender;-(ibaction) Button_5: (UIButton*) sender;-(ibaction) Button_6: (UIButton*) sender;-(ibaction) Button_7: (UIButton*) sender;-(ibaction) Button_8: (UIButton*) sender;-(ibaction) Button_9: (UIButton*) sender;-(ibaction) Button_jia: (UIButton*) sender;-(ibaction) Button_jian: (UIButton*) sender;-(ibaction) Button_cheng: (UIButton*) sender;-(ibaction) Button_chu: (UIButton*) sender;@property(Weak,nonatomic)IboutletUitextfield *result;
The following click events in VIEWCONTROLLER.M to write the button:
- (ibaction) Button_0: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"0"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_dian: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"."]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_dengyu: (UIButton*) Sender {NSString*resstring = Self. Result. Text;DoubleSumif([Stroperator isequaltostring:@"+"] {sum = [str doublevalue] + [resstring doublevalue]; Self. Result. Text=[NSStringstringwithformat:@"%f", sum]; }Else if([Stroperator isequaltostring:@"-"] {sum = [str doublevalue]-[resstring doublevalue]; Self. Result. Text=[NSStringstringwithformat:@"%f", sum]; }Else if([Stroperator isequaltostring:@"+"] {sum = [str doublevalue] * [resstring doublevalue]; Self. Result. Text=[NSStringstringwithformat:@"%f", sum]; }Else if([Stroperator isequaltostring:@"+"] {sum = [str doublevalue]/[resstring Doublevalue]; Self. Result. Text=[NSStringstringwithformat:@"%f", sum]; }}- (ibaction) Button_1: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"1"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_2: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"2"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_3: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"3"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_4: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"4"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_5: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"5"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_6: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"6"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_7: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"7"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_8: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"8"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_9: (UIButton*) Sender {NSString*resstring = Self. Result. Text;NSString*newstring = [Resstring stringbyappendingstring:@"9"]; Self. Result. Text= [NSStringstringwithformat:@"%@", newstring];} - (ibaction) Button_jia: (UIButton*) Sender {str = Self. Result. Text; Stroperator = @"+"; Self. Result. Text= [NSStringstringwithformat:@" "];} - (ibaction) Button_jian: (UIButton*) Sender {str = Self. Result. Text; Stroperator = @"-"; Self. Result. Text= [NSStringstringwithformat:@" "];} - (ibaction) Button_cheng: (UIButton*) Sender {str = Self. Result. Text; Stroperator = @"*"; Self. Result. Text= [NSStringstringwithformat:@" "];} - (ibaction) Button_chu: (UIButton*) Sender {str = Self. Result. Text; Stroperator = @"/"; Self. Result. Text= [NSStringstringwithformat:@" "];}
Note that the STR and stroperator here are declared in ViewController.h:
*strOperator; *str;
This is the end of a simple calculator ~
The simple calculator for iOS development