码迷,mamicode.com
首页 > 其他好文 > 详细

Calculator

时间:2015-08-06 22:27:17      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) UIWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    // 设置一个RootVIewController
    RootViewController *rootVC=[[RootViewController alloc]init];
    self.window.rootViewController=rootVC;
    [rootVC release];

    return YES;
}

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()<UITextFieldDelegate>

@property(nonatomic,retain)NSMutableString *string;

@property(nonatomic,retain)UILabel *label;

@property(nonatomic,assign)double num1,num2,num3,num4;// num1是保存输入的数值,num2是保存运算符之前的数值,num3是运算结果,num4是判断何种运算结果

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"7.jpg"]]];
//    self.view.alpha=0.8;




    // button0
    UIButton *button0=[UIButton buttonWithType:UIButtonTypeSystem];
    button0.frame=CGRectMake(10, 572, 175, 85);
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    button0.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button0 setTintColor:[UIColor grayColor]];
    [button0 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    button0.layer.borderWidth=1;
    button0.layer.borderColor=[UIColor lightGrayColor].CGColor;
    button0.layer.cornerRadius=20;
    button0.layer.masksToBounds=YES;
    [button0 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button0];

    // buttonPoint
    UIButton *buttonPoint=[UIButton buttonWithType:UIButtonTypeSystem];
    buttonPoint.frame=CGRectMake(190, 572, 85, 85);
    buttonPoint.backgroundColor=[UIColor lightGrayColor];
//    buttonPoint.alpha=0.3;
    [buttonPoint setTitle:@"." forState:UIControlStateNormal];
    buttonPoint.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [buttonPoint setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    buttonPoint.layer.cornerRadius=20;
    buttonPoint.layer.masksToBounds=YES;
    [buttonPoint setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [buttonPoint addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonPoint];

    //button =
    UIButton *go=[UIButton buttonWithType:UIButtonTypeSystem];
    go.frame=CGRectMake(280, 482, 85, 175);
    go.backgroundColor=[UIColor orangeColor];
    [go setTitle:@"=" forState:UIControlStateNormal];
    go.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [go setTintColor:[UIColor grayColor]];
    [go setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    go.layer.borderWidth=1;
    go.layer.borderColor=[UIColor lightGrayColor].CGColor;
    go.layer.cornerRadius=20;
    go.layer.masksToBounds=YES;
    [go addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    go.tag=10;
    [self.view addSubview:go];

// 这里button的创建可以用for循环生成,更简便

    //button1
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame=CGRectMake(10, 482, 85, 85);
    button1.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button1 setTitle:@"1" forState:UIControlStateNormal];
    button1.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button1.layer.cornerRadius=20;
    button1.layer.masksToBounds=YES;
    [button1 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button1 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag=1;
    [self.view addSubview:button1];

    // button2
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame=CGRectMake(100, 482, 85, 85);
    button2.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button2 setTitle:@"2" forState:UIControlStateNormal];
    button2.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button2.layer.cornerRadius=20;
    button2.layer.masksToBounds=YES;
    [button2 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button2 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag=2;
    [self.view addSubview:button2];

    // button3
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame=CGRectMake(190, 482, 85, 85);
    button3.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button3 setTitle:@"3" forState:UIControlStateNormal];
    button3.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button3 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button3.layer.cornerRadius=20;
    button3.layer.masksToBounds=YES;
    [button3 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button3 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag=3;
    [self.view addSubview:button3];

    // button4
    UIButton *button4=[UIButton buttonWithType:UIButtonTypeSystem];
    button4.frame=CGRectMake(10, 392, 85, 85);
    button4.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button4 setTitle:@"4" forState:UIControlStateNormal];
    button4.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button4 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button4.layer.cornerRadius=20;
    button4.layer.masksToBounds=YES;
    [button4 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button4 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button4.tag=4;
    [self.view addSubview:button4];

    // button5
    UIButton *button5=[UIButton buttonWithType:UIButtonTypeSystem];
    button5.frame=CGRectMake(100, 392, 85, 85);
    button5.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button5 setTitle:@"5" forState:UIControlStateNormal];
    button5.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button5 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button5.layer.cornerRadius=20;
    button5.layer.masksToBounds=YES;
    [button5 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button5 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button5.tag=5;
    [self.view addSubview:button5];

    // button6
    UIButton *button6=[UIButton buttonWithType:UIButtonTypeSystem];
    button6.frame=CGRectMake(190, 392, 85, 85);
    button6.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button6 setTitle:@"6" forState:UIControlStateNormal];
    button6.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button6 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button6.layer.cornerRadius=20;
    button6.layer.masksToBounds=YES;
    [button6 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button6 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button6.tag=6;
    [self.view addSubview:button6];

    // buttonA
    UIButton *buttonA=[UIButton buttonWithType:UIButtonTypeSystem];
    buttonA.frame=CGRectMake(280, 392, 85, 85);
//    buttonA.backgroundColor=[UIColor orangeColor];
    [buttonA setTitle:@"+" forState:UIControlStateNormal];
    buttonA.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [buttonA setTintColor:[UIColor grayColor]];
    [buttonA setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    buttonA.layer.borderWidth=1;
    buttonA.layer.borderColor=[UIColor lightGrayColor].CGColor;
    buttonA.layer.cornerRadius=20;
    buttonA.layer.masksToBounds=YES;
    [buttonA addTarget:self action:@selector(c:) forControlEvents:UIControlEventTouchUpInside];
    buttonA.tag=11;
    [self.view addSubview:buttonA];

    // button7
    UIButton *button7=[UIButton buttonWithType:UIButtonTypeSystem];
    button7.frame=CGRectMake(10, 302, 85, 85);
    button7.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button7 setTitle:@"7" forState:UIControlStateNormal];
    button7.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button7 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button7.layer.cornerRadius=20;
    button7.layer.masksToBounds=YES;
    [button7 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button7 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button7.tag=7;
    [self.view addSubview:button7];

    // button8
    UIButton *button8=[UIButton buttonWithType:UIButtonTypeSystem];
    button8.frame=CGRectMake(100, 302, 85, 85);
    button8.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button8 setTitle:@"8" forState:UIControlStateNormal];
    button8.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button8 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button8.layer.cornerRadius=20;
    button8.layer.masksToBounds=YES;
    [button8 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button8 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button8.tag=8;
    [self.view addSubview:button8];

    // button9
    UIButton *button9=[UIButton buttonWithType:UIButtonTypeSystem];
    button9.frame=CGRectMake(190, 302, 85, 85);
    button9.backgroundColor=[UIColor lightGrayColor];
    //    buttonPoint.alpha=0.3;
    [button9 setTitle:@"9" forState:UIControlStateNormal];
    button9.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [button9 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button9.layer.cornerRadius=20;
    button9.layer.masksToBounds=YES;
    [button9 setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    [button9 addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    button9.tag=9;
    [self.view addSubview:button9];

    // buttonR
    UIButton *buttonR=[UIButton buttonWithType:UIButtonTypeSystem];
    buttonR.frame=CGRectMake(280, 302, 85, 85);
    //    buttonA.backgroundColor=[UIColor orangeColor];
    [buttonR setTitle:@"-" forState:UIControlStateNormal];
    buttonR.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [buttonR setTintColor:[UIColor grayColor]];
    [buttonR setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    buttonR.layer.borderWidth=1;
    buttonR.layer.borderColor=[UIColor lightGrayColor].CGColor;
    buttonR.layer.cornerRadius=20;
    buttonR.layer.masksToBounds=YES;
    [buttonR addTarget:self action:@selector(c:) forControlEvents:UIControlEventTouchUpInside];
    buttonR.tag=12;
    [self.view addSubview:buttonR];

    // buttonC
    UIButton *clear=[UIButton buttonWithType:UIButtonTypeSystem];
    clear.frame=CGRectMake(10, 212, 85, 85);
    //    buttonA.backgroundColor=[UIColor orangeColor];
    [clear setTitle:@"C" forState:UIControlStateNormal];
    clear.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [clear setTintColor:[UIColor orangeColor]];
    [clear setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    clear.layer.borderWidth=1;
    clear.layer.borderColor=[UIColor lightGrayColor].CGColor;
    clear.layer.cornerRadius=20;
    clear.layer.masksToBounds=YES;
    [clear addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    clear.tag=13;
    [self.view addSubview:clear];

    // buttonX
    UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem];
    back.frame=CGRectMake(100, 212, 85, 85);
    //    buttonA.backgroundColor=[UIColor orangeColor];
    [back setTitle:@"?" forState:UIControlStateNormal];
//    [buttonX setBackgroundImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal];
    back.titleLabel.font=[UIFont systemFontOfSize:15.0];
    [back setTintColor:[UIColor grayColor]];
    [back setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    back.layer.borderWidth=1;
    back.layer.borderColor=[UIColor lightGrayColor].CGColor;
    back.layer.cornerRadius=20;
    back.layer.masksToBounds=YES;
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    back.tag=14;
    [self.view addSubview:back];


    // buttonD
    UIButton *buttonD=[UIButton buttonWithType:UIButtonTypeSystem];
    buttonD.frame=CGRectMake(190, 212, 85, 85);
    //    buttonA.backgroundColor=[UIColor orangeColor];
    [buttonD setTitle:@"÷" forState:UIControlStateNormal];
    //    [buttonX setBackgroundImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal];
    buttonD.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [buttonD setTintColor:[UIColor grayColor]];
    [buttonD setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    buttonD.layer.borderWidth=1;
    buttonD.layer.borderColor=[UIColor lightGrayColor].CGColor;
    buttonD.layer.cornerRadius=20;
    buttonD.layer.masksToBounds=YES;
    [buttonD addTarget:self action:@selector(c:) forControlEvents:UIControlEventTouchUpInside];
    buttonD.tag=15;
    [self.view addSubview:buttonD];

    // buttonM
    UIButton *buttonM=[UIButton buttonWithType:UIButtonTypeSystem];
    buttonM.frame=CGRectMake(280, 212, 85, 85);
    //    buttonA.backgroundColor=[UIColor orangeColor];
    [buttonM setTitle:@"×" forState:UIControlStateNormal];
    buttonM.titleLabel.font=[UIFont systemFontOfSize:25.0];
    [buttonM setTintColor:[UIColor grayColor]];
    [buttonM setBackgroundImage:[UIImage imageNamed:@"6.jpg"] forState:UIControlStateHighlighted];
    buttonM.layer.borderWidth=1;
    buttonM.layer.borderColor=[UIColor lightGrayColor].CGColor;
    buttonM.layer.cornerRadius=20;
    buttonM.layer.masksToBounds=YES;
    [buttonM addTarget:self action:@selector(c:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonM];


    // 设置一个label
    self.label=[[UILabel alloc]initWithFrame:CGRectMake(10, 122, 355, 85)];
    [self.view addSubview:self.label];
    self.label.textColor=[UIColor grayColor];
    self.label.tag=20;
    [self.label release];


    // 创建一个空的可变字符串
    self.string=[[NSMutableString alloc]init];

}
// 加一个BOOL类型判断当前是否有一个点,有一个点就YES,两个或多个就NO;
// 0~9
-(void)add:(id)sender
{
    if ([self.string hasPrefix:@"+"]||[self.string hasPrefix:@"-"]||[self.string hasPrefix:@"×"]||[self.string hasPrefix:@"÷"]) {
        [self.string setString:@""];// 字符串清零
    }
    [self.string appendString:[sender currentTitle]];// 在self.string后追加字符串,但是不支持连续输入
    self.label.text=[NSString stringWithString:self.string];// 显示数值
    self.num1=[self.label.text doubleValue];
    NSLog(@"%f",self.num1);

}

// + - * /

-(void)c:(id)sender
{
    [self.string setString:@""];// 字符串清零
    [self.string appendString:[sender currentTitle]];// 在字符串后添加字符串
    self.label.text=[NSString stringWithString: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;
    }
}

// go
-(void)go:(id)sender
{
    // 判断是+
    if (self.num4==1) {
        self.num3=self.num2+[self.label.text doubleValue];// [self.label.text doubleValue];是每次输入时最后输入的数字
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];// 显示结果
        self.num1=[self.label.text doubleValue];// num1为最后一次输入的数字   为了可以连加,保存结果
        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.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    // 判断是*
    else if (self.num4==3){
        self.num3=self.num2*[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
    // 判断是/
    else if (self.num4==4)
    {
        self.num3=self.num2/[self.label.text doubleValue];
        self.label.text=[NSString stringWithFormat:@"%f",self.num3];
        self.num1=[self.label.text doubleValue];
        self.num3=0;
        [self.string setString:@""];
    }
}

// clear
-(void)clear:(id)sender
{
    [self.string setString:@""];
    self.num2=0;
    self.num3=0;
    self.label.text=@"0";
//    [self.string setString:@"0"];
}

// back
-(void)back:(id)sender
{
    [self.string deleteCharactersInRange:NSMakeRange(self.string.length - 1, 1)];// 删除最后一个字符
    self.label.text=[NSString stringWithString:self.string];// 显示结果
}

生成的计算器界面如下
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Calculator

标签:

原文地址:http://blog.csdn.net/muran33/article/details/47323075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!