[IOS development diary] simple calculator and ios development diary Calculator

Source: Internet
Author: User

[IOS development diary] simple calculator and ios development diary Calculator

I haven't written it for a long time. Today I am going to share a Demo of the calculator I wrote earlier.


Not yet learnedUICollectionView, so this interface is all written with labels and buttons.

The code below

This is written in the. h file.


#import <UIKit / UIKit.h>

@interface ViewController: UIViewController {
    UILabel * _label; // Display box
    int a; // Digital tag
    int b; // The tag of the operator
    float result; // Result
    BOOL flag; // Judge whether it is right or wrong
}


@end
This is written in the .m file

  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
This is all the content, I hope to help useful students

This does not implement the four operations in the calculation. Only the result of the previous operator element is calculated when the second operator is pressed. The next calculation is performed.

If there is something wrong, I hope everyone can point out that I am grateful

Here is a Demo: https://github.com/sosoneo/Calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.