杨庆荣的博客

工作之余接单:公司产品展示网站建设、电商网站建设、小程序、app开发。+86 13714715608

ios写的简单计算器

点击 1857    创建时间 2011-11-01 17:00:31

filename: View1ViewController.h

 #import <UIKit/UIKit.h> 

 @interface View1ViewController : UIViewController { 
  IBOutlet UITextField *num1; 
  IBOutlet UITextField *num2; 
  IBOutlet UITextField *op; 
  IBOutlet UILabel *result; 
 } 

 @property(nonatomic,retain) UITextField *num1; 
 @property(nonatomic,retain) UITextField *num2; 
 @property(nonatomic,retain) UITextField *op; 
 @property(nonatomic,retain) UILabel *result; 

 -(IBAction)click:(id)sender; 
 @end

filename2 View1ViewController.m

 #import "View1ViewController.h" 

 @implementation View1ViewController 

 @synthesize num1; 
 @synthesize num2; 
 @synthesize op; 
 @synthesize result; 

 -(IBAction) click:(id)sender{ 
  //[num1 setText:@"hello,world"]; 
  //[num2 setText:@"num2"]; 
  //[op setText:@"+"]; 
  //[result setText:@"aaaa"]; 
  double n1 = [[num1 text] doubleValue]; 
  double n2 = [[num2 text] doubleValue]; 
  NSString *opStr = [op text]; 
  double rs =0; 

  if( [opStr isEqualToString:@"+"] ) 
  { 
    rs = n1 + n2; 
  } 
  if( [opStr isEqualToString:@"-"] ) 
  { 
    rs = n1 - n2; 
  } 
  if( [opStr isEqualToString:@"*"] ) 
  { 
    rs = n1 * n2; 
  } 
  if( [opStr isEqualToString:@"/"] ) 
  { 
    rs = n1 / n2; 
  } 
  NSString *rsStr = [[NSString alloc] initWithFormat:@"%f%@%f=%f",n1,opStr,n2,rs]; 
  [result setText:rsStr]; 
 } 






- (void)didReceiveMemoryWarning { 
  // Releases the view if it doesn't have a superview. 
  [super didReceiveMemoryWarning]; 

  // Release any cached data, images, etc that aren't in use. 
 } 

- (void)viewDidUnload { 
  // Release any retained subviews of the main view. 
  // e.g. self.myOutlet = nil; 
 } 


- (void)dealloc { 
    [super dealloc]; 
 } 

 @end

上一篇: zen cart 的列表的属性有一个特价
下一篇: discuz x2 插件笔记一