iOS小型计算器

//

//  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小型计算器

时间: 2024-08-24 23:08:23

iOS小型计算器的相关文章

iOS之计算器

// //  zyAppDelegate.h //  Calculator // //  Created by nimami on 15/7/20. //  Copyright (c) 2015年 zhiyou. All rights reserved. // #import <UIKit/UIKit.h> @interface zyAppDelegate : UIResponder <UIApplicationDelegate> { int a,b,c; NSString * a

iOS 收款计算器算法

一个收款计算器算法,从之前高仿有赞Demo里面抽离的一个界面 demo 在这里 https://github.com/L-vinCent/calculView_function 显示计算记录 不能连续输入俩个计算符号 小数点的位数不能超过俩位 单个小数点的时候不能输入 00 点击 + ,直接显示计算结果,正则匹配计算 设置最大显示金额 - (IBAction)calculate:(UIButton *)sender { UIButton *btn=(UIButton *)sender; NSAr

C指针原理(10)-编译原理-小型计算器实现

.打开cygwin,进入home目录,home目录在WINDOWS系统的cygwin安装目录映射为home目录. 2.首先,在home目录中新建文件夹,在文件夹中放置如下内容的test1.l /*统计字数*/ %{ int?chars=0; int?words=0; int?lines=0; %} %% [a-zA-Z]+??{words++;chars+=strlen(yytext);} \n??{chars++;lines++;} .???{chars++;} %% main(int?arg

C指针原理(11)-编译原理-小型计算器实现

我们接着完善这个计算器程序,让算式能显示出来,修改calculator.l 我们接着完善这个计算器程序,让算式能显示出来,修改calculator.l 通过加入printf语句,打印词法分析器解析到的字符.比如 : .................. [0-9]+?{yylval=atoi(yytext);printf("%d",yylval);return?NUMBER;} \n??{return?EOL;} [?\t]?/blank/ .?/invalid?char/ %% 然后

c 语言简单计算器源码

//  main.c //  计算器 //  Created by qianfeng on 14-7-15. //  Copyright (c) 2014年 ___FGY___. All rights reserved. //iPhone自带计算器不够好,由于你技术出众,你被安排去开发一款iOS新式计算器. /*项目经理认为计算器第一版要支持表达式求值,所以要求如下: 输入任意表达式 求出他的值(支持负数,不支持小数) 这里支持6种表达式 () * / + - ()优先级最高, * /优先级其次

二道shell面试题

1.按照给出的运行结果,编写一个名为xunhuan 的shell过程(用循环语句). 0 10 210 3210 43210 543210 6543210 76543210 876543210 2.编写一个名为cala的shell过程,其功能是小型计算器,可以进行两数加.减.乘.除运算.两操作数和运算符由位置参数给出(位置参数1和3分别是两操作数,位置参数2是运算符),并将四种运行结果追加存入cal文件中. 一: A=""; for i in `seq 0 8`; do A=$i$A

node第二天

node第二天 1.0各种shell: 1.1什么是shell: 就是系统内核的一层壳,作用是保护内核同时传递人与计算机交互的信息.它只是系统的一个工具,我们可以使用它来操作计算器. 1.2常用的shell: 1)CMD: windows + r  打开面板中输入cmd回车 特点:很多都是window下面的指令 2)powerShell: 特点:它能够兼容windows和 (linux ,uinx)下面的指令 Osx -> unix 3)git bash: 特点:它能够兼容windows和 *i

nodejs的第二天学习

一. Shell: 1) 常用的shell a) CMD: window+r 打开面板中输入cmd 回车   特点:很多都是window下面的指令 b) powerShell:   特点:它能够兼容window 和 (Linux,uinx)下面的指令 Osx -? uinx c) git bash 特点:它能够兼容window和*inux(linux,uinx)下面的指令 2)常用的shell指令 a)cd:切换路径 (1)cd ./ 当前目录 (2)cd ../ 回到上级目录 (3)cd a

你很熟悉CSS,却没掌握这些CSS技巧

转载来自 http://www.html5cn.org/article-9294-1.html 做前端开发的人都很熟悉CSS,一个漂亮的网页由HTML标签和控制这些标签布局的CSS组成,因此CSS在开发中起到功不可没的作用,在我们频繁使用CSS过程中掌握一些技巧是必须的,本文分享了22个方便且很重要的CSS技巧,值得收藏! 混合模式 目前,Firefox 和 Safari 开始支持混合模式,就像 Photoshop 一样.Chrome 和 Opera 也支持,只是有些差异. 你可以创建不同的样式