[关闭]
@fiy-fish 2015-07-15T12:11:35.000000Z 字数 1017 阅读 1271

day05-01-数组练习

Objective-c


  1. // main.m
  2. // day05-01-数组练习
  3. //
  4. // Created by Aaron on 15/7/7.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Company.h"
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. Company *c = [[Company alloc] init];
  12. NSLog(@"%ld",[c leftForMonth:11]);
  13. }
  14. return 0;
  15. }

  1. #import <Foundation/Foundation.h>
  2. /*
  3. 2.设计一个工厂类,创建一个手机厂
  4. 该手机厂每个月生产1000部手机,A品及格率80%(不及格的B品要回炉再造)
  5. 从7月到11月是销售旺季,每个月能卖出1100部手机
  6. 其他月份是销售淡季,每个月能卖出500部手机
  7. 问:1月创建厂,实现一个方法,要求输入该年某月,求出当月该手机厂有多少库存?
  8. */
  9. @interface Company : NSObject
  10. {
  11. NSInteger _product;
  12. float _percent;
  13. NSArray *_saleArray;
  14. }
  15. -(NSInteger)leftForMonth:(NSInteger)month;
  16. @end

  1. #import "Company.h"
  2. @implementation Company
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _saleArray = @[@500,@500,@500,@500,@500,@500,@1100,@1100,@1100,@1100,@1100,@500];
  8. _product = 1000;
  9. _percent = 0.8;
  10. }
  11. return self;
  12. }
  13. -(NSInteger)leftForMonth:(NSInteger)month
  14. {
  15. //1.求总产量
  16. NSInteger total = _product*_percent*month;
  17. //2.求销售量
  18. NSInteger sale = 0;
  19. for(int i = 0; i < month; i++)
  20. {
  21. sale = sale + [_saleArray[i] integerValue];
  22. }
  23. //3.求库存
  24. return total-sale >= 0 ?total-sale:0;
  25. }
  26. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注