简易计算器

.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值