我正在尝试将点功能添加到我的计算器中,但我一直在努力让它工作。
到目前为止它的作用是添加小数点,但是当我按下另一个数字时,无论你按下的数字是什么,它都会加0 ...
示例:用户输入9(点操作/十进制)然后5 =到9.0
这当然是完全错误的。
最后,我特意发布了点函数和数字9,这些是产生答案的函数。
标题文件..
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 {
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;
}
如果你们需要更多参考代码,请告诉我并提供它或任何问题,以帮助我也知道,我将提供信息:)
答案 0 :(得分:2)
通过认识到输入的数值在您必须进行计算之前并不重要,可以简化问题。您可以让按钮和文本字段仅关注文本行为,然后在您需要时随时提取值。 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];
然后,只有在稍后,当您需要进行计算时,您可以得到如下数值:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *number = [f numberFromString:self.myTextField.text];
最后,另一个很好的代表性模式是将数值状态保持为NSNumbers,在需要数学时转换为标量类型...
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];
答案 1 :(得分:0)
您应该让用户在NSMutableString
中输入号码,我们称之为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
}
}
要获取浮点值,可以在缓冲区上调用-floatValue。