#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
//1、定义一个字典每个数字(key)对应相应的字符(value)
NSMutableDictionary *dict=[NSMutableDictionary dictionary]; dict.dictionary=@{@"0":@"zero",@"1":@"one",@"2":@"two",@"3":@"three",@"4":@"four",@"5":@"five",@"6":@"six",@"7":@"seven",@"8":@"eight",@"9":@"nine"};
//2、定义一个整型变量用于接收输入的数字
printf("请输入一个整数\n");
int a;
scanf("%d",&a);
//3、定义一个可变字符串用于接收数字对应的value值,
NSMutableString *mstr=[NSMutableString string];
//定义一个不可变数组用于存储输入的数字
NSString *str=[NSString stringWithFormat:@"%d",a];
for (int i=0; i<str.length; i++) {
unichar ch=[str characterAtIndex:i];
//printf("%c\n",ch);
//将ch转换成nsnumber
NSNumber *num=[NSNumber numberWithUnsignedShort:ch-48];
NSLog(@"%@",num);
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc] init];
NSString *num2 = [numberFormatter stringFromNumber:num];
//存储转换的元素@"zero",@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",@"nine"
[mstr appendString:[dict objectForKey:num2]];
//添加空格
[mstr appendString:@" "];
NSLog(@"%@",mstr);
}
}
return 0;
} |
|