objective c - Roman Numeral to Arabic -
my function part of roman numeral arabic conversion function. evaluates fine there pair cm, pair cd, there single character c, , combinations of cm, cd, m, d , c.
however, final character in string c (e.g. mcdccc), function crashes on final 'c'. ive put stack of nslog in there trace problem can't work out why crashing on final 'c'.
any welcome ive been trying figure out week. ps, ive rewritten switch function im getting same kind of problem.
-(nsstring *) decimaltoromannumeral : (nsstring*) romannumeral { nslog(@"roman numeral %@", romannumeral); int sumofdecimals = 0; int stringlength = [romannumeral length]; nslog(@"romannumeral length %i characters", stringlength); int arrayofprimitiveintegers[stringlength]; nslog(@"array size %i spaces", stringlength); (int c=0; c<[romannumeral length]; c++) { nslog(@"character @ counter %i %c", c, [romannumeral characteratindex:c] ); if ([romannumeral characteratindex:c] == 'm') { nslog(@"**********************************"); nslog(@"within evaluator m"); nslog(@"counter %i", c); arrayofprimitiveintegers[c] = 1000; nslog(@"value in array @ position %i %i", c, arrayofprimitiveintegers[c] ); sumofdecimals = sumofdecimals + arrayofprimitiveintegers[c]; nslog(@"sum of decimals %i", sumofdecimals); nslog(@"**********************************"); } else if ( ([romannumeral length]>1 ) && (([romannumeral characteratindex:c] == 'c') && ([romannumeral characteratindex:c+1] == 'd')) ) { nslog(@"**********************************"); nslog(@"within evaluator cd"); nslog(@"counter %i", c); arrayofprimitiveintegers[c] = -100; nslog(@"value in array @ position %i %i", c, arrayofprimitiveintegers[c] ); sumofdecimals = sumofdecimals + arrayofprimitiveintegers[c]; nslog(@"sum of decimals %i", sumofdecimals); nslog(@"**********************************"); } else if ( ([romannumeral length]>1 ) && (([romannumeral characteratindex:c] == 'c') && ([romannumeral characteratindex:c+1] == 'm')) ) { nslog(@"**********************************"); nslog(@"within evaluator cm"); nslog(@"counter %i", c); arrayofprimitiveintegers[c] = -100; nslog(@"value in array @ position %i %i", c, arrayofprimitiveintegers[c] ); sumofdecimals = sumofdecimals + arrayofprimitiveintegers[c]; nslog(@"sum of decimals %i", sumofdecimals); nslog(@"**********************************"); } else if ([romannumeral characteratindex:c] == 'd') { nslog(@"**********************************"); nslog(@"within evaluator d"); nslog(@"counter %i", c); arrayofprimitiveintegers[c] = 500; nslog(@"value in array @ position %i %i", c, arrayofprimitiveintegers[c] ); sumofdecimals = sumofdecimals + arrayofprimitiveintegers[c]; nslog(@"sum of decimals %i", sumofdecimals); nslog(@"**********************************"); } else if (([romannumeral length]==1 ) && ([romannumeral characteratindex:c] == 'c')) { nslog(@"**********************************"); nslog(@"within evaluator c"); nslog(@"counter %i", c); arrayofprimitiveintegers[c] = 100; nslog(@"value in array @ position %i %i", c, arrayofprimitiveintegers[c] ); sumofdecimals = sumofdecimals + arrayofprimitiveintegers[c]; nslog(@"sum of decimals %i", sumofdecimals); nslog(@"**********************************"); } else if ([romannumeral characteratindex:c] == 'c') { nslog(@"**********************************"); nslog(@"within evaluator c>1"); nslog(@"counter %i", c); arrayofprimitiveintegers[c] = 100; nslog(@"value in array @ position %i %i", c, arrayofprimitiveintegers[c] ); sumofdecimals = sumofdecimals + arrayofprimitiveintegers[c]; nslog(@"sum of decimals %i", sumofdecimals); nslog(@"**********************************"); } } nsstring * numberstring = [[nsstring alloc]init]; //numberstring = [nsstring stringwithformat:@"%d", decimalnumber]; return numberstring; } @end
romannumeral characteratindex:c+1
-- seem to out of bounds on final loop iteration.
you getting nsrangeexception?
Comments
Post a Comment