pangfengliu / programmingtasks

programming tasks from my courses
68 stars 17 forks source link

Accounts #320

Open morris821028 opened 8 years ago

morris821028 commented 8 years ago

Problem Description

Write as program to manage a set of accounts within a binary file. An account has the following information.

account.h

#ifndef _ACCOUNT_H
#define _ACCOUNT_H

typedef struct account {
    int balance;
    int accountNum;
    int zipcode;
    int age;
} Account;

#endif

First you need to read the name of the file from the standard input. After you read the account information from the file, you need to write a summary report.

You may assume that the constants AGE and ZIPCODE are 1 and 2 respectively.

Subtasks

If we sort the array according to a field, it becomes much easier to sum up all balance from that field.

Sample Input

p10075-sample.bin

file download

Sample Output (if a #define SORTBY ZIPCODE is defined)

account, age, zipcode, balance
0, 24, 100, 6000
2, 24, 101, 6200
3, 25, 100, 6500
5, 24, 100, 6300
7, 26, 101, 6000
zipcode, sum_balance
100, 18800
101, 12200

Sample Output (if a #define SORTBY AGE is defined)

account, age, zipcode, balance
0, 24, 100, 6000
2, 24, 101, 6200
3, 25, 100, 6500
5, 24, 100, 6300
7, 26, 101, 6000
age, sum_balance
24, 18500
25, 6500
26, 6000

Compile

$ gcc -std=c99 -O2 a.c -DSORTBY=ZIPCODE -DAGE=1 -DZIPCODE=2 -o testzip.out
$ gcc -std=c99 -O2 a.c -DSORTBY=AGE -DAGE=1 -DZIPCODE=2 -o testage.out

part of main.c

#define AGE 1
#define ZIPCODE 2