它应该做什么:每次将新字符附加到 NSMutableString 数字时,将输入的字符串组所有数字并打印出来。然后,如果当前字符不是数字,它会检查它的 + 或 x,或 *,-。如果它是其中之一,那么它将它附加到一个数组并打印出来。
它在做什么:输出中文
`Please enter math: 12+56x45
2012-05-02 23:52:06.538 CALC[1921:403] 퀱
2012-05-02 23:52:06.541 CALC[1921:403] 퀱퀲
2012-05-02 23:52:06.542 CALC[1921:403] running array (
"+"
)
2012-05-02 23:52:06.543 CALC[1921:403] 퀱퀲퀵
2012-05-02 23:52:06.544 CALC[1921:403] 퀱퀲퀵퀶
2012-05-02 23:52:06.544 CALC[1921:403] 퀱퀲퀵퀶큸
2012-05-02 23:52:06.545 CALC[1921:403] 퀱퀲퀵퀶큸퀴
2012-05-02 23:52:06.546 CALC[1921:403] 퀱퀲퀵퀶큸퀴퀵`
问题:我相信它与 unichar 有关,char current = [InputString characterAtIndex:i];
但是当我在没有它的else if
部分的情况下运行代码时,它可以正常工作。正如您所看到的,尽管作为字符串一部分的字符数是预期的数字,但问题似乎是它们使用了错误的语言。
我的代码:
int main ()
{
char userInput[99];
NSMutableString *number = [NSMutableString string];
int i;
printf( "Please enter math: " );
scanf( "%s", userInput );
fpurge( stdin );
NSString *InputString = [NSString stringWithUTF8String:userInput];
NSMutableArray *broken = [NSMutableArray array];
for (i=0; i < [InputString length]; i++) {
char current = [InputString characterAtIndex:i];
NSString *cur = [NSString stringWithFormat:@"%c" , current];
if (isalnum(current)) {
[number appendString:[NSString stringWithCharacters:¤t length:1]];
NSLog(@"%@", number);
}
else if (current == '+'|| current == 'x'||current == '*'||current == '-') {
[broken addObject:[NSString stringWithFormat:cur]];
NSLog(@"running array %@", broken);
}
}
return 0;
}