//身份证的前4位代表省份和地区(例如4201代表湖北省武汉市),最后一位代表性别(1或3代表男性) //编写一个OC程序,通过身份证号码判断某人是否是武汉人及其性别 #import<Foundation/Foundation.h>
int main() { NSString *num = [NSString stringWithFormat:@"420123199206262871"]; //创建字符串
NSMutableString *num2 = [[NSMutableString alloc] init]; //定义一个新的字符串来装num得前四位
NSString *num4 = @"4201";
NSString *num5 = @"1";
NSString *num6 = @"3";
for (int i = 0; i < 4; i++) //遍历 { char c = [num characterAtIndex:i]; //取出字符串前四位
NSMutableString *num3 = [NSMutableString stringWithFormat:@"%c",c];
[num2 appendString:num3]; //将钱四位装进num2 }
BOOL b = [num2 isEqualToString:num4]; //比较与4201是不是一样的
if (b == 1) { NSLog(@"是武汉!"); } else NSLog(@"不是武汉");
char c = [num characterAtIndex:17]; //取出身份证最后一位
NSString *s = [NSString stringWithFormat:@"%c",c]; BOOL d = [s isEqualToString:num5];
BOOL e = [s isEqualToString:num6];
if(d == 1 || e == 1) { NSLog(@"男"); }
else NSLog(@"不是男");
return 0;
}
|