snaggedagge

p2 IO

Jan 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include "stringdb.h"
  5. #include "tree.h"
  6. #include "table.h"
  7. #include "file.h"
  8. #include "calculate.h"
  9. #include "initialize.h"
  10. #include "IOfunctions.h"
  11.  
  12. static int newEntrys=0; // To keep track if/how many entrys user has entered
  13.  
  14. void IO_getSearchStructs(TABLE tmptable,TREE tmpsearchtree)
  15. {
  16.   searchtree = tmpsearchtree;
  17.   table = tmptable;
  18. }
  19.  
  20. void IO_printDate(int year, int month)
  21. {
  22.     printf("\n");
  23.     for(int i=1;i <= 31;i++)
  24.     {
  25.         int key=year*10000 + month*100 +i;
  26.         int index= tree_find(searchtree,key);
  27.         if(index != KEY_NOT_FOUND)
  28.         printf("%s\n",stringdb_get(index) );
  29.     }
  30.     printf("\n");
  31. }
  32.  
  33. void IO_printDay(int month,int day)
  34. {
  35.     int key = (month-1)*31 + day-1;
  36.     int matches = table_count(table,key);
  37.     if(matches)
  38.     {
  39.         printf("\n");
  40.       for(int i = 1; i <= matches; i++)
  41.       {
  42.         printf("%s\n",stringdb_get(table_find(table,key,i)) );
  43.       }
  44.         printf("\n");
  45.     }
  46. }
  47.  
  48. void IO_searchMenu(void)
  49. {
  50.     int firstInput=0;
  51.     int secondInput=0;
  52.  
  53.     while(firstInput<1||secondInput<1||secondInput>12)
  54.     {
  55.       printf("Enter (year and month) or (day and month) #> ");
  56.       scanf(" %d %d",&firstInput,&secondInput);
  57.     }
  58.  
  59.     if(firstInput > 31)
  60.         IO_printDate(firstInput,secondInput);
  61.  
  62.     else
  63.         IO_printDay(secondInput,firstInput);
  64. }
  65.  
  66. void IO_addeventMenu(void)
  67. {
  68.     int year,month,day;
  69.     char newEvent[300];
  70.     char entry[317];
  71.     while(month<1||month>12||day<1||day>31||year<32)  // To reduce inputs that will not work
  72.     {
  73.         printf("Enter date of event (YYYY/MM/DD) #> ");
  74.         scanf("%d/%d/%d",&year,&month,&day);
  75.     }
  76.     while(getchar()!='\n'); // To clear input buffer
  77.  
  78.     printf("Enter what happened on that day #> ");
  79.     fgets(newEvent,300,stdin);
  80.  
  81.     convert_date(day,month,year,entry);
  82.     strcat(entry,newEvent); // stored the strings separately, then fuse them together
  83.     insertEvent(entry);
  84.     newEntrys++;
  85. }
  86.  
  87. void IO_entrytofile()
  88. {
  89.   char choice='n';
  90.   printf("Want to save your new entrys?(y/n) #> ");
  91.   scanf(" %c",&choice);
  92.  
  93.   int size = stringdb_size();
  94.     while(newEntrys-- && choice == 'y')
  95.     {
  96.         file_writeline(stringdb_get(size));
  97.         size--;
  98.     }
  99. }
  100.  
  101. int searchIO(void)
  102. {
  103.     int choice;
  104.     printf("Welcome to historychannel\n1.) Shutdown program\n2.) Search database\n3.) Add historic events\n");
  105.     printf("Your choice #> ");
  106.     scanf(" %d",&choice);
  107.  
  108.     switch(choice)
  109.     {
  110.     case 1:
  111.         if(newEntrys)
  112.           IO_entrytofile();
  113.         return 0;
  114.     case 2:
  115.         IO_searchMenu();
  116.         break;
  117.     case 3:
  118.         IO_addeventMenu();
  119.         break;
  120.     }
  121.   return 1;
  122. }
Add Comment
Please, Sign In to add comment