iOS實現(xiàn)簡易的計算器
更新時間:2022年01月27日 14:50:58 作者:XYQ全哥
這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)簡易的計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS實現(xiàn)簡易的計算器的具體代碼,供大家參考,具體內(nèi)容如下
初步接觸視圖,制作了一個簡易的計算器,基本上簡單的計算是沒有問題的,不是很完美,可能還有一些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;
}
//觸發(fā)算數(shù)運算事件
- (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;
}
//做結(jié)果運算操作
- (IBAction)buttonEqual:(UIButton *)sender
{
? ? for(int i=0; i<[_string length]; i++)
? ? {
? ? ? ? self.arr = [[NSArray alloc]init];
? ? ? ? //只輸入一個數(shù),不做運算
? ? ? ? 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;
? ? ? ? }
? ? }
? ? //輸出結(jié)果
? ? [_string setString:[NSString stringWithFormat:@"%.2f",self.temp]];
? ? self.ResultField.text = _string;
}
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? //創(chuàng)建一個可變的字符串
? ? _string = [NSMutableString stringWithCapacity:20];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS封裝倒計時按鈕HLCountDownButton示例詳解
這篇文章主要為大家介紹了iOS封裝倒計時按鈕HLCountDownButton示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來總結(jié)一下iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法:2016-07-07
詳解iOS開發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問題
這篇文章主要介紹了詳解iOS開發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問題,具有一定的參考價值,有興趣的可以了解一下。2016-12-12

