iOS01 计算器


        #import "ViewController.h"  

        @interface ViewController ()
        {
            UILabel *_display;
            NSString *_operator1;
            NSString *_operator;
            NSString *_operator2;
        }
        @end  

        @implementation ViewController  

        - (void)viewDidLoad
        {
            [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
            //创建UI界面
            _display = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.view.frame.size.width-20, 100)];
            _display.backgroundColor = [UIColor grayColor];
            _display.alpha = 0.8;
            _display.textAlignment = NSTextAlignmentRight;
            _display.numberOfLines = 2;
            _display.text = @"";
            _operator = @"";
            _operator1 = @"";
            _operator2 = @"";
            [self.view addSubview:_display ];
            NSArray *titleArray1 = @[@"MC",@"M+",@"M-",@"MR",@"清除"];
            NSArray *titleArray2 = @[@[@"7",@"8",@"9",@"+"],@[@"4",@"5",@"6",@"-"],@[@"1",@"2",@"3",@"*"],@[@"0",@".",@"=",@"/"]];
            CGFloat size1 = (self.view.frame.size.width-60)/5;
            CGFloat size2 = (self.view.frame.size.width-50)/4;
            for (int i=0; i<5; i++)
            {
                CGRect frame = CGRectMake(10+i*(size1+10), 180, size1, 50);
                [self createButtonByFrame:frame withTitle:titleArray1[i]];
            }
            for (int i=0; i<4; i++)
            {
                for (int j=0; j<4; j++)
                {
                    [self createButtonByFrame:CGRectMake(10+j*(size2+10), 240+i*60, size2, 50) withTitle:titleArray2[i][j]];
                }
            }
        }  

        - (void)createButtonByFrame:(CGRect)frame withTitle:(NSString *)title
        {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            btn.frame = frame;
            btn.backgroundColor = [UIColor grayColor];
            btn.alpha = 0.8;
            btn.layer.cornerRadius = 10;
            [btn setTitle:title forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:25];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];  

            [self.view addSubview:btn];
        }  

        - (void)btnClicked:(UIButton *)btn
        {
            NSString *title = btn.currentTitle;
            if ([title hasPrefix:@"M"])
            {  

            }
            if ([title isEqualToString:@"清除"])
            {
                _display.text = @"";
                _operator = @"";
                _operator1 = @"";
                _operator2 = @"";
                return;
            }
            if ([self isOperator:title])
            {
                if (_operator.length)//判断操作运算符是否已经存在
                {
                    float result = 0.0;
                    float value1 = [_operator1 floatValue];
                    float value2 = [_operator2 floatValue];
                    unichar ch = [_operator characterAtIndex:0];
                    switch (ch)
                    {
                        case ‘+‘:
                            result = value1+value2;
                            break;
                        case ‘-‘:
                            result = value1-value2;
                            break;
                        case ‘*‘:
                            result = value1*value2;
                            break;
                        case ‘/‘:
                            result = value1/value2;
                            break;
                        default:
                            break;
                    }
                    _operator1 = [NSString stringWithFormat:@"%g",result];
                    _display.text = [NSString stringWithFormat:@"%@\n%@",_operator1,title];
                    _operator2 = @"";
                    if ([btn.currentTitle isEqualToString:@"="])
                    {
                        _operator = @"";
                    }
                    else
                    {
                        _operator = title;
                    }
                }
                else
                {
                    _operator = title;
                    _display.text = [NSString stringWithFormat:@"%@\n%@",_operator1,_operator];
                }
            }
            else//是操作数
            {
                if (_operator.length)//判断是哪个操作数
                {
                    if ([_operator2 containsString:@"."] && [title isEqualToString:@"."])
                    {  

                    }
                    else
                    {
                        _operator2 = [_operator2 stringByAppendingString:title];
                        _display.text = [NSString stringWithFormat:@"%@\n%@%@",_operator1,_operator,_operator2];
                    }
                }
                else
                {
                    if ([_operator1 containsString:@"."] && [title isEqualToString:@"."])
                    {  

                    }
                    else
                    {
                        _operator1 = [_operator1 stringByAppendingString:title];
                        _display.text = _operator1;
                    }
                }
            }
        }  

        //判断标题是否为运算符+ - * / =
        - (BOOL)isOperator:(NSString *)title
        {
            if ([[NSCharacterSet characterSetWithCharactersInString:@"+-*/="] characterIsMember:[title characterAtIndex:0]])
            {
                return YES;
            }  

            return NO;
        }
        - (void)didReceiveMemoryWarning {
            [super didReceiveMemoryWarning];
            // Dispose of any resources that can be recreated.
        }
        @end 
时间: 2024-10-12 04:50:50

iOS01 计算器的相关文章

【自动化__GUI自动化】__java__Windows应用程序识别__计算器

一.代码如下 package www.woniu.gui.one; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.aw

bzoj2242: [SDOI2011]计算器.

2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 4353  Solved: 1679[Submit][Status][Discuss] Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数. In

j2ee-JSP之简单计算器

来源韩顺平.j2ee视频实战教程jsp第1讲(下集) -------------------------------------------------------------------------------------------------------- 简单计算器,可以控制输入的数(仅第一个数)不能为空且不能为字符串 myCal.jsp代码 1 <!--这是计算器的界面 --> 2 <!-- 可以控制输入的数不能为空且不能为字符串 --> 3 <%@ page co

通过键盘接收数值和字符,实现计算器功能。

import java.util.Scanner; /** * @author 蓝色以太 * 通过键盘接收数值和字符,实现计算器功能. */ public class Calculator { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入第一个数值:"); double num1=sc.nextDouble(); System.out

Android计算器APP练习(1)--- 界面

Android Studio 2.3.2 .参考文章:http://blog.csdn.net/like_program/article/details/51813632 1. 新建工程 MyCalculator,一路下一步,均采用默认值. 2. 按照要求修改三个xml文件内容.很多地方工程名字不一样,改成自己的名字. 3. 按照步骤修改Activity_main.XML文件,遇到问题: android:singleLine="false" 过时 暂时处理方式:删掉此行. 4. 按照步

一个简单的税利计算器(网页版)

嗯嗯,做一个简单的网页版的税率计算器,功能比较简单,但是相对比较实用.因为参考了一些其他作品,所以在计算汇率的时候习惯性的是以美元做单位.具体的功能有着较为详细的标注.仅供大家学习参考下. <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>JavaScript Loan Calculator</title>

反转波兰计算器

#include <iostream>using namespace std;typedef double stackEntry; const int overflow = 1;const int underflow = 2;const int success = 0; const int maxstack = 100;//栈的最大尺寸class stack{public: stack(); int pop(); int push(const stackEntry &item); in

JAVA编写的简单计算器

package com.hellojava.practice.test; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; impo

winform与面向对象应用做一个计算器12月28日

代码部分: public partial class 计算器 : Form { public 计算器() { InitializeComponent(); } private double sum = 0;//存放运算结果 private string biaodashi="";//用于存放除了刚点过的运算符的前面表达式部分 private string preyunsuanfu="";//用来存放上一步运算符,用于下次点运算符的时候算上一步的结果 private