创建简易计算器

@interface AppDelegate ()
{
    UIView *_containerView;
    UILabel *_lable;
    CGFloat _fristNum;
    CGFloat _secondNum;
    NSInteger _tempNum;
    NSMutableString *_str;
}
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _containerView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:_containerView];
    [_containerView release];

    _lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];
    _lable.backgroundColor = [UIColor blackColor];
    _lable.text = @"0";
    _lable.textColor = [UIColor whiteColor];
    _lable.textAlignment = NSTextAlignmentRight;
    _lable.font = [UIFont systemFontOfSize:30];
    _lable.enabled = YES;
    [_containerView addSubview:_lable];
    [_lable release];

    NSArray *number = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    for (int i = 0; i < 3; i++) {
        for (int j = 0 ; j < 3; j++) {
            UIButton *bun = [[UIButton alloc] initWithFrame:CGRectMake(10 + 80 * j, 450 - i * 90 , 70, 65)];
            bun.backgroundColor = [UIColor grayColor];
            bun.layer.cornerRadius = 5;
            bun.titleLabel.font = [UIFont systemFontOfSize:30];
            [bun setTitle:number[j + i * 3] forState:UIControlStateNormal];
            [bun addTarget:self action:@selector(click:) forControlEvents: UIControlEventTouchUpInside];
            [_containerView addSubview:bun];
        }
    }

    NSArray *operat = @[@"+",@"-",@"*",@"/"];
    for (int i = 0; i < 4; i++) {
        UIButton *opBun = [[UIButton alloc] initWithFrame:CGRectMake(10 + i * 78, 180, 70, 65)];
        opBun.backgroundColor = [UIColor blueColor];
        opBun.layer.cornerRadius = 5;
        opBun.titleLabel.font = [UIFont systemFontOfSize:30];
        [opBun setTitle:operat[i] forState:UIControlStateNormal];
        [opBun addTarget:self action:@selector(ract:) forControlEvents:UIControlEventTouchUpInside];
        [_containerView addSubview:opBun];
    }

    //创建=号
    UIButton *equal = [[UIButton alloc] initWithFrame:CGRectMake(250, 450, 65, 65)];
    equal.backgroundColor = [UIColor redColor];
    equal.layer.cornerRadius = 5;
    equal.titleLabel.font = [UIFont systemFontOfSize:20];
    [equal setTitle:@"=" forState:UIControlStateNormal];
    [equal addTarget:self action:@selector(equal:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:equal];

    //创建0
    UIButton *zero = [[UIButton alloc] initWithFrame:CGRectMake(250, 360, 65, 65)];
    zero.backgroundColor = [UIColor grayColor];
    zero.layer.cornerRadius = 5;
    zero.titleLabel.font = [UIFont systemFontOfSize:30];
    [zero setTitle:@"0" forState:UIControlStateNormal];
    [zero addTarget:self action:@selector(zero:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:zero];

    //创建.
    UIButton *point = [[UIButton alloc] initWithFrame:CGRectMake(250, 270, 65, 65)];
    point.backgroundColor = [UIColor grayColor];
    point.layer.cornerRadius = 5;
    point.titleLabel.font = [UIFont systemFontOfSize:30];
    [point setTitle:@"." forState:UIControlStateNormal];
    [point addTarget:self action:@selector(point:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:point];

    //创建AC
    UIButton *ac = [[UIButton alloc] initWithFrame:CGRectMake(170, 100, 65, 65)];
    ac.backgroundColor = [UIColor grayColor];
    ac.layer.cornerRadius = 5;
    ac.titleLabel.font = [UIFont systemFontOfSize:20];
    [ac setTitle:@"AC" forState:UIControlStateNormal];
    [ac addTarget:self action:@selector(ac:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:ac];

    //创建back
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(245, 100, 65, 65)];
    back.backgroundColor = [UIColor grayColor];
    back.layer.cornerRadius = 5;
    back.titleLabel.font = [UIFont systemFontOfSize:20];
    [back setTitle:@"back" forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:back];

    _str = [[NSMutableString alloc] init];

    return YES;
}
//数字键
- (void)click:(UIButton *)bun
{
    [_str appendString:[bun currentTitle]];
    _lable.text = _str;
}

//符号
- (void)ract:(UIButton *)bun
{
   if ([bun.titleLabel.text isEqualToString:@"+"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 1;
    }
    if ([bun.titleLabel.text isEqualToString:@"-"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 2;
    }
    if ([bun.titleLabel.text isEqualToString:@"*"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 3;
    }
    if ([bun.titleLabel.text isEqualToString:@"/"]) {
        _fristNum = _lable.text.floatValue;
        _tempNum = 4;
    }
    [_str setString:@" "];
}

//=号
- (void)equal:(UIButton *)bun
{
    _secondNum = _lable.text.floatValue;
    if (_tempNum == 1) {
        _lable.text = [NSString stringWithFormat:@"%g",_secondNum + _fristNum];
    }
    if (_tempNum == 2) {
        _lable.text = [NSString stringWithFormat:@"%g",_fristNum - _secondNum];
    }
    if (_tempNum == 3) {
        _lable.text = [NSString stringWithFormat:@"%g",_fristNum * _secondNum];
    }
    if (_tempNum == 4) {
        _lable.text = [NSString stringWithFormat:@"%g",_fristNum / _secondNum];
    }
    if (_tempNum != 1 && _tempNum != 2 && _tempNum != 3 &&_tempNum != 4) {
        _lable.text = [NSString stringWithFormat:@"%g",_secondNum];
    }
    _tempNum = 0;
    [_str setString:@" "];
}

//0
- (void)zero:(UIButton *)bun
{
    [_str appendString:[bun currentTitle]];
    _lable.text = _str;
}
//小数点
- (void)point:(UIButton *)bun
{
    if ([_lable.text isEqualToString:@"0"]) {
        [_str appendString:@"0"];
    }
    [_str appendString:[bun currentTitle]];
    _lable.text = _str;
}
//清除
- (void)ac:(UIButton *)bun
{
    _lable.text = @"0";
    [_str setString:@" "];
}
//撤销
- (void)back:(UIButton *)bun
{
    if ([_str length] > 0) {
        [_str deleteCharactersInRange:NSMakeRange([_str length]-1, 1)];
        _lable.text = _str;
    }
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

通过以上代码,计算器能进行基本的加减乘除运算.

创建简易计算器

时间: 2024-10-13 16:13:39

创建简易计算器的相关文章

C# 简易计算器

编写如下界面的简易计算器界面代码: using System; using System.Windows.Forms; using exp; namespace calculator { public partial class Form1 : Form { public Form1() { InitializeComponent(); } enum symbol { plus,dec,mult,div}; private void button1_Click(object sender, Ev

JS制作一个简易计算器

//javascript简易计算器 <!DOCTYPE html> <html>  <head>   <title> 事件</title>     <script type="text/javascript">    function count(){             //获取第一个输入框的值     var node1=document.getElementById('txt1').value;     

基于Tkinter用50行Python代码实现简易计算器

Tkinter一般是python自带的,所以代码不需要其他组件,本程序是在python2.7版本实现的. 主要涉及了tkinter的使用,函数定义和调用,匿名函数的使用,类成员函数定义等python基础知识,适合新手学习. 代码如下: from Tkinter import * #创建横条型框架 def frame(root, side): w = Frame(root) w.pack(side = side, expand = YES, fill = BOTH) return w #创建按钮

C#Windows Form简易计算器实现(上)

第一次写博客,来分享一个简易计算器的代码.作为一名准程序员,就是要多写代码才能孰能生巧.重视基础知识才能飞的更快更高以及更稳. 代码可能会写的很糟糕,不完美不安全之处希望发现的越多越好 c#编写计算器带窗口的,对于新手来说是如何建立窗体以及实现按钮的响应事件吧!那么,首先来探索下窗口是怎么实现的吧! 步骤1:新建项目→C#windows窗体应用程序→新建解决方案 此时你会发现有两个名称,一个是解决方案名称,一个是项目名称.对于小程序来说其实没什么区别.但对于大点的程序最好就要区别开了.解决方案就

JAVA开发简易计算器界面-SWT

大家好,我是成都[LD],博客四年前就申请了,一直没打理,最近正好有时间,遂萌生了写技术博客的念头.我不得不感慨现在新技术更新很快,一不小心,就感觉自身就Out了.记得一年前,当时我也是在51CTO上了解到NoSQL和Hadoop这样的信息,当时就简单觉得很新奇,没想到一年之后发展如此迅速~~当然我这样说,并不是叫大家去追寻新技术,最根本的还是基础打牢靠,休息的时候多去了解下最新的IT动态.学习下前辈高手的一些技能~~打铁还需自身硬嘛! 我写博客的目的:一来是为了促进自身的进步,二来是为了希望与

0821基础控件-作业(简易计算器,QQ登录界面)

简易计算器 一.将NSString类型转换成NSInteger类型 [text integerValue]; //这个NSInteger类型在32/64位系统下能够自动转换所占字节数 二.定义NSInteger类型数据: NSInteger num = [text interValue]; //不需要* QQ登录界面 一.可以设置在文本框中输入完毕回车跳转至其他文本框的效果 1.首先要由ViewController类实现<UITextFieldDelegate>协议,这样可以在.m文件中实现-

C++.NET的简易计算器的制作

计算器的制作需要实现一下几个功能:加减乘除,连续计算,重复计算. 加减乘除就是简单的二元运算,连续计算就是不使用等号连续进行几次二元运算,重复计算就是进行一次二元运算之后再次单击等号可以将之前的运算再次进行一次. 由于是C++的窗体程序,所以先设计出窗体的界面.界面如下. 需要注意的是,上面的两个显示框用的是TextBox,其余的均是Button. 窗体的FormBorderStyle属性应改成FixedSingle或其他,不能用None,这个样子计算器窗体框的大小就是固定不可变的. Maxim

原生JS实现简易计算器

<!doctype html> <html> <head> <meta charset="utf-8"> <title>简易计算器</title> <style> #div{ width: 300px; height: 500px; background: #ccc; margin: auto; border: 1px solid red; } table{ width: 97%; height: 48

函数调用_猜数字和简易计算器

package app1; import java.util.*; public class TestFunction{     public static void main(String[] args){         Scanner sc=new Scanner(System.in);         System.out.print("请选择一项应用:\n1.猜数字\n2.简易计算器");         int n=sc.nextInt();         switch(