A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© herobin 中级黑马   /  2016-1-6 23:06  /  573 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//
//  main.m
//  练习:整数变英语
//
//  Created by herobin on 15/12/18.
//  Copyright © 2015年 herobin. All rights reserved.
//


/*

代码的分组

用到一个预处理的指令
1 #pragma mark 分组的名称
2 #pragma mark - 分组的名称   //多增加一条线
3 #pragma mark - 只有一条线


注意:#pragma mark -     中划线后面不能有空格,否则出现两条线


*/

#import <Foundation/Foundation.h>

#pragma mark 数字变英语类
//定义一个类:数字变英语
@interface ShuToEnglish : NSObject

-(void) toEnglish:(int)shuZhi;

@end

@implementation ShuToEnglish

-(void) toEnglish:(int)shuZhi{
    switch (shuZhi) {
        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;
    }
}

@end


//位数类
#pragma mark - 位数类
@interface WeiShu : NSObject
{
    int _shu;
}
-(int)deWeiShu:(int)zheGeShu;
@end

@implementation WeiShu:NSObject

-(int)deWeiShu:(int)zheGeShu{
    int i=1;
    while(zheGeShu/10>0)
    {
        zheGeShu/=10;
        i++;
    }
    return i;
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int userNum;
        int weiShu;
        int a=0,b=0,c=0,d=0,e=0;
        int i=0;
        int wei[5];
        
        
        //调用两个类
        ShuToEnglish *ste=[ShuToEnglish new];
        WeiShu *ws=[WeiShu new];
        
        //输入一个不超过五位的整数
        NSLog(@"输入一个数(最多五位数):");
        scanf("%d",&userNum);
        
        weiShu=[ws deWeiShu:userNum];
        
        NSLog(@"weiShu=%d",weiShu);
        
        //通过位数把数的第一位分离
        switch (weiShu) {
            case 5:
                a=userNum/10000;
                b=(userNum-a*10000)/1000;
                c=(userNum-a*10000-b*1000)/100;
                d=(userNum-a*10000-b*1000-c*100)/10;
                e=userNum%10;
                break;
            case 4:
                a=userNum/1000;
                b=(userNum-a*1000)/100;
                c=(userNum-a*1000-b*100)/10;
                d=userNum%10;
                break;
            case 3:
                a=userNum/100;
                b=(userNum-a*100)/10;
                c=userNum%10;
                break;
            case 2:
                a=userNum/10;
                b=userNum%10;
                break;
            case 1:
                a=userNum;
               
            default:
                break;
        }
        
        NSLog(@"%d %d %d %d %d",a,b,c,d,e);
        
        //把每一位放在数组中
        wei[0]=a;
        wei[1]=b;
        wei[2]=c;
        wei[3]=d;
        wei[4]=e;
        
        
        //调用方法输出英语
        for(i=1;i<=weiShu;i++)
        {
            [ste toEnglish:wei[i-1]];
        }
        
    }
   
   
    return 0;
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马