标签:style io ar sp for on 文件 2014 log
不要笑话我,这是我凌晨一点写完的代码。尽量把白天学的知识用上了。昨天学的是NSNumber,NSString,CGRange,CGPiont,CGSize,CGRect。一些类的用法,结构的用法。掌握的不熟练,所以做作业的时候就尽量把学的用上。不多说,上代码。
// // main.m // vip // // Created by yuan on 14-12-12. // Copyright (c) 2014年 yuan. All rights reserved. // #import <Foundation/Foundation.h> #import "vip.h"
//写了个块,和类里面的那个方法是一样的作用,这两部分可以写到一起。 void (^block)(char *,char *)=^(char * a,char * b) { while(*b) { *a++=*b++; } *a='\0'; }; int main(int argc, const char * argv[]) { @autoreleasepool { vip * user = [[vip alloc]init]; struct brithday brithday2; //我这段代码用了GOTO语句,感觉不太好,但是实现了功能,等着想个办法不用GOTO语句了 NSLog(@"请输入用户名:"); char str1[17]; label1: scanf("%s",str1);
//用户名传进来,备份一个name3,因为要求是字母数字组成,我先把name全部变成大写,这样少一部分判断。但是后面还要赋值,所以留了一个备份。 NSString * name2 =[NSString stringWithUTF8String:str1]; NSString * name3 =[NSString stringWithUTF8String:str1];
//转化成大写字母 name3 = [name2 uppercaseString];
//判断符合条件的。只由数字字母组合的名字 if(([name2 length]>=8&&[name2 length]<=16)&&(([name3 compare:@"A"]==1&&[name3 compare:@"Z"]==-1)||([name2 compare:@"0"]==1&&[name2 compare:@"9"]==-1))) { user.name=name2; }else { NSLog(@"请重新输入用户名:");
//名字不合法,就提示重新输入,并跳到scanf那里,继续接收用户名 goto label1; } NSLog(@"请输入密码:"); char str2[17]; label2: scanf("%s",str2);
//密码这里和用户名类似,我写的不健壮,因为制图着完成功能了只 NSString * pwd2 =[NSString stringWithUTF8String:str2]; // pwd2 = [pwd2 uppercaseString]; if([pwd2 length]>=8&&[pwd2 length]<=16) { // if(([pwd2 compare:@"A"]==1&&[pwd2 compare:@"Z"]==-1)||([pwd2 compare:@"0"]==1&&[pwd2 compare:@"9"]==-1)) // { // // } user.pwd=pwd2; }else { NSLog(@"请重新输入密码:"); goto label2; } NSLog(@"请输入生日:"); char y[5],m[5],d[5]; scanf("%s %s %s",y,m,d);
//接收生日,并把生日转化成<span style="font-family: Arial, Helvetica, sans-serif;">NSString,因为后面要对生日格式的合法性做判断,转化成字符串对象以后,取值,然后比较。</span> // NSString * year2=[NSString stringWithUTF8String:y]; label3:; NSString * month2=[NSString stringWithUTF8String:m]; label4:; NSString * day2=[NSString stringWithUTF8String:d]; // vip * u1=[user initWithThree:name2 :pwd2 :brithday2 ]; user = [user initWithThree:name2 :pwd2 :brithday2 ]; if([month2 intValue]<=0||[month2 intValue]>12) { NSLog(@"您输入的月份不合法,请重新输入:"); scanf("%s",m); goto label3; }else if([day2 intValue]<=0||[day2 intValue]>31) { NSLog(@"您输入的日期不合法,请重新输入:"); scanf("%s",d); goto label4; }else{}
//把接收了的生日赋值给生日 block(brithday2.year,y); block(brithday2.month,m); block(brithday2.day,d); //构造会员 user = [user initWithThree:name2 :pwd2 :brithday2];
//打印信息 [user showMemberInfo]; } return 0; }这是main函数,下面是类的。
// // vip.m // vip // // Created by yuan on 14-12-12. // Copyright (c) 2014年 yuan. All rights reserved. // #import "vip.h" #import <time.h> @implementation vip @synthesize name,pwd,number,sorce; //因为方法里要用到给字符串赋值,就用了这个笨办法,挨个赋值。 -(void)copyString:(char *)a :(char *)b { while(*b) { *a++=*b++; } *a='\0'; } //三参构造 -(vip *)initWithThree:(NSString *)name1 :(NSString *)pwd1 :(struct brithday)brithday1 { if (self=[super init]) { self.name=name1; self.pwd=pwd1; self.sorce=0;
//把传进来的生日赋给对象的生日 [self copyString:self->Brithday1.year :brithday1.year]; [self copyString:self->Brithday1.month :brithday1.month]; [self copyString:self->Brithday1.day :brithday1.day];
//把对象的生日转化成NSString对象。 NSString * year1=[NSString stringWithUTF8String:self->Brithday1.year]; NSString * month1=[NSString stringWithUTF8String:self->Brithday1.month]; NSString * day1=[NSString stringWithUTF8String:self->Brithday1.day];
//产生三位随机数,用于组成编号 srand((unsigned)time(NULL)); NSNumber * num = [[NSNumber alloc]initWithInt:rand()%1000];
//编号前缀 NSString * tmp = @"DS";
//“合成”编号。这里的这个初始化没成功,不知道为什么,没细看,有空再看看。 [self.number appendFormat:@"%@%@%@%@%@",tmp,year1,month1,day1,num];
// self->Brithday.year=brithday1.year; return self; }else { return self; } }
//无参构造 -(vip *)initWithAll { if(self=[super init]) { return self; }else { return self; } } -(void)showMemberInfo {
//把对象生日转化成<span style="font-family: Arial, Helvetica, sans-serif;">NSString,感觉这里可以封装一下,这段代码用了不止一次。也可以想办法不用转化。突击的时候,只想着完成功能了,没细处理。</span> NSString * year1=[NSString stringWithUTF8String:self->Brithday1.year]; NSString * month1=[NSString stringWithUTF8String:self->Brithday1.month]; NSString * day1=[NSString stringWithUTF8String:self->Brithday1.day]; // srand((unsigned)time(NULL)); // NSNumber * num = [[NSNumber alloc]initWithInt:rand()%1000]; // [self.number appendFormat:@"DS",year1,month1,day1,num];
//因为初始化的时候,编号不知为啥不能正确输出,就在打印的时候再“合成”了一遍 srand((unsigned)time(NULL)); NSNumber * num = [[NSNumber alloc]initWithInt:(rand()%1000)]; NSString * tmp = @"DS"; [self.number appendFormat:@"%@%@%@%@%@",tmp,year1,month1,day1,num];
//打印全部信息 NSLog(@"\n会员名:%@\n生日:%@年%@月%@日\n会员编号:%@%@%@%@%@\n会员积分:%i分",self.name,year1,month1,day1,@"DS",year1,month1,day1,num,self.sorce); } @end下面是头文件
// // vip.h // vip // // Created by yuan on 14-12-12. // Copyright (c) 2014年 yuan. All rights reserved. // #import <Foundation/Foundation.h> @interface vip : NSObject {
//题目要求生日用结构体 struct brithday { char year[5]; char month[5]; char day[5]; }Brithday1; }
//属性,会员账户,密码,编号,积分 @property NSString * name; @property NSString * pwd; @property NSMutableString * number; @property int sorce; //三参构造函数。里面把没传参的初始化了。 -(vip *)initWithThree:(NSString *)name1 :(NSString *)pwd1 :(struct brithday)Brithday;
//无参构造。我没有做处理,直接用了super的init。 -(vip *)initWithAll; //打印会员所有信息 -(void)showMemberInfo; @end
标签:style io ar sp for on 文件 2014 log
原文地址:http://blog.csdn.net/low_key_acm/article/details/41904811