//

//  ViewController.m

//  计算器

//屏幕的宽和高

#define SCREEN_W self.view.frame.size.width

#define SCREEN_H self.view.frame.size.height

#import "ViewController.h"

@interface ViewController ()

//用于存储输入的第一个数字

@property (nonatomic,assign) CGFloat num1;

//用于存储输入的第二个数字

@property (nonatomic,assign) CGFloat num2;

//用于存储最终结果

@property (nonatomic,assign)  CGFloat numResult;

//用于判断符号的标记

@property (nonatomic,assign) NSInteger tag;

//显示屏

@property (nonatomic,strong) UILabel *screen;

//

@property (nonatomic,strong) NSMutableString *str;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//开辟空间

_str = [[NSMutableString alloc]init];

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

#pragma mark - screen

//先创建一个标签,用于显示

_screen = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-400-5*10)];

//设置屏幕的字体颜色

[_screen setTextColor:[UIColor whiteColor]];

//设置屏幕的背景颜色

_screen.backgroundColor = [UIColor blackColor];

//设置屏幕的字体大小

_screen.font = [UIFont systemFontOfSize:40];

//设置屏幕的字体自适应

_screen.adjustsFontSizeToFitWidth = YES;

//设置字体的对齐方式

_screen.textAlignment = NSTextAlignmentRight;

//清零

_screen.text = [NSMutableString stringWithString:@"0"];

[self.view addSubview:_screen];

#pragma mark - buttonwith+_*/

NSArray *signArr = @[@"÷",@"x",@"-",@"+"];

for (int i = 0; i < [signArr count]; i++) {

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

//设置按钮的文字

[btn setTitle:signArr[i] forState:UIControlStateNormal];

//设置按钮的位置和大小

btn.frame = CGRectMake(((SCREEN_W-30)/4.0)*3.0+30, _screen.frame.size.height+90*(i%4)+10, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

btn.backgroundColor = [UIColor orangeColor];

//设置按钮字体的大小

btn.titleLabel.font = [UIFont systemFontOfSize:40];

//给每个按钮添加事件

[btn addTarget:self action:@selector(calculate:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

#pragma mark - 单独添加“=”

UIButton *goBtn = [UIButton buttonWithType:UIButtonTypeSystem];

//设置按钮的大小和位置

goBtn.frame = CGRectMake(((SCREEN_W-30)/4.0)*3.0+30, _screen.frame.size.height+90*4+10, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

goBtn.backgroundColor = [UIColor orangeColor];

//设置按钮的文字

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

//设置文字的大小

goBtn.titleLabel.font = [UIFont systemFontOfSize:40];

//为每个按钮添加事件

[goBtn addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:goBtn];

#pragma mark - number

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

for (int i = 0 ; i < [numArr count]; i++) {

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮的大小和位置

btn.frame = CGRectMake(((SCREEN_W-30)/4.0+10)*(i%3), _screen.frame.size.height+90*(i/3)+90+10, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

btn.backgroundColor = [UIColor lightGrayColor];

//设置按钮的文字

[btn setTitle:numArr[i] forState:UIControlStateNormal];

//设置文字的大小

btn.titleLabel.font = [UIFont systemFontOfSize:40];

//设置文字的颜色

[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//为每个按钮添加事件

[btn addTarget:self action:@selector(expressNum:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

}

#pragma mark - 添加清除键

UIButton *cleanBtn = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮的大小和位置

cleanBtn.frame = CGRectMake(0, _screen.frame.size.height+10, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

cleanBtn.backgroundColor = [UIColor lightGrayColor];

//设置按钮的文字

[cleanBtn setTitle:@"AC" forState:UIControlStateNormal];

//设置文字的大小

cleanBtn.titleLabel.font = [UIFont systemFontOfSize:40];

//设置文字的颜色

[cleanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//为每个按钮添加事件

[cleanBtn addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:cleanBtn];

#pragma mark - 单独添加0

UIButton *btn0 = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮的大小和位置

btn0.frame = CGRectMake(0, SCREEN_H - 80, (SCREEN_W-30)/4.0*2+10, 80);

//设置按钮的背景颜色

btn0.backgroundColor = [UIColor lightGrayColor];

//设置按钮的文字

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

//设置文字的大小

btn0.titleLabel.font = [UIFont systemFontOfSize:40];

//设置文字的颜色

[btn0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//为每个按钮添加事件

[btn0 addTarget:self action:@selector(expressNum:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn0];

#pragma mark - 单独添加百分号%

UIButton *btnPrecent = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮的大小和位置

btnPrecent.frame = CGRectMake(((SCREEN_W-30)/4.0+10)*2, _screen.frame.size.height+10, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

btnPrecent.backgroundColor = [UIColor lightGrayColor];

//设置按钮的文字

[btnPrecent setTitle:@"%" forState:UIControlStateNormal];

//设置文字的大小

btnPrecent.titleLabel.font = [UIFont systemFontOfSize:40];

//设置文字的颜色

[btnPrecent setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//为每个按钮添加事件

[btnPrecent addTarget:self action:@selector(precent:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnPrecent];

#pragma mark - 单独添加点"."

UIButton *btnPoint = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮的大小和位置

btnPoint.frame = CGRectMake(btn0.frame.size.width+10, SCREEN_H-80, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

btnPoint.backgroundColor = [UIColor lightGrayColor];

//设置按钮的文字

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

//设置文字的大小

btnPoint.titleLabel.font = [UIFont systemFontOfSize:40];

//设置文字的颜色

[btnPoint setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//为每个按钮添加事件

[btnPoint addTarget:self action:@selector(expressNum:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnPoint];

#pragma mark - 单独添加点"+/-"

UIButton *btnSign = [UIButton buttonWithType:UIButtonTypeCustom];

//设置按钮的大小和位置

btnSign.frame = CGRectMake(cleanBtn.frame.size.width+10, _screen.frame.size.height+10, (SCREEN_W-30)/4.0, 80);

//设置按钮的背景颜色

btnSign.backgroundColor = [UIColor lightGrayColor];

//设置按钮的文字

[btnSign setTitle:@"+/-" forState:UIControlStateNormal];

//设置文字的大小

btnSign.titleLabel.font = [UIFont systemFontOfSize:40];

//设置文字的颜色

[btnSign setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//为每个按钮添加事件

[btnSign addTarget:self action:@selector(signNum:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnSign];

}

-(void) expressNum:(UIButton *)sender

{

//先判断首字母是不是符号

if ([_str hasPrefix:@"+"]||[_str hasPrefix:@"-"]||[_str hasPrefix:@"x"]||[_str hasPrefix:@"÷"]) {

//如果是,就清零

_str = [NSMutableString stringWithString:@""];

}

//连续追加数字

[_str appendString:sender.currentTitle];

_screen.text = [NSString stringWithString:_str];

//将输入的第一个数字保存在num1中

_num1 = [_screen.text doubleValue];

//用于调试

//NSLog(@"-----%ld",_num1);

}

//判断符号

-(void)calculate:(UIButton *)sender

{

//先清零

_str = [NSMutableString stringWithString:@""];

//再重新追加符号

[_str appendString:sender.currentTitle];

//显示(这个可写可不写)

_screen.text = [NSString stringWithString:_str];

if ([_str hasPrefix:@"+"]) {

_num2 = _num1;

_tag = 1;

}else if ([_str hasPrefix:@"-"]){

_num2 = _num1;

_tag = 2;

}else if ([_str hasPrefix:@"x"]) {

_num2 = _num1;

_tag = 3;

}else if ([_str hasPrefix:@"÷"]) {

_num2 = _num1;

_tag = 4;

}

}

//计算结果

-(void)go:(UIButton *)sender

{

switch (_tag) {

case 1:

_numResult = _num2 + _num1;

break;

case 2:

_numResult = _num2 - _num1;

break;

case 3:

_numResult = _num2 * _num1;

break;

case 4:

_numResult = _num2 / _num1;

break;

}

_screen.text = [NSString stringWithFormat:@"%lg",_numResult];

_num1 = _numResult;

}

//清除

-(void)clean:(UIButton *)sender

{

_str = [NSMutableString stringWithString:@""];

_screen.text = @"0";

_num1 = 0;

_num2 = 0;

_numResult = 0;

}

//百分号

-(void)precent:(UIButton *)sender

{

_num1 = _num1 * 0.01;

//注意,这里要以最简形式(占用宽度最小)输出,并且不会输出无意义的0

_screen.text = [NSString stringWithFormat:@"%lg",_num1];

}

//正负号

-(void)signNum:(UIButton *)sender

{

//先判断数字是否大于0,如果是,就变负

if (_num1 >= 0) {

//添加负号

[_str insertString:@"-" atIndex:0];

_num1 = [_str doubleValue];

_screen.text = [NSString stringWithFormat:@"%lg",_num1];

}

//如果数字小于0,那么变为正数

else if (_num1 < 0)

{

//删除负号

[_str deleteCharactersInRange:NSMakeRange(0, 1)];

_num1 = [_str doubleValue];

_screen.text = [NSString stringWithFormat:@"%lg",_num1];

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

//大家看了之后觉得哪里代码可以更加优化,或者功能上有什么补充的,麻烦评论下,感谢了

iOS小型计算器的更多相关文章

  1. IOS之计算器实现

    本文利用ios实现计算器app,后期将用mvc结构重构 import UIKit class CalculViewController: UIViewController { @IBOutlet we ...

  2. IOS实现小型计算器

    作为一名初学者,编辑一款能够在IOS操作系统上运行的计算器是一件很值得自豪的事情,网络上虽然后很多相关的文章和代码,功能也很强大但是我感觉相关的计算器比加复杂,晦涩难懂,所以我想通过这个小小的计算器, ...

  3. IOS OC 计算器算法(不考虑优先级)

    个人见解:为还在计算器算法方面迷惑的同学一个数据处理解决方案:定义一个可变数组array,一个可变字符串str,使字符通过[array addObject:str];方法添加到可变数组,每当触发运算符 ...

  4. ios小型服务器环境配置

    之前买的一台二手iphone4退役了,上闲鱼上一看,就值200,而且耳机声音也有点轻,估计买不了什么钱 于是网上看看能不能有什么废物利用的法子,看到说做行车记录仪的,有说做git服务器的,感觉挺有兴趣 ...

  5. iOS 收款计算器算法

    一个收款计算器算法,从之前高仿有赞Demo里面抽离的一个界面 demo 在这里 https://github.com/L-vinCent/calculView_function 显示计算记录 不能连续 ...

  6. html5 javascript 小型计算器

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. iOS 减法计算器

    一: 在界面上拖入相应的控件 二: 给每个控件设置关联 //监听按钮的点击 - (IBAction)compute:(id)sender; //第一个文本输入框的值 @property (weak, ...

  8. c 语言简单计算器源码

    //  main.c //  计算器 //  Created by qianfeng on 14-7-15. //  Copyright (c) 2014年 ___FGY___. All rights ...

  9. 使用Olami SDK 语音控制一个支持HomeKit的智能家居的iOS程序

    前言 HomeKit是苹果发布的智能家居平台.通过HomeKit组件,用户可以通过iphone.iPad和ipod Touch来控制智能灯泡,风扇.空调等支持HomeKit的智能家居,尤其是可以通过S ...

随机推荐

  1. Android上滑手势触发和不增加布局层级扩大点击区域

    最近项目中需要实现手势上滑或者点击滑出界面的效果,实现上是利用GestureDetector,然后在onFling中判断,但遇到一个问题:手势上滑是针对整个布局的,但如果有对单独的View设置点击监听 ...

  2. [AngularJS] AngularJS系列(1) 基础篇

    目录 什么是AngularJS? 为什么使用/ng特性 Hello World 内置指令 内置过滤器 模块化开发 一年前开始使用AngularJS(以后简称ng),如今ng已经出2了.虽说2已完全变样 ...

  3. .Net语言 APP开发平台——Smobiler学习日志:如何在webview中加载网页

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的“Smobiler Components” ...

  4. SQL Server 统计信息

    SELECT * FROM SYS.stats _WA_Sys_00000009_00000062:统计对象的名称.不同的机器名称不同,自动创建的统计信息都以_WA_Sys开头,00000009表示的 ...

  5. 使用HTML.ActionLink实现一个图片链接

    学习ASP.NET MVC 的Razor的语法,尝试把一段普能的图片链接<a ...><img ... />改为HTML.ActionLink实现. 最原始的代码: <a ...

  6. MVC之前的那点事儿系列(9):MVC如何在Pipeline中接管请求的?

    文章内容 上个章节我们讲到了,可以在HttpModules初始化之前动态添加Route的方式来自定义自己的HttpHandler,最终接管请求的,那MVC是这么实现的么?本章节我们就来分析一下相关的M ...

  7. 修复 XE8 Win 平台 Firemonkey Memo 卷动后会重叠的问题

    问题:XE8 Firemonkey 在 Windows 平台 Memo 卷动时,在第 1 , 2 行会产生重叠现象. 更新:XE8 update 1 已经修复这个问题,无需再使用下面方法. 修改前: ...

  8. 内存只有4G的MBP要怎么破

    开发工具包括浏览器都是极占内存的,没有个8G根本不行啊. 怎一个慢字了得? 补记: 放弃谷歌浏览器是正道

  9. servlet基本原理(手动创建动态资源+工具开发动态资源)

    一.手动开发动态资源 1 静态资源和动态资源的区别 静态资源: 当用户多次访问这个资源,资源的源代码永远不会改变的资源. 动态资源:当用户多次访问这个资源,资源的源代码可能会发送改变. <scr ...

  10. 利用Redis cache优化app查询速度实践

    注意:本篇文章译自speeding up existing app with a redis cache,如需要转载请注明出处. 发现问题 在应用解决方法之前,我们需要对我们面对的问题有一个清晰的认识 ...