代码之家  ›  专栏  ›  技术社区  ›  ThinkCode

在Objective C中帮助使用数组

  •  0
  • ThinkCode  · 技术社区  · 16 年前

    例如:

    110
    
    Should give the following o/p : 
    
    one
    one 
    zero
    

    #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;
    }
    
    3 回复  |  直到 16 年前
        1
  •  2
  •   walkytalky    16 年前

    有很多方法可以实现你想要的。既然你在谈论动态数组,那么显而易见的事情就是使用NoFoundation类NSMutabReLayx来构建你的数字堆栈,尽管这确实有一个缺点,就是需要通过NSCONT进行一个小小的舞蹈来存储和检索:

    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];
    

    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");
    }
    

    这种方法表面上更简单,但肯定更容易出错(事实上,由于我没有运行这段代码,所以几乎可以肯定其中会有错误!)

    另一种方法是完全围绕这个问题,利用 sprintf 要按正确顺序提取数字:

    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");
    }
    

        2
  •  1
  •   mpontillo    13 年前

    我先设置这个初始值:

    NSArray* myArray = [NSArray arrayWithObjects: @"zero", @"one", @"two", @"three",
                        @"four", @"five", @"six", @"seven", @"eight", @"nine", nil];
    
        3
  •  -1
  •   Feldur    16 年前

    当然没必要,但他的问题是数组,而不是重新解析问题。