#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; }
时间: 2024-10-11 09:44:07