-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget_calculations.c
114 lines (90 loc) · 3.37 KB
/
budget_calculations.c
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
112
113
114
/*
* Name: budget_calculations.c
*
* Purpose: Functions for calculating all desired budget figures.
*
* Author: jjones4
*
* Copyright (c) 2024 Jerad Jones
*
* This file is part of c_budget. c_budget may be freely distributed under the
* MIT license. For all details and documentation, see:
*
* https://github.com/jjones4/c_budget
*/
/*
* Any income/gift over $3,000.00 goes into a different table so it doesn't
* skew my monthly figures or my averages.
*/
#define LARGE_INCOME_THRESHHOLD 3000.00
/*
* Any expense over $2,400.00 goes into a different table so it doesn't skew my
* monthly figures or my averages.
*/
#define LARGE_EXPENSE_THRESHHOLD (-2400.00)
#include <stdio.h>
#include <stdlib.h>
#include "size_limits.h"
#include "return_codes.h"
#include "get_data.h"
int calculate_budget_figures(char **line, double *monthly_credits,
double *monthly_debits, double *monthly_margins, double
*large_amounts_credits, double *large_amounts_debits, double
*large_amounts_margins, double *yearly_credits, double *yearly_debits,
double *yearly_margins) {
int array_index_for_monthly_figures = 0;
int array_index_for_yearly_figures = 0;
int month = 0; int year = 0;
int error_code = 0;
double amount = 0.00;
char *date_string = NULL;
char *amount_string = NULL;
date_string = malloc(DATE_LENGTH * sizeof(char));
amount_string = malloc(AMOUNT_LENGTH_MAX * sizeof(char));
if(date_string == NULL || amount_string == NULL) {
printf("\nThere was an error getting the required memory.\n");
return MEMORY_ERROR;
}
get_date_string(*line, date_string);
get_amount_string(*line, amount_string);
month = get_month(date_string, &error_code);
if(error_code == MEMORY_ERROR) {
printf("\nThere was an error getting the required memory.\n");
free(date_string);
free(amount_string);
return MEMORY_ERROR;
}
year = get_year(date_string, &error_code);
if(error_code == MEMORY_ERROR) {
printf("\nThere was an error getting the required memory.\n");
free(date_string);
free(amount_string);
return MEMORY_ERROR;
}
amount = get_amount(amount_string);
array_index_for_monthly_figures = 12 * (year - START_YEAR) + month - 1;
array_index_for_yearly_figures = year - START_YEAR;
if(amount < -2400.00) {
large_amounts_debits[array_index_for_monthly_figures] += amount;
large_amounts_margins[array_index_for_monthly_figures] += amount;
}
else if(amount < 0) {
monthly_debits[array_index_for_monthly_figures] += amount;
yearly_debits[array_index_for_yearly_figures] += amount;
monthly_margins[array_index_for_monthly_figures] += amount;
yearly_margins[array_index_for_yearly_figures] += amount;
}
else if(amount < 3000.00) {
monthly_credits[array_index_for_monthly_figures] += amount;
yearly_credits[array_index_for_yearly_figures] += amount;
monthly_margins[array_index_for_monthly_figures] += amount;
yearly_margins[array_index_for_yearly_figures] += amount;
}
else {
large_amounts_credits[array_index_for_monthly_figures] += amount;
large_amounts_margins[array_index_for_monthly_figures] += amount;
}
free(date_string);
free(amount_string);
return 0;
}