Accounting System

struct Account

#define N 64
typedef struct Account{
	int id;
	char name[N];
}Account;
Accounting System

struct ArrayList

typedef struct ArrayList{
	Account *arr;
	int capacity;
	int size;
}ArrayList;
Accounting System

struct Entry

typedef struct Entry{
	int debit, credit;
	long amount;
	char remark[N];
}Entry;
Accounting System

struct Balance

typedef struct Balance{
	int id;
	long amount;
}Balance;
Accounting System

struct T

typedef struct T{
	int id;
	long openning;
	long debits[MAX];
	long credits[MAX];
	int ndedit, ncredit;
}T;
Accounting System

function initT

void initT(T *t){
	t->ndedit = 0;
	t->ncredit = 0;
	t->openning = 0;
}
Accounting System

function initArrayList

void initArrayList(ArrayList* l, int capacity){
	l->capacity = capacity;
	l->arr = (Account*)malloc(sizeof(Account) * capacity);
	l->size = 0;
}
Accounting System

function isEmpty

bool isEmpty(const ArrayList *l){
	return l->size <= 0;
}
Accounting System

function add

void add(ArrayList *l, Account acc){
	l->arr[l->size++] = acc;
}
Accounting System

function saveAccount

void saveAccount(const Account* acc){
	FILE* f;
	fopen_s(&f, "account.txt", "a");
	fwrite(acc, sizeof(*acc), 1, f);
	fclose(f);
}
Accounting System

function print

void print(const ArrayList *l){
	for (int i = 0; i < l->size; i++){
		printf("%d %s\n", l->arr[i].id, l->arr[i].name);
	}
}
Accounting System

function clearEndl

void clearEndl(char *str){
	int n = strlen(str);
	for (int i = n - 1; i >= 0; i--){
		if (str[i] == '\n'){
			str[i] = '\0';
		}else{
			break;
		}
	}
}
Accounting System

function getAccount

Account getAccount(char line[]){
	Account acc;
	char *context = NULL;
	char *w = strtok_s(line, "#", &context);
	acc.id = atoi(w);
	w = strtok_s(NULL, "#", &context);
	strcpy_s(acc.name, N, w);
	clearEndl(acc.name);
	return acc;
}
Accounting System

function showAccount

void showAccount(){
	FILE *f;
	fopen_s(&f, "account.txt", "rt");
	ArrayList l;
	initArrayList(&l, 3);
	char line[MAX];
	while (!feof(f)){
		fgets(line, MAX, f);
		Account acc = getAccount(line);
		add(&l, acc);
	}
	fclose(f);
	print(&l);
}

Accounting System

function firstNumber

int firstNumber(int id){
	while (id >= 10){
		id /= 10;
	}
	return id;
}
Accounting System

function isAsset

bool isAsset(int id){
	int f = firstNumber(id);
	return f == 1 || f == 2;
}
Accounting System

function printBalance

void printBalance(const Balance *b){
	printf("%d %ld\n", b->id, b->amount);
}
Accounting System

function openningBalance

void openningBalance(){
	FILE *f;
	fopen_s(&f, "openningbalance.txt", "rt");
	Balance b;
	while (fscanf_s(f, "%d %d", &b.id, &b.amount) > 0){
		printBalance(&b);
	}
	fclose(f);
}
Accounting System

function fetchDoubleEntry

void fetchDoubleEntry(T *t){
	FILE *f;
	fopen_s(&f, "doubleentry.txt", "rt");
	char line[MAX];
	while (!feof(f)){
		if (fgets(line, MAX, f) != NULL){
			Entry e = getEntry(line);
			if (e.debit == t->id){
				t->debits[t->ndedit++] = e.amount;
			}else if (e.credit == t->id){
				t->credits[t->ncredit++] = e.amount;
			}
		}
	}
	fclose(f);
}
Accounting System

function getT

T getT(){
	T t;
	initT(&t);
	printf("Input Id: ");
	scanf_s("%d", &t.id);
	t.openning = findOpenning(t.id);
	fetchDoubleEntry(&t);
	return t;
}
Accounting System

function printI

void printI(const T *t, long s[]){
	int i;
	for (i = 0; i < t->ndedit && i < t->ncredit; i++){
		s[0] += t->debits[i];
		s[1] += t->credits[i];
		printf("%ld\t|\t%ld\n", t->debits[i], t->credits[i]);
	}
	for (; i < t->ndedit; i++){
		s[0] += t->debits[i];
		printf("%ld\t|\n", t->debits[i]);
	}
	for (; i < t->ncredit; i++){
		s[1] += t->credits[i];
		printf("\t|\t%ld\n", t->credits[i]);
	}
}
Accounting System

function printT

void printT(const T *t){
	if (isAsset(t->id)){
		printf("%ld\t\n", t->openning);
	}else{
		printf("\t|\t%ld\n", t->openning);
	}
	printf("*************************\n");
	long s[] = { 0, 0 };
	printI(t, s);
	printf("*************************\n");
	printf("%ld\t|\t%ld\n", s[0], s[1]);
	printf("*************************\n");
	if (isAsset(t->id)){
		printf("%ld\t|\n", t->openning + s[0] - s[1]);
	}else{
		printf("\t|\t%ld\n", t->openning + s[1] - s[0]);
	}
}
Accounting System

function schemaT

void schemaT(){
	T t = getT();
	printT(&t);
}
Accounting System

function main

void main(){
	char *menu[] = { "Exit", "Show Account", "Openning Balance", 
		"Double Entry", "Schema T", "Balance Sheet" };
	void(*f[])() = { exit2, showAccount, openningBalance, 
		doubleEntry, schemaT };	
	int choice = 0;
	int n = sizeof(menu) / sizeof(*menu);
	do{
		for (int i = 0; i < n; i++){
			printf("%d %s\n", i, menu[i]);
		}
		scanf_s("%d", &choice);
		f[choice]();
	} while (choice > 0 && choice < n);
}
Accounting System

Double Entry

153#112#12000#mua cong cu dung cu
152#331#100000#mua nguyen vat lieu
341#331#40000#vay ngan han tra no nguoi ban
111#112#30000#rut tien gui ngan hang
334#112#70000#chi tien mat tra luong cho dn
112#131#70000#khach hang tra no
341#112#40000#tra no vay ngan han
333#112#20000#nop thue nha nuoc
Accounting System

Double Entry

111#Cash on hand
112#Cash in bank
111#Cash on hand
112#Cash in bank
131#Receivables from customers
141#Advances (detailed by receivers)
152#Raw materials
153#Instrument & tools
154#Cost for work in process
155#Finished products
211#Tangible fixed assets
Accounting System

Openning Asset

111 15000
112 100000
131 70000
141 5000
152 300000
153 60000
154 10000
155 140000
211 800000
331 60000
333 30000
334 35000
338 40000
341 150000
353 15000
411 900000
414 220000
421 50000