用iOS 做一个简易计算器 (功能完备)

源码(.m文件)

#import "ZKJAppDelegate.h"



@interface ZKJAppDelegate ()

@property (retain,nonatomic) UIView *containView;

@property (retain, nonatomic) UIButton *button;

@property (retain, nonatomic) UILabel *label;

@property (retain, nonatomic) NSMutableString *string; //保存字符

@property (assign,nonatomic) double num1,num2,num3,num4;

//num1保存输入数值,2,保存运算前数值,3是运算结果,4推断进行何种运算

@end






@implementation ZKJAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.containView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];

    self.containView.backgroundColor = [UIColor clearColor];

    [self.window addSubview:self.containView];

    [self.containView release];

    

    

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(30, 40, 190, 50)];

    self.label.backgroundColor = [UIColor lightGrayColor];

    self.label.textAlignment = UITextAlignmentLeft;

    self.label.font = [UIFont systemFontOfSize:32];

    [self.containView addSubview:self.label];

    [self.label release ];

    

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    backButton.backgroundColor = [UIColor lightGrayColor];

    [backButton setFrame:CGRectMake(225, 40, 60, 50)];

    [backButton setTitle:@"back" forState:UIControlStateNormal];

    [backButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.containView addSubview:backButton];

    

    //加入1 - 9 的button

    NSArray *array = [NSArray arrayWithObjects:@"1",

                      @"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];

    int n = 0;

    for (int i = 0 ; i < 3; i++ ) {

        for (int j = 0 ; j < 3; j++ ) {

            self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

            self.button.frame = CGRectMake(30 + 65 * j , 150 + 65 * i, 60 , 60);

            self.button.backgroundColor = [UIColor lightGrayColor];

            [self.button setTitle:array[n++] forState:UIControlStateNormal];

            [self.containView addSubview:self.button];

            [self.button addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside];

        }

    }

    //加入零

    UIButton *button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button0 setFrame:CGRectMake(30, 345, 60, 60)];

    button0.backgroundColor = [UIColor lightGrayColor];

    [button0 setTitle:@"0" forState:UIControlStateNormal];

    [button0 addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside] ;

    [self.containView addSubview:button0];

    

    

    //加入点

    UIButton *point = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [point setFrame:CGRectMake(95, 345, 60, 60)];

    point.backgroundColor = [UIColor lightGrayColor];

    [point setTitle:@"." forState:UIControlStateNormal];

    [point addTarget:self action:@selector(one:) forControlEvents:UIControlEventTouchUpInside] ;

    [self.containView addSubview:point];

//    [point release];

    

    

    //加入删除

    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [deleteButton setFrame:CGRectMake(160, 345, 60, 60)];

    deleteButton.backgroundColor = [UIColor lightGrayColor];

    [deleteButton setTitle:@"delete" forState:UIControlStateNormal];

    [deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.containView addSubview:deleteButton];

//    [backButton release];

    

    

    //加入运算符

    

    NSArray *array1 = [NSArray arrayWithObjects:@"+",@"-",@"*",@"/", nil];

    for (int i = 0 ; i < 4; i++ ) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        [button setFrame:CGRectMake(225, 150 + 65 * i, 60 , 60)];

        button.backgroundColor = [UIColor lightGrayColor];

        [button setTitle:array1[i] forState:UIControlStateNormal];

        [self.containView addSubview:button];

        [button addTarget:self action:@selector(two:) forControlEvents:UIControlEventTouchUpInside];

//        [button release];

        

    }

    //加入等号

    UIButton *istoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [istoButton setFrame:CGRectMake(30, 100, 255, 40)];

    istoButton.backgroundColor = [UIColor lightGrayColor];

    [istoButton setTitle:@"=" forState:UIControlStateNormal];

    [istoButton addTarget:self action:@selector(isto: ) forControlEvents:UIControlEventTouchUpInside];

    [self.containView addSubview:istoButton];

    

    self.string = [[NSMutableString alloc] init];

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}


//键盘输入 数字事件

- (void)one:(id)sender {

    if ([self.string hasPrefix:@"+"] || [self.string hasPrefix:@"-"] || [self.string hasPrefix:@"*"] || [self.string hasPrefix:@"/"]) {

        [self.string setString:@""];

    }

    //*********

    [self.string appendString:[sender currentTitle]];//数字连续输入;;;

    self.label.text = [NSString stringWithString:self.string];

    self.num1 = [self.label.text doubleValue];//将字符串转化为基本数据类型并赋给num1;

    NSLog(@"%f",self.num1);

    

    

}

//运算符事件

- (void)two:(id)sender{

    [self.string setString:@""];

    //*********

    [self.string appendString:[sender currentTitle]];

    

    self.label.text = self.string;

    if ([self.string hasPrefix:@"+"]) {

        self.num2 = self.num1;

        self.num4 = 1;

    } else if ([self.string hasPrefix:@"-"]){

        self.num2 = self.num1;

        self.num4 = 2;

    } else if ([self.string hasPrefix:@"*"]) {

        self.num2 = self.num1;

        self.num4 = 3;

    } else if ([self.string hasPrefix:@"/"]) {

        self.num2 = self.num1;

        self.num4 = 4;

    }

    

    

}

//等号事件

- (void)isto:(id)sender{

    if (self.num4 == 1) {

        self.num3 = self.num2 + [self.label.text doubleValue];

        self.label.text = [NSString stringWithFormat:@"%f",self.num3];

//        self.num3 = 0;

        [self.string setString:@""];

    } else if (self.num4 == 2){

        self.num3 = self.num2 - [self.label.text doubleValue];

        self.label.text = [NSString stringWithFormat:@"%f",self.num3];

//        self.num3 = 0;

    } else if (self.num4 == 3){

        self.num3 = self.num2 * [self.label.text doubleValue];

        self.label.text = [NSString stringWithFormat:@"%f",self.num3];

//        self.num3 = 0;

    } else if (self.num4 == 4) {

        self.num3 = self.num2 / [self.label.text doubleValue];

        self.label.text = [NSString stringWithFormat:@"%f",self.num3];

//        self.num3 = 0;

    }

}


//所有删除的响应事件

- (void)deleteClick:(id)sender{

    [self.string setString:@"" ];

//    [self.string appendString:[sender currentTitle]];

    self.label.text = self.string;

}



//消除一个数的响应事件

- (void)backClick:(id)sender{

//    NSMutableString

    if ([self.string length] > 0) {

        NSInteger n = [self.string length];

        [self.string deleteCharactersInRange:NSMakeRange(n - 1, 1)];

        self.label.text = self.string;

    }


    

}







- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

- (void)dealloc{

    [self.window release];

    [super dealloc];

}

@end



转载于:https://www.cnblogs.com/ljbguanli/p/7058019.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值