博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OC20简易计算器
阅读量:6979 次
发布时间:2019-06-27

本文共 8516 字,大约阅读时间需要 28 分钟。

hot3.png

152134_8py2_2319073.jpg

152134_1XHU_2319073.png

////  ViewController.h//  ZJFraction_Calculator////  Created by Zoujie on 15/12/6.//  Copyright © 2015年 Zoujie. All rights reserved.//#import 
#import "Calculator.h"@interface ViewController : UIViewController@property (strong, nonatomic) IBOutlet UILabel *display;-(void) processDigit:(int)digit;-(void) processOp:(char) theOp;-(void) storeFracPart;//数字键-(IBAction)clickDigit:(UIButton *)sender;//算术操作键-(IBAction)clickPlug;-(IBAction)clickMinus;-(IBAction)clickMultiply;-(IBAction)clickDivide;//Misc键-(IBAction)clickOver;-(IBAction)clickEquals;-(IBAction)clickClear;@end
////  ViewController.m//  ZJFraction_Calculator////  Created by Zoujie on 15/12/6.//  Copyright © 2015年 Zoujie. All rights reserved.//#pragma 显示两个数字4则运算的计算器#import "ViewController.h"@interface ViewController ()@end@implementation ViewController{    char op;    int currentNumber;    BOOL firstOperand,isNumerator;    Calculator *myCalculator;    NSMutableString *displayString;    }@synthesize display;- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    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]];    display.text = displayString;}-(IBAction) clickDigit:(UIButton *)sender{    NSInteger digit = sender.tag;        [self processDigit:(int)digit];    }-(void) processOp:(char)theOp{    NSString *opStr;        op = theOp;        switch (theOp) {        case '+':            opStr = @"+";            break;        case '-':            opStr = @"-";            break;        case '*':            opStr = @"*";            break;        case '/':            opStr = @"÷";            break;        default:            break;    }        [self storeFracPart];    firstOperand = NO;    isNumerator = YES;        [displayString appendString:opStr];    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;// 3/2 * 4    }    else{        myCalculator.operand2.denominator = currentNumber;        firstOperand = YES;    }    currentNumber = 0;}-(IBAction) clickOver{    [self storeFracPart];    isNumerator = NO;    [displayString appendString:@"/"];    display.text  = displayString;}//算术操作键-(IBAction) clickPlug{    if ([self handleSymbol:display.text] == NO) {        return;    }    else    [self processOp:'+'];}-(IBAction) clickMinus{    if ([self handleSymbol:display.text] == NO) {        return;    }    else    [self processOp:'-'];}-(IBAction) clickMultiply{    if ([self handleSymbol:display.text] == NO) {        return;    }    else [self processOp:'*'];}-(IBAction) clickDivide{    if ([self handleSymbol:display.text] == NO) {        return;    }    else    [self processOp:'/'];}//Misc-(IBAction) clickEquals{    if ( firstOperand == NO)    {        [self storeFracPart];        [myCalculator performOperation:op];                [displayString appendString:@" = "];        [displayString appendString:[myCalculator.accumulator convertToString]];        display.text = displayString;                currentNumber = 0;        isNumerator = YES;        firstOperand = YES;        [displayString setString:@""];    }}-(IBAction)clickClear{    isNumerator = YES;    firstOperand = YES;    currentNumber = 0;    [myCalculator clear];        [displayString setString:@""];    display.text = displayString;}//处理运算符号-(BOOL) handleSymbol:(NSString *) displayText{    if (displayText.length == 0)        return NO;    NSRange foundObj1 =[displayText rangeOfString:@"+" options:NSCaseInsensitiveSearch];    NSRange foundObj2 =[displayText rangeOfString:@"-" options:NSCaseInsensitiveSearch];    NSRange foundObj3 =[displayText rangeOfString:@"*" options:NSCaseInsensitiveSearch];    NSRange foundObj4 =[displayText rangeOfString:@"÷" options:NSCaseInsensitiveSearch];    if (foundObj1.length >= 1 ||foundObj2.length >= 1||foundObj3.length >= 1||foundObj4.length >= 1)        return NO;    else        return YES;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end
#import 
@interface Fraction : NSObject@property int numerator,denominator;-(void) print;-(void) setTo:(int) n over:(int) d;-(Fraction *) add:(Fraction *) f;-(Fraction *) substract:(Fraction *) f;-(Fraction *) multiply:(Fraction *) f;-(Fraction *) divide:(Fraction *) f;-(void) reduce;-(double) convertToNum;-(NSString *) convertToString;@end////  Fraction.m//  ZJFraction_Calculator////  Created by Zoujie on 15/12/6.//  Copyright © 2015年 Zoujie. All rights reserved.//#import "Fraction.h"@implementation Fraction@synthesize numerator,denominator;-(void) setTo:(int)n over:(int)d{    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)            return @"0";        else            return @"1";    }    else if (denominator == 1)    {        return [NSString stringWithFormat:@"%i",numerator];    }    else        return [NSString stringWithFormat:@"%i/%i",numerator,denominator];}//添加一个分数到消息的接收器-(Fraction *) add:(Fraction *)f{//将两个分数相加    //a/b+c/d = ((a*d)+(b*c))/(b*d)    Fraction *result = [[Fraction alloc]init];        result.numerator = numerator* f.denominator + denominator *f.numerator;        result.denominator = denominator * f.denominator;        //存储相加后的结果    [result reduce];    return  result;}-(Fraction *) substract:(Fraction *)f{//将两个分数相减    //a/b - c/d = ((a*d) - (b*c))/(b*d)    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 d = denominator;    int temp;        if (u == 0)    {        return;    }    else if (u < 0 )    {        u = -u;    }        while ( d != 0) {        temp = u % d;        u = d;        d = temp;//        numerator /= u;//        denominator /= u;    }    numerator /= u;    denominator /= u;}@end
#import 
#import "Fraction.h"@interface Calculator : NSObject@property (strong,nonatomic) Fraction *operand1;@property (strong,nonatomic) Fraction *operand2;@property (strong,nonatomic) Fraction *accumulator;-(Fraction *) performOperation:(char) op;-(void) clear;@end#import "Calculator.h"@implementation Calculator@synthesize operand1,operand2,accumulator;-(id)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 substract:operand2];            break;        case '*':            result = [operand1 multiply:operand2];            break;        case '/':            result = [operand1 divide:operand2];            break;        default:            break;    }    accumulator.numerator = result.numerator;    accumulator.denominator = result.denominator;    return accumulator;}@end

转载于:https://my.oschina.net/u/2319073/blog/540246

你可能感兴趣的文章
Asp.net mvc4用JQuery插件实现异步上传
查看>>
使用组策略控制可移动存储访问
查看>>
监控平台实施方案
查看>>
RSA2012系列(2):HP谈他们的安全智能平台
查看>>
统帅转型:轻时尚时代挺进年轻领地
查看>>
Photoshop制作一只可爱的卡通小鸟
查看>>
飞鹤借力品质打造奶粉生态 胜算几何?
查看>>
1-2-Active Directory 域服务准备概述
查看>>
分享Silverlight/WPF/Windows Phone一周学习导读(07月25日-07月31日)
查看>>
理解并取证:动态路由协议RIP的工作原理
查看>>
演示:思科路由器到路由器的IOS镜像管理
查看>>
Lync 小技巧-33-BlueStacks安卓模拟器 For Lync 2013
查看>>
从云原生看企业云的未来
查看>>
2013款Mac Pro“神秘”主机详解
查看>>
解决FastJson 1.2.39的bug
查看>>
谈谈VIP漂移那点破事
查看>>
通过脚本案例学习shell(三) --- 通过交互式脚本自动创建Apache虚拟主机
查看>>
hao123联盟新政的机制效用
查看>>
vSphere 6.5密码正确不能登录解决方法
查看>>
ACM HDU 1020Encoding
查看>>