//
//  main.c
//  test
//
//  Created by cbd on 01/01/2017.
//  Copyright © 2017 cbd. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "isGood.h"
#include "isTop.h"

int64_t weight;

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

    // way1-extern
    weight = 0;
    char * mark = "0,1,1,2";
    bool is_good = isGood(mark);
    bool is_top = isTop(mark);
    is_good ? printf("已加精👍\t") : printf("未加精🚫\t");
    is_top ? printf("已置顶🆙\t") : printf("未置顶🚫\t");
    printf("\nweight:%lld\n\n", weight);

    // way2-pointer
    int64_t * wp = malloc(sizeof(int64_t));
    *wp = 0;
    bool is_good2 = isGood2(mark, wp);
    bool is_top2 = isTop2(mark, wp);
    is_good2 ? printf("已加精👍\t") : printf("未加精🚫\t");
    is_top2 ? printf("已置顶🆙\t") : printf("未置顶🚫\t");
    printf("\nwp:%lld\n\n", *wp);
    free(wp);


    return EXIT_SUCCESS;
}

//
//  isGood.c
//  test
//
//  Created by cbd on 04/01/2017.
//  Copyright © 2017 cbd. All rights reserved.
//

#include "isGood.h"

extern int64_t weight;

bool isGood(const char * mark) {
    char * p = memchr(mark, '1', strlen(mark));

    if (p) {

        weight += WEIGHT_OFFSET_GOOD;
        return true;
    } else {
        return false;
    }
}

bool isGood2(const char * mark, int64_t * wp) {
    char * p = memchr(mark, '1', strlen(mark));

    if (p) {
        *wp += WEIGHT_OFFSET_GOOD;
        weight += WEIGHT_OFFSET_GOOD;
        return true;
    } else {
        return false;
    }
}

//
//  isGood.h
//  test
//  是否加精:1
//
//  Created by cbd on 04/01/2017.
//  Copyright © 2017 cbd. All rights reserved.
//

#ifndef isGood_h
#define isGood_h

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define WEIGHT_OFFSET_GOOD 500000000000000000

bool isGood(const char * mark);
bool isGood2(const char * mark, int64_t * wp);

#endif /* isGood_h */

//
//  isTop.c
//  test
//
//  Created by cbd on 04/01/2017.
//  Copyright © 2017 cbd. All rights reserved.
//

#include "isTop.h"

bool isTop(const char * mark) {
    char * p = memchr(mark, '2', strlen(mark));

    if (p) {
        extern int64_t weight;
        weight += WEIGHT_OFFSET_TOP;
        return true;
    } else {
        return false;
    }
}

bool isTop2(const char * mark, int64_t * wp) {
    char * p = memchr(mark, '2', strlen(mark));

    if (p) {
        *wp += WEIGHT_OFFSET_TOP;

        extern int64_t weight;
        weight += WEIGHT_OFFSET_TOP;
        return true;
    } else {
        return false;
    }
}

//
//  isTop.h
//  test
//  是否置顶:2
//
//  Created by cbd on 04/01/2017.
//  Copyright © 2017 cbd. All rights reserved.
//

#ifndef isTop_h
#define isTop_h

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define WEIGHT_OFFSET_TOP 900000000000000000

bool isTop(const char * mark);
bool isTop2(const char * mark, int64_t * wp);

#endif /* isTop_h */