如何将数字转换为文本?

时间:2011-07-01 20:50:11

标签: ios objective-c

我一直在学习Kochan书中的Objective-C,但我无法弄清楚如何做这个练习计划。只有奇数编号的练习在网上列出,这个是偶数。练习是将数字转换为单词。因此,如果输入“932”,程序应返回:“九三二”

我使用了dowhile循环,但这些词语向后出现,如“两个三九”。任何人都可以建议一种适用于此的技术吗?

int number, digit;


NSLog(@"Type in your integer.");
scanf("%i", &number);


 do
 {
     digit = number % 10;

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

     number /= 10;
 }
while (number != 0);

8 个答案:

答案 0 :(得分:12)

这不是完全你想要的,但是为了你的考虑:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterSpellOutStyle];

NSString *s = [f stringFromNumber:[NSNumber numberWithInt:932]];
NSLog(@"%@", s);
[f release];

这将记录:

nine hundred and thirty-two

同样,它不是你想要的“九三二”,但它也很好而且简短。 :)

答案 1 :(得分:5)

由于您要将数字添加到字符串中,并且您想要从右到左计算它们,因此请在每个新数字前面加上字符串。类似的东西:

numberString = [NSString stringWithFormat:@"%@ %@", theNewNumber, numberString];

其中newNumber是一个字符串(如@“six”),而numberString是你想要输出的字符串......

(哦,不要忘记在开始循环之前初始化numberString ......类似于:

NSString *numberString = @"";

=====

根据您刚刚发布的代码,您可以通过数学方式进行操作,也可以预先挂起这样的字符串:

将此变量放在.h文件中:

NSString *numberString;

然后把它放在.m:

- (void) prependNumber:(NSString *)num {
  numberString = [NSString stringWithFormat:@"%@ %@", num, numberString];
}

NSLog(@"Type in your integer.");
scanf("%i", &number);
numberString = @"";


 do
 {
     digit = number % 10;

     if (digit == 0)
         [self prependNumber:@"zero"];
     if (digit == 1)
         [self prependNumber:@"one"];
     if (digit == 2)
         [self prependNumber:@"two"];
     if (digit == 3)
         [self prependNumber:@"three"];
     if (digit == 4)
         [self prependNumber:@"four"];
     if (digit == 5)
         [self prependNumber:@"five"];
     if (digit == 6)
         [self prependNumber:@"six"];
     if (digit == 7)
         [self prependNumber:@"seven"];
     if (digit == 8)
         [self prependNumber:@"eight"];
     if (digit == 9)
         [self prependNumber:@"nine"];

     number /= 10;
 }
while (number != 0);

NSLog (@"%@", numberString);

答案 2 :(得分:2)

如果你能够将数字转换为单词,即使向后输出,听起来就像你已经到了一半。

假设您正在遍历数据,递增索引,只需从数字的字符长度开始,向后递减索引,然后反转输出。

如果没有看到您的实际代码,我们无法帮助您。 ;)

答案 3 :(得分:2)

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int a, b, number, logNum, nThNum;

    NSLog(@"Please enter a valid integer: ");
    scanf("%d", &number); // read input as a decimal integer

    if (!number) // if zero or something other than a number is entered output zero
        NSLog(@"Zero");
    else if (number < 0) { // convert negatives to something that can be used
        number = -number;
        NSLog(@"(negative)"); // but output negative first then continue as usual
    }

    logNum = (log10(number) + 1); // find how many digits there are in the number

    for (int j=0; j < logNum; j++) {// loop based on number of digits
        a = pow(10,logNum-j);
        b = pow(10,logNum-1-j);
        nThNum = (number % a) / b;// find the nth digit in a number, in our case 1st
        switch (nThNum) {// output current digit that was found
            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;
        }
    }

    [pool drain];
    return 0;
}

好了,既然您已经发布了代码,那么如果您首先反转数字,那么您的方法会很有效。所以,你可以写一个简短的例程来做,然后使用你自己的代码。

答案 4 :(得分:1)

作为一项学习练习,我修改了Dave的代码:

+(NSString*)doIt:(NSString*)inString delimiter:(NSString*)delimiter{
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterSpellOutStyle];
    NSMutableString* outString= [[NSMutableString alloc]init];
    for (int i=0; i< [inString length]; i++) {
        unsigned char oneChar= [inString characterAtIndex:i];
        if (oneChar>47 && oneChar<58) {
            NSString* temp=[f stringFromNumber:[NSNumber numberWithUnsignedChar:oneChar-48]];
            [outString appendFormat:@"%@",temp];
            [outString appendString:delimiter];
        }
    }
    [f release];
    [outString autorelease];
    return outString;
}

答案 5 :(得分:0)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

    // insert code here...
    int number;     //store the value the user enter
    int lastDigit;  //pick of the last digit of the integer
    int tempNum;    //a temporary storage of the integer the user enter
    int count = 0;  //used to count how many digits were entered
    int count2;     //going to be use as a duplicate of count



    NSLog(@"Enter an integer");
    scanf("%i", &number);
    tempNum = number;


    //Loop to find out how many digits were entered
    for (; number != 0; number /= 10) {
        count +=1;
        }

    //Loop to convert the numbers into words
    for (; count != 0; count -= 1) {
        count2 = count;     //set count2 to count so the for and while loop use them independently
        number = tempNum;   //restore the value entered by by the user to the number variable


        //Loop to reverse the order of the last digit

        while (count2 != 0) {           //loops to the same number of counts to get the first digit
            lastDigit = number % 10;    //picks off the last value in the integer
            number /= 10;               //enables the loop to set the last value of the integer to zero
            count2 -=1;                 //loops one less time to get the numbers from front to back

        }
        //switch statements
        switch (lastDigit) {
            case 9:
                NSLog(@"nine");
                break;
            case 8:
                NSLog(@"eight");
                break;
            case 7:
                NSLog(@"seven");
                break;
            case 6:
                NSLog(@"six");
                break;
            case 5:
                NSLog(@"five");
                break;
            case 4:
                NSLog(@"four");
                break;
            case 3:
                NSLog(@"three");
                break;
            case 2:
                NSLog(@"two");
                break;
            case 1:
                NSLog(@"one");
                break;
            case 0:
                NSLog(@"zero");
                break;
            default:
                break;
        }
    }
   }
return 0;
}

答案 6 :(得分:0)

我在嵌套循环下使用嵌套,但相信这是有效的

    int i, j, number, reversenumber = 0;

    NSLog(@" Input Number:");
    scanf( "%i", &number);

    if (number != 0)
        // chekcing for zero entry
    {
        for (;number!= 0; number = number/10)
            //for reversing the number entered so that the words doesn't come reversed when printed
        {
            i = number%10;
            reversenumber = reversenumber * 10 + i;
        }

        NSLog(@"Reverser Number for the input number is %i", reversenumber);
        // mid routine check to print the reversed number

        while(reversenumber != 0)
        {
            j = reversenumber % 10;
            switch (j)
            {
                case 9:
                    NSLog(@"nine");
                    break;
                case 8:
                    NSLog(@"eight");
                    break;
                case 7:
                    NSLog(@"seven");
                    break;
                case 6:
                    NSLog(@"six");
                    break;
                case 5:
                    NSLog(@"five");
                    break;
                case 4:
                    NSLog(@"four");
                    break;
                case 3:
                    NSLog(@"three");
                    break;
                case 2:
                    NSLog(@"two");
                    break;
                case 1:
                    NSLog(@"one");
                    break;
                default:
                    NSLog(@"zero");
            }
            reversenumber /= 10;
        }

    }
    else
        NSLog(@"Zero");
}

    return 0;

}

答案 7 :(得分:0)

非常简单,有很多方法,但我通常会尝试这个:

$(document).ready(function() {
  question_block();
});

function question_block() {
  $.ajax({
    // settings here...
    success: function(result) {
      // build html...
      $("#questions").append(question);

      count_blocks(); // call here instead
    },
    error: function(x, s, e) {
      console.dir(x);
      console.log(s);
      console.log(e);
    }
  });
}