//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];
//将点击事件强转成butn的title存入字符串中
self.str1=button1.currentTitle;
//这时在点击加或者减运算时的isUserInputNumber为no 输入的数值就不会连在一起。
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