#import <Foundation/Foundation.h>
@interface Calculator : NSObject { @public
int _num1;
int _num2;
int _result;
char _ch; }
-(int)Ch;
@end
#import "Calculator.h"
@implementation Calculator
-(int)Ch { if (_ch == '+') {
return _num1 + _num2;
} else if (_ch == '-'){
return _num1 - _num2;
}else if (_ch == '*'){
return _num1 * _num2;
}else {
return _num1/_num2;
}
}
@end
#import <Foundation/Foundation.h> #import "Calculator.h"
int main(int argc, const char * argv[]) { @autoreleasepool {
Calculator * Result = [Calculator new];
NSLog(@"请输入需要运算的两个整数,(例如:1+1):");
scanf("%d%c%d",&Result->_num1,&Result->_ch,&Result->_num2);
int add = [Result Ch];
NSLog(@"%d %c %d = %d",Result->_num1,Result->_ch,Result->_num2,add);
} return 0; }
|