码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发之-计算器的改进

时间:2015-07-22 09:30:18      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:ios开发   objective-c   ios   计算器   方法调用   

#import <Foundation/Foundation.h>

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 <Foundation/Foundation.h>
#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 = '\0';
    
    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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

ios开发之-计算器的改进

标签:ios开发   objective-c   ios   计算器   方法调用   

原文地址:http://blog.csdn.net/u012701023/article/details/46997447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!