问题
I'm trying to add the dot functionality to my calculator but I have been struggling getting it to work.
So far what it does is that adds the decimal point but then when I press another number what it does is add 0 regardless the number you pressed..
Example: user input 9 (dot operation/decimal) and then 5 = to 9.0
Which of course is totally incorrect.
Finally, I'm posting specifically the dot function and the number 9 which are the ones that produce that answer.
Header file..
int Method;
float SelectNumber;
float RunningTotal;
bool DecimalActived;
@interface ViewController : UIViewController{
IBOutlet UILabel *Screen;
}
-(IBAction)Number9:(UIButton *)sender;
-(IBAction)Dot:(UIButton *)sender;
@end
Implementation file..(UPDATED)
-(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];
}
}
Calculations (UPDATED)
- (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 {
SelectNumber = [[NSNumberFormatter alloc] init];
[SelectNumber setNumberStyle:NSNumberFormatterDecimalStyle];
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 need more reference to the code please let me know and will supply it or any question to help in this regard let me know too and I will provide the information :)
回答1:
The problem can be simplified by realizing that the numerical value of the input doesn't matter until you must do a calculation. You can let the buttons and the text field just concern themselves with textual behavior, then extract a value any time you need it. e.g.
// do this for digits and @"."
- (IBAction)button9:(id)sender {
[self appendDigit:@"9"];
}
- (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.myTextField.text isEqualToString:@"0"] && ![digit isEqualToString:@"."]) {
self.myTextField.text = digit;
} else {
self.myTextField.text = [myTextField.text stringByAppendingString:digit];
}
}
// negative sign is special too...
self.myTextField.text = [@"-" stringByAppendingString:myTextField.text];
Then, only later, when you need to do a calculation, you can get the numerical value like this:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *number = [f numberFromString:self.myTextField.text];
Finally, another nice representational pattern is to keep numerical state as NSNumbers, converting to scalar types when math is needed...
NSNumber *runningTotal;
NSNumber *currentValue;
// say the current operation is multiplication
float runningTotalFloat = [runningTotal floatValue];
float currentValueFloat = [currentValue floatValue];
float newRunningTotal = runningTotalFloat * currentValueFloat;
runningTotal = [NSNumber numberWithFloat: newRunningTotal];
回答2:
You should let the user enter the number into an NSMutableString
, let's call it buffer
.
- (void)appendCharacter:(char)c
{
if(isdigit(c)) {
[buffer appendFormat:@"%c", c];
} else if(c == '.') {
if([buffer rangeOfString:@"."].location != NSNotFound) {
// user tried to add more than 1 decimal point
BEEP();
} else {
[buffer appendString:@"."];
}
} else {
// user writes some other character.
// do whatever you like
}
}
To get the float value, you can call -floatValue on buffer.
来源:https://stackoverflow.com/questions/23172537/append-or-add-the-decimal-point-functionality-in-calculator