OC习题

编写一个程序用于接受从终端输入的整数,提取并用英语现实这个数的每一个数字。例如输入932,显示:

nine

three

two

#import <Foundation/Foundation.h>
int main (int argc,const char * argv[])
{
    @autoreleasepool {
        int number, numberStorage, leftDigit, index, indexStorage;
        
        NSLog(@"Please, input the number that you would like to convert");
        scanf(" %i", &number);
        
        /*----Entering nest statements and loops section----*/
        
        if ( number != 0 )                                // if 'number' equals zero skip loops and output "Zero"
        {
            numberStorage = number;
            
            /*----Loop to obtain the number of digits entered by user and store in variable 'index'----*/
            
            for( index = 0; number !=0; ++index )
                number /= 10;
            
            indexStorage = index;
            number = numberStorage;                           // reset 'number' to the original input value
            
            /*----Loop to extract and printout each digit from left to right----*/
            
            for ( int i =1; i <= index; i++ )
            {
                
                while (indexStorage != 0 )
                {
                    leftDigit = number % 10;
                    number /= 10;
                    indexStorage--;
                }
                
                switch (leftDigit)
                {
                    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;
                }
                
                indexStorage = index - i;                        // reset 'indexStorage' to orginal 'index' value minus 'i'
                number = numberStorage;                           // reset 'number' to orginal user input value
            }
            /*----perform loop until 'i' is equal to index----*/ 
        }
        else
            NSLog(@"Zero");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值