我尝试使用NSNumbers进行计算并跟踪用户输入的数字..
问题是我的应用程序在更新我要添加的代码后现在没有进行任何计算。用户按下点按钮时的十进制值。可以找到Append or Add the decimal point functionality in calculator。
我已经看到跟踪用户输入并进行计算的正确方法是使用NSNumber但是因为我是Objective-c的新手,所以我一直在努力理解使用和实现它。 / p>
所以,希望有人能指导我找到合适的解决方案。
这是标题......
int Method;
float SelectNumber;
float RunningTotal;
bool DecimalActived;
@interface ViewController : UIViewController{
IBOutlet UILabel *Screen;
}
-(IBAction)Number9:(UIButton *)sender;
-(IBAction)Dot:(UIButton *)sender;
@end
这是实施文件..
-(IBAction)Number9:(UIButton *)sender{
[self appendDigit:@"9"];
}
- (IBAction)Dot:(UIButton *)sender {
NSString *currentText = Screen.text;
if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
[self appendDigit:@"."];
}
}
- (void)appendDigit:(NSString *)digit {
// handle two special cases: append to only zero means just replace
// but append decimal point to zero is a regular append
if ([self->Screen.text isEqualToString:@"0"] && ![digit isEqual:@"."]) {
self->Screen.text = digit;
} else {
self->Screen.text = [Screen.text stringByAppendingString:digit];
}
}
- (IBAction)Percent:(UIButton *)sender {
[self MySwitch];
Method = 5;
SelectNumber = 0;
DecimalActived = FALSE;
Screen.text = [NSString stringWithFormat:@"%.2g", RunningTotal];
}
- (IBAction)PositiveOrNegative:(UIButton *)sender {
[self MySwitch];
Method = 6;
SelectNumber = 0;
DecimalActived = FALSE;
Screen.text = [NSString stringWithFormat:@"%g", RunningTotal];
}
-(IBAction)Equals:(UIButton *)sender{
[self MySwitch];
Method = 0;
SelectNumber = 0;
DecimalActived = FALSE;
Screen.text = [NSString stringWithFormat:@"%g", RunningTotal];
}
-(IBAction)AllClear:(UIButton *)sender{
Method = 0;
RunningTotal = 0;
SelectNumber = 0;
Screen.text = [NSString stringWithFormat:@"0"];
}
- (double) MySwitch {
NSNumberFormatter SelectNumber = [[NSNumberFormatter alloc] init];
[SelectNumber setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber RunningTotal = [SelectNumber numberFromString:self->Screen.text];
if (RunningTotal == 0) {
RunningTotal = SelectNumber;
} else{
switch (Method) {
case 1:
RunningTotal = RunningTotal * SelectNumber;
break;
case 2:
RunningTotal = RunningTotal / SelectNumber;
break;
case 3:
RunningTotal = RunningTotal - SelectNumber;
break;
case 4:
RunningTotal = RunningTotal + SelectNumber;
break;
case 5:
RunningTotal = RunningTotal / 100;
break;
case 6:
if(RunningTotal > 0){
RunningTotal = - RunningTotal;
} else{
RunningTotal = + RunningTotal;
}
break;
default:
break;
}
}
return RunningTotal;
}
如果您有任何问题或需要有关我的计划的更多信息,请随时提出,我将提供尽可能多的信息或回答您可能有的任何问题.. :)
答案 0 :(得分:0)
标题看起来应该更像这样:
// this uses more conventional (lowercase) property names
// and removes a couple that looked extraneous
@interface ViewController : UIViewController
@property(strong,nonatomic) IBOutlet UILabel *screen;
@property(assign,nonatomic) NSInteger method;
@property(strong,nonatomic) NSNumber *runningTotal;
@end
当用户按下数字,十进制,减号时,会附加到screen
标签。当用户按下操作按钮时,记录操作的整数并记录NSNumber
标签的当前(screen
)值。做计算...
- (void)doComputation {
float screenF = [[self screenValue] floatValue];
float runningF = [self.runningTotal floatValue];
float result;
switch (self.method) {
case 1:
result = runningF * screenF;
break;
case 2:
result = (screenF == 0.0)? 0.0 : runningF / screenF;
break;
case 3:
result = runningF - screenF;
break;
case 4:
result = runningF + screenF;
break;
default:
break;
}
self.screen.text = [NSString stringWithFormat:@"%0.8f", result];
self.runningTotal = [NSNumber numberWithFloat:result];
}
(屏幕值,就像你拥有它一样......)
- (NSNumber *)screenValue {
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
return [f numberFromString:self.screen.text];
}
答案 1 :(得分:0)
这是如何运作的?
NSNumberFormatter SelectNumber = [[NSNumberFormatter alloc] init];
[SelectNumber setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber RunningTotal = [SelectNumber numberFromString:self->Screen.text];
if (RunningTotal == 0) {
RunningTotal = SelectNumber;
} else{
switch (Method) {
case 1:
RunningTotal = RunningTotal * SelectNumber;
break;
此代码甚至不起作用。 SelectNumber是浮点数,也可以是NSNumberFormatter *