---
# System prepended metadata

title: 2022 TTU 程設 Lab 12

---

# 2022 TTU 程設 Lab 12
## Lab 12-1.c
```clike=
#include <stdio.h>
char week[7]="SMTWTFS";
char month_name[12][5]={"Jan.","Feb.","Mar","Apr.","May.","Jun.","Jul.","Oct","Sep.","Nov.","Dec."};
int is_lunar(int year){
    if(year%4==0&&year%100!=0||year%400==0) return 1;
    else return 0;
}

int month_days(int year,int month){
    if(month==0) return 0;
    int table[12]={1,-1,1,0,1,0,1,1,0,1,0,1};
    if(month==2) return (is_lunar(year))?29:28;
    if(table[month-1]) return 31;
    else return 30;
}

int year_days(int year,int month){
    int total=0;
    for(int i=0;i<month;i++){
        total+=month_days(year,i+1);
    }
    return total;
}

int from_2006(int year){
    int total=0;
    for(int i=2006;i<=year;i++){
        total+=year_days(i,12);
    }
    return total;
}

int year_shift_days(int year){
    int delta=from_2006(year-1);
    return delta%7;
}

int month_shift_days(int year,int month){
    int shifted=year_shift_days(year);
    shifted+=year_days(year,month-1);
    return shifted%7;
}

int calander[6][7]={0};
int main(){
    int year;
    printf("Input a year(2006~2013): ");
    scanf("%d",&year);
    if(year>2013&&year<2006) return 0;

    printf("\n%d is %sa leap year\n",year,(is_lunar(year))?"":"not ");

    for(int i=0;i<12;i++){
        // 印出年月
        printf("%s %d\n",month_name[i],year);
        // 印出星期
        for(int i=0;i<7;i++){
            printf("%2c ",week[i]);
        }
        printf("\n");

        int j=0;
        int shifted_m=month_shift_days(year,i+1);
        for(j=0;shifted_m!=0&&j<shifted_m;j++){
            printf("%2s "," ");
        }
        for(;j<month_days(year,i+1)+shifted_m;j++){
            if(j>0&&j%7==0) printf("\n");
            printf("%2d ",j+1-shifted_m);
        }
        printf("\n\n");
    }
    return 0;
}
```

## Lab 12-2.c
```clike=
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void arrcpy(int a[],int b[],int len){
    for(int i=0;i<len;i++) a[i]=b[i];
}

int is_exist(int target,int arr[],int len){
    for(int i=0;i<len;i++){
        if(arr[i]==target) return 1;
    }
    return 0;
}

void rand_deck(int deck[],int len){
    for(int i=0;i<len;i++){
        int t_rand=1+rand()%len;
        while(is_exist(t_rand,deck,i)){
            t_rand=1+rand()%len;
        }
        deck[i]=t_rand;
    }
}

void print_arr(int arr[],int begin,int len){
    for(int i=begin;i<len;i++){
        printf("%3d",arr[i]);
    }
}

void print_deck(int deck[],int len){
    int mid=len/2;
    print_arr(deck,0,mid);
    printf("  ||");
    print_arr(deck,mid,len);
    printf("\n");
}

void shuffle(int deck[],int len){
    int temp[len];
    int mid=len/2;
    int c=0;
    for(int i=0,c=0;i<len;i+=2,c++){
        temp[i]=deck[c];
        temp[i+1]=deck[mid+c];
    }
    for(int i=0;i<len;i++) deck[i]=temp[i];
}

int is_same(int a[],int b[],int len){
    for(int i=0;i<len;i++){
        if(a[i]!=b[i]) return 0;
    }
    return 1;
}

int deck[18];
int main(){
    srand(time(NULL));
    int n;
    printf("Input a num(0-9): ");
    scanf("%d",&n);
    while(n<1||n>9){
        printf("Input a num(0-9): ");
        scanf("%d",&n);
    }
    int deck_size=2*n;
    rand_deck(deck,deck_size);
    int ori[deck_size];
    arrcpy(ori,deck,deck_size);
    print_deck(deck,deck_size);
    shuffle(deck,deck_size);
    print_deck(deck,deck_size);
    while(!is_same(ori,deck,deck_size)){
        shuffle(deck,deck_size);
        print_deck(deck,deck_size);
    }

    return 0;
}

```