Ios development-calculator improvement, ios development Calculator

Source: Internet
Author: User

Ios development-calculator improvement, ios development Calculator

#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;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.