簡易計算器

.h文件#import @interface ViewController : UIViewController{ int first; int second; NSString *str1; char operate; NSString *result;}@property (strong, nonatomic) IBOutlet UITextField *test;- (IBAction)add:(id)sender;- (IBAction)sub:(id)sender;- (IBAction)mul:(id)sender;- (IBAction)div:(id)sender;- (IBAction)mybutton:(id)sender;@end.m文件#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)add:(id)sender { str1=self.test.text; first=[str1 intValue]; NSString *str2; str2=@"+"; operate='+'; str1=[str1 stringByAppendingString:str2]; self.test.text=str1;}- (IBAction)sub:(id)sender { str1=self.test.text; first=[str1 intValue]; NSString *str2; str2=@"-"; operate='-'; str1=[str1 stringByAppendingString:str2]; self.test.text=str1;}- (IBAction)mul:(id)sender { str1=self.test.text; first=[str1 intValue]; NSString *str2; str2=@"*"; operate='*'; str1=[str1 stringByAppendingString:str2]; self.test.text=str1;}- (IBAction)div:(id)sender { str1=self.test.text; first=[str1 intValue]; NSString *str2; str2=@"/"; operate='/'; str1=[str1 stringByAppendingString:str2]; self.test.text=str1;}- (IBAction)mybutton:(id)sender { NSString *str2=[NSString alloc]; NSString *str3=[NSString alloc]; str2=self.test.text; NSInteger a; a=[str1 length]; str3=[str2 substringFromIndex:a]; second=[str3 intValue]; NSLog(@"first=%d",first); NSLog(@"second=%d",second); NSLog(@"operate=%c",operate); switch(operate) { case '+':first+=second;break; case '-':first-=second;break; case '*':first*=second;break; case '/':first/=second;break; }str1=[NSString stringWithFormat:@"%d",first];self.test.text=str1;}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject] ; if (![touch.view isMemberOfClass:[UITextField class]]) { [self.test resignFirstResponder] ; }}@end
發佈了27 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章