IOS计算器的实现

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
//
//  main.m
//  Calcultor

/*
            计算器的简单实现
               1.加法
               2.减法
               3.乘法
               4.除法
            实现两个数的运算
 */
#import <Foundation/Foundation.h>
#import "Calcultor.h"
#import "Menu.h"

int main(int argc, const char * argv[])
{
    Calcultor * cal = [[Calcultor alloc]init];
    
    //[cal setNumber1:10];
    
    //[cal setNumber2:10];
    
    //[cal setResult:0];
    
    //[cal setCalcultorOperator:'+'];
    

//     [cal initWithNumber1:10 andInitWithNumber2:10 andInitWithCalcultorOperator:'+'];
    //[cal counter];
    //NSLog(@"%@",cal);
    double number1;
    double number2;
 //   double result;
    char operator;

    NSLog(@"请输入一个算式例如A+B:\n");
    scanf("%lf",&number1);
    scanf("%c",&operator);
    scanf("%lf",&number2);
    [cal setNumber1:number1];
    [cal setNumber2:number2];
  //[cal setCalcultorOperator:operator];
 //   [cal counter:operator];
    
    NSLog(@"您计算的结果是=%.lf",[cal counter:operator]);
    
    
//    
//    Menu * menu = [[Menu alloc]init];
//    
//    [menu showMenu];
    
    return 0;
}
#import <Foundation/Foundation.h>
#import "Calcultor.h"

@interface Menu : NSObject

{
    Calcultor *  _cal;
}
/**显示菜单选项*/
-(void)showMenu:(Calcultor *)cal;

@end

#import "Menu.h"

@implementation Menu

/**显示菜单选项*/
-(void)showMenu:(Calcultor *)cal;
{
    
//    NSLog(@"菜单");
//    NSLog(@"1.计算 + - * /");
//    NSLog(@"2.计算一个数的平方");
//    NSLog(@"请输入您的的选项:");
//    int selectNumber;
//    scanf("%d",&selectNumber);
//    switch () {
//        case :
//            break;
//            
//        default:
//            break;
//    }

}
@end

#import <Foundation/Foundation.h>

@interface Calcultor : NSObject
{
    @public
    /**用来记录操作数1*/
    double _number1;
    
    /**用来记录操作数2*/
    double _number2;
    
    /**用来记录结果*/
    
    double _result;
    
    /**用来记录操作符*/
    char   _calcultorOperator;
}
//@property声明
@property double number1;
@property double number2;
@property double result;
@property char calcultorOperator;

/**加法*/
-(double)add;

/**减法*/
-(double)subtract;

/**乘法*/
-(double)multiply;

/**除法*/
-(double)divide;

-(double)square;

/**计算器的简单实现*/
-(double)counter:(char)calcultor;


//-(void)initWithNumber1:(double)number1 andInitWithNumber2:(double)number2 andInitWithCalcultorOperator:(char)calcultor;

@end

#import "Calcultor.h"

@implementation Calcultor
-(id)init
{
    if(self = [super init])
    {
        _result = 0.0;
    }
    return self;
}
/**加法*/
- (double)add
{
    return (_number1 + _number2);
    //NSLog(@"%.1lf",_result);
}

/**减法*/
- (double)subtract
{
    return _number1 - _number2;
    //NSLog(@"%.1lf",_result);
}

/**乘法*/
- (double)multiply
{
    return _number1 * _number2;
    //NSLog(@"%.1lf",_result);
}

/**除法*/
- (double)divide

{
    return _number1 / _number2;
    //NSLog(@"%.1lf",_result);
}


-(double)square
{
    return _number1 * _number1;
}


/**计算器的简单实现*/
-(double)counter:(char)calcultor
{
    //char calcultor;//局部变量
    
        switch (calcultor) {
            case '+':
                _result = [self add];
                break;
            case '-':
                _result = [self subtract];
                break;
            case '*':
                _result = [self multiply];
                break;
            case '/':
                _result = [self divide];
                break;
            default:
               break;
        }
    return _result;
}


//- (void)initWithNumber1:(double)number1 andInitWithNumber2:(double)number2 andInitWithCalcultorOperator:(char)calcultor
//{
//    _number1 = number1;
//    _number2 = number2;
//    _calcultorOperator = calcultor;
//}


/**!!!调试用的!!!*/
-(NSString *)description
{
    return [NSString stringWithFormat:@"操作数1:%.1lf 操作数2:%.1lf 结果 : %.1lf 操作符 : %c",_number1,_number2,_result,_calcultorOperator];
}
@end


标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Android自定义类似于QQ的消息提示框

下一篇:iOS秒表功能实现