ios开发之-计算器的改进

Posted clnchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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开发之简单计算器

iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用(代码片段

iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用(代码片段

iOS代码片段CodeSnippets

iOS之UI--彩虹动画进度条学习和自主封装改进

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情