我的功能是罗马数字到阿拉伯语转换功能的一部分。如果有一对CM,一对CD,其中有一个字符C,以及CM,CD,M,D和C的所有组合,它会评估很好。
但是,如果字符串中的最后一个字符是C(例如MCDCCC),则函数会在最终的“C”上崩溃。我已经在那里放了一堆NSLog来追踪问题,但我无法弄清楚为什么它会在最后的'C'上崩溃。
任何帮助欢迎,因为我一直试图弄清楚这一周。 PS,我把它重写为切换功能,但我遇到了同样的问题。
-(NSString *) decimalToRomanNumeral : (NSString*) romanNumeral
{
NSLog(@"Roman Numeral is %@", romanNumeral);
int sumOfDecimals = 0;
int stringLength = [romanNumeral length];
NSLog(@"RomanNumeral Length is %i characters", stringLength);
int arrayOfPrimitiveIntegers[stringLength];
NSLog(@"Array Size is %i spaces", stringLength);
for (int c=0; c<[romanNumeral length]; c++)
{
NSLog(@"Character at counter %i is %C", c, [romanNumeral characterAtIndex:c] );
if ([romanNumeral characterAtIndex:c] == 'M')
{
NSLog(@"**********************************");
NSLog(@"Within Evaluator M");
NSLog(@"Counter is %i", c);
arrayOfPrimitiveIntegers[c] = 1000;
NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
NSLog(@"Sum of Decimals is %i", sumOfDecimals);
NSLog(@"**********************************");
}
else if ( ([romanNumeral length]>1 ) && (([romanNumeral characterAtIndex:c] == 'C') && ([romanNumeral characterAtIndex:c+1] == 'D')) )
{
NSLog(@"**********************************");
NSLog(@"Within Evaluator CD");
NSLog(@"Counter is %i", c);
arrayOfPrimitiveIntegers[c] = -100;
NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
NSLog(@"Sum of Decimals is %i", sumOfDecimals);
NSLog(@"**********************************");
}
else if ( ([romanNumeral length]>1 ) && (([romanNumeral characterAtIndex:c] == 'C') && ([romanNumeral characterAtIndex:c+1] == 'M')) )
{
NSLog(@"**********************************");
NSLog(@"Within Evaluator CM");
NSLog(@"Counter is %i", c);
arrayOfPrimitiveIntegers[c] = -100;
NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
NSLog(@"Sum of Decimals is %i", sumOfDecimals);
NSLog(@"**********************************");
}
else if ([romanNumeral characterAtIndex:c] == 'D')
{
NSLog(@"**********************************");
NSLog(@"Within Evaluator D");
NSLog(@"Counter is %i", c);
arrayOfPrimitiveIntegers[c] = 500;
NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
NSLog(@"Sum of Decimals is %i", sumOfDecimals);
NSLog(@"**********************************");
}
else if (([romanNumeral length]==1 ) && ([romanNumeral characterAtIndex:c] == 'C'))
{
NSLog(@"**********************************");
NSLog(@"Within Evaluator C");
NSLog(@"Counter is %i", c);
arrayOfPrimitiveIntegers[c] = 100;
NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
NSLog(@"Sum of Decimals is %i", sumOfDecimals);
NSLog(@"**********************************");
}
else if ([romanNumeral characterAtIndex:c] == 'C')
{
NSLog(@"**********************************");
NSLog(@"Within Evaluator C>1");
NSLog(@"Counter is %i", c);
arrayOfPrimitiveIntegers[c] = 100;
NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
NSLog(@"Sum of Decimals is %i", sumOfDecimals);
NSLog(@"**********************************");
}
}
NSString * numberString = [[NSString alloc]init];
//numberString = [NSString stringWithFormat:@"%d", decimalNumber];
return numberString;
}
@end
答案 0 :(得分:1)
romanNumeral characterAtIndex:c+1
- 这似乎超出了最终循环迭代的范围。
您收到NSRangeException?