iOS 计算器 基本功能实现

//mathModel.h文件

#import <Foundation/Foundation.h>

@interface mathModel : NSObject
+(NSString*)addN1:(double)number1 andN2:(double) number;
+(NSString *)subN1:(double)number1 andN2:(double)number;
+(NSString *)nulN1:(double)number1 andN2:(double)number;
+(NSString *)divN1:(double)number1 andN2:(double)number;
+(NSString *)sqrtN1:(double)number1;
+(NSString *)powN1:(double)number1 ;

@end




//mathModel.m文件中

#import "mathModel.h"


@implementation mathModel

+(NSString *)addN1:(double)number1 andN2:(double)number

{

   return [NSStringstringWithFormat:@"%lg",number+number1];

    

}

+(NSString *)subN1:(double)number1 andN2:(double)number

{

   return [NSStringstringWithFormat:@"%lg",number1-number];


}

+(NSString *)nulN1:(double)number1 andN2:(double)number

{

   return [NSStringstringWithFormat:@"%lg",number*number1];

    

}

+(NSString *)divN1:(double)number1 andN2:(double)number

{

   return [NSStringstringWithFormat:@"%lg",number/number1];

    

}

+(NSString *)sqrtN1:(double)number1

{

   return [NSStringstringWithFormat:@"%lf",sqrt(number1)];

    

}

+(NSString *)powN1:(double)number1

{

   return [NSStringstringWithFormat:@"%lf",pow(number1, 2)];

    

}


@end



//ViewController.h文件中

#import <UIKit/UIKit.h>


@interface ViewController :UIViewController

@property (retain,nonatomic) IBOutletUILabel *outlable;


@property(assign,nonatomic)double opNumberA,opNumberB;

@property(assign,nonatomic)BOOL haspoint;


@property(assign,nonatomic)BOOL isUserInputNumber;

@property(retain,nonatomic)NSString *str1;

- (IBAction)numberTouch:(id)sender;

- (IBAction)clear:(id)sender;

- (IBAction)addpoint:(id)sender;



- (IBAction)method1:(id)sender;

// 

- (IBAction)Equalbt:(id)sender;

- (IBAction)method2:(id)sender;

//   平方  开方  小数点

@end



//

//ViewController.m文件中

//  ViewController.m

//  math

//

//

//  Copyright (c) 2013 wzf. All rights reserved.

//


#import "ViewController.h"

#import "mathModel.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.


    


}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc {

    

    [_outlable release];

    [_str1release];

   

    [superdealloc];

}


- (IBAction)numberTouch:(id)sender {

   UIButton *button=(UIButton*)sender;

    

    if (self.isUserInputNumber)

    {

       if (self.haspoint) {

            NSMutableString *mst1= [NSMutableStringstringWithFormat:@"%@",self.outlable.text];

            [mst1appendFormat:@"%@",button.currentTitle];

           self.outlable.text=mst1;

            

        }

       else

        {

           double inputNumber=[self.outlable.textdoubleValue];

        //获得文本框内容转成长整形

       inputNumber =inputNumber*10+button.currentTitle.doubleValue;

       //获得文本标题

       self.outlable.text=[NSStringstringWithFormat:@"%lg",inputNumber];

        }

    }

   else{

       self.outlable.text=button.currentTitle;

        self.isUserInputNumber=YES;

    }

}

- (IBAction)clear:(id)sender {

    self.haspoint=false;

   self.outlable.text=@"0";

    self.isUserInputNumber=NO;

    

}


- (IBAction)addpoint:(id)sender

{

//    UIButton *button1=(UIButton*)sender;

   if (!self.haspoint) {

        NSMutableString *mustr2=[NSMutableStringstringWithFormat:@"%@%@",self.outlable.text,@"."];

       self.outlable.text=mustr2;

       self.haspoint=true;

    }

}


// + -

- (IBAction)method1:(id)sender {

   UIButton *button1=(UIButton*)sender;

    //将前面一个输入的数字保存在opNumberA

    self.opNumberA=[self.outlable.textfloatValue];

    //将点击事件强转成butntitle存入字符串中

   self.str1=button1.currentTitle;

    //这时在点击加或者减运算时isUserInputNumberno 输入的 数值就不会连在一起。

    self.isUserInputNumber=NO;

    self.haspoint=false;

    

    

}


- (IBAction)Equalbt:(id)sender {

    


    self.opNumberB=[self.outlable.textdoubleValue];

    if ([self.str1isEqualToString:@"+"])

    {

        self.outlable .text=[mathModeladdN1:self.opNumberAandN2:self.opNumberB ];

        self.opNumberA=[self.outlable.textdoubleValue];

        

        self.isUserInputNumber=NO;

           

        

    }

    if ([self.str1isEqualToString:@"-"]) {

        self.outlable .text=[mathModelsubN1:self.opNumberAandN2:self.opNumberB ];

        self.opNumberA=[self.outlable.textdoubleValue];

        

        self.isUserInputNumber=NO;

    }

    if ([self.str1isEqualToString:@"*"]) {

        self.outlable .text=[mathModelnulN1:self.opNumberAandN2:self.opNumberB ];

        self.opNumberA=[self.outlable.textdoubleValue];

        

        self.isUserInputNumber=NO;

        

    }

    if ([self.str1isEqualToString:@"/"]) {

        self.outlable .text=[mathModeldivN1:self.opNumberAandN2:self.opNumberB];

        self.opNumberA=[self.outlable.textdoubleValue];

        

        self.isUserInputNumber=NO;

        

    }

    if ([self.str1isEqualToString:@"√x"]) {

        self.outlable .text=[mathModelsqrtN1:self.opNumberA  ];

        self.opNumberA=[self.outlable.textdoubleValue];

        

        self.isUserInputNumber=NO;

        

    }

    if ([self.str1isEqualToString:@"x^2"])

    {

        self.outlable .text=[mathModelpowN1:self.opNumberA ];

        self.opNumberA=[self.outlable.textdoubleValue];

        

        self.isUserInputNumber=NO;

        

    }


self.haspoint=false;


}


- (IBAction)method2:(id)sender {

    self.haspoint=false;

   UIButton *button2=(UIButton*)sender;

    self.opNumberA=[self.outlable.textdoubleValue];

   self.str1=button2.currentTitle;

    self.isUserInputNumber=NO;

   }

@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值