ios实现简单的计算器

<span style="font-size:24px;"></span>
<span style="font-size:24px;">//
//  ZYAppDelegate.m
//  Calculator
//
//  Created by mac on 15-7-30.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//

#import "ZYAppDelegate.h"

@implementation ZYAppDelegate

- (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];
    show=[[UITextField alloc] initWithFrame:CGRectMake(20, 30, 260, 30)];
    show.borderStyle=UITextBorderStyleRoundedRect;
    show.text=@"0";
    
    show.backgroundColor=[UIColor redColor];
    [self.window addSubview:show];
    
    
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame=CGRectMake(20, 80, 40, 40);
    [btn1 setTitle:@"1" forState:UIControlStateNormal];
    btn1.backgroundColor=[UIColor blueColor];
    btn1.tag=1;
    [btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn1];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame=CGRectMake(80, 80, 40, 40);
    [btn2 setTitle:@"2" forState:UIControlStateNormal];
    btn2.backgroundColor=[UIColor blueColor];
    btn2.tag=2;
    [btn2 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn2];
    
    UIButton *btn3=[UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame=CGRectMake(140, 80, 40, 40);
    [btn3 setTitle:@"3" forState:UIControlStateNormal];
    btn3.backgroundColor=[UIColor blueColor];
    btn3.tag=3;
    [btn3 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn3];
    
  

    
    UIButton *btn4=[UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame=CGRectMake(20, 140, 40, 40);
    [btn4 setTitle:@"4" forState:UIControlStateNormal];
    btn4.backgroundColor=[UIColor blueColor];
    btn4.tag=4;
    [btn4 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn4];
    
    UIButton *btn5=[UIButton buttonWithType:UIButtonTypeCustom];
    btn5.frame=CGRectMake(80, 140, 40, 40);
    [btn5 setTitle:@"5" forState:UIControlStateNormal];
    btn5.backgroundColor=[UIColor blueColor];
    btn5.tag=5;
    [btn5 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn5];
    
    UIButton *btn6=[UIButton buttonWithType:UIButtonTypeCustom];
    btn6.frame=CGRectMake(140, 140, 40, 40);
    [btn6 setTitle:@"6" forState:UIControlStateNormal];
    btn6.backgroundColor=[UIColor blueColor];
    btn6.tag=6;
    [btn6 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn6];
    

    
    
    UIButton *btn7=[UIButton buttonWithType:UIButtonTypeCustom];
    btn7.frame=CGRectMake(20, 200, 40, 40);
    [btn7 setTitle:@"7" forState:UIControlStateNormal];
    btn7.backgroundColor=[UIColor blueColor];
    btn7.tag=7;
    [btn7 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn7];
    
    UIButton *btn8=[UIButton buttonWithType:UIButtonTypeCustom];
    btn8.frame=CGRectMake(80, 200, 40, 40);
    [btn8 setTitle:@"8" forState:UIControlStateNormal];
    btn8.backgroundColor=[UIColor blueColor];
    btn8.tag=8;
    [btn8 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn8];
    
    UIButton *btn9=[UIButton buttonWithType:UIButtonTypeCustom];
    btn9.frame=CGRectMake(140, 200, 40, 40);
    [btn9 setTitle:@"9" forState:UIControlStateNormal];
    btn9.backgroundColor=[UIColor blueColor];
    
    btn9.tag=9;
    [btn9 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn9];
    
    
 
    

    UIButton *btn0=[UIButton buttonWithType:UIButtonTypeCustom];
    btn0.frame=CGRectMake(20, 260, 100, 40);
    [btn0 setTitle:@"0" forState:UIControlStateNormal];
    btn0.backgroundColor=[UIColor blueColor];
    btn0.tag=10;
    [btn0 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn0];
    
    
    
    
    
    UIButton *jia=[UIButton buttonWithType:UIButtonTypeCustom];
    jia.frame=CGRectMake(200, 140, 40, 40);
    [jia setTitle:@"+" forState:UIControlStateNormal];
    jia.backgroundColor=[UIColor blueColor];
    jia.tag=100101;
    [jia addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jia];
    
    
    UIButton *jian=[UIButton buttonWithType:UIButtonTypeCustom];
    jian.frame=CGRectMake(200, 200, 40, 40);
    [jian setTitle:@"-" forState:UIControlStateNormal];
    jian.backgroundColor=[UIColor blueColor];
    jian.tag=100102;
    [jian addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:jian];
    
    
    
    UIButton *cheng=[UIButton buttonWithType:UIButtonTypeCustom];
    cheng.frame=CGRectMake(200, 80, 40, 40);
    [cheng setTitle:@"*" forState:UIControlStateNormal];
    cheng.backgroundColor=[UIColor blueColor];
    cheng.tag=100103;
    [cheng addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:cheng];
    
    
    UIButton *chu=[UIButton buttonWithType:UIButtonTypeCustom];
    chu.frame=CGRectMake(200, 260, 40, 40);
    [chu setTitle:@"/" forState:UIControlStateNormal];
    chu.backgroundColor=[UIColor blueColor];
    chu.tag=100104;
    [chu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:chu];
  

    UIButton *deng=[UIButton buttonWithType:UIButtonTypeCustom];
    deng.frame=CGRectMake(140, 260, 40, 40);
    [deng setTitle:@"=" forState:UIControlStateNormal];
    deng.backgroundColor=[UIColor blueColor];
    deng.tag=100105;
    [deng addTarget:self action:@selector(equal) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:deng];

    return YES;
}

-(void)onClick:(UIButton *)btn{

    switch (btn.tag) {
        case 1:
            num1=@"1";
            show.text=num1;
            a=[num1 intValue];
            
            break;
        case 2:
            num1=@"2";
            show.text=num1;
            a=[num1 intValue];
            break;
        
        case 3:
            num1=@"3";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 4:
            num1=@"4";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 5:
            num1=@"5";
            show.text=num1;
            a=[num1 intValue];

            break;
        case 6:
            num1=@"6";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 7:
            num1=@"7";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 8:
            num1=@"8";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 9:
            num1=@"9";
            show.text=num1;
            a=[num1 intValue];
            break;
        case 10:
            num1=@"0";
            show.text=num1;
            a=[num1 intValue];

            break;
        default:
            break;
    }

}
-(void)click:(UIButton *)btn{
    switch (btn.tag) {
        case 100101:
            b=1;
            c=a;
            break;
        case 100102:
            b=2;
            c=a;
            break;
        case 100103:
            b=3;
            c=a;
            break;
        case 100104:
            b=4;
            c=a;
            break;
       
    }
    
}
-(void)equal{

    if (b==1) {
        show.text=[NSString stringWithFormat:@"%d",(c+a)];
    }
    if (b==2) {
        show.text=[NSString stringWithFormat:@"%d",(c-a)];
    }
    if (b==3) {
        show.text=[NSString stringWithFormat:@"%d",(c*a)];
    }
    if (b==4) {
        show.text=[NSString stringWithFormat:@"%d",(c/a)];
    }

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.window endEditing:YES];
}
- (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:.
}

@end
</span>




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很抱歉,作为一个自然语言处理模型,我无法写出完整的代码,但是下面是一个简单计算器的 Objective-C 代码示例: ``` #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; double value1, value2; char operator; NSLog (@"请输入第一个数字和算术运算符:"); scanf ("%lf %c", &value1, &operator); NSLog (@"请输入第二个数字:"); scanf ("%lf", &value2); if ( operator == '+' ) NSLog (@"%.2f + %.2f = %.2f", value1, value2, value1 + value2); else if ( operator == '-' ) NSLog (@"%.2f - %.2f = %.2f", value1, value2, value1 - value2); else if ( operator == '*' ) NSLog (@"%.2f * %.2f = %.2f", value1, value2, value1 * value2); else if ( operator == '/' ) NSLog (@"%.2f / %.2f = %.2f", value1, value2, value1 / value2); else NSLog (@"无效的运算符"); [pool drain]; return 0; } ``` 这是一个简单计算器代码示例,它可以实现加减乘除运算,你可以根据需要扩展其他功能。 ### 回答2: iOS上运行的计算器代码可以使用Swift语言编写。以下是一个简单计算器代码示例: ```swift import UIKit class ViewController: UIViewController { @IBOutlet weak var displayLabel: UILabel! var operand1: Double = 0 var operand2: Double = 0 var operation: String = "" override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func numberButtonPressed(_ sender: UIButton) { let digit = sender.currentTitle! displayLabel.text! += digit } @IBAction func operationButtonPressed(_ sender: UIButton) { operation = sender.currentTitle! operand1 = Double(displayLabel.text!)! displayLabel.text = "" } @IBAction func equalsButtonPressed(_ sender: UIButton) { operand2 = Double(displayLabel.text!)! var result: Double = 0 switch operation { case "+": result = operand1 + operand2 case "-": result = operand1 - operand2 case "×": result = operand1 * operand2 case "÷": result = operand1 / operand2 default: break } displayLabel.text = String(result) } @IBAction func clearButtonPressed(_ sender: UIButton) { displayLabel.text = "" operand1 = 0 operand2 = 0 operation = "" } } ``` 这个代码示例包括一个ViewController类,它继承自UIViewController。它具有数字按钮、运算符按钮、等于按钮和清除按钮的响应函数。 CalculatorViewController类中的一些重要成员变量包括`operand1`和`operand2`,用于存储操作数的值,`operation`用于存储当前选择的运算符。 在数字按钮和操作按钮的响应函数中,将所点击的按钮的标题添加到显示标签上。 在等于按钮的响应函数中,将当前显示标签上的文本转换为数字,并根据存储的运算符执行相应的计算。然后,将结果显示在显示标签上。 清除按钮的响应函数将显示标签上的内容、操作数和运算符重置为初始状态。 这是一个简单iOS计算器代码示例,可以根据需要进行扩展和改进。 ### 回答3: 以下是一个简单iOS计算器代码示例: ```swift import UIKit class ViewController: UIViewController { // 创建计算器显示区域的标签 let displayLabel: UILabel = { let label = UILabel() label.font = UIFont.systemFont(ofSize: 40) label.text = "0" label.textAlignment = .right return label }() var currentNumber = "" var previousNumber = "" var operation = "" override func viewDidLoad() { super.viewDidLoad() // 设置背景颜色 view.backgroundColor = .white // 设置计算器显示区域的布局和约束 displayLabel.frame = CGRect(x: 20, y: 100, width: view.frame.size.width - 40, height: 100) view.addSubview(displayLabel) // 设置数字按钮 for i in 0..<10 { let button = UIButton(type: .system) button.setTitle("\(i)", for: .normal) button.titleLabel?.font = UIFont.systemFont(ofSize: 30) button.frame = CGRect(x: CGFloat((i % 3)) * (view.frame.size.width/3), y: CGFloat(i / 3) * (view.frame.size.width/3) + 220, width: view.frame.size.width/3, height: view.frame.size.width/3) button.addTarget(self, action: #selector(numberButtonTapped), for: .touchUpInside) view.addSubview(button) } // 设置运算符按钮 let operators = ["+", "-", "*", "/"] for (index, operator) in operators.enumerated() { let button = UIButton(type: .system) button.setTitle(operator, for: .normal) button.titleLabel?.font = UIFont.systemFont(ofSize: 30) button.frame = CGRect(x: view.frame.size.width - 80, y: CGFloat(index) * (view.frame.size.width/3) + 220, width: 60, height: view.frame.size.width/3) button.addTarget(self, action: #selector(operatorButtonTapped), for: .touchUpInside) view.addSubview(button) } // 设置等号按钮 let equalsButton = UIButton(type: .system) equalsButton.setTitle("=", for: .normal) equalsButton.titleLabel?.font = UIFont.systemFont(ofSize: 30) equalsButton.frame = CGRect(x: view.frame.size.width - 80, y: CGFloat(operators.count) * (view.frame.size.width/3) + 220, width: 60, height: view.frame.size.width/3) equalsButton.addTarget(self, action: #selector(equalsButtonTapped), for: .touchUpInside) view.addSubview(equalsButton) } // 当数字按钮被点击时调用 @objc func numberButtonTapped(_ sender: UIButton) { if let numberText = sender.titleLabel?.text { currentNumber += numberText displayLabel.text = currentNumber } } // 当运算符按钮被点击时调用 @objc func operatorButtonTapped(_ sender: UIButton) { if let operatorText = sender.titleLabel?.text { previousNumber = currentNumber currentNumber = "" operation = operatorText } } // 当等号按钮被点击时调用 @objc func equalsButtonTapped(_ sender: UIButton) { let number1 = Double(previousNumber) ?? 0.0 let number2 = Double(currentNumber) ?? 0.0 var result: Double switch operation { case "+": result = number1 + number2 case "-": result = number1 - number2 case "*": result = number1 * number2 case "/": result = number1 / number2 default: result = 0.0 } displayLabel.text = String(result) currentNumber = "" previousNumber = "" operation = "" } } ``` 这个计算器可以处理基本的加、减、乘、除运算,并在屏幕上显示结果。界面使用了按钮和标签来显示数字和运算符,并实现了按钮点击事件的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值