//
// main.m
// 计算机的简单类
//
// Created by herobin on 15/12/17.
// Copyright © 2015年 herobin. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Calculator : NSObject{
@public
double result;
}
-(void) clear;
-(void) jia:(double)value;
-(void) jian:(double)value;
-(void) cheng:(double)value;
-(void) chu:(double)value;
@end
@implementation Calculator
-(void) clear{
result=0;
NSLog(@"结果:%g",result);
}
-(void) jia:(double)value{
result+=value;
}
-(void) jian:(double)value{
result-=value;
}
-(void) cheng:(double)value{
result*=value;
}
-(void) chu:(double)value{
result/=value;
}
@end
/*int main(int argc, const char * argv[]) {
@autoreleasepool {
Calculator *Cal=[Calculator new];
[Cal clear];
[Cal jia:150.0];
[Cal chu:15.0];
[Cal jian:5];
[Cal cheng:10.0];
NSLog(@"the result is %g",Cal->result);
}
return 0;
}*/
int main(int argc,const char *argv[]){
@autoreleasepool {
Calculator *Cal=[Calculator new];
double value1,value2;
char operator;
[Cal clear];
NSLog(@"请输入运算,格式如x+y:");
scanf("%lf%c%lf",&value1,&operator,&value2);
Cal->result=value1;
switch (operator) {
case '+':
[Cal jia:value2];
NSLog(@"结果=%.2f",Cal->result);
break;
case '-':
[Cal jian:value2];
NSLog(@"结果=%.2f",Cal->result);
break;
case '*':
[Cal cheng:value2];
NSLog(@"结果=%.2f",Cal->result);
break;
case '/':
[Cal chu:value2];
NSLog(@"结果=%.2f",Cal->result);
break;
default:
break;
}
}
}
|
|