Sie sind auf Seite 1von 7

NAME ID

MUHAMMAD FAIZAL BIN BAILI EP0100796

MUHAMMAD HARIZ B SHAMSHUL BAHRI EP0100533

SUBJECT : C PROGRAMMING

LECTURER : AIMAN BIN ISMAIL

REPORT MINI PROJECT: SPENDING TRACKER


PROBLEM STATEMENT

 How to manipulate C language to code a program that can help user to track their expenditure.
 Features of the program :
-Enable the user to enter/select the date to sort their expenditure.
-Able to give the user the output on total expense, total income and balance of a certain time period.
-Provide the user with a list of their expenditure with details such as date and amount.

METHADOLOGY
Begin (main)
While x!=4
Do
x=0
Call function date_selection
Ask user enter the date
Switch
Case 1 until case 12
set the month name to be used for file operation
default
set the month name to “not valid”
While month_name == not valid
Set file name

While x!=3
Read data from file
Call function home _display
Ask user enter the selection
If (x=1)
Ask user enter the amount
Ask user enter the category / description
Total expense = call function sum
Exp. = -exp
Balance = call function sum
Add data to file
If (x=2)
Ask user enter the amount
Ask user enter the category
Total income = call function sum
Balance = call function sum
Add data to file
Display “Exit successful”
End

Begin (date_selection)
Display the date selection menu
End

Begin (home_display)
Display the home menu, balance, total expense, total income
End

Begin (sum)
Sum = n1+n2
End
RESULT
CONCLUSION

The objective of this project is fulfilled. With the uses of pointer , array , user defined data type, string , file
operation, calculation and a few more elements in C language , a program that enable user to track their expenditure is
created.

This program is 100% functioning and satisfied the objective of the project. However, some modification can be
done in the future such as adding a feature where user can determine the time period of output either it is daily,
monthly or yearly. Adding category view feature to the programme also can give the user more option. This modification
can develope the program productivity and flexibility.

In the conclusion, C language can be used to solve many type of problem as long as the algorithm and format is
perfectly correct.

APPENDIX

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

void date_selection_display (void);


char* combine_name (char*, char*, char*);
double sum (double, float*);
void home_display (double, double, double, int, int, int, char*);

int main(void){
setvbuf (stdout, NULL, _IONBF, 0);setvbuf (stderr, NULL, _IONBF, 0);

int x=0, d, m, y;
char day[5], month[5], year[5], month_name1[10], month_name2[30], month_name3[30], month_name4[30];
char *month_fn, *bal_fn, *exp_fn, *inc_fn;
char file_ext[]=".txt", balfile_ext[]="bal.txt", expfile_ext[]="exp.txt", incfile_ext[]="inc.txt";
char arrbal[100], arrexp[100], arrinc[100], desc[100];
double balance, total_expense, total_income;
float expense, income, *pexp, *pinc;

pexp = &expense;//pointer *pexp pointing to expense


pinc = &income;

FILE*bal_file;
FILE*exp_file;
FILE*inc_file;
FILE*list_file;

while (x != 4){
strcpy(arrbal, "0.00");strcpy(arrexp, "0.00");strcpy(arrinc, "0.00");

do{
x=0;
date_selection_display ();// call date_selection_display function
scanf ("%d/%d/%d", &d, &m, &y);
itoa (d, day, 10);itoa (m, month, 10);itoa (y, year, 10);

switch (m){
case 1:
strcpy (month_name1,"JAN");strcpy (month_name2,"JAN");
strcpy (month_name3,"JAN");strcpy (month_name4,"JAN");
break;
case 2:
strcpy (month_name1, "FEB");strcpy (month_name2, "FEB");
strcpy (month_name3, "FEB");strcpy (month_name4, "FEB");
break;
case 3:
strcpy (month_name1, "MAR");strcpy (month_name2, "MAR");
strcpy (month_name3, "MAR");strcpy (month_name4, "MAR");
break;
case 4:
strcpy (month_name1, "APR");strcpy (month_name2, "APR");
strcpy (month_name3, "APR");strcpy (month_name4, "APR");
break;
case 5:
strcpy (month_name1, "MAY");strcpy (month_name2, "MAY");
strcpy (month_name3, "MAY");strcpy (month_name4, "MAY");
break;
case 6:
strcpy (month_name1, "JUN");strcpy (month_name2, "JUN");
strcpy (month_name3, "JUN");strcpy (month_name4, "JUN");
break;
case 7:
strcpy (month_name1, "JUL");strcpy (month_name2, "JUL");
strcpy (month_name3, "JUL");strcpy (month_name4, "JUL");
break;
case 8:
strcpy (month_name1, "AUG");strcpy (month_name2, "AUG");
strcpy (month_name3, "AUG");strcpy (month_name4, "AUG");
break;
case 9:
strcpy (month_name1, "SEP");strcpy (month_name2, "SEP");
strcpy (month_name3, "SEP");strcpy (month_name4, "SEP");
break;
case 10:
strcpy (month_name1, "OCT");strcpy (month_name2, "OCT");
strcpy (month_name3, "OCT");strcpy (month_name4, "OCT");
break;
case 11:
strcpy (month_name1, "NOV");strcpy (month_name2, "NOV");
strcpy (month_name3, "NOV");strcpy (month_name4, "NOV");
break;
case 12:
strcpy (month_name1, "DIS");strcpy (month_name2, "DIS");
strcpy (month_name3, "DIS");strcpy (month_name4, "DIS");
break;
default:
strcpy (month_name1, "NOT VALID");
printf ("%s\n", month_name1);
break;
}

}
while (strcmp (month_name1, "NOT VALID") == 0);//compare month name

month_fn = combine_name (month_name1, year, file_ext);//set file name


bal_fn = combine_name (month_name2, year, balfile_ext);
exp_fn = combine_name (month_name3, year, expfile_ext);
inc_fn = combine_name (month_name4, year, incfile_ext);

while (x != 4 && x!=3){


do{
bal_file = fopen (bal_fn,"r");//open file
fgets (arrbal, 10, bal_file);// read from file, store as string to arrbal
sscanf (arrbal, "%lf", &balance);//store DOUBLE value at string arrbal to int
balance
exp_file = fopen (exp_fn ,"r");
fgets (arrexp, 10, exp_file);
sscanf (arrexp, "%lf", &total_expense);
inc_file = fopen (inc_fn ,"r");
fgets (arrinc, 10, inc_file);
sscanf (arrinc, "%lf", &total_income);

fclose (bal_file);fclose (exp_file);fclose (inc_file);

home_display(balance, total_expense, total_income, d, m, y, month_fn);


printf ("Please enter your selection : ");
scanf ("%d", &x);

if (x == 1){
printf
("\n***********************************************************\n\n\n");
printf ("Enter the amount:\n");
scanf ("%f", &expense);
printf ("Enter the category/description:");
scanf ("%s", desc);
printf
("\n\n\n***********************************************************\n");

total_expense = sum (total_expense, pexp);//call sum function and return


value to total_expense
exp_file = fopen(exp_fn, "w+");// destroy file if already exist and
open a new blank file
fprintf (exp_file, "%lf", total_expense);//print total_expense to file
fclose (exp_file);

*pexp = -*pexp;

list_file = fopen(month_fn, "a+");// open file to add data at the end of


file
fprintf (list_file, "%2d/%2d/%d %.2f %s\n", d, m, y, expense,
desc);//print to file
fclose (list_file);

balance = sum (balance, pexp);


bal_file = fopen (bal_fn, "w+");
fprintf (bal_file, "%lf", balance);
fclose (bal_file);
}
else if (x == 2){
printf
("\n***********************************************************\n\n\n");
printf ("Enter the amount:");
scanf ("%f", &income);
printf ("\nEnter the category/description:");
scanf ("%s", desc);
printf
("\n\n\n***********************************************************\n");

total_income = sum (total_income, pinc);


inc_file = fopen(inc_fn, "w+");
fprintf (inc_file, "%lf", total_income);
fclose (inc_file);

list_file = fopen(month_fn, "a+");


fprintf (list_file, "%02d/%02d/%d %.2f %s\n", d, m, y, income,
desc);
fclose (list_file);

balance = sum (balance, pinc);


bal_file = fopen (bal_fn, "w+");
fprintf (bal_file, "%lf", balance);
fclose (bal_file);
}
}
while (x != 3 && x != 4);
}
}
printf("Exit successful");
}
void date_selection_display (void){
printf ("***********************************************************\n");
printf("Please enter the date dd/mm/yyyy");
printf ("\n***********************************************************\n");
}
char* combine_name (char* s1, char* s2, char* s3){
strcat(s1, s2);strcat(s1, s3);
return s1;
}
double sum (double n1, float *n2){
double sum;
sum = n1 + *n2;
return sum;
}
void home_display (double bal, double exp, double inc, int d, int m, int y, char* m_fn){
printf ("***********************************************************\n\n\n");
printf("%s\n", m_fn);printf("%02d/%02d/%d\n\n", d, m, y);printf ("Balance : %.2lf\n", bal);
printf ("Total expense : %.2lf\n", exp);printf ("Total income : %.2lf\n\n", inc);
printf ("(1) +expense\n");printf ("(2) +income\n\n");printf ("(3) Change date\n");printf ("(4)
Exit");
printf ("\n\n\n***********************************************************\n");}

Das könnte Ihnen auch gefallen