计算器添加小数按钮

时间:2013-11-11 23:11:08

标签: ios objective-c cocoa-touch decimal

我是编码新手,所以请不要评价我。我找到了问题的解决方案,但仍然 不明白。

我需要添加一个小数点按钮,我尝试将其添加为单独的IBAction,或将其添加到现有的,但是一直出现问题。

我需要的是我的 - (IBAction)点:( id)发送按钮,执行添加小数点的操作,例如我想输入334.21或1.65,所以我可以用这个值执行操作。

在我的情况下需要建议。非常感谢。

.h文件

#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController{

    IBOutlet UILabel *displayLabel;
    double x, y;
    NSInteger operation;
    BOOL xFlag, yFlag;
} 

- (IBAction)clearAll:(id)sender;
- (IBAction)clear:(id)sender;

- (IBAction)inverseSign:(id)sender;
- (IBAction)dot:(id)sender;

- (IBAction)digit:(id)sender;
- (IBAction)operation:(id)sender;


@end

.m文件

#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

enum{
    OP_PLUS     = 101,
    OP_MINUS    = 102,
    OP_MULT     = 103,
    OP_DIV      = 104
};

- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (IBAction)clearAll:(id)sender {

    x = 0;
    y = 0;

    xFlag = NO;
    yFlag = NO;

    [self showScreen];
}

- (IBAction)clear:(id)sender {

    x = 0;
    [self showScreen];
}

- (IBAction)inverseSign:(id)sender {

    x = -x;
    [self showScreen];
}

- (IBAction)dot:(id)sender {

   // ???
}

- (IBAction)digit:(id)sender {

    if (xFlag) {
        y = x;
        x = 0;
        xFlag = NO;
    }

    x = (10.0f * x) + [sender tag];

    [self showScreen];
}

- (IBAction)operation:(id)sender {

    if (yFlag && !xFlag) {

        switch (operation) {

        case OP_PLUS:
            x = y + x;
            break;

        case OP_MINUS:
            x = y - x;
            break;

        case OP_MULT:
            x = y * x;
            break;

        case OP_DIV:
            x = y / x;
            break;

        default:
            break;
        }
    }

    y = x;
    xFlag = YES;
    yFlag = YES;

    operation = [sender tag];

    [self showScreen];
}

- (void) showScreen {

    NSString *str = [NSString stringWithFormat:@"%0.12g", x];
    [displayLabel setText:str];

}

@end

1 个答案:

答案 0 :(得分:0)

如果您想将点发送到您的显示器,并且单独的数字用于数字和点按钮:

 NSString *digit = [[sender titleLabel] text];
    [display setText: [[display text] stringByAppendingString:digit]];
相关问题