Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions 21651030张灏/project01/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cal: Month.m MonthPrintManager.m main.m
clang -fno-objc-arc -framework foundation Month.m MonthPrintManager.m main.m -o cal
test: cal
./cal && ./cal 10 2016 && ./cal 2016
run: cal
./cal
clean:
rm cal
47 changes: 47 additions & 0 deletions 21651030张灏/project01/Month.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// DayRange.h
// cal
//
// Created by zhang on 16/11/1.
// Copyright © 2016年 zhang. All rights reserved.
//

/*
每个月份打印的结果最多占六行

日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42

比如 _start = 7; _end = 37,表示月份:

日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

*/
#import <Foundation/Foundation.h>

@interface Month : NSObject {
int _start;
int _end;
NSString* _title;
}

@property int start;
@property int end;
@property (copy) NSString* title;

+ (Month*) monthWithStart: (int) start end: (int) end title: (NSString*) title;

- (id) initWithStart: (int) start end: (int) end title: (NSString*) title;

@end
28 changes: 28 additions & 0 deletions 21651030张灏/project01/Month.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// DayRange.m
// cal
//
// Created by zhang on 16/11/1.
// Copyright © 2016年 zhang. All rights reserved.
//

#import "Month.h"

@implementation Month

- (instancetype)initWithStart:(int) start end:(int) end title: (NSString*) title
{
self = [super init];
if (self) {
_start = start;
_end = end;
_title = title;
}
return self;
}

+ (Month *)monthWithStart:(int)start end:(int)end title:(NSString*) title {
return [[Month alloc] initWithStart:start end:end title: title];
}

@end
30 changes: 30 additions & 0 deletions 21651030张灏/project01/MonthPrintManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// MonthPrintManager.h
// cal
//
// Created by zhang on 16/11/1.
// Copyright © 2016年 zhang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Month.h"

@interface MonthPrintManager : NSObject {
NSMutableArray* _months;
int _numOfOneLine; // 每行打印的 月数
NSString* _title;
}
@property int numOfOneLine;
@property (copy) NSMutableArray* months;
@property (copy) NSString* title;
@property int maxNum;

- (void) addMonth: (Month*) month;

// 打印所有月份
- (void) print;

- (int) calcLenOfCNString: (NSString*) str;

- (void) printSpace: (int) num;
@end
105 changes: 105 additions & 0 deletions 21651030张灏/project01/MonthPrintManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// MonthPrintManager.m
// cal
//
// Created by zhang on 16/11/1.
// Copyright © 2016年 zhang. All rights reserved.
//

#import "MonthPrintManager.h"

@implementation MonthPrintManager {

}

- (instancetype)init
{
self = [super init];
if (self) {
_months = [[NSMutableArray alloc] initWithCapacity:12];
_numOfOneLine = 3;
_maxNum = 42;
}
return self;
}

- (void)addMonth:(Month *)month {
[_months addObject:month];
}

- (void) print {
//居中打印年标题 ( 总字符数为:maxCol*(7*2+6*1)+ (maxCol-1)*2 )
if (_title != nil && ![_title isEqual: @""]) {
// 当总月份数小于_numOfOneLine时,居中显示不一样
int maxCol = [_months count] > _numOfOneLine ? _numOfOneLine : (int)[_months count];
int pos = maxCol*10 + (maxCol-1) - [self calcLenOfCNString: _title]/2 - 1;
[self printSpace: pos];
printf("%s\n\n", [_title UTF8String]);
}


for (int i = 0; i < [_months count]; i += _numOfOneLine) { // 月份行数 (比如12个月,没行打3个月,那么月份行数为4)
// 每行月份打印的 月数(最大月份行数时有可能比_numOfOneLine小)
int col = [_months count]-i > _numOfOneLine ? _numOfOneLine : (int)[_months count]-i;

// 居中打印月份标题 (总字符数为 7*2+6*1)
for (int k = 0; k < col; k++) {
Month* month = _months[i+k];
int len = [self calcLenOfCNString: month.title];
int left = 10-(len+1)/2;
int right = 20 - len - left;
[self printSpace:left];
printf("%s", [month.title UTF8String]);
k+1 < col ? [self printSpace:right+2] : printf("\n");
}
// 打印 “日 一 二 三 四 五 六”
for (int k = 0; k < col; k++) {
printf("日 一 二 三 四 五 六");
k+1 < col ? printf(" ") : printf("\n");
}

for (int r = 0; r < 6; r++) { //每行月份 要打印六行
for (int k = 0; k < col; k++) { // 每行月份打印的 月数(有可能比_numOfOneLine小)
Month* month = _months[i+k];
for (int n = 1; n <= 7; n++) { // 打印周日到周六 总共7天
if (r*7 + n >= month.start && r*7 + n <= month.end) {
printf("%2d", r*7 + n - month.start + 1);
}
else if ((r*7 + n > month.end) && n == 1 && k+1 >= col){
break;
}
else {
printf(" ");
}
if (n != 7) {
if (r*7 + n +1 > month.end && k+1 >= col) {
break;
}else {
printf(" ");
}
}
}
k+1 < col ? printf(" ") : printf("\n");
}
}
}
}

- (int)calcLenOfCNString:(NSString *)str {
int cnNum = 0;
for(int i = 0; i < [str length]; i++){
int a = [str characterAtIndex: i];
if( a >= 0x4e00 && a <= 0x9fff)
cnNum++;
}
int len = (int)[str length] + cnNum;
return len;
}

- (void)printSpace:(int)num {
for (int i = 0; i < num; i ++) {
printf(" ");
}
}

@end
2 changes: 2 additions & 0 deletions 21651030张灏/project01/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#cal
前几年通不过,比如./cal 10 不对
Loading