-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathds1.c
499 lines (372 loc) · 11.4 KB
/
ds1.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 50
#define M 30
double currentincome=0;
double currentexpense=0;
struct node
{
char date[M];
double amount;
char category[N];
struct node *next;
}*income=NULL,*expense=NULL;
struct record
{
double x,y;
}*point=NULL;
void create(char x[],double y,char z[],struct node **temp);
void display(int a3);
struct node *readnext(struct node *ptr,FILE *fpointer);
void writeincome(struct node *ptr);
void writeexpense(struct node *ptr);
void deleterecord(struct node *ptr);
struct node *readincome(struct node *ptr);
struct node *readexpense(struct node *ptr);
void write(struct record *point);
struct record *readrecord();
int main()
{
int option,value;
double b;
char c[N],a[M];
char s1[15],s2[15],s3[15];
if(fopen("Record.bin","rb")!=NULL)/*first time it will give null bcoz there is no file name record.bin,therefor it will not take income
and expense from file,assign value will be displayed i.e 0,0..after creating income and expense first time
user exits then record.bin
will be create and again when user runs program then this if will execute and take income and expense from file*/
{
point=readrecord();
currentincome=point->x;
currentexpense=point->y;
}
if(fopen("myincome.bin","rb")!=NULL)/*it means file is already created and some data is already there,and we have to get that and create
linked list,address of first node will be in income pointer. step bcoz if we dont do this than
after closing terminal if we create inome and saved it than previous data will be deletedand it
overrtites in file...after doing this income will point to first node and all data will be come in ram
in form linked list*/
{
income=readincome(income);
}
if(fopen("myexpense.bin","rb")!=NULL)
{
expense=readexpense(expense);
}
do{
printf(" _______________________________________________\n " );
printf(" | YOUR INCOME = %.2lf INR \n ",currentincome);
printf(" | YOUR EXPENSE = %.2lf INR \n ",currentexpense);
printf(" | YOUR BALANCE = %.2lf INR \n ",currentincome-currentexpense);
printf(" |_______________________________________________\n");
printf("ENTER THE OPTION FROM THE BELOW \n\n");
printf("1.INSERT INCOME \n");
printf("2.INSERT EXPENSE \n");
printf("3.VIEW INCOME RECORD \n");
printf("4.VIEW EXPENSE RECORD \n");
printf("5.EXIT\n");
scanf("%d",&option);
printf("\n\n\n");
switch(option)
{
case 1:
printf("************** ADD INCOME *****************\n\n");
printf("Enter The Date(e.g day month year)\n");
scanf("%s %s %s",s1,s2,s3);//gets,fgets and other functions are not working thats why these stepes have done
strcpy(a,"");
strcat(a,s1);
strcat(a," ");
strcat(a,s2);
strcat(a," ");
strcat(a,s3);
printf("Enter The Amount\n");
scanf("%lf",&b);
printf("Enter the Category\n");
scanf("%s",c);
currentincome=currentincome+b;
create(a,b,c,&income);
writeincome(income);
break;
case 2:
printf("************** ADD EXPENSE *****************\n\n");
printf("Enter The Date(e.g day month year)\n");
scanf("%s %s %s",s1,s2,s3);
strcpy(a,"");
strcat(a,s1);
strcat(a," ");
strcat(a,s2);
strcat(a," ");
strcat(a,s3);
printf("Enter The Amount\n");
scanf("%lf",&b);
printf("Enter The Category\n");
scanf("%s",c);
currentexpense=currentexpense+b;
create(a,b,c,&expense);
writeexpense(expense);
break;
case 3:
printf("********* YOUR INCOME RECORD IS *******\n\n");
display(3);
break;
case 4:
printf("********* YOUR EXPENSE RECORD IS *******\n\n");
display(4);
break;
case 5:
point=(struct record*)malloc(sizeof(struct record));
point->x=currentincome;
point->y=currentexpense;
write(point);
break;
default:
printf("WRONG OPTION SELECTED -Enter Valid Option");
break;
}
}while(option!=5);
return 0;
}
void create(char x[],double y,char z[],struct node **temp) /* it isokay to write *temp if we pass only income rather than &income*/
{
struct node *newnode,*ptr;
newnode=(struct node*)malloc(sizeof(struct node));
if(*temp==NULL)
{
strcpy(newnode->date,x);
newnode->amount=y;
strcpy(newnode->category,z);
newnode->next=NULL;
*temp=newnode;
}
else
{
ptr=*temp;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
strcpy(newnode->date,x);
newnode->amount=y;
strcpy(newnode->category,z);
newnode->next=NULL;
ptr->next=newnode;
}
}
void deleterecord(struct node *ptr)
{
struct node *freeme =ptr;
struct node *holdme=NULL;
while(freeme!=NULL)
{
holdme=freeme->next;
free(freeme);
freeme=holdme;
}
}
struct node *readnext(struct node *ptr,FILE *fpointer)
{
if(ptr==NULL)
{
ptr=(struct node *)malloc(sizeof(struct node));
fread(ptr,sizeof(struct node),1,fpointer);
ptr->next=NULL;
}
else
{
struct node *ptr1=ptr;
struct node *ptr2=(struct node *)malloc(sizeof(struct node));
while(ptr1->next!=NULL)
{
ptr1=ptr1->next;
}
fread(ptr2,sizeof(struct node),1,fpointer);
ptr1->next=ptr2;
ptr2->next=NULL;
}
return ptr;
}
struct node *readincome(struct node *ptr)
{
FILE *fpointer;
fpointer=fopen("myincome.bin","rb");
if(fpointer!=NULL)
{
deleterecord(ptr);
ptr=NULL;
fseek(fpointer,0,SEEK_END);
long filesize=ftell(fpointer);
rewind(fpointer);
int entries=(int)(filesize/(sizeof(struct node)));
for(int i=0;i<entries;i++)
{
fseek(fpointer,(sizeof(struct node)*i),SEEK_SET);
ptr=readnext(ptr,fpointer);
}
}
else
{
printf("ERROR IN OPENINNG FILE\n");
}
return ptr;
}
void display(int a3)
{
if(a3==3)//if case 3 is executed,we have to print income record
{
if(fopen("myincome.bin","rb")==NULL)/*if user try to view record when there is no any record,i.e when user runs program
first time and wants to view record i.e there is no any
file created yet as myincome.bin,then it will =NULL */
{
printf("NO RECORDS AVAILABLE\n\n");
printf("________________________________________________________________________________________________________________\n\n");
}
else
{
//income=readincome(income);
struct node *ptr2=income;
while(ptr2!=NULL)
{
printf("Date: %s\nAmount: %.2lf INR\nCategory: %s\n\n",ptr2->date,ptr2->
amount,ptr2->category);
ptr2=ptr2->next;
}
printf("________________________________________________________________________________________________________________\n\n");
}
}
else if(a3==4)//if case 4 is executed then we have to print expenese record
{
if(fopen("myexpense.bin","rb")==NULL)/*if user try to view record when there is no any record,i.e when user runs program
first time and wants to view record i.e there is no any
file created yet as myexpense.bin,then it will =NULL */
{
printf("NO RECORDS AVAILABLE\n\n");
printf("________________________________________________________________________________________________________________\n\n");
}
else
{
// expense=readexpense(expense);
struct node *ptr2=expense;
while(ptr2!=NULL)
{
printf("Date: %s\nAmount: %.2lf INR\nCategory: %s\n\n",ptr2->date,ptr2->
amount,ptr2->category);
ptr2=ptr2->next;
}
printf("________________________________________________________________________________________________________________\n\n");
}
}
}
void writeincome(struct node *ptr)
{
FILE *fpointer;
fpointer=fopen("myincome.bin","wb");
if(fpointer!=NULL)
{
struct node *ptr1=ptr;
struct node *holdnext=NULL;
while(ptr1!=NULL)
{
holdnext=ptr1->next;
ptr1->next=NULL;
fseek(fpointer,0,SEEK_END);
fwrite(ptr1,sizeof(struct node),1,fpointer);/*everytime we write into file,it will overwrite the data...... whole data will be deleted and new data willl be written intofile*/
ptr1->next=holdnext;
holdnext=NULL;
ptr1=ptr1->next;
}
fclose(fpointer);
fpointer=NULL;
printf("\nINCOME SAVED SUCCESSFULLY\n\n");
printf("________________________________________________________________________________________________________________\n\n");
}
else{
printf("\nCANNOT SAVE INCOME..TRY AGAIN\n");
printf("________________________________________________________________________________________________________________\n\n");
}
}
void writeexpense(struct node *ptr)
{
FILE *fpointer;
fpointer=fopen("myexpense.bin","wb");
if(fpointer!=NULL)
{
struct node *ptr1=ptr;
struct node *holdnext=NULL;
while(ptr1!=NULL)
{
holdnext=ptr1->next;
ptr1->next=NULL;
fseek(fpointer,0,SEEK_END);
fwrite(ptr1,sizeof(struct node),1,fpointer);/*everytime we write into file,it will overwrite the data.....
whole data will be deleted and new data willl be written intofile*/
ptr1->next=holdnext;
holdnext=NULL;
ptr1=ptr1->next;
}
fclose(fpointer);
fpointer=NULL;
printf("\nEXPENSE SAVED SUCCESSFULLY\n\n");
printf("________________________________________________________________________________________________________________\n\n");
}
else{
printf("\nCANNOT SAVE EXPENSE..TRY AGAIN\n\n");
printf("________________________________________________________________________________________________________________\n\n");
}
}
struct node *readexpense(struct node *ptr)
{
FILE *fpointer;
fpointer=fopen("myexpense.bin","rb");
if(fpointer!=NULL)
{
deleterecord(ptr);
ptr=NULL;
fseek(fpointer,0,SEEK_END);
long filesize=ftell(fpointer);
rewind(fpointer);
int entries=(int)(filesize/(sizeof(struct node)));
for(int i=0;i<entries;i++)
{
fseek(fpointer,(sizeof(struct node)*i),SEEK_SET);
ptr=readnext(ptr,fpointer);
}
}
else
{
printf("cannonot open file\n");
}
return ptr;
}
void write(struct record *point)
{
FILE *fpointer;
fpointer=fopen("Record.bin","wb");
if(fpointer!=NULL)
{
fseek(fpointer,0,SEEK_END);
fwrite(point,sizeof(struct record),1,fpointer);/*everytime we write into file,it will overwrite the data.....
whole data will be deleted and new data willl be written intofile*/
}
else{
printf("FILEOPEN ERROR\n");
}
fclose(fpointer);
fpointer=NULL;
}
struct record *readrecord()
{
FILE *fpointer;
fpointer=fopen("Record.bin","rb");
struct record *ptr=NULL;
if(fpointer!=NULL)
{
fseek(fpointer,0,SEEK_SET);
ptr=(struct record *)malloc(sizeof(struct record));
fread(ptr,sizeof(struct record),1,fpointer);
}
else
{
printf("CANNOT OPEN FILE\n");
}
return ptr;
}