Advertisement
GeeckoDev

pcompte-1

Aug 24th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. // Suivi de compte bancaire
  2. // Geecko, 2011
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8.  
  9. typedef struct
  10. {
  11.   char name[16];
  12.   int (*f)();
  13. } Command;
  14.  
  15. typedef struct
  16. {
  17.   int c, i;
  18.   char** v;
  19. } Argument;
  20.  
  21. typedef struct
  22. {
  23.   short day, month;
  24.   int year;
  25.   char desc[32];
  26.   float balance;
  27. } Operation;
  28.  
  29. char file_path[128];
  30. Argument arg;
  31. Operation *op = NULL;
  32. int op_size = 0;
  33.  
  34. // Common
  35.  
  36. int checkCommand(Command cmd[], int size, char name[])
  37. {
  38.   int i;
  39.   for (i=0; i<size; i++)
  40.   {
  41.     if (!strcmp(cmd[i].name,name)) return i;
  42.   }
  43.   return -1;
  44. }
  45.  
  46.  
  47. int remainingArg()
  48. {
  49.   return arg.c - arg.i;
  50. }
  51.  
  52.  
  53. void exec(Command cmd[], int size)
  54. {
  55.   int err;
  56.  
  57.   if (arg.i >= arg.c) return;
  58.  
  59.   err = checkCommand(cmd,size,arg.v[arg.i]);
  60.   if (err < 0) printf("Invalid command (%s)\n",arg.v[arg.i]);
  61.   arg.i++;
  62.  
  63.   err = cmd[err].f();
  64.   if (err > 0) printf("Error arg #%d\n",arg.i+err-1);
  65.   arg.i--;
  66. }
  67.  
  68. void push(Operation t)
  69. {
  70.   op = realloc(op,(++op_size) * sizeof(Operation));
  71.   op[op_size-1] = t;
  72. }
  73.  
  74. void pop()
  75. {
  76.   op = realloc(op,(--op_size) * sizeof(Operation));
  77. }
  78.  
  79. // File management
  80.  
  81. void load()
  82. {
  83.   op = NULL;
  84.   op_size = 0;
  85.  
  86.   FILE* file = fopen(file_path,"r");
  87.   if (file == NULL)
  88.   {
  89.     printf("Error opening file for reading\n");
  90.     return;
  91.   }
  92.  
  93.   Operation t;
  94.   for (;;)
  95.   {
  96.     if (fscanf(file,"%*d %2hd%2hd%4d \"%[^\"]\" %f\n",
  97.                &t.day,&t.month,&t.year,t.desc,&t.balance) <= 0) break;
  98.                
  99.     op = realloc(op,(++op_size) * sizeof(Operation));
  100.     op[op_size-1] = t;
  101.   }
  102.  
  103.   fclose(file);
  104. }
  105.  
  106.  
  107. void write()
  108. {
  109.   int i;
  110.  
  111.   FILE* file = fopen(file_path,"w");
  112.   if (file == NULL)
  113.   {
  114.     printf("Error opening file for writing\n");
  115.     return;
  116.   }
  117.  
  118.   Operation t;
  119.   for (i=0; i<op_size; i++)
  120.   {
  121.     t = op[i];
  122.     if (fprintf(file,"%d %02hd%02hd%4d \"%s\" %.2f\n",
  123.                 i+1,t.day,t.month,t.year,t.desc,t.balance) <= 0) break;
  124.   }
  125.  
  126.   fclose(file);
  127. }
  128.  
  129. // "new"
  130.  
  131. int new()
  132. {
  133.   int amount;
  134.   char desc[32];
  135.  
  136.   if (remainingArg() < 2) return arg.c - arg.i + 1;
  137.   if (sscanf(arg.v[arg.i+0],"%s",desc) <= 0) return 1;
  138.   if (sscanf(arg.v[arg.i+1],"%d",&amount) <= 0) return 2;
  139.  
  140.   time_t rawTime;
  141.   struct tm* timeInfo;
  142.   time(&rawTime);
  143.   timeInfo = localtime(&rawTime);
  144.  
  145.   Operation t;
  146.   t.day = timeInfo->tm_mday;
  147.   t.month = timeInfo->tm_mon + 1;
  148.   t.year = timeInfo->tm_year + 1900;
  149.   t.balance = amount + (op_size>0 ? op[op_size-1].balance : 0);
  150.   strncpy(t.desc,desc,32);
  151.   push(t);
  152.  
  153.   write();
  154.   printf("New operation (%02d/%02d/%4d) :\n",t.day,t.month,t.year);
  155.   printf(" - ID is #%d\n",op_size);
  156.   printf(" - Current balance is %.2f euro(s)\n",op[op_size-1].balance);
  157.  
  158.   return 0;
  159. }
  160.  
  161. // "delete"
  162.  
  163. int delete()
  164. {
  165.   if (remainingArg() != 0) return 1;
  166.   if (op_size <= 0)
  167.   {
  168.     printf("Nothing to delete\n");
  169.     return 0;
  170.   }
  171.  
  172.   printf("Deleting the last operation (%02d/%02d/%4d) :\n",
  173.          op[op_size-1].day,op[op_size-1].month,op[op_size-1].year);
  174.   printf(" - ID is back to #%d\n",op_size);
  175.   printf(" - Current balance is back to %.2f euro(s)\n",op[op_size-2].balance);
  176.  
  177.   pop();
  178.   write();  
  179.  
  180.   return 0;
  181. }
  182.  
  183. // Main
  184.  
  185. Command cmd_main[2] = {{"new",new},{"delete",delete}};
  186.  
  187. int main(int argc, char **argv)
  188. {
  189.   arg.c = argc;
  190.   arg.v = argv;
  191.   arg.i = 1;
  192.  
  193.   strcpy(file_path,argv[0]);
  194.   strcpy(strrchr(file_path,'/')+1,"cmpt");
  195.  
  196.   load();  
  197.   exec(cmd_main,2);
  198.  
  199.   return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement