How to split integer into component digits

左心房为你撑大大i 提交于 2019-12-25 02:57:41

问题


I need to know how to split an integer into digits in the correct order, e.g. 1234 displays as 1 2 3 4. I want it to split the integer and display the digits as words, so 1234 = 1 2 3 4 = one two three four. This is what i've got so far. It splits an integer and displays the digits as words but they are in reversed order. Sorry if its a bit messy. Also i've only just begun learning objective c so I don't know about arrays and all that stuff yet.

 int number, right_digit, counter;

    counter = 1;

    NSLog(@"Enter your number");

    while (counter != 0) {
    scanf("%i", &number);


    do {

  right_digit = number % 10;
  number /= 10;



    if (right_digit == 1)
        NSLog(@"one");

    else if (right_digit == 2)
        NSLog(@"two");

    else if (right_digit == 3)
        NSLog(@"three");

    else if (right_digit == 4)
        NSLog(@"four");

    else if (right_digit == 5)
        NSLog(@"five");

    else if (right_digit == 6)
        NSLog(@"six");

    else if (right_digit == 7)
        NSLog(@"seven");

    else if (right_digit == 8)
        NSLog(@"eight");

    else if (right_digit == 9)
        NSLog(@"nine");

    else if (right_digit == 0)
        NSLog(@"zero"); 

    }
    while (number != 0);

    }
}
return 0;
}

回答1:


NSInteger num=1234;
NSInteger temp=num;

NSMutableArray *digits=[NSMutableArray new];
while(temp >0){
    NSInteger digit=temp%10; //4
    temp/=10;//123
    digits[digits.count]=@(digit);
}

for(NSNumber *d in [digits reverseObjectEnumerator]){
    switch([d integerValue]){
         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;
    }
}



回答2:


You are doing it essentially right, except that you should store the words (or the digits) as you go through the original number, and then "play them backwards" as you print them out. The simplest way to accomplish this would be using an NSMutableArray: add words to it as you go through your number, and then iterate the array in reverse order for printing.

One note is about your use of the switch: in cases when you do the same thing in all cases, and only your data is different, you should use an array or a dictionary for the lookup, like this:

NSString *digitWords[] = {@"zero", @"one", @"two", @"three", ...};

With this array in place, you could call

NSLog("%@", digitWords[digit]);

and get rid of the switch statement.




回答3:


just modified Anoop Vaidya answer to exact answer

NSInteger num=1234567890;
NSInteger temp=num;

NSMutableArray *digits=[NSMutableArray new];
while(temp >0){
    NSInteger digit=temp%10; //4
    temp/=10;//123
    [digits addObject:[NSString stringWithFormat:@"%d",digit]];

}

NSMutableArray *show = [NSMutableArray new];

for(NSNumber *d in [digits reverseObjectEnumerator]){
    switch([d integerValue]){
        case 0:[show addObject:@"Zero"];    break;
        case 1:[show addObject:@"One"];     break;
        case 2:[show addObject:@"Two"];     break;
        case 3:[show addObject:@"Three"];   break;
        case 4:[show addObject:@"Four"];    break;
        case 5:[show addObject:@"Five"];    break;
        case 6:[show addObject:@"Six"];     break;
        case 7:[show addObject:@"Seven"];   break;
        case 8:[show addObject:@"Eight"];   break;
        case 9:[show addObject:@"Nine"];    break;
    }
}
NSLog(@"%d=%@",num,[show componentsJoinedByString:@", "]);


来源:https://stackoverflow.com/questions/15728816/how-to-split-integer-into-component-digits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!