In this project, you are required to develop a similar program with project 1 bu
ID: 3692902 • Letter: I
Question
In this project, you are required to develop a similar program with project 1 but using C programming language. This program named "MyWallet.c" is a simple bills manager system. Each bill corresponds to a record (line) in a text file named "bills.txt" (if this file is inexistent, create it). Each record consists of 7 fields (record ID, category, detail, amount, year, month, day) separate by percent sign(%) 1 %restaurant%subway%5.58%201 6%2%11 2%gas station%shell%20.15%2016963961 . No percent sign(%) in each field because % is the field separator. For example, (1 %resta%urant%subway%5.58%201 6%2061 1 ) is forbidden because the category field contains a 90 .The "record ID" is an unique non-negative int number (starts from 0) and generated by this system automatically. (when create a new record, the ID should be the last record's ID number +1.) .The category and the detail are string values with maximum length of 255 characters, the amount is double number. All of these three values are input by users The year, month, and day are three int numbers generated by this system by using the record creation time . MyWallet should provide a menu with following functionalities that users can choose from at the top level menu 1. Enter "a" to add a new bill . Ask for categoryExplanation / Answer
Program: mywellet.c
#include <stdio.h>
#include <time.h>
int main()
{
int recordID=1, amount, year, month, day,temp,del;
int syear,smonth,sday,eyear,emonth,eday,count;
char category[255], detail[255],ch,c,ss[100],cat;
FILE *fp, *fp2;
time_t t = time(NULL);
struct tm *tm;
fp=fopen("bills.txt", "a+");//Opens a text file for both reading and writing. It creates the file if it does not exist
fclose(fp);
printf(" Enter 'a' to add a new bill Enter ''d' to remove a bill Enter 'c' to count amount Enter 'v' to view bills Enter 'q' to quit this system choice: ");
scanf("%c",&ch);
while(ch!='q')
{
switch(ch)
{
case 'a'://add a new bill
ch='t';
while(ch=='t')
{
fp=fopen("bills.txt", "w");
tm =localtime(&t);
printf(" enter category ");
scanf("%s",&category);
printf(" enter details ");
scanf("%s",&detail);
printf(" enter Amount ");
scanf("%d",&amount);
year=tm->tm_year + 1900;
month=tm->tm_mon + 1;
day=tm->tm_mday;
fprintf(fp," %d %-15s %-15s %d %d %d %d ",recordID,category,detail,amount,year,month,day);
fclose(fp);
printf(" enter t for try again and b for back to menu ");
scanf("%c",&ch);
}
break;
case 'd'://remove a bill
fp = fopen("bills.txt", "r");
fp2 = fopen("File_copy.txt", "w+");
printf(" enter which record want to delete, record id ");
scanf("%d",&del);
while (fscanf(fp," %d %s %s %d %d %d %d",&temp,category,detail,&amount,&year,&month,&day) > 0) {
if(temp != 1)
{
fprintf(fp," %d %-15s %-15s %d %d %d %d ",temp,category,detail,amount,year,month,day);
}
}
fclose(fp);
fclose(fp2);
remove("bills.txt");
rename( "File_copy.txt", "bills.txt" );
break;
case 'c'://count amount
count=0;
fp = fopen("bills.txt", "r");
printf(" enter starting date ");
scanf("%d%d%d",&syear,&smonth,&sday);
printf(" enter ending date ");
scanf("%d%d%d",&eyear,&emonth,&eday);
while (fscanf(fp," %d %s %s %d %d %d %d",&temp,category,detail,&amount,&year,&month,&day) > 0) {
if(syear<=year&&eyear>=year&&sday<=day&&eday>=day&&smonth<=month&&emonth>=month)
{
count=count+amount;
}
}
fclose(fp);
printf("Total amount between %d/%d/%d to %d,%d,%d is %d ",sday,smonth,syear,eday,emonth,eyear,count);
break;
case 'v'://view bills
printf("enter c for category , t for time and r for record");
scanf("%c",&c);
if(c =='c')
{
printf(" enter category ");
scanf("%s",ss);
printf("record ID category detail amount year month day ");
fp = fopen("bills.txt", "r");
while (fscanf(fp," %d %s %s %d %d %d %d",&temp,category,detail,&amount,&year,&month,&day) > 0) {
if(ss==category)
printf(" %d %s %s %d %d %d %d",temp,category,detail,amount,year,month,day);
}
fclose(fp);
}
else if(c == 't')
{
printf(" enter starting date ");
scanf("%d%d%d",&syear,&smonth,&sday);
printf("record ID category detail amount year month day ");
fp = fopen("bills.txt", "r");
while (fscanf(fp," %d %s %s %d %d %d %d",&temp,category,detail,&amount,&year,&month,&day) > 0) {
if(sday==day&&syear==year&&smonth==month)
printf(" %d %s %s %d %d %d %d",temp,category,detail,amount,year,month,day);
}
fclose(fp);
}
else
{
printf(" enter record ID ");
scanf("%d",&count);
printf("record ID category detail amount year month day ");
fp = fopen("bills.txt", "r");
while (fscanf(fp," %d %s %s %d %d %d %d",&temp,category,detail,&amount,&year,&month,&day) > 0) {
if(count==temp)
printf(" %d %s %s %d %d %d %d",temp,category,detail,amount,year,month,day);
}
fclose(fp);
}
break;
case 'q'://quit from
printf("exit from wallet");
default:
printf("enter correct choice ");
}
printf(" Enter 'a' to add a new bill Enter ''d' to remove a bill Enter 'c' to count amount Enter 'v' to view bills Enter 'q' to quit this system choice: ");
scanf("%c",&ch);
}
return 0;
}
OUTPUT:
sh-4.3$ gcc -o main *.c
sh-4.3$ main
Enter 'a' to add a new bill
Enter ''d' to remove a bill
Enter 'c' to count amount
Enter 'v' to view bills
Enter 'q' to quit this system
choice: a
enter category asha
enter details laptops
enter Amount 1000
enter t for try again and b for back to menu b
Enter 'a' to add a new bill
Enter ''d' to remove a bill
Enter 'c' to count amount
Enter 'v' to view bills
Enter 'q' to quit this system
choice: v
enter c for category , t for time and r for record r
enter record ID 1
record ID category detail amount year month day
1 asha laptops 1000 2016 4 23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.