在计算器中使用NSNumbers进行计算? [英] Doing calculations with NSNumbers in a calculator?
问题描述
我尝试使用NSNumbers进行计算并跟踪用户输入的数字..
Im trying to do the calculations using NSNumbers and keep track of the numbers that the user inputs..
问题是我的应用现在没有进行任何计算更新我的代码后追加。用户按下点按钮时的十进制值。哪个可以找到附加或在计算器中添加小数点功能。
The problem is that my app is not doing any calculations now after updated my code to append . the decimal value to value whenever the user press the dot button. Which can be found Append or Add the decimal point functionality in calculator.
我已经看到了跟踪用户输入并进行计算的正确方法是使用NSNumber但是因为我是Objective-c的新手,所以我一直在努力理解使用和实现它。
I have seen that the proper way to keep track of the inputs from the user and do the calculation is using the NSNumber but as I'm really new to Objective-c I have been struggling understanding the use and implementing it.
所以,希望有人可以指导我找到合适的解决方案。
So, hopefully someone could walk me through to find the proper solution.
这是标题......
This is the header...
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;
}
如果您有任何问题或需要有关我的计划的更多信息,请随意要求,我将提供尽可能多的信息或回答你们可能有的任何问题.. :)
If you guys have any question or need more information regarding my program please feel free to ask and I will provide as much information as possible or answer any questions that you guys may have.. :)
推荐答案
如何这有用吗?
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 *
This code shouldn't even work. Either SelectNumber is a float or it's a NSNumberFormatter*
这篇关于在计算器中使用NSNumbers进行计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!