In this project, you are required to develop a similar program with project 1 bu
ID: 3696489 • 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 le named “bills.txt” (if this le is inexistent, create it). Each record consists of 7 elds (record ID, category, detail, amount, year, month, day) separate by percent sign(%). e.g.: 1%restaurant%subway%5.58%2016%2%11 2%gas station%shell%20.15%2016%3%1 • No percent sign(%) in each eld because % is the eld separator. For example, (1%resta%urant%subway%5.58%2016%2%11) is forbidden because the category eld contains a %. • 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 category • Ask for detail • Ask for amount • Generate record ID and date • Add this new record into “bills.txt” le • At the end, print “New bill added successfully” • Provide option “t” for “Try again” and “b” for “Back to main menu” 2. Enter “d” to remove a bill • Ask for record ID • Examine if this record exits. if no, print a message “No such record!”, otherwise, display this record and remove it from “bills.txt” le. • Provide option “t” for “Try again” and “b” for “Back to main menu” 3. Enter “c” to count amount • Ask for time range (start date(year, month,day) and end date(year,month,day)) • Count and display amount of all bills in this time period. (add all satised records’ amount values together) • Provide option “t” for “Try again” and “b” for “Back to main menu” 4. Enter “v” to view bills • Ask for search type (by category, by time, or by record ID) • Ask for category if by category • Ask for time range if by time • Ask record ID if by record ID • Display all bills satised the condition • Provide option “t” for “Try again” and “b” for “Back to main menu” 5. Enter “q” to quit this system • Terminate this system
Explanation / Answer
#include<studio.h>
#include<string.h>
struct element
{
int recordid;
time_t s;
float amount;
char category[25];
char Details[25];
}
int countlines(char *filename)
{
// count the number of lines in the file called filename
FILE *fp = fopen(filename,"r");
int ch=0;
int lines=0;
if (fp == NULL);
return 0;
lines++;
while ((ch = fgetc(fp)) != EOF)
{
if (ch == ' ')
lines++;
}
fclose(fp);
return lines;
}
void copyDelete(const char *thefilename,const char *tmpfilename,int l)
{
int dl;
dl=lin;
FILE *thefile = fopen(thefilename, "r");
FILE *tmpfile = fopen(tmpfilename, "w");
lines=countlines(thefile);
while (fgets(line, sizeof(line), thefile))
{
for(j=0;j<=lines;j++)
if (j!=d)
fputs(line, tmpfile);
}
fclose(tmpfile);
fclose(thefile);
}
main()
{
char *cdate;
FILE *fOut,reads;
char ch;
char line[255];
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char s[64];
strftime(s, sizeof(s), "%c", tm);
Printf("Enter a) for adding New Record d)Delete a record c) View the amount d)display the bills e)exit");
scanf("%c,"&ch);
switch(ch)
{
case "a":
Printf("Enter Category");
scanf("%s",category);
Printf("Enter Details");
scanf("%s",details);
Printf("Enter Amount");
scanf("%f",&amount;
l=countlines("bills.txt");
recordid=l+1
cdate=s;
fOut = fopen("fileOut.txt", "w");
strcat(line,recordid);
strcat(line," ");
strcar(line,s);
strcat(line," ");
trcat(line,amount);
strcat(line," ");
strcat(line,category);
strcat(line," ");
strcat(line,Details);
strcat(line, " ");
fputs(line, fOut);
fclose(f0ut);
break;
case "d":
printf("Enter Record ID:");
scanf("%d",&rno);
lines=countlines("bills.txt");
if(rno<lines)
{
copyData("bills.txt","new_bills.txt");
remove("bills.txt");
rename("new_bills.txt","bills.txt");
}
else
{
printf("No record exists");
}
break;
case "c":
Printf("Enter State Date");
scanf("%s",sdate);
Printf("End Date:");
Scanf("%s"edate);
reads=fopen("bills.txt", "r");
if (reads==NULL) {
perror("Error");
return 1;
}
else {
while(!feof(reads)) {
struct element *n= (struct element*)malloc(sizeof(struct element));
fscanf(reads,"%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
if((n->s > time(sdate)) || (n->s < time(edate))
printf("%f",n->amount);
printf(" ");
}
}
fclose(reads);
break;
case "v":
printf("Enter Search Type");
scanf("%s",st);
if(strcmp(st,"category")==0)
{
Printf("Enter Category");
scanf("%s",category);
reads=fopen("bills.txt", "r");
if (reads==NULL) {
perror("Error");
return 1;
}
else {
while(!feof(reads)) {
struct element *n= (struct element*)malloc(sizeof(struct element));
fscanf(reads,"%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
if(strcmp(n->category,category)==0)
{
printf("%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
printf(" ");
}
}
}
}
if(strcmp(st,"recordid")==0)
{
printf("Enter Record id");
scanf("%d",recordid);
reads=fopen("bills.txt", "r");
if (reads==NULL) {
perror("Error");
return 1;
}
else {
while(!feof(reads)) {
struct element *n= (struct element*)malloc(sizeof(struct element));
fscanf(reads,"%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
if((n->recordid == recordid)
printf("%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
printf(" ");
}
}
}
if(strcmp(st,"timerange")==0)
{
Printf("Enter State Date");
scanf("%s",sdate);
Printf("End Date:");
Scanf("%s"edate);
reads=fopen("bills.txt", "r");
if (reads==NULL) {
perror("Error");
return 1;
}
else {
while(!feof(reads)) {
struct element *n= (struct element*)malloc(sizeof(struct element));
fscanf(reads,"%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
if((n->s > time(sdate)) || (n->s < time(edate))
printf("%d %f %s %s %s", n->recordid, n-->s, n->amount, n->category, n->Details);
printf(" ");
}
}
fclose(reads);
}
break;
case "e":
Printf("exiting");
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.