//
// main.m
// iOS8期考核题
//
// Created by Binky Lee on 15/12/30.
// Copyright © 2015年 Binky Lee. All rights reserved.
//
#import <Foundation/Foundation.h>
#define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
static NSString * const keyProductId = @"productId";//商品ID
static NSString * const keyProductPrice = @"productPrice";//商品价格
static NSString * const keyProductName = @"productName";//商品名称
static NSString * const keyProductAmount = @"productAmount";//商品数量
int main(int argc, const char * argv[]) {
@autoreleasepool {
/**
* 第1题 请将你的身份证号的出生日期部分用*代替,并输出(例如:210304******2248)
* PS:要求除打印外,代码行数最多为3行,
*/
NSLog(@"第一题");
NSString *str=@"220106199408291813";
NSString *string = [str stringByReplacingOccurrencesOfString:@"19940829" withString:@"******"];
NSLog(@"%@",string);
/**
* 第2题 shoppingCart 为购物车中存储的商品数据,请对相同的商品进行商品合并,并输出合并后的商品名称,数量,单价,合计
*/
NSLog(@"第二题");
NSArray *shoppingCart = @[@{keyProductId:@"A1001",
keyProductName:@"iPod5",
keyProductPrice:@1488.0,
keyProductAmount:@1},
@{keyProductId:@"A1003",
keyProductName:@"iPhone6",
keyProductPrice:@4488.0,
keyProductAmount:@1},
@{keyProductId:@"A1001",
keyProductName:@"iPod5",
keyProductPrice:@1488.0,
keyProductAmount:@1},
@{keyProductId:@"A1002",
keyProductName:@"iPhone6s",
keyProductPrice:@5288.0,
keyProductAmount:@1},
@{keyProductId:@"A1003",
keyProductName:@"iPhone6",
keyProductPrice:@4488.0,
keyProductAmount:@1},
@{keyProductId:@"A1001",
keyProductName:@"iPod5",
keyProductPrice:@1488.0,
keyProductAmount:@1}];
int i=0,j=0,k=0;
for(NSDictionary *dic in shoppingCart){
//ipod5
if([dic[keyProductId] isEqual:@"A1001"]){
i++;
}
//iphone6
if([dic[keyProductId] isEqual:@"A1003"]){
j++;
}
//iPhone6s
if([dic[keyProductId] isEqual:@"A1002"]){
k++;
}
}
NSSet *set=[NSSet setWithArray:shoppingCart];
for(NSDictionary *dic in set){
int number=[dic[keyProductPrice] intValue];
if([dic[keyProductId] isEqual:@"A1001"]){
NSLog(@"商品名称: %@,数量:%d,单价:%@,合计:%d",dic[keyProductName],i,dic[keyProductPrice],i*number);
}
if([dic[keyProductId] isEqual:@"A1003"]){
NSLog(@"商品名称: %@,数量:%d,单价:%@,合计:%d",dic[keyProductName],j,dic[keyProductPrice],j*number);
}
if([dic[keyProductId] isEqual:@"A1002"]){
NSLog(@"商品名称:%@,数量:%d,单价:%@,合计:%d",dic[keyProductName],k,dic[keyProductPrice],k*number);
}
}
}
return 0;
}