Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.42 KB | None | 0 0
  1. //michal groberman id 307994988
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. //#define  ENDOFLINE '\0'; /////////////////////////
  7. #define INIT_SIZE 1
  8. typedef int BOOL;
  9. #define FALSE 0
  10. #define TRUE 1
  11.  
  12.  
  13. void history(void);
  14. char *getstr(int *str_size);
  15. void free_one_space_in_stock(char** stock, int size);
  16. void insert_line_in_stock(char *str, char ** stock, int size, int count);
  17. int command_line(char **stock, char *str, int  size, int count);
  18. int printlastwithout(char **stock, char *str, int size, int count);
  19. int print_last_start_with_str(char **stock, char *str, int size, int count);
  20. void print_stock(char** stock, int size, int count);
  21. int print_n_line(char **stock, int size, int count, int n);
  22.  
  23.  
  24. void main()
  25. {
  26.     history();  // Start the history() function
  27. }
  28. void history(void)
  29. {
  30.     char**stock = NULL;
  31.     int size = 0;
  32.     char *tempstr = NULL;
  33.     char ch;
  34.     int count = 0;
  35.     int str_size = 0;
  36.     int write = 0;
  37.     char*quit_line = "!quit";
  38.  
  39.     //int compare = 1;
  40.     printf(" Please enter the history storage size >\n");
  41.     scanf("%d", &size);
  42.     scanf("%c", &ch);
  43.     printf("The storage size was set to %d\n", size);
  44.     stock = (char**)malloc(size*sizeof(char*));
  45.     if (stock == NULL)
  46.         exit(1);
  47.  
  48.  
  49.     tempstr = getstr(&str_size);
  50.  
  51.  
  52.     if ((strcmp(tempstr, quit_line)) == 0)
  53.         printf("Thanks for using the history application, bye bye\n");
  54.  
  55.     while ((strcmp(tempstr, quit_line)) != 0)
  56.     {
  57.  
  58.  
  59.         if (tempstr[0] != '!')
  60.         {
  61.  
  62.             insert_line_in_stock(tempstr, stock, size, count);
  63.             count++;
  64.         }
  65.         else
  66.         {
  67.             count = command_line(stock, tempstr, size, count);
  68.         }
  69.  
  70.  
  71.         tempstr = getstr(&str_size);
  72.  
  73.     }
  74.  
  75.  
  76.     printf("Thanks for using the history application, bye bye\n");//////שלא ידפיס פעמיים אם הודפס בעבר/
  77. }
  78.  
  79. char *getstr(int *str_size)
  80. {
  81.     {
  82.         char *str = NULL;
  83.         int psize, lsize;
  84.         psize = 1;
  85.         lsize = 0;
  86.         char ch;
  87.         BOOL FT = TRUE;
  88.         str = (char*)malloc(sizeof(char));
  89.         if (!str)
  90.         {
  91.             printf("out of memory");
  92.             exit(1);
  93.         }
  94.  
  95.         while (FT == TRUE || ch != '\n')
  96.         {
  97.             FT = FALSE;
  98.             scanf("%c", &ch);
  99.             if (lsize == psize)
  100.             {
  101.                 psize *= 2;
  102.                 str = (char*)realloc(str, (psize *sizeof(char)));
  103.                 if (!str)
  104.                 {
  105.                     printf("out of memory");
  106.                     exit(1);
  107.                 }
  108.             }
  109.             if (ch != '\n') //
  110.             {
  111.                 str[lsize] = ch;
  112.                 lsize++;
  113.             }
  114.  
  115.         }
  116.         str[lsize++] = '\0';
  117.         if (lsize < psize)//תנאי "הידוק"
  118.             str = (char*)realloc(str, lsize *sizeof(char));
  119.  
  120.         *str_size = lsize;
  121.  
  122.         return(str);
  123.     }
  124. }
  125.  
  126. void free_one_space_in_stock(char** stock, int size)
  127. {
  128.     int i;
  129.     free(stock[0]);
  130.     for (i = 0; i < size - 1; i++)
  131.     {
  132.         stock[i] = stock[i + 1];
  133.     }
  134.     stock[size - 1] = NULL;
  135. }
  136.  
  137. void insert_line_in_stock(char *str, char** stock, int size, int count)
  138. {
  139.     int place_to_insert;
  140.     if (count >= size)
  141.     place_to_insert = count;
  142.  
  143.     if (count >= size)
  144.     {
  145.         place_to_insert = (count%size);
  146.  
  147.         if (place_to_insert == 0)
  148.             place_to_insert = size - 1;
  149.         else
  150.             place_to_insert = (count%size)-1;
  151.     }
  152.  
  153.     else
  154.     {
  155.         place_to_insert = count;
  156.     }
  157.  
  158.     stock[place_to_insert] = str;
  159. }
  160. int command_line(char **stock, char *str, int  size, int count)
  161. {
  162.     char ch;
  163.     ch = str[1];
  164.     int last_inserted;
  165.     char *str1;
  166.     int n = 0;
  167.     int len;
  168.     int i;
  169.  
  170.     if (ch == '!')
  171.     {
  172.         if (count >= size)
  173.         {
  174.             last_inserted = ((count+1)%size);
  175.  
  176.             if (last_inserted == 0)
  177.                 last_inserted = size - 1;
  178.             else
  179.                 last_inserted = ((count + 1) % size) - 1;
  180.         }
  181.  
  182.     else
  183.         last_inserted = count-1;
  184.  
  185.         printf("%s\n", stock[last_inserted]);
  186.         printf("inserted to position %d",last_inserted);
  187.         insert_line_in_stock(stock[last_inserted], stock, size, count);
  188.         return count + 1;
  189.     }
  190.     else if (strcmp(str, "!L") == 0)
  191.     {
  192.         printf("Number of last line is %d\n", count);
  193.         str1 = (char*)malloc(2 * sizeof(char));//making a string size 2, will hold the number of the line;
  194.         sprintf(str1, "%d", count);//sprintf insert int to string;
  195.         insert_line_in_stock(str1, stock, size, count);
  196.         return count + 1;
  197.     }
  198.  
  199.     else if (ch == '~')
  200.     {
  201.         len = strlen(str) - 2;
  202.         str1 = (char*)malloc(len*sizeof(char));
  203.         strcpy(str1, str + 2);
  204.         count = printlastwithout(stock, str1, size, count);
  205.         return(count);
  206.     }
  207.     else if (strcmp(str, "!print") == 0)
  208.     {
  209.         print_stock(stock, size, count);
  210.         return count;
  211.     }
  212.     else if ((ch >= '0') && (ch <= '9'))
  213.     {
  214.         n = (int)ch - '0';
  215.         count = print_n_line(stock, size, count, n);
  216.         return count;
  217.     }
  218.     else//look for string in start of stock strings
  219.     {
  220.         len = strlen(str) - 1;
  221.         str1 = (char*)malloc(len*sizeof(char));
  222.         strcpy(str1, str + 1);
  223.         count = print_last_start_with_str(stock, str1, size, count);
  224.         return(count);
  225.  
  226.     }
  227.     return(count);
  228. }
  229. int printlastwithout(char **stock, char *str, int size, int count)
  230. {
  231.     int i;
  232.     BOOL flag = FALSE;
  233.     char *exist = NULL;
  234.     if (count < size)
  235.     {
  236.         for (i = count - 1; (flag == FALSE) && (i >= 0); i--)////
  237.         {
  238.             exist = strstr(stock[i], str);/////////////////////////////////////////////////////////////
  239.  
  240.             if (exist == NULL)
  241.             {
  242.                 printf("%s", stock[i]);
  243.                 insert_line_in_stock(stock[i], stock, size, count);
  244.                 count++;
  245.                 flag = TRUE;
  246.             }
  247.         }
  248.     }
  249.     else
  250.     {
  251.         for (i = size - 1; (flag == FALSE) && (i >= 0); i--)////
  252.         {
  253.             exist = strstr(stock[i], str);/////////////////////////////////////////////////////////////
  254.  
  255.             if (exist == NULL)
  256.             {
  257.                 printf("%s", stock[i]);
  258.                 insert_line_in_stock(stock[i], stock, size, count);
  259.                 count++;
  260.                 flag = TRUE;
  261.             }
  262.         }
  263.  
  264.     }
  265.  
  266.     if (flag == FALSE)
  267.         printf("line doesn't excist");///
  268.     return(count);
  269. }
  270. int print_last_start_with_str(char **stock, char *str, int size, int count)
  271. {
  272.     int i;
  273.     BOOL different = TRUE;
  274.     char *exist;
  275.  
  276.     if (count < size)
  277.     {
  278.         for (i = count - 1; (different == TRUE) && (i >= 0); i--)
  279.         {
  280.  
  281.             exist = strstr(stock[i], str);
  282.  
  283.             if (exist == stock[i])////
  284.             {
  285.                 printf("%s", stock[i]);
  286.                 insert_line_in_stock(stock[i], stock, size, count);
  287.                 count++;
  288.                 different = FALSE;
  289.             }
  290.         }
  291.     }
  292.     else
  293.     {
  294.         for (i = size - 1; (different == TRUE) && (i >= 0); i--)////
  295.         {
  296.             //printf("stock[%d] %s str %s\n", i, stock[i], str);
  297.             exist = strstr(stock[i], str);
  298.             if (stock[i] == exist)
  299.             {
  300.                 printf("%s", stock[i]);
  301.                 insert_line_in_stock(stock[i], stock, size, count);
  302.                 count++;
  303.                 different = FALSE;
  304.             }
  305.         }
  306.  
  307.     }
  308.  
  309.     if (different == TRUE)
  310.         printf("line doesn't excist");///
  311.     return(count);
  312.     /*for (i = count; (i >= 0) && (different == TRUE); i--)
  313.     {
  314.     excisit = strstr(stock[i], str);
  315.     if (stock[i] == excisit)
  316.     {
  317.     printf("%s", stock[i]);
  318.     insert_line_in_stock(stock[i], stock, size, count);
  319.     count++;
  320.     different = FALSE;
  321.     }
  322.     }
  323.     if (different == TRUE)
  324.     printf("error");
  325.     return(count);*/
  326. }
  327. void print_stock(char** stock, int size, int count)
  328. {
  329.     int i, j = 0;
  330.     for (i = 0; (j < count) && (i<size); i++)//=
  331.     {
  332.         j = count - size + i + 1;
  333.         printf("%d. %s\n", j, stock[i]);
  334.     }
  335.     return;
  336. }
  337. int print_n_line(char **stock, int size, int count, int n)
  338. {
  339.     if ((count < size) && (n <= count))
  340.     {
  341.         printf("%s", stock[n - 1]);
  342.         insert_line_in_stock(stock[n - 1], stock, size, count);
  343.         count++;
  344.     }
  345.  
  346.     else if ((n>count) || (n <= count - size))
  347.         printf("Could not restore line number %d, currently lines %d-%d are in the storage", n, count - size + 1, count);
  348.     else
  349.     {
  350.         printf("%s", stock[size - (count - n + 1)]);
  351.         insert_line_in_stock(stock[size - (count - n + 1)], stock, size, count);
  352.         count++;
  353.     }
  354.     return count;
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement