ios开发简单计算器

来源:互联网 发布:it 网络 编辑:程序博客网 时间:2024/05/22 07:00

此篇博客为ios开发的基础篇:ios开发简单计算器


1.基本功能:

(1)、可进行普通的加减乘除;

(2)、可进行连加、连减、连乘、连除;

(3)、可进行“退后”和“清除”处理;


2.代码:

.h文件中:

//

//  ViewController.h

//  SimpleCalculator

//

//  Created by 郑俊杭的MacBook  Air on 16/4/20.

//  Copyright © 2016郑俊杭的MacBook  Air. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController

@property (retain,nonatomic)UILabel *label;

@property (retain,nonatomic)UIButton *button1,*button2,*button3,*button4,*button5,*button6;

@property (retain,nonatomic)NSMutableString *mutableString;

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

@property (assign,nonatomic)int n,m;

@property (retain,nonatomic)NSString *fuhao,*point;



@end


.m文件中:

//

//  ViewController.m

//  SimpleCalculator

//

//  Created by 郑俊杭的MacBook  Air on 16/4/20.

//  Copyright © 2016郑俊杭的MacBook  Air. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)shuzi:(id)sender{

//    self.mutableString=[sender titleForState:UIControlStateNormal];将点击的button储存起来用NSString来接

//    [self.mutableString appendString:i];将字符串加到可变字符串中

    self.n++;

    NSLog(@"n:%d",self.n);

    if(self.n==0){

        [self.mutableStringsetString:@""];//将取到的值清空

        [self.mutableStringappendString:[sendercurrentTitle]];

        self.label.text=self.mutableString;

        NSLog(@"%@",self.mutableString);

    }

    else{

        [self.mutableStringappendString:[sendercurrentTitle]];

        self.point=[sendertitleForState:UIControlStateNormal];

        self.label.text=self.mutableString;

        NSLog(@"%@",self.mutableString);

    }

}


- (void)yunsuan:(id)sender{

    self.m++;

    NSLog(@"m:%d",self.m);

    if(self.m==1){

        self.num1=[self.label.textdoubleValue];//将第一次文本框的值转换成int型,并赋值给self.num1

        [self.mutableStringsetString:@""];//将取到的值清空

        self.fuhao=[sendertitleForState:UIControlStateNormal];//接受到符号

//        self.label.text=self.fuhao;//将符号赋值给文本框

    }

    else{

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

        self.num3=[self.label.textdoubleValue];

        NSLog(@"num3:%f",self.num3);

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

        {

//            NSLog(@"进行的运算为“+”");

            self.label.text=[NSStringstringWithFormat:@"%f",self.num1+self.num3];

            self.num1=self.num1+self.num3;

            [self.mutableStringsetString:@""];

            self.fuhao=[sendertitleForState:UIControlStateNormal];

        }

        else if([self.fuhaoisEqualToString:@"-"])

        {

//            NSLog(@"进行的运算为“-”");

            self.label.text=[NSStringstringWithFormat:@"%f",self.num1-self.num3];

            self.num1=self.num1-self.num3;

            [self.mutableStringsetString:@""];

            self.fuhao=[sendertitleForState:UIControlStateNormal];

        }

        else if([self.fuhaoisEqualToString:@"×"])

        {

//            NSLog(@"进行的运算为“×”");

            self.label.text=[NSStringstringWithFormat:@"%f",self.num1*self.num3];

            self.num1=self.num1*self.num3;

            [self.mutableStringsetString:@""];

            self.fuhao=[sendertitleForState:UIControlStateNormal];

        }

        else if([self.fuhaoisEqualToString:@"÷"])

        {

//            NSLog(@"进行的运算为“÷”");

            self.label.text=[NSStringstringWithFormat:@"%f",self.num1/self.num3];

            self.num1=self.num1/self.num3;

            [self.mutableStringsetString:@""];

            self.fuhao=[sendertitleForState:UIControlStateNormal];

        }


    }

}


- (void)tuihou:(id)sender{

    [self.mutableStringdeleteCharactersInRange:NSMakeRange([self.mutableStringlength]-1,1)];

    self.label.text=self.mutableString;

    self.n--;

    NSLog(@"n:%d",self.n);

}


- (void)equal:(id)sender{

    self.num2=[self.label.textdoubleValue];

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

    NSLog(@"self.num2:%f",self.num2);

    self.label.text=[NSStringstringWithFormat:@"%f",self.num2];

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

    {

        //        NSLog(@"进行的运算为“+”");

        self.label.text=[NSStringstringWithFormat:@"%f",self.num1+self.num2];

        NSLog(@"结果:%f",self.num1+self.num2);

        self.n=-1;

        self.m=0;

    }

    elseif([self.fuhaoisEqualToString:@"-"])

    {

        //        NSLog(@"进行的运算为“-”");

        self.label.text=[NSStringstringWithFormat:@"%f",self.num1-self.num2];

        NSLog(@"结果:%f",self.num1-self.num2);

        self.n=-1;

        self.m=0;

    }

    elseif([self.fuhaoisEqualToString:@"×"])

    {

        //        NSLog(@"进行的运算为“×”");

        self.label.text=[NSStringstringWithFormat:@"%f",self.num1*self.num2];

        NSLog(@"结果:%f",self.num1*self.num2);

        self.n=-1;

        self.m=0;

    }

    elseif([self.fuhaoisEqualToString:@"÷"])

    {

        //        NSLog(@"进行的运算为“÷”");

        self.label.text=[NSStringstringWithFormat:@"%f",self.num1/self.num2];

        NSLog(@"结果:%f",self.num1/self.num2);

        self.n=-1;

        self.m=0;

    }

}


- (void)qingchu:(id)sender{

    self.n=0;

    NSLog(@"n:%d",self.n);

    self.m=0;

    NSLog(@"m:%d",self.m);

    [self.mutableStringsetString:@""];//将取到的值清空

    NSLog(@"self.mutableString:“%@”",self.mutableString);

    self.num1=0;

    self.num2=0;

    self.label.text=@"0";//将第二次取到的值赋值给文本框

    self.fuhao=@"";

    NSLog(@"清除完毕!");

    UIAlertController *alert=[UIAlertControlleralertControllerWithTitle:@"提示"message:@"清除完毕!"preferredStyle:UIAlertControllerStyleAlert];

    [alert addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil]];

    [selfpresentViewController:alertanimated:truecompletion:nil];

}


- (void)viewDidLoad {

    [superviewDidLoad];

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

    self.mutableString=[[NSMutableStringalloc]init];

    

    self.label=[[UILabelalloc]initWithFrame:CGRectMake(45,60,320,100)];

    self.label.backgroundColor=[UIColorgrayColor];

    self.label.layer.masksToBounds = YES ;//label的边框设置圆角

    self.label.layer.cornerRadius = 20// 设置圆角大小

    self.label.text=@"0";

    self.label.font=[UIFontsystemFontOfSize:30];

    self.label.textColor=[UIColorwhiteColor];

    [self.viewaddSubview:self.label];

    

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

    {

        for(int j=0;j<4;j++)

        {

            if(j+i*4>9)

            {

                break;

            }

            else

            {

                self.button1=[[UIButtonalloc]initWithFrame:CGRectMake(45+j*80,200+i*100,80,50)];

//                self.button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];

                self.button1.layer.masksToBounds =YES ;//button1的边框设置圆角

                self.button1.layer.cornerRadius =20 ;

                [self.button1setTitle:[NSStringstringWithFormat:@"%d",j+i*4]forState:UIControlStateNormal];

                self.button1.titleLabel.font=[UIFontsystemFontOfSize:40];

                [self.button1setBackgroundColor:[UIColorblueColor]];

                [self.button1setTitleColor:[UIColorwhiteColor]forState:UIControlStateHighlighted];

                self.button1.showsTouchWhenHighlighted=YES;

                [self.viewaddSubview:self.button1];

                [self.button1addTarget:selfaction:@selector(shuzi:)forControlEvents:UIControlEventTouchUpInside];

            }

        }

    }

    

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

        self.button2=[[UIButtonalloc]initWithFrame:CGRectMake(45+i*80,500,80,50)];

//        self.button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];

        self.button2.layer.masksToBounds = YES ;//button2的边框设置圆角

        self.button2.layer.cornerRadius = 20 ;

        if(i==0)

        {

            [self.button2setTitle:@"+"forState:UIControlStateNormal];

        }

        else if(i==1)

        {

            [self.button2setTitle:@"-"forState:UIControlStateNormal];

        }

        else if(i==2)

        {

            [self.button2setTitle:@"×"forState:UIControlStateNormal];

        }

        else if(i==3)

        {

            [self.button2setTitle:@"÷"forState:UIControlStateNormal];

        }

        self.button2.titleLabel.font=[UIFontsystemFontOfSize:40];

        [self.button2setBackgroundColor:[UIColorblueColor]];

        [self.button2setTitleColor:[UIColorwhiteColor]forState:UIControlStateHighlighted];

        self.button2.showsTouchWhenHighlighted=YES;

        [self.viewaddSubview:self.button2];

        [self.button2addTarget:selfaction:@selector(yunsuan:)forControlEvents:UIControlEventTouchUpInside];

    }

    

    self.button3=[[UIButtonalloc]initWithFrame:CGRectMake(205,400,80,50)];

//    self.button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.button3.layer.masksToBounds = YES ;//button3的边框设置圆角

    self.button3.layer.cornerRadius = 20 ;

    [self.button3setBackgroundColor:[UIColorblueColor]];

    [self.button3setTitle:@"."forState:UIControlStateNormal];

    self.button3.titleLabel.font=[UIFontsystemFontOfSize:40];

    [self.button3setTitleColor:[UIColorwhiteColor]forState:UIControlStateHighlighted];

    self.button3.showsTouchWhenHighlighted=YES;

    [self.viewaddSubview:self.button3];

    [self.button3addTarget:selfaction:@selector(shuzi:)forControlEvents:UIControlEventTouchUpInside];

    

    self.button4=[[UIButtonalloc]initWithFrame:CGRectMake(285,400,80,50)];

//    self.button4=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.button4.layer.masksToBounds = YES ;//button4的边框设置圆角

    self.button4.layer.cornerRadius = 20 ;

    [self.button4setBackgroundColor:[UIColorblueColor]];

    [self.button4setTitle:@"="forState:UIControlStateNormal];

    self.button4.titleLabel.font=[UIFontsystemFontOfSize:40];

    [self.button4setTitleColor:[UIColorwhiteColor]forState:UIControlStateHighlighted];

    self.button4.showsTouchWhenHighlighted=YES;

    [self.viewaddSubview:self.button4];

    [self.button4addTarget:selfaction:@selector(equal:)forControlEvents:UIControlEventTouchUpInside];

    

    self.button5=[[UIButtonalloc]initWithFrame:CGRectMake(45,600,160,50)];

//    self.button5=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.button5.layer.masksToBounds = YES ;//button5的边框设置圆角

    self.button5.layer.cornerRadius = 20 ;

    [self.button5setBackgroundColor:[UIColorblueColor]];

    [self.button5setTitle:@"退后"forState:UIControlStateNormal];

    self.button5.titleLabel.font=[UIFontsystemFontOfSize:40];

    [self.button5setTitleColor:[UIColorwhiteColor]forState:UIControlStateHighlighted];

    self.button5.showsTouchWhenHighlighted=YES;

    [self.viewaddSubview:self.button5];

    [self.button5addTarget:selfaction:@selector(tuihou:)forControlEvents:UIControlEventTouchUpInside];

    

    self.button6=[[UIButtonalloc]initWithFrame:CGRectMake(205,600,160,50)];

//    self.button6=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.button6.layer.masksToBounds = YES ;//button6的边框设置圆角

    self.button6.layer.cornerRadius = 20 ;

    [self.button6setBackgroundColor:[UIColorblueColor]];

    [self.button6setTitle:@"清除"forState:UIControlStateNormal];

    self.button6.titleLabel.font=[UIFontsystemFontOfSize:40];

    [self.button6setTitleColor:[UIColorwhiteColor]forState:UIControlStateHighlighted];

    self.button6.showsTouchWhenHighlighted=YES;

    [self.viewaddSubview:self.button6];

    [self.button6addTarget:selfaction:@selector(qingchu:)forControlEvents:UIControlEventTouchUpInside];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


本人申明:

       本文为本人的原创文章,未经本人允许不得转载;


本人也是刚开始学ios开发,属于初学者,望各位大神能多给点建议,提出一些程序的不足或需要改进、完善的地方,欢迎发俺的邮箱:854306566@qq.com(非诚勿扰哦O.o)。


1 0