//// 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