开发者

Help with Arrays in Objective C

Problem : Take an integer as input and print out number equivalents of each number from input. I hacked my thoughts to work in this case but I know it is not an efficient solution.

For instance :

110

Should give the following o/p : 

one
one 
zero

Could so开发者_运维知识库meone throw light on effective usage of Arrays for this problem?

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int input, i=0, j,k, checkit;
    int temp[i];

    NSLog(@"Enter an integer :");
    scanf("%d", &input);
    checkit = input;

    while(input > 0)
    {
        temp[i] = input%10;
        input = input/10;
        i++;
    }

    if(checkit != 0)
    {
    for(j=i-1;j>=0;j--)
    {
        //NSLog(@" %d", temp[j]);
        k = temp[j];
        //NSLog(@" %d", k);

        switch (k) {

            case 0:
                NSLog(@"zero");
                break;
            case 1:
                NSLog(@"one");
                break;
            case 2:
                NSLog(@"two");
                break;
            case 3:
                NSLog(@"three");
                break;
            case 4:
                NSLog(@"four");
                break;
            case 5:
                NSLog(@"five");
                break;
            case 6:
                NSLog(@"six");
                break;
            case 7:
                NSLog(@"seven");
                break;
            case 8:
                NSLog(@"eight");
                break;
            case 9:
                NSLog(@"nine");
                break;

            default:
                break;
        }

    }
    }
    else
        NSLog(@"zero");

    [pool drain];
    return 0;
}


There are a number of ways to achieve what you want. Since you're talking about dynamic arrays, the obvious thing to do would be to use the foundation class NSMutableArray to build your stack of digits, although this does have the disadvantage of requiring a little dance through NSNumber to store and retrieve:

static NSString names[] = { @"zero", @"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine" };
NSMutableArray* digits = [[NSMutableArray alloc] initWithCapacity:10] // arbitrary initial capacity
while ( input > 0 )
{
    [digits insertObject:[NSNumber numberWithInt:input%10] atIndex:0];
    input /= 10;
}

if ( [digits count] )
{
    for ( int i = 0; i < [digits count]; ++i )
    {
        NSLog(@"%@", names[[[digits objectAtIndex:i] intValue]]);
    }
}
else
{
    NSLog(@"zero");
}

[digits release];

Otherwise, you could use a C array, but rather than initialising the array to zero length (which really seems like a Bad Idea), you could initialise it to the maximum capacity you will ever need, to wit: the number of digits of the biggest int. This may vary depending on the word length you're compiling for, but you can determine that before you compile. A 32 bit int can be up to 10 digits long; a 64 bit 20. The code then is reasonably similar:

int digits[20];  // hard coded array size constants are evil, of course
int last = 0;
while ( input > 0 )
{
    digits[last++] = input % 10;
    input /= 10;
}

if ( last )
{
    while ( last --> 0 )
    {
        NSLog ( @"%@", names[digits[last]] );
    }
}
else
{
    NSLog(@"zero");
}

This approach is superficially simpler, but certainly more error prone. (Indeed, since I haven't run this code there are almost guaranteed to be errors in it!)

Another way to do this is to sleaze around the problem altogether and use sprintf to extract the digits in the right order:

char digits[21]; // hard coded array size constants are *still* evil
sprintf(digits, "%d", input);
int ptr = 0;
while ( digits[ptr] )
{
    NSLog(@"%@", names[digits[ptr++]]);
}

if ( ptr == 0 )
{
    NSLog(@"zero");
}

This probably counts as cheating, though.

Note that scanf will handle a leading minus sign in the input, so the input number may be negative; dealing with this situation is left as an exercise.


I'd start by setting up this initializer:

NSArray* myArray = [NSArray arrayWithObjects: @"zero", @"one", @"two", @"three",
                    @"four", @"five", @"six", @"seven", @"eight", @"nine", nil];


Of course it's unnecessary, but his question was arrays, not re-parse the problem.

As far as NGTechie's reply, I now don't understand what you're trying to do. Clarify?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜