你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios開發之-計算器的改進

ios開發之-計算器的改進

編輯:IOS開發綜合
#import 

extern double add(double x,double y);

extern double subtract(double x,double y);

extern double multiply(double x,double y);

extern double divide(double x,double y);
#import 
#import MathOperation.h

BOOL isAnOperator(const char value)
{
    return ((value == '+')||(value == '-')||(value =='*')||(value == '/'));
}

int main(int argc,const char * argv[])
{
    double result = 0;
    char operator = '';
    
    NSString * equation = [NSString stringWithUTF8String:argv[0]];
    
    NSArray *eqParts = [equation componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    for (int n = 0; n < [eqParts count]; n++) {
        
        NSString * argString = [eqParts objectAtIndex:n];
        char firstChar = [argString characterAtIndex:0];
        
        if(isAnOperator(firstChar))
        {
            operator = firstChar;
            continue;
        }
        
        double newValue = [argString doubleValue];
        
        switch (operator) {
            case '+':
                result = add(result, newValue);
                break;
            case '-':
                result = subtract(result,newValue);
                break;
            case '*':
                result = multiply(result,newValue);
                break;
            case '/':
                result = divide(result,newValue);
            default:
                break;
        }
    }
    
    NSLog(@%.3f,result);
    
}
//@implementation Calculator
//
//@end

#include MathOperation.h

double add(double x,double y)
{
    return x + y;
}

double subtract(double x,double y)
{
    return x - y;
}

double multiply(double x,double y)
{
    return  x * y;
}

double divide(double x,double y)
{
    return x / y;
}


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved