千家信息网

iOS实现简易计算器的代码怎么写

发表于:2025-01-16 作者:千家信息网编辑
千家信息网最后更新 2025年01月16日,本篇内容主要讲解"iOS实现简易计算器的代码怎么写",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"iOS实现简易计算器的代码怎么写"吧!初步接触视图,制作了
千家信息网最后更新 2025年01月16日iOS实现简易计算器的代码怎么写

本篇内容主要讲解"iOS实现简易计算器的代码怎么写",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"iOS实现简易计算器的代码怎么写"吧!

初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉。

////  ViewController.m//  计算器////  Created by ma c on 15/8/25.//  Copyright (c) 2015年 bjsxt. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *ResultField;@property(nonatomic,assign)CGFloat temp;@property(nonatomic,assign)CGFloat num1;@property(nonatomic,assign)CGFloat num2;@property(nonatomic,strong)NSMutableString *string;@property(nonatomic,strong)NSArray *arr;@end@implementation ViewController- (IBAction)buttonClear:(UIButton *)sender{    [_string setString:@""];         //重新开始计算,文本框置空    self.ResultField.text = _string;}- (IBAction)button7:(UIButton *)sender{    [_string appendString:@"7"];    self.ResultField.text = _string;}- (IBAction)button8:(UIButton *)sender{    [_string appendString:@"8"];    self.ResultField.text = _string;}- (IBAction)button9:(UIButton *)sender{    [_string appendString:@"9"];    self.ResultField.text = _string;}- (IBAction)button4:(UIButton *)sender{    [_string appendString:@"4"];    self.ResultField.text = _string;}- (IBAction)button5:(UIButton *)sender{    [_string appendString:@"5"];    self.ResultField.text = _string;}- (IBAction)button6:(UIButton *)sender{    [_string appendString:@"6"];    self.ResultField.text = _string;}- (IBAction)button1:(UIButton *)sender{    [_string appendString:@"1"];    self.ResultField.text = _string;}- (IBAction)button3:(UIButton *)sender{    [_string appendString:@"3"];    self.ResultField.text = _string;}- (IBAction)button2:(UIButton *)sender{    [_string appendString:@"2"];    self.ResultField.text = _string;}- (IBAction)button0:(UIButton *)sender{    [_string appendString:@"0"];    self.ResultField.text = _string;}- (IBAction)buttonPoint:(UIButton *)sender{    [_string appendString:@"."];    self.ResultField.text = _string;}//触发算数运算事件- (IBAction)buttonDiv:(UIButton *)sender{    [_string appendString:@"/"];    self.ResultField.text = _string;}- (IBAction)buttonMul:(UIButton *)sender{    [_string appendString:@"*"];    self.ResultField.text = _string;}- (IBAction)buttonSub:(UIButton *)sender{    [_string appendString:@"-"];    self.ResultField.text = _string;}- (IBAction)buttonAdd:(UIButton *)sender{    [_string appendString:@"+"];    self.ResultField.text = _string;}//做结果运算操作- (IBAction)buttonEqual:(UIButton *)sender{    for(int i=0; i<[_string length]; i++)    {        self.arr = [[NSArray alloc]init];        //只输入一个数,不做运算        if([_string length] == 1)        {            self.temp = [_string doubleValue];            break;        }        //做加法运算        if([_string characterAtIndex:i] == '+')        {            self.arr = [_string componentsSeparatedByString:@"+"];            self.num1 = [self.arr[0] doubleValue];            self.num2 = [self.arr[1] doubleValue];            self.temp = self.num1 + self.num2;            break;        }        //做减法运算        if([_string characterAtIndex:(i+1)] == '-')        {            self.arr = [_string componentsSeparatedByString:@"-"];            if([self.arr count] == 2)            {                self.num1 = [self.arr[0] doubleValue];                self.num2 = [self.arr[1] doubleValue];                self.temp = self.num1 - self.num2;            }            else            {                self.num1 = [self.arr[1] doubleValue];                self.num2 = [self.arr[2] doubleValue];                self.temp = -(self.num1 + self.num2);            }            break;        }        //做除法运算        if([_string characterAtIndex:i] == '/')        {            self.arr = [_string componentsSeparatedByString:@"/"];            self.num1 = [self.arr[0] doubleValue];            self.num2 = [self.arr[1] doubleValue];            self.temp = self.num1 / self.num2;            break;        }        //做乘法运算        if([_string characterAtIndex:i] == '*')        {            self.arr = [_string componentsSeparatedByString:@"*"];            self.num1 = [self.arr[0] doubleValue];            self.num2 = [self.arr[1] doubleValue];            self.temp = self.num1 * self.num2;            break;        }    }    //输出结果    [_string setString:[NSString stringWithFormat:@"%.2f",self.temp]];    self.ResultField.text = _string;}- (void)viewDidLoad {    [super viewDidLoad];    //创建一个可变的字符串    _string = [NSMutableString stringWithCapacity:20];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

到此,相信大家对"iOS实现简易计算器的代码怎么写"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0