IO實(shí)現(xiàn)計(jì)算器功能
本文實(shí)例為大家分享了IO實(shí)現(xiàn)計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
// // ViewController.m // Fraction_Calculator // // Created by 魯軍 on 2021/4/26. // #import "ViewController.h" #import "Calculator.h" @interface ViewController () @property(nonatomic,strong)IBOutlet UILabel *display; -(void)processDigit:(int) digit; -(void)processOp:(char)theOp; -(void)storeFracPart; //數(shù)字鍵 -(IBAction)clickDigit:(UIButton*)sender; // 算術(shù)操作鍵 -(IBAction) clickPlus; -(IBAction) clickMinus; -(IBAction) clickMultiply; -(IBAction) clickDivde; //Misc鍵 -(IBAction) clickOver; -(IBAction) clickEquals; -(IBAction) clickClear; @end @implementation ViewController { char op; int currentNumber; BOOL firstOperand,isNumerator; Calculator *myCalculator; NSMutableString *displayString; } - (void)viewDidLoad { [super viewDidLoad]; self.display.text=@""; firstOperand = YES; isNumerator = YES; displayString = [NSMutableString stringWithCapacity: 40]; myCalculator = [[Calculator alloc] init]; } -(void) processDigit:(int) digit{ currentNumber = currentNumber * 10 + digit; [displayString appendString:[NSString stringWithFormat:@"%i",digit]]; self.display.text = displayString; } -(IBAction)clickDigit:(UIButton *)sender{ int digit = (int)sender.tag; [self processDigit:digit]; } -(void)processOp:(char)theOp{ NSString * opStr; op = theOp; switch (theOp) { case '+': opStr = @" + "; break; case '-': opStr = @" - "; break; case '*': opStr = @" *"; break; case '/': opStr = @" / "; break; } [self storeFracPart]; firstOperand = NO; isNumerator = YES; [displayString appendString:opStr]; self.display.text = displayString; } -(void) storeFracPart{ if(firstOperand){ if(isNumerator){ myCalculator.operand1.numerator = currentNumber; myCalculator.operand1.denominator = 1; //例如 3 * 4/5 = } else { myCalculator.operand1.denominator = currentNumber; } } else if (isNumerator) { myCalculator.operand2.numerator = currentNumber; myCalculator.operand2.denominator = 1; }else{ myCalculator.operand1.denominator = currentNumber; firstOperand = YES; } currentNumber = 0; } -(IBAction)clickOver{ [self storeFracPart]; isNumerator = NO; [displayString appendString:@"/"]; self.display.text = displayString; } //算術(shù)操作鍵 -(IBAction)clickPlus{ [self processOp:'+']; } - (void)clickMinus{ [self processOp:'-']; } - (void)clickMultiply{ [self processOp:'*']; } - (void)clickDivde{ [self processOp:'/']; } //Misc 鍵 - (void)clickEquals{ if(firstOperand==NO){ [self storeFracPart]; [myCalculator performOperation:op]; [displayString appendString:@" = "]; [displayString appendString:[myCalculator.accumulator convertToString]]; self.display.text = displayString; currentNumber = 0; isNumerator = YES; firstOperand = YES; [displayString setString:@""]; } } - (void)clickClear{ isNumerator = YES; firstOperand = YES; currentNumber = 0; [myCalculator clear]; [displayString setString:@""]; self.display.text = displayString; } @end
// // Calculator.h // Fraction_Calculator // // Created by 魯軍 on 2021/5/9. // #import <Foundation/Foundation.h> #import "Fraction.h" NS_ASSUME_NONNULL_BEGIN @interface Calculator : NSObject @property(nonatomic,strong)Fraction *operand1,*operand2,*accumulator; -(Fraction *)performOperation:(char)op; -(void)clear; @end NS_ASSUME_NONNULL_END
// // Calculator.m // Fraction_Calculator // // Created by 魯軍 on 2021/5/9. // #import "Calculator.h" @implementation Calculator - (instancetype)init { self = [super init]; if (self) { _operand1 = [[Fraction alloc] init]; _operand2 = [[Fraction alloc] init]; _accumulator = [[Fraction alloc] init]; } return self; } - (void)clear{ _accumulator.numerator = 0; _accumulator.denominator = 0; } - (Fraction *)performOperation:(char)op{ Fraction *result; switch (op) { case '+': result = [_operand1 add:_operand2]; break; case '-': result = [_operand1 subtract:_operand2]; break; case '*': result = [_operand1 multiply:_operand2]; break; case '/': result = [_operand1 divide:_operand2]; break; } _accumulator.numerator = result.numerator; _accumulator.denominator = result.denominator; return _accumulator; } @end
// // Fraction.h // Fraction_Calculator // // Created by 魯軍 on 2021/5/9. // #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface Fraction : NSObject @property int numerator,denominator; -(void) print; -(void) setTo:(int) n over:(int) d; -(Fraction *) add:(Fraction *)f; -(Fraction *) subtract:(Fraction *)f; -(Fraction *) multiply:(Fraction *)f; -(Fraction *) divide:(Fraction *)f; -(void)reduce; -(double)convertToNum; -(NSString*)convertToString; @end NS_ASSUME_NONNULL_END
// // Fraction.m // Fraction_Calculator // // Created by 魯軍 on 2021/5/9. // #import "Fraction.h" @implementation Fraction - (void)setTo:(int)n over:(int)d{ self.numerator = n; _denominator = d; } - (void)print{ NSLog(@"%i/%i",_numerator,_denominator); } - (double)convertToNum{ if(_denominator!=0) return (double) _numerator / _denominator; else return NAN; } - (NSString *)convertToString{ if(_numerator==_denominator) if(_numerator==0) return @"0"; else return @"1"; else if (_denominator==1) return [NSString stringWithFormat:@"%i",_numerator]; else return [NSString stringWithFormat:@"%i/%i",_numerator,_denominator]; } //添加一個(gè)分?jǐn)?shù)到消息的接收器 - (Fraction *)add:(Fraction *)f{ //將兩個(gè)分?jǐn)?shù)相加 Fraction *result = [[Fraction alloc] init]; result.numerator = _numerator *f.denominator + _denominator * f.numerator; result.denominator = _denominator*f.denominator; [result reduce]; return result; } - (Fraction *)subtract:(Fraction *)f{ Fraction *result = [[Fraction alloc] init]; result.numerator = _numerator*f.denominator - _denominator*f.numerator; result.denominator = _denominator*f.denominator; [result reduce]; return result; } - (Fraction *)multiply:(Fraction *)f{ Fraction *result = [[Fraction alloc] init]; result.numerator = _numerator*f.numerator; result.denominator = _denominator *f.denominator; [result reduce]; return result; } - (Fraction *)divide:(Fraction *)f{ Fraction *result = [[Fraction alloc] init]; result.numerator = _numerator*f.denominator; result.denominator = _denominator *f.numerator; [result reduce]; return result; } - (void)reduce{ int u = _numerator; int v = _denominator; int temp; if(u==0) return; else if (u<0) u = -u; while(v!=0){ temp = u%v; u=v; v=temp; } _numerator/=u; _denominator/=u; } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)中使用UIDynamic來捕捉動(dòng)畫組件的重力行為
這篇文章主要介紹了iOS開發(fā)中使用UIDynamic來捕捉動(dòng)畫組件的重力行為的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解
這篇文章主要為大家介紹了Ios蘋果app應(yīng)用程序開發(fā)者如何獲取IPA簽名證書詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11iOS Mask屬性的詳細(xì)介紹及應(yīng)用實(shí)例
這篇文章主要介紹了iOS Mask屬性的詳細(xì)介紹的相關(guān)資料,這里對(duì)Mask的屬性進(jìn)行了詳細(xì)說明并附簡(jiǎn)單代碼實(shí)例,幫助大家更直接學(xué)習(xí)理解,這部分知識(shí),需要的朋友可以參考下2016-11-11iOS 利用動(dòng)畫和貝塞爾實(shí)現(xiàn)咻咻效果
這篇文章主要介紹了iOS 利用動(dòng)畫和貝塞爾實(shí)現(xiàn)咻咻效果的相關(guān)資料,需要的朋友可以參考下2016-09-09Objective-C中利用正則去除非數(shù)字字母漢字方法實(shí)例
正則表達(dá)式對(duì)我們?nèi)粘i_發(fā)來說是必不可少的,下面這篇文章主要給大家介紹了關(guān)于Objective-C中如何利用正則去除非數(shù)字字母漢字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06IOS開發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
在制作IOS項(xiàng)目中,我們經(jīng)常要用到倒計(jì)時(shí)功能,今天就分享下使用nstimer實(shí)現(xiàn)的倒計(jì)時(shí)功能的代碼,希望對(duì)大家能有所幫助2014-09-09iOS實(shí)現(xiàn)多個(gè)垂直滑動(dòng)條并列視圖
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)多個(gè)垂直滑動(dòng)條并列視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03IOS UIImagePickerController從拍照、圖庫、相冊(cè)獲取圖片
這篇文章主要介紹了IOS UIImagePickerController從拍照、圖庫、相冊(cè)獲取圖片的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09