-1

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

我需要添加一个十进制按钮,我尝试将其添加为单独的 IBAction,或者将其添加到现有的按钮中,但总是出现问题。

我需要的是我的 - (IBAction)dot:(id)sender 按钮,用于执行添加小数点的操作,例如我想输入 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
4

1 回答 1

0

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

 NSString *digit = [[sender titleLabel] text];
    [display setText: [[display text] stringByAppendingString:digit]];
于 2013-11-12T00:09:41.723 回答