iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
初步接觸視圖,制作了一個(gè)簡(jiǎn)易的計(jì)算器,基本上簡(jiǎn)單的計(jì)算是沒(méi)有問(wèn)題的,不是很完美,可能還有一些bug,再接再厲。

//
// ?ViewController.m
// ?計(jì)算器
//
// ?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:@""]; ? ? ? ? //重新開始計(jì)算,文本框置空
? ? 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ù)運(yùn)算事件
- (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é)果運(yùn)算操作
- (IBAction)buttonEqual:(UIButton *)sender
{
? ? for(int i=0; i<[_string length]; i++)
? ? {
? ? ? ? self.arr = [[NSArray alloc]init];
? ? ? ? //只輸入一個(gè)數(shù),不做運(yùn)算
? ? ? ? if([_string length] == 1)
? ? ? ? {
? ? ? ? ? ? self.temp = [_string doubleValue];
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? //做加法運(yùn)算
? ? ? ? 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;
? ? ? ? }
? ? ? ? //做減法運(yùn)算
? ? ? ? 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;
? ? ? ? }
? ? ? ? //做除法運(yùn)算
? ? ? ? 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;
? ? ? ? }
? ? ? ? //做乘法運(yùn)算
? ? ? ? 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)建一個(gè)可變的字符串
? ? _string = [NSMutableString stringWithCapacity:20];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
@end以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS封裝倒計(jì)時(shí)按鈕HLCountDownButton示例詳解
這篇文章主要為大家介紹了iOS封裝倒計(jì)時(shí)按鈕HLCountDownButton示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
iOS實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫的3種方法示例
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫的3種方法,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
iOS實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要介紹了iOS實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能,一種方法是利用NSTimer計(jì)時(shí)器,另一種方法是利用GCD實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
iOS App開發(fā)中通過(guò)UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來(lái)監(jiān)測(cè)iOS設(shè)備的電量了,然后再實(shí)現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來(lái)總結(jié)一下iOS App開發(fā)中通過(guò)UIDevice類獲取設(shè)備信息的方法:2016-07-07
詳解iOS開發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問(wèn)題
這篇文章主要介紹了詳解iOS開發(fā)中解析JSON中的boolean類型的數(shù)據(jù)遇到的問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12

