我不要仅仅只做一个Manager, 我要努力成为一个Creator! Fighting!
This Is A Beginning To Be Big A Man!
//
// ViewController.m
// Calc
//
// Created by ChenJackson on 7/1/16.
// Copyright © 2016 ChenJackson. All rights reserved.
//
#import "ViewController.h"
#import "Calculator.h"
@interface ViewController ()
@end
@implementation ViewController
{
SEL Op;
bool isDigitEnd;
int setPoint;
long int currentDigit;
int countOp;
Calculator *myCalc;
NSMutableString *displayString;
}
@synthesize display,display1;
-(void) processDigit: (long int) Digit
{
if (setPoint) {
myCalc.CurrentNum = myCalc.CurrentNum + (float) Digit / (setPoint * 10);
[displayString appendString: [NSMutableString stringWithFormat:@"%i", (int)Digit]];
display1.text = displayString;
}
else {
myCalc.CurrentNum = myCalc.CurrentNum * 10 + Digit;
[displayString appendString: [NSMutableString stringWithFormat:@"%i", (int)Digit]];
display1.text = displayString;
}
}
-(void) storeValue
{
myCalc.Num1 = (double) myCalc.CurrentNum;
myCalc.CurrentNum = 0;
}
-(void) processOp: (char) theOp
{
isDigitEnd = YES;
NSString *opStr;
switch (theOp) {
case '+' :
opStr = @" + ";
Op = @selector(execPlus);
break;
case '-' :
opStr = @" - ";
Op = @selector(execMinus);
break;
case 'x' :
opStr = @" x ";
Op = @selector(execMultiply);
break;
case '/' :
opStr = @" / ";
Op = @selector(execDivided);
break;
case '%' :
opStr = @" % ";
Op = @selector(execMod);
break;
default:
break;
}
[displayString appendString: opStr];
display1.text = displayString;
}
-(void) showResult
{
[myCalc performSelector: Op];
if ( myCalc.Error ) {
myCalc = [[Calculator alloc] init] ;
display.text = @"Error";
displayString = [NSMutableString stringWithString: @""];
myCalc.Error -= 1;
}
else {
display.text = [NSMutableString stringWithFormat:@"%g", [myCalc Accumulator]];
}
myCalc.Num1 = myCalc.Accumulator;
myCalc.CurrentNum = 0;
}
-(IBAction) clickDigit: (UIButton *) sender
{
isDigitEnd = NO;
long int digit = sender.tag;
[self processDigit: digit];
currentDigit = digit;
}
-(IBAction) clickPoint
{
setPoint = 1;
[displayString appendString: @"."];
display1.text = displayString;
}
-(IBAction) clickMinus
{
if (countOp > 0) {
[self showResult];
}
else {
[self storeValue];
}
countOp++;
setPoint = 0;
[self processOp: '-'];
}
-(IBAction) clickPlus
{
if (countOp) {
[self showResult];
}
else {
[self storeValue];
}
countOp++;
setPoint = 0;
[self processOp: '+'];
}
-(IBAction) clickMultiply
{
if (countOp) {
[self showResult];
}
else {
[self storeValue];
}
countOp++;
setPoint = 0;
[self processOp: 'x'];
}
-(IBAction) clickDivided
{
if (countOp) {
[self showResult];
}
else {
[self storeValue];
}
countOp++;
setPoint = 0;
[self processOp: '/'];
}
-(IBAction) clickMod
{
if (countOp) {
[self showResult];
}
else {
[self storeValue];
}
countOp++;
setPoint = 0;
[self processOp: '%'];
}
-(IBAction) clickEqual
{
if ( countOp == 0 ) {
if (isDigitEnd == NO) {
myCalc.Accumulator = myCalc.CurrentNum;
display.text = [NSMutableString stringWithFormat:@"%g", myCalc.Accumulator];
myCalc.CurrentNum = 0;
}
else
display.text = @"0";
}
else
[self showResult];
isDigitEnd = YES;
countOp = 0;
setPoint = 0;
Op = @selector(Num1);
myCalc.CurrentNum = 0;
myCalc.Num1 = myCalc.Accumulator;
displayString = [NSMutableString stringWithString: @""];
display1.text = @"=";
}
-(IBAction) clickAC
{
isDigitEnd = YES;
myCalc.CurrentNum = 0;
countOp = 0;
setPoint = 0;
myCalc = [[Calculator alloc] init];
myCalc.Num1 = 0;
displayString = [NSMutableString stringWithString: @""];
display.text = @"";
display1.text = @"";
}
-(IBAction) clickDel
{
if (isDigitEnd == NO) {
myCalc.CurrentNum = (myCalc.CurrentNum - currentDigit) / 10;
}
// else
}
- (void)viewDidLoad {
[super viewDidLoad];
displayString = [NSMutableString stringWithCapacity:40];
displayString = [NSMutableString stringWithString:@""];
[self clickAC];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25521690/viewspace-2121545/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25521690/viewspace-2121545/