50033. Accounts

難度:3.5/5

很多syntax要記,附在下方。

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "account.h"
#include <stdio.h>
#include <stdlib.h>
#define AGE 1
#define ZIPCODE 2

Account accts[100005];

int cmp(const void* a, const void* b){
Account *acct_a = (Account*)a;
Account *acct_b = (Account*)b;
if((acct_a->accountNum) > (acct_b->accountNum)) return 1;
else if((acct_a->accountNum) < (acct_b->accountNum)) return -1;
else return 0;
}
int cmp_age(const void* a, const void* b){
Account *acct_a = (Account*)a;
Account *acct_b = (Account*)b;
if((acct_a->age) > (acct_b->age)) return 1;
else if((acct_a->age) < (acct_b->age)) return -1;
else return 0;
}
int cmp_zipcode(const void* a, const void* b){
Account *acct_a = (Account*)a;
Account *acct_b = (Account*)b;
if((acct_a->zipcode) > (acct_b->zipcode)) return 1;
else if((acct_a->zipcode) < (acct_b->zipcode)) return -1;
else return 0;
}

int get_size(FILE *f){
int prev = ftell(f);
fseek(f, 0L, SEEK_END);
int size = ftell(f);
fseek(f, prev, SEEK_SET);
return size;
}

static inline void print_line(Account* acct){
printf("%d, %d, %d, %d\n", acct->accountNum, acct->age, acct->zipcode, acct->balance);
}
static inline void print_acct(int n){
printf("account, age, zipcode, balance\n");
for(int i = 0; i < n; i++){
print_line(&(accts[i]));
}
}

static inline void print_info(int a, int b){
printf("%d, %d\n", a, b);
}

void get_age_info(int n){
printf("age, sum_balance\n");

int now_age = accts[0].age;
int now_sum_balance = accts[0].balance;
for(int i = 1; i < n; i++){
if(accts[i].age == now_age){
now_sum_balance += accts[i].balance;
}
else{
print_info(now_age, now_sum_balance);
now_age = accts[i].age;
now_sum_balance = accts[i].balance;
}
}
print_info(now_age, now_sum_balance);
return;
}

void get_zipcode_info(int n){
printf("zipcode, sum_balance\n");

int now_zipcode = accts[0].zipcode;
int now_sum_balance = accts[0].balance;
for(int i = 1; i < n; i++){
if(accts[i].zipcode == now_zipcode){
now_sum_balance += accts[i].balance;
}
else{
print_info(now_zipcode, now_sum_balance);
now_zipcode = accts[i].zipcode;
now_sum_balance = accts[i].balance;
}
}
print_info(now_zipcode, now_sum_balance);
return;
}


int main(){
char s[256]; scanf("%s", s);
FILE *f = fopen(s, "rb");
int n = get_size(f) / sizeof(Account);

fread(accts, sizeof(Account), n, f);
qsort(accts, n, sizeof(Account), cmp);
print_acct(n);

#if defined(SORTBY) && SORTBY == AGE
qsort(accts, n, sizeof(Account), cmp_age);
get_age_info(n);
#elif defined(SORTBY) && SORTBY == ZIPCODE
qsort(accts, n, sizeof(Account), cmp_zipcode);
get_zipcode_info(n);
#endif

return 0;
}

編譯參數帶入Macro

1
-D{macro_name}={macro_value}

Ex.

1
gcc -o out -DSORTBY=ZIPCODE main.c

找到檔案大小

1
2
3
4
5
6
7
int get_size(FILE *f){
int prev = ftell(f);
fseek(f, 0L, SEEK_END);
int size = ftell(f);
fseek(f, prev, SEEK_SET);
return size;
}

讀取binary file到struct

1
fread({struct_arr}, sizeof({the_struct}), {n}, {file_ptr});

用編譯時帶入的Macro

1
2
3
4
5
#if defined({macro_name}) && {macro_name} == {value_a}
// code
#elif defined({macro_name}) && {macro_name} == {value_b}
// code
#endif

50033. Accounts
https://aaronlin1229.github.io/judgegirl_50033/
Author
Akizumi
Posted on
July 17, 2023
Licensed under