0
所以我正在計算器應用程序和我的代碼目前不正確執行。我知道什麼是錯,但我不知道如何去解決它。目前,我有我的當前代碼ViewController.m當我按下添加按鈕:我沒有正確執行我的push/pop方法,有人可以幫我嗎?
#import "ViewController.h"
#import "CalcLogic.h"
@interface ViewController()
@property (weak, nonatomic) IBOutlet UILabel *display;
@property (weak, nonatomic) IBOutlet UILabel *lastOperation;
@property (strong, nonatomic) CalcLogic* logic;
@end
@implementation ViewController
double result = 0;
//Last operation entered into the calculator
NSString* lastEntered;
@synthesize logic;
-(IBAction)numPressed:(UIButton *)sender{
BOOL hasBeenCleared = [self.lastOperation.text isEqualToString:@"Clear"];
if ([self.display.text isEqualToString:@"0."]) {
self.display.text = sender.currentTitle;;
self.lastOperation.text = sender.currentTitle;;
[self.logic pushNumber:[sender.currentTitle doubleValue]];
}
else{
self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
if (self.lastOperation.text.length > 1 && hasBeenCleared != TRUE) {
self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
}
else {
self.lastOperation.text = sender.currentTitle;
}
[self.logic pushNumber:[sender.currentTitle doubleValue]];
}
}
-(IBAction)clearPressed:(UIButton *)sender{
self.display.text = @"0.";
self.lastOperation.text = @"Clear";
[self.logic clearStack];
result = 0;
}
-(IBAction)operation:(UIButton *)sender{
[logic pushOperation:sender.currentTitle];
NSString* resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;
if ([self.lastOperation.text isEqualToString:@"Clear"]) {
self.lastOperation.text = @"";
self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
}
else{
self.lastOperation.text = [self.lastOperation.text stringByAppendingString:sender.currentTitle];
}
}
-(IBAction)equalHit:(UIButton *)sender{
result = [self.logic performOperation];
self.display.text = [NSString stringWithFormat:@"%g", result];
}
我的問題是壓入和彈出對象數組。這些數組位於logic
,我試圖將數字推送到logic
中的兩個數組中的一個,並將運算符推送到對象中的另一個數組。但是,我必須做錯事,因爲我檢入控制檯時沒有任何東西被推入(據我所知)。我對這種語言仍然陌生,並且來自Java包裝。
既然被定義爲公共財產,也許它是由外部類分配。否則就不需要公共財產。 – rmaddy 2013-02-27 18:23:57
@rmaddy公共財產有很多用途。就目前而言,他的代碼沒有證明對象正在被初始化,所以這是我第一個假設它爲什麼不起作用,如果你在代碼中使用self.logic,那麼沒有'CalcLogic'類的進一步實現細節 – 2013-02-27 18:24:54
然後初始化它與自己像 self.logic = [[CalcLogic alloc] init]; – razibdeb 2013-02-27 18:27:39