[iOS开发日记]简易计算器

好久没写了,今天来一篇之前写的计算器的小Demo

当时还没有学UICollectionView,于是这个界面全部是有label和button写出来的

下面上代码

这是.h文件里面写的

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    UILabel *_label;//显示框
    int     a;//数字的tag
    int     b;//运算符的tag
    float     result;//结果
    BOOL    flag;//判断是非按等号
}


@end

这是.m文件里面写的

  1 #import "ViewController.h"
  2 #define HEIGHT     [[UIScreen mainScreen] bounds].size.height
  3 #define WIDTH      [[UIScreen mainScreen] bounds].size.width
  4 @interface ViewController ()
  5 
  6 @end
  7 
  8 @implementation ViewController
  9 
 10 - (void)viewDidLoad {
 11     [super viewDidLoad];
 12     [self creat];
 13     // Do any additional setup after loading the view, typically from a nib.
 14 }
 15 - (void)creat{
 16     UIImage *num001 = [UIImage imageNamed:@"001.png"];
 17     UIImage *num002 = [UIImage imageNamed:@"002.png"];
 18     UIImage *num101 = [UIImage imageNamed:@"101.png"];
 19     UIImage *num102 = [UIImage imageNamed:@"102.png"];
 20     UIImage *num201 = [UIImage imageNamed:@"201.png"];
 21     UIImage *num202 = [UIImage imageNamed:@"202.png"];
 22     UIImage *num301 = [UIImage imageNamed:@"301.png"];
 23     UIImage *num302 = [UIImage imageNamed:@"302.png"];
 24     UIImage *num401 = [UIImage imageNamed:@"401.png"];
 25     UIImage *num402 = [UIImage imageNamed:@"402.png"];
 26     UIImage *num501 = [UIImage imageNamed:@"501.png"];
 27     UIImage *num502 = [UIImage imageNamed:@"502.png"];
 28     UIImage *num601 = [UIImage imageNamed:@"601.png"];
 29     UIImage *num602 = [UIImage imageNamed:@"602.png"];
 30     UIImage *num701 = [UIImage imageNamed:@"701.png"];
 31     UIImage *num702 = [UIImage imageNamed:@"702.png"];
 32     UIImage *num801 = [UIImage imageNamed:@"801.png"];
 33     UIImage *num802 = [UIImage imageNamed:@"801.png"];
 34     UIImage *num901 = [UIImage imageNamed:@"901.png"];
 35     UIImage *num902 = [UIImage imageNamed:@"902.png"];
 36     
 37     
 38     UIImage *calj01 = [UIImage imageNamed:@"j01.png"];
 39     UIImage *calj02 = [UIImage imageNamed:@"j02.png"];
 40     UIImage *caljj01 = [UIImage imageNamed:@"jj01.png"];
 41     UIImage *caljj02 = [UIImage imageNamed:@"jj02.png"];
 42     UIImage *calc01 = [UIImage imageNamed:@"c01.png"];
 43     UIImage *calc02 = [UIImage imageNamed:@"c02.png"];
 44     UIImage *calcc01 = [UIImage imageNamed:@"cc01.png"];
 45     UIImage *calcc02 = [UIImage imageNamed:@"cc02.png"];
 46     UIImage *cald01 = [UIImage imageNamed:@"d01.png"];
 47     UIImage *cald02 = [UIImage imageNamed:@"d02.png"];
 48     UIImage *caldd = [UIImage imageNamed:@"dd01.png"];
 49     
 50     
 51     
 52     
 53     self.view.backgroundColor = [UIColor whiteColor];
 54     _label = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, WIDTH - 40, 80)];
 55     _label.backgroundColor = [UIColor blackColor];
 56     _label.textColor = [UIColor whiteColor];
 57     _label.textAlignment = NSTextAlignmentRight;
 58     _label.font = [UIFont systemFontOfSize:30];
 59     _label.lineBreakMode = NSLineBreakByTruncatingHead;
 60     _label.text = @"0";
 61     [self.view addSubview:_label];
 62     
 63     UIButton *clearBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 130, (WIDTH-55)/4, (WIDTH-55)/4)];
 64     [clearBtn setTitle:@"C" forState:UIControlStateNormal];
 65 
 66     [clearBtn setImage:[UIImage imageNamed:@"ccc01.png"] forState:UIControlStateNormal];
 67     clearBtn.tag = 1;
 68     [clearBtn addTarget:self action:@selector(deleteInfo:) forControlEvents:UIControlEventTouchUpInside];
 69     [self.view addSubview:clearBtn];
 70     
 71     UIButton *deleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-100, 130, (WIDTH-55)/4, (WIDTH-55)/4)];
 72     deleteBtn.tag = 2;
 73     [deleteBtn setTitle:@"<-" forState:UIControlStateNormal];
 74 
 75     [deleteBtn setImage:[UIImage imageNamed:@"t02.png"] forState:UIControlStateNormal];
 76     [deleteBtn setImage:[UIImage imageNamed:@"t01.png"] forState:UIControlStateHighlighted];
 77     [deleteBtn addTarget:self action:@selector(deleteInfo:) forControlEvents:UIControlEventTouchUpInside];
 78     [self.view addSubview:deleteBtn];
 79     NSArray *arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"+",@"4",@"5",@"6",@"-",@"7",@"8",@"9",@"x",@".",@"0",@"=",@"/", nil];
 80     NSArray *back01 = @[num101,num201,num301,calj02,num401,num501,num601,caljj02,num701,num801,num901,calc02,caldd,num001,cald02,calcc02];
 81     NSArray *back02 = @[num102,num202,num302,calj01,num402,num502,num602,caljj01,num702,num802,num902,calc01,caldd,num002,cald01,calcc01];
 82     for (int i = 0; i< arr.count; i++) {
 83         UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(20+i%4*((WIDTH-55)/4 + 5), 220+i/4 * ((WIDTH-55)/4 +5), (WIDTH-55)/4, (WIDTH-55)/4)];
 84         btn.tag = i+1;
 85         [btn setImage:[back01 objectAtIndex:i] forState:UIControlStateNormal];
 86         [btn setImage:[back02 objectAtIndex:i] forState:UIControlStateHighlighted];
 87         [btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
 88         [btn addTarget:self action:@selector(manage:) forControlEvents:UIControlEventTouchUpInside];
 89         [self.view addSubview:btn];
 90         
 91     }
 92     flag = NO;
 93     a = 0;
 94 }
 95 -(void)deleteInfo:(UIButton *)btn{
 96     if (btn.tag == 1) {
 97         _label.text = @"0";
 98         a = 0;
 99     }else{
100         if (_label.text.length == 1) {
101             _label.text = @"0";
102             return;
103         }
104         _label.text = [_label.text substringToIndex:_label.text.length-1];
105     }
106 }
107 -(void)manage:(UIButton *)btn{
108     if (btn.tag%4 == 0||btn.tag == 15) {
109         flag = YES;
110         b = a;
111         a = (int)btn.tag;
112         //  NSLog(@"%f",b);
113         if (b == 0) {
114             result = [_label.text floatValue];
115         }else{
116             switch (b/4) {
117                 case 1:
118                     result  += [_label.text floatValue];
119                     break;
120                 case 2:
121                     result  -= [_label.text floatValue];
122                     break;
123                 case 3:
124                     result  *= [_label.text floatValue];
125                     break;
126                 case 4:
127                     result  /= [_label.text floatValue];
128                     break;                default:
129                     
130                     break;
131             }
132             
133         }
134         if (result > (int)result) {
135             _label.text = [NSString stringWithFormat:@"%.2f",result];
136         }else{
137             _label.text = [NSString stringWithFormat:@"%d",(int)result];
138         }
139         if (btn.tag == 15) {
140             result = 0;
141             a = 0;
142         }
143         
144     }else if(btn.tag != 13){
145         if ([_label.text isEqualToString:@"0"]) {
146             _label.text = @"";
147         }
148         if (flag == YES) {
149             flag = NO;
150             _label.text = @"";
151         }
152         if (btn.tag == 14) {
153             _label.text = [_label.text stringByAppendingString:@"0"];
154         }else{
155             _label.text = [_label.text stringByAppendingFormat:@"%zi",btn.tag - btn.tag/4];
156         }
157     }else{
158         NSRange range = [_label.text rangeOfString:@"."];
159         if (range.location == NSNotFound) {
160             _label.text = [_label.text stringByAppendingString:@"."];
161         }
162     }
163 }
164 - (void)didReceiveMemoryWarning {
165     [super didReceiveMemoryWarning];
166     // Dispose of any resources that can be recreated.
167 }
168 
169 @end

这就是全部的内容了,希望能够帮到有用的同学们

这个没有实现里四则运算,只能在按第二个运算符的时候计算出上一个运算符元算出来的结果在进行下一步计算

如果有错误的地方,希望大家指出,小弟感激不尽

下面给一个Demo的下载地址:https://github.com/sosoneo/Calculator

posted @ 2015-08-24 15:38  0error  阅读(1047)  评论(2编辑  收藏  举报