zoukankan      html  css  js  c++  java
  • 计算器

    #import "MainViewController.h"

    #define kScreenWidth CGRectGetWidth([[UIScreen mainScreen] bounds])

    #define kScreenHeight CGRectGetHeight([[UIScreen mainScreen] bounds])

    #define kButtonWidth ((kScreenWidth - 5) / 4)

    #define kButtonHeight kButtonWidth


    #define kButtonOffsetX 1

    #define kButtonOffsetY 1

    #define kButtonOriginY (kScreenHeight - (kButtonHeight + kButtonOffsetY) * 5)

    @interface MainViewController ()


    @property (nonatomic,retain)UILabel *displayLabel;

    @property (nonatomic, assign) BOOL isFuhao;

    @property (nonatomic, retain) NSMutableArray *shu;

    @property (nonatomic, retain) NSMutableArray *yunsuanfu;


    @end

    @implementation MainViewController


    -(void)dealloc{

        [_displayLabel release];

        [_yunsuanfu release];

        [_shu release];

        [super dealloc];

    }


    -(NSMutableArray *)shu{

        if (!_shu) {

            self.shu = [NSMutableArray array];

        }

        return _shu;

    }


    -(NSMutableArray *)yunsuanfu{

        if (!_yunsuanfu) {

            self.yunsuanfu = [NSMutableArray array];

        }

        return _yunsuanfu;

    }


    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        self.view.backgroundColor = [UIColor blackColor];

        self.displayLabel =[[[UILabel alloc]initWithFrame:CGRectMake(10, kButtonOriginY-120, kScreenWidth-20, 120)]autorelease];

        

        self.displayLabel.font=[UIFont systemFontOfSize:100];//设置字体大小

        self.displayLabel.textColor=[UIColor whiteColor];  //设置文本颜色

        self.displayLabel.textAlignment=NSTextAlignmentRight //设置向右对齐

        self.displayLabel.text=@"0";    //设置文本为0

        self.displayLabel.minimumScaleFactor=0.4//最小缩放比例

        self.displayLabel.adjustsFontSizeToFitWidth=YES;

        [self.view addSubview:self.displayLabel];

       

        /*

         AC +- % /

         7  8  9 *

         4  5  6 -

         1  2  3 +

         0     . =

         */

         NSArray *titles = @[@[@"AC", @"+-", @"baifenhao", @"chuhao"], @[@"7", @"8", @"9", @"chenghao"], @[@"4", @"5", @"6", @"jianhao"], @[@"1", @"2", @"3", @"jiahao"], @[@"0",@"", @"dian", @"dengyu"]];

        

        for (int i = 0; i < titles.count; i++) {

            NSArray *subTitles = titles[i];

            for (int j = 0; j < subTitles.count; j++) {

                NSString *title = subTitles[j];

                if ([title isEqualToString:@""]) {

                    continue;

                }

                

       UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];


       CGRect frame = CGRectMake(kButtonOffsetX + (kButtonWidth + kButtonOffsetX) * j, kButtonOriginY + (kButtonHeight + kButtonOffsetY) * i, kButtonWidth, kButtonHeight);

           if ([title isEqualToString:@"0"]) {

            frame.size.width = frame.size.width * 2 + kButtonOffsetX;

     

             }

                

                aButton.frame = frame;


           NSString *normalImageName = [subTitles[j] stringByAppendingString:@"_btn_nor"];

          NSString *highlightedImageName = [subTitles[j] stringByAppendingString:@"_btn_highlighted"];

           [aButton setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];

                [aButton setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];

               

    [aButton setTitle:title forState:UIControlStateNormal]; 

        [aButton addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];

                

                [self.view addSubview:aButton];


                     }

        }

    }

     /*

     主要功能:实现计算

     输入:通过点击button输入

     输出:label

     

     */


    -(void)handleButtonAction:(UIButton *)sender{

        NSString *title = [sender titleForState:UIControlStateNormal];

        NSArray *numbers = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];

        NSArray *opeartion = @[@"jiahao",@"jianhao",@"chenghao",@"chuhao",@"dengyu"];

        if ([numbers containsObject:title]) {

            if ([self.displayLabel.text isEqual:@"0"] || self.isFuhao == YES) {

                self.displayLabel.text = title;

            }

            else {

                self.displayLabel.text = [self.displayLabel.text stringByAppendingString:title];

            }

            self.isFuhao = NO;

        }

        

        if ([title isEqual:@"dian"]) {

        //一个数字中有且只有一个

            if (![self.displayLabel.text containsString:@"."]) {

                self.displayLabel.text = [self.displayLabel.text stringByAppendingString:@"."];

            }

        }  

        if ([opeartion containsObject:title]) {

            [self.yunsuanfu addObject:title];

            [self.shu addObject:self.displayLabel.text];

            self.isFuhao = YES;

            

            //按下等于号开始计算所有的算式

       if ([title isEqual:@"dengyu"]) {

                //乘除

         for (int i = 0; i < self.yunsuanfu.count; i++) {

            if ([self.yunsuanfu[i] isEqual:@"chenghao"] || [self.yunsuanfu[i] isEqual:@"chuhao"]) {

              CGFloat num1 = [self.shu[i] floatValue];

              CGFloat num2 = [self.shu[i + 1] floatValue];

              CGFloat result;

              [self.shu removeObjectAtIndex:i];

              [self.shu removeObjectAtIndex:i];

                        

            if ([self.yunsuanfu[i] isEqual:@"chenghao"]) {

                     result = num1 * num2;

              }

                 else{

                   if (num2 == 0) {

                     self.displayLabel.text = @"";

                          return;

                    }

                       result = num1 / num2;

                }

        [self.shu insertObject:[NSString stringWithFormat:@"%g",result] atIndex:i];

        [self.yunsuanfu removeObjectAtIndex:i];

                 i--;

         }

           //加减

         for (int i = 0; i < self.yunsuanfu.count; i++) {

             if ([self.yunsuanfu[i] isEqual:@"jiahao"] || [self.yunsuanfu[i] isEqual:@"jianhao"]) {

                  CGFloat num1 = [self.shu[i] floatValue];

                  CGFloat num2 = [self.shu[i + 1] floatValue];

                  CGFloat result;

                  [self.shu removeObjectAtIndex:i];

                  [self.shu removeObjectAtIndex:i];

                        

              if ([self.yunsuanfu[i] isEqual:@"jiahao"]) {

                        result = num1 + num2;

                  }

                    else{

                        result = num1 - num2;

                    }

         [self.shu insertObject:[NSString stringWithFormat:@"%g",result] atIndex:i];

               [self.yunsuanfu removeObjectAtIndex:i];

                       i--;

                }

            }

                self.displayLabel.text = self.shu.lastObject;

                [self.shu removeAllObjects];

                [self.yunsuanfu removeAllObjects];

            }

        }

                    if ([title isEqual:@"AC"]) {

                [self.shu removeAllObjects];

                [self.yunsuanfu removeAllObjects];

                self.displayLabel.text = @"0";

                self.isFuhao = NO;

           }

        

           if ([title isEqual:@"+-"]) {


              if ([self.displayLabel.text hasPrefix:@"-"]) {

                self.displayLabel.text = [self.displayLabel.text substringFromIndex:1];

            }

            else {

                NSString *a = @"-";

                self.displayLabel.text = [a stringByAppendingString:self.displayLabel.text];

            }

        }

        

          if ([title isEqual:@"baifenhao"]) {

              CGFloat num = [self.displayLabel.text floatValue];

                  num = num / 100;

                self.displayLabel.text = [NSString stringWithFormat:@"%g",num];

        }

    }



  • 相关阅读:
    【C#技术】一篇文章搞掂:LLBL
    【前端技术】一篇文章搞掂:JS
    dapper 分页根据时间条件查询时中的一个坑
    后台页面常用模板
    JMeter强大的性能测试工具
    批量生成xml文件数据C#实现
    asp.net mvc中用 log4net记录日志到数据库中
    asp.net mvc model attribute and razor and form and jquery validate 完美结合
    前端素材网站
    原生dapper中新增用户后根据用户id,在用户角色表中添加关联数据,事务处理
  • 原文地址:https://www.cnblogs.com/jyd1992/p/4907878.html
Copyright © 2011-2022 走看看