Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Suivi de compte bancaire
- // Geecko, 2011
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- typedef struct
- {
- char name[16];
- int (*f)();
- } Command;
- typedef struct
- {
- int c, i;
- char** v;
- } Argument;
- typedef struct
- {
- short day, month;
- int year;
- char desc[32];
- float balance;
- } Operation;
- char file_path[128];
- Argument arg;
- Operation *op = NULL;
- int op_size = 0;
- // Common
- int checkCommand(Command cmd[], int size, char name[])
- {
- int i;
- for (i=0; i<size; i++)
- {
- if (!strcmp(cmd[i].name,name)) return i;
- }
- return -1;
- }
- int remainingArg()
- {
- return arg.c - arg.i;
- }
- void exec(Command cmd[], int size)
- {
- int err;
- if (arg.i >= arg.c) return;
- err = checkCommand(cmd,size,arg.v[arg.i]);
- if (err < 0) printf("Invalid command (%s)\n",arg.v[arg.i]);
- arg.i++;
- err = cmd[err].f();
- if (err > 0) printf("Error arg #%d\n",arg.i+err-1);
- arg.i--;
- }
- void push(Operation t)
- {
- op = realloc(op,(++op_size) * sizeof(Operation));
- op[op_size-1] = t;
- }
- void pop()
- {
- op = realloc(op,(--op_size) * sizeof(Operation));
- }
- // File management
- void load()
- {
- op = NULL;
- op_size = 0;
- FILE* file = fopen(file_path,"r");
- if (file == NULL)
- {
- printf("Error opening file for reading\n");
- return;
- }
- Operation t;
- for (;;)
- {
- if (fscanf(file,"%*d %2hd%2hd%4d \"%[^\"]\" %f\n",
- &t.day,&t.month,&t.year,t.desc,&t.balance) <= 0) break;
- op = realloc(op,(++op_size) * sizeof(Operation));
- op[op_size-1] = t;
- }
- fclose(file);
- }
- void write()
- {
- int i;
- FILE* file = fopen(file_path,"w");
- if (file == NULL)
- {
- printf("Error opening file for writing\n");
- return;
- }
- Operation t;
- for (i=0; i<op_size; i++)
- {
- t = op[i];
- if (fprintf(file,"%d %02hd%02hd%4d \"%s\" %.2f\n",
- i+1,t.day,t.month,t.year,t.desc,t.balance) <= 0) break;
- }
- fclose(file);
- }
- // "new"
- int new()
- {
- int amount;
- char desc[32];
- if (remainingArg() < 2) return arg.c - arg.i + 1;
- if (sscanf(arg.v[arg.i+0],"%s",desc) <= 0) return 1;
- if (sscanf(arg.v[arg.i+1],"%d",&amount) <= 0) return 2;
- time_t rawTime;
- struct tm* timeInfo;
- time(&rawTime);
- timeInfo = localtime(&rawTime);
- Operation t;
- t.day = timeInfo->tm_mday;
- t.month = timeInfo->tm_mon + 1;
- t.year = timeInfo->tm_year + 1900;
- t.balance = amount + (op_size>0 ? op[op_size-1].balance : 0);
- strncpy(t.desc,desc,32);
- push(t);
- write();
- printf("New operation (%02d/%02d/%4d) :\n",t.day,t.month,t.year);
- printf(" - ID is #%d\n",op_size);
- printf(" - Current balance is %.2f euro(s)\n",op[op_size-1].balance);
- return 0;
- }
- // "delete"
- int delete()
- {
- if (remainingArg() != 0) return 1;
- if (op_size <= 0)
- {
- printf("Nothing to delete\n");
- return 0;
- }
- printf("Deleting the last operation (%02d/%02d/%4d) :\n",
- op[op_size-1].day,op[op_size-1].month,op[op_size-1].year);
- printf(" - ID is back to #%d\n",op_size);
- printf(" - Current balance is back to %.2f euro(s)\n",op[op_size-2].balance);
- pop();
- write();
- return 0;
- }
- // Main
- Command cmd_main[2] = {{"new",new},{"delete",delete}};
- int main(int argc, char **argv)
- {
- arg.c = argc;
- arg.v = argv;
- arg.i = 1;
- strcpy(file_path,argv[0]);
- strcpy(strrchr(file_path,'/')+1,"cmpt");
- load();
- exec(cmd_main,2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement