学生字典计算年龄差 随机50个数

OC第四讲 数组、字典、集

1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出

2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())

3、 创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组

4、 选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值

5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组

王政-成都睿峰科技有限公  15:25:29

研究性知识点:

1、NSSet  KVC

*/

//  main.m

//  Homework4

//

//  Created by rimi on 15/5/21.

//  Copyright (c) 2015年 rimi. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "Student.h"

int main(int argc, const char * argv[]) {

@autoreleasepool {

#pragma mark --1、取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出

NSString *string = @"123-456-789-000";

//替换

NSString *newString =  [string stringByReplacingOccurrencesOfString:@"-" withString:@""];

NSLog(@"newString = %@",newString);

//可变字符串删除

//遍历可变字符串,进行删除

#pragma mark --2、随机获得100个50-100的数字字符串,存入一个数组,最后打印输出这个数组(arc4random())

//2

//        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:100];

//        for (int i = 0; i < 100; i ++) {

//            NSString * string = [NSString stringWithFormat:@"%d",arc4random() % 51 + 50];

//            [mutableArray addObject:string];

//        }

////       NSLog(@"compare = %@",[mutableArray sortedArrayUsingSelector:@selector(compare:)]);

//        NSLog(@"mutableString = %@",mutableArray);

//        NSLog(@"fjjj= %ld",mutableArray.count);

//

//        NSSet *set = [NSSet setWithArray:mutableArray];

//        NSLog(@"set = %@",set);

#pragma mark - 3、 创建一个Student类,将学员信息存放进一个字典,将3个学员信息的字典存放到一个数组

//  4、 选取第一个学员和最后一个学员输出其姓名,并计算他们年龄的差值

// 5、自学NSSet,然后讲第二题中的重复数字去掉,输出结果数组

Student * student1 = [Student studentWithName:@"tom1"

age:20

gender:@"男"

code:@"110101199403021210"];

Student * student2 = [[Student alloc] initWithName:@"tom2"

age:14

gender:@"女"

code:@"110101200010100303"];

Student * student3 = [Student studentWithName:@"tom3"

age:12

gender:@"男"

code:@"11010120120303101"];

NSDictionary * dictionary1 = @{student1.name : student1};

NSDictionary * dictionary2 = @{student2.name : student2};

NSDictionary * dictionary3 = @{student3.name : student3};

NSArray * array = @[dictionary1, dictionary2, dictionary3];

NSLog(@"array = %@",array);

Student * student = [[Student alloc] init];

int result = [student differenceBetweenAges:array];

NSLog(@"result = %d", result);

}

return 0;

}

Student.h文件

#import <Foundation/Foundation.h>

@interface Student : NSObject

{

NSString * _name;//姓名

NSInteger _age;//年龄

NSString *_gender;//性别

NSString * _code;//身份证号

}

- (instancetype)initWithName:(NSString *)name

age:(NSInteger)age

gender:(NSString *)gender

code:(NSString *)code;

+ (instancetype)studentWithName:(NSString *)name

age:(NSInteger)age

gender:(NSString *)gender

code:(NSString *)code;

- (void)setName:(NSString *)name;

- (NSString *)name;

- (NSInteger)age;

- (int)differenceBetweenAges:(NSArray *)array;

- (NSComparisonResult)compareAge:(Student *)student;

@end

Student.m文件

#import "Student.h"

@implementation Student

- (instancetype)initWithName:(NSString *)name

age:(NSInteger)age

gender:(NSString *)gender

code:(NSString *)code

{

//    NSLog(@"%s", __FUNCTION__);

self = [super init];

if (self) {

_name = name;

_age =age;

_gender = gender;

_code = code;

}

return self;

}

+ (instancetype)studentWithName:(NSString *)name

age:(NSInteger)age

gender:(NSString *)gender

code:(NSString *)code

{

//    NSLog(@"%s", __FUNCTION__);

Student * student = [[Student alloc] initWithName:name

age:age

gender:gender

code:code];

return student;

}

- (void)setName:(NSString *)name

{

_name = name;

}

- (NSString *)name

{

return _name;

}

- (NSInteger)age

{

return _age;

}

- (int)differenceBetweenAges:(NSArray *)array

{

NSMutableArray * array1 = [NSMutableArray arrayWithArray:array];

NSDictionary *dictionary1 = array1[0];

NSLog(@"arr = %@",array1[0]);

Student * student1 = [[dictionary1 allValues] firstObject];

NSLog(@"value = %@",[dictionary1 allValues]);

NSDictionary *dictionary2 = array1[2];

Student * student2 = [[dictionary2 allValues] firstObject];

NSLog(@"student1 name = %@,student2 name = %@",student1.name,student2.name);

//取到学生年龄

//得到差值

return abs((int)student1.age - (int)student2.age);

}

- (NSComparisonResult)compareAge:(Student *)student

{

if (_age < student.age) {

return NSOrderedAscending;

}

if (_age > student.age) {

return NSOrderedDescending;

}

return NSOrderedSame;

}

- (NSString *)description

{

return [NSString stringWithFormat:@"name:%@,age:%ld,gender:%@,code:%@", _name, _age, _gender, _code];

}

@end

课堂代码

//  main.m

//  0521

//

//  Created by rimi on 15/5/21.

//  Copyright (c) 2015年 ruifeng. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "Person.h"

int main(int argc, const char * argv[]) {

@autoreleasepool {

NSString *string [email protected]"abc";

NSString *string1 [email protected]"cd";

NSInteger count = 3;

NSNumber *number = [NSNumber numberWithInteger:3];

NSInteger counts[3];

//oc中的集合

//1.只能放对象

//2.基本数据类型需要格式化才能放

//3.每个元素都不能为nil,所有元素依次放完,最后添加nil作为结束符

NSArray *array = [NSArray arrayWithObjects:@0,string,string1,nil];

NSLog(@"%@",array);

NSArray *array1 = [NSArray arrayWithObjects:string,string1,nil];

NSLog(@"%@",array);

//数组的3个初始化

NSArray *array0 = [[NSArray alloc]initWithObjects:string1,string,nil];

NSArray *array2 = @[string,string1];//不要加nil

//数组的操作

//取值  index

id string4 = [array objectAtIndex:0];

[string4 appendFormat:@"ab"];

NSString *string5 = [array objectAtIndex:1];//取到的值必须与存放

[array firstObject];

[array lastObject];

//截取数组

//        NSArray *newArray = [array subarrayWithRange:(0,2)];

//拼接数组

NSArray *newArray19 = [array arrayByAddingObject:@"abc"];

//@"1",@"2",@"3"

//@"4",@"5"

//

NSArray *array7 = @[@"1",@"2",@"3"];

//        array7

NSArray *array8 = [NSArray arrayWithObjects:@"4",@"5",nil];

NSArray *array9 = [array7 arrayByAddingObjectsFromArray:array8];

NSLog(@"%@",array9);

//数组与字符串的转化

NSString *stringAll= [array9 componentsJoinedByString:@"-"];

NSArray *array10 = [stringAll componentsSeparatedByString:@"-"];

NSLog(@"%@",stringAll);

//数组排序

NSArray *numbers [email protected][@"1",@"01",@"11",@"12",@"4"];

SEL sel = @selector(compare:);

NSArray *sortedArray = [numbers sortedArrayUsingSelector:sel];

NSLog(@"%@",sortedArray);

NSString *text = @"good morning ios program";

//        NSString *chars = @"asfesfiuahgfiasghisag";

//        NSArray *charects = [chars componentsSeparatedByCharactersInSet:];

//        NSLog(@"%@",sortedArray)

//将它按照单词首字母排序,并生成新的字符串

NSArray *words = [text componentsSeparatedByString:@" "];//将语句用空格分割,成单词的数组

//        NSLog(@"@",words);

NSArray *sortedWords = [words sortedArrayUsingSelector:@selector(compare:)];

NSString *newText = [sortedWords componentsJoinedByString:@" "];//用空格

NSLog(@"%@",newText);

//============================================================================

//字典  索引

//字典:用键值对(key-value)的形式存放对象数据的集合

NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"张三",@"name",nil];

NSDictionary *dic1 = [[NSDictionary alloc]dictionaryWithObjectsAndKeys:@"张三",@"name",nil];

NSDictionary *dic2 = @{@"name":@"张三",

@"age":@"20"};//key-value,key在前,每个键值对为一个元素

NSLog(@"%@",dic2);

NSDictionary *dic3 = [[NSDictionary dictionaryWithObjects:@"123",@"456",@"789"] forKeys:@[@"1"@"4"@"7"]];

NSLog(@"%@",dic3);

//假设一个人有姓名,年龄,

Person *per = [[[Person alloc]init];

per.name = @"Tom";

per.age = 20;

per.birthday [email protected]"19880920";

NSDictionary *personInfo = [NSDictionary dictionaryWithObjectsAndKeys:per.name,@"name",

per.age,@"age",

per.birthday,@"birthday",nil];

NSLog("@"%@",personInfo);

//建立一个新的Person,将字典中的数据取出来放到它的属性里

Person *newPerson = [[Person alloc]init];

newPerson.name = [personInfo objectForKey:@"name"];

newPerson.age = [[personInfo objectForKey:@"age"]integerValue];

newPerson.birthday = [personInfo objectForKey:@"birthday"];

NSDictionary *personInfo = [NSDictionary ]

//研究性知识点:KVC

//字典中不可能出现相同的key

//字典也不能放nil

//mutable 数组和字典的遍历运算

NSMutableArray *mArray = [NSMutableArray array];

[mArray addObject:@"abc"];

[mArray addObject:@"def"];//数组添加元素,直接加在最后

NSLog(@"%@",mArray);

[mArray removeObject:@"abc"];//移除所有等于它的元素

[mArray removeObjectAtIndex:0];//只移除那个位置上的元素

NSLog(@"%@",mArry);

//可变数组的其他方法

[mArray insterObject:@"ghi" atIndex:0];//插入到指定下标的位置

//排序

[mArray sortUsingSelector:@selector(compare:)];

//将20个1~20的随机数字字符串放到数组,并排序

NSMutableArray *[numberArray array];

for (NSInteger i=0;i<20;i++) {

//制造20个随机数数字符串

NSInteger number = arc4random()%20+1;

NSString *numberString = [NSString stringWithFormat:@"%ld",number];

[numberArray addObject:numberString];

}

[numberArray sortUsingSelector:@selector(compare:)];

NSLog(@"%@",numberArray);

//将上述数组中,大于10的数去掉

//不能在遍历数组的同时删除元素

//建立标记对象的数组,储存要删除的元素,

for (NSInteger i=0;i<20;i++) {

NSString *string = [numberArray objectAtIndex:i];

}

for (NSString *aNumber in numberArray) {//列举法

if (aNumber.integerValue >10) {

[deleteArray addObject:aNumber];

}

}

[numberArray removeObjectsInArray:deleteArray];

NSLog(@"%@",[numberArray];

//NSMutableDictionary

NSMutableDictionary *mDic = [NSMutableDictionary dictionary];

[mDic setObject:@"张三" forKey:@"name"];

[mDic removeObjectForKey:@"name"];//操作字典,绝大部分情况都是操作key

//字典的遍历(重点)

for (NSString *key in mDic) {

//                        NSLog(@"%@",sring);

NSString *value = [mDic objectForKey:key];

//如果要删除,保存要删除的key,循环结束之后一次性删

}

//        id obj [2] = {string,string1};

//        NSArray *array3 = [NSArray arrayWithObjects:objs count:2];

}

return 0;

}

时间: 2024-11-06 18:04:19

学生字典计算年龄差 随机50个数的相关文章

iOS 获取当前时间以及计算年龄(时间差)

获取当前时间 NSDate *now = [NSDate date]; NSLog(@"now date is: %@", now); NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarU

SPSS根据生日计算年龄并分组

COMPUTE Age=DATEDIFF($TIME,BirthDate,"years"). EXECUTE. SAVE OUTFILE='D:\MaryKay\Data\PRD\ConsultantInfoPrd.sav' /COMPRESSED. SAVE OUTFILE='D:\MaryKay\Data\PRD\ConsultantInfoPrd.sav' /COMPRESSED. STRING AgeGroup1 (A8). RECODE Age (19 thru 24='19

Java 计算年龄

1 public static String getAgeTxt(String birthTime,String beginTime,int level){ 2 if(StringUtils.isBlank(birthTime)||StringUtils.isBlank(beginTime)){ 3 System.out.println("参数中有空值!"); 4 } 5 6 int year = 0,month=0,day=0,hour=0; 7 8 Date birthDate =

Java 根据年月日精确计算年龄

1 import java.text.SimpleDateFormat; 2 import java.util.Calendar; 3 import java.util.Date; 4 5 /** 6 * Created by qing on 2017/3/28. 7 */ 8 public class AgeUtils { 9 // 根据年月日计算年龄,birthTimeString:"1994-11-14" 10 public static int getAgeFromBirthT

[华为机试练习题]46.计算二进制数的0的个数

题目 描述: 输入一个10进制数字,请计算该数字对应二进制中0的个数,注意左数第一个1之前的所有0都不需要计算.不需要考虑负数的情况. 题目类别: 位运算 难度: 初级 运行时间限制: 无限制 内存限制: 无限制 阶段: 入职前练习 输入: 要计算的十进制非负数 输出: 二进制中第一个1之后0 的个数 样例输入: 2 样例输出: 1 代码 /*--------------------------------------- * 日期:2015-07-03 * 作者:SJF0115 * 题目:计算二

通过出生年月日按要求计算年龄的算法

最近项目里碰到个需求,有关于出生年月日计算年龄的,无奈网上搜了搜,感觉都不满足客户的需求只得自己动手了. 不废话,上需求 上算法: 类似于上面这个 yyyy-MM-dd 给一个这个值 算出来了多大年龄 l 不足一月新生儿显示日龄.2 不足一年婴幼儿显示月龄+日龄,如3个月12天.3 超过1周岁到12周岁,显示年龄+月龄,如12岁3个月. public static String getAgeAchen(Context context, Calendar birthday, Calendar no

java根据身份证计算年龄

/** * 根据身份证计算年龄 * * @param idcard * @return */ public static Integer idCardToAge(String idcard) { Integer selectYear = Integer.valueOf(idcard.substring(6, 10)); //出生的年份 Integer selectMonth = Integer.valueOf(idcard.substring(10, 12)); //出生的月份 Integer

依据出生日期Date 计算年龄

依据出生日期计算年龄 public class DateGetAge { public static int getAge(Date birthDay) throws Exception { Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { throw new IllegalArgumentException( "The birthDay is before Now.It's unbelievable!"

问题:oracle 计算年龄;结果:oracle中根据生日计算年龄的问题

SELECT FLOOR(MONTHS_BETWEEN(SYSDATE,birthday)/12,1) FROM ltteacherinfo where name='朱雪东111'这个报错ORA 00909 :invalid number of arguments这个是怎么错了,计算年龄该怎么算,年龄要整数 --计算员工年龄SELECT FLOOR(MONTHS_BETWEEN(SYSDATE,birthdate)/12) FROM   ps_personal_data