Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //michal groberman id 307994988
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //#define ENDOFLINE '\0'; /////////////////////////
- #define INIT_SIZE 1
- typedef int BOOL;
- #define FALSE 0
- #define TRUE 1
- void history(void);
- char *getstr(int *str_size);
- void free_one_space_in_stock(char** stock, int size);
- void insert_line_in_stock(char *str, char ** stock, int size, int count);
- int command_line(char **stock, char *str, int size, int count);
- int printlastwithout(char **stock, char *str, int size, int count);
- int print_last_start_with_str(char **stock, char *str, int size, int count);
- void print_stock(char** stock, int size, int count);
- int print_n_line(char **stock, int size, int count, int n);
- void main()
- {
- history(); // Start the history() function
- }
- void history(void)
- {
- char**stock = NULL;
- int size = 0;
- char *tempstr = NULL;
- char ch;
- int count = 0;
- int str_size = 0;
- int write = 0;
- char*quit_line = "!quit";
- //int compare = 1;
- printf(" Please enter the history storage size >\n");
- scanf("%d", &size);
- scanf("%c", &ch);
- printf("The storage size was set to %d\n", size);
- stock = (char**)malloc(size*sizeof(char*));
- if (stock == NULL)
- exit(1);
- tempstr = getstr(&str_size);
- if ((strcmp(tempstr, quit_line)) == 0)
- printf("Thanks for using the history application, bye bye\n");
- while ((strcmp(tempstr, quit_line)) != 0)
- {
- if (tempstr[0] != '!')
- {
- insert_line_in_stock(tempstr, stock, size, count);
- count++;
- }
- else
- {
- count = command_line(stock, tempstr, size, count);
- }
- tempstr = getstr(&str_size);
- }
- printf("Thanks for using the history application, bye bye\n");//////שלא ידפיס פעמיים אם הודפס בעבר/
- }
- char *getstr(int *str_size)
- {
- {
- char *str = NULL;
- int psize, lsize;
- psize = 1;
- lsize = 0;
- char ch;
- BOOL FT = TRUE;
- str = (char*)malloc(sizeof(char));
- if (!str)
- {
- printf("out of memory");
- exit(1);
- }
- while (FT == TRUE || ch != '\n')
- {
- FT = FALSE;
- scanf("%c", &ch);
- if (lsize == psize)
- {
- psize *= 2;
- str = (char*)realloc(str, (psize *sizeof(char)));
- if (!str)
- {
- printf("out of memory");
- exit(1);
- }
- }
- if (ch != '\n') //
- {
- str[lsize] = ch;
- lsize++;
- }
- }
- str[lsize++] = '\0';
- if (lsize < psize)//תנאי "הידוק"
- str = (char*)realloc(str, lsize *sizeof(char));
- *str_size = lsize;
- return(str);
- }
- }
- void free_one_space_in_stock(char** stock, int size)
- {
- int i;
- free(stock[0]);
- for (i = 0; i < size - 1; i++)
- {
- stock[i] = stock[i + 1];
- }
- stock[size - 1] = NULL;
- }
- void insert_line_in_stock(char *str, char** stock, int size, int count)
- {
- int place_to_insert;
- if (count >= size)
- place_to_insert = count;
- if (count >= size)
- {
- place_to_insert = (count%size);
- if (place_to_insert == 0)
- place_to_insert = size - 1;
- else
- place_to_insert = (count%size)-1;
- }
- else
- {
- place_to_insert = count;
- }
- stock[place_to_insert] = str;
- }
- int command_line(char **stock, char *str, int size, int count)
- {
- char ch;
- ch = str[1];
- int last_inserted;
- char *str1;
- int n = 0;
- int len;
- int i;
- if (ch == '!')
- {
- if (count >= size)
- {
- last_inserted = ((count+1)%size);
- if (last_inserted == 0)
- last_inserted = size - 1;
- else
- last_inserted = ((count + 1) % size) - 1;
- }
- else
- last_inserted = count-1;
- printf("%s\n", stock[last_inserted]);
- printf("inserted to position %d",last_inserted);
- insert_line_in_stock(stock[last_inserted], stock, size, count);
- return count + 1;
- }
- else if (strcmp(str, "!L") == 0)
- {
- printf("Number of last line is %d\n", count);
- str1 = (char*)malloc(2 * sizeof(char));//making a string size 2, will hold the number of the line;
- sprintf(str1, "%d", count);//sprintf insert int to string;
- insert_line_in_stock(str1, stock, size, count);
- return count + 1;
- }
- else if (ch == '~')
- {
- len = strlen(str) - 2;
- str1 = (char*)malloc(len*sizeof(char));
- strcpy(str1, str + 2);
- count = printlastwithout(stock, str1, size, count);
- return(count);
- }
- else if (strcmp(str, "!print") == 0)
- {
- print_stock(stock, size, count);
- return count;
- }
- else if ((ch >= '0') && (ch <= '9'))
- {
- n = (int)ch - '0';
- count = print_n_line(stock, size, count, n);
- return count;
- }
- else//look for string in start of stock strings
- {
- len = strlen(str) - 1;
- str1 = (char*)malloc(len*sizeof(char));
- strcpy(str1, str + 1);
- count = print_last_start_with_str(stock, str1, size, count);
- return(count);
- }
- return(count);
- }
- int printlastwithout(char **stock, char *str, int size, int count)
- {
- int i;
- BOOL flag = FALSE;
- char *exist = NULL;
- if (count < size)
- {
- for (i = count - 1; (flag == FALSE) && (i >= 0); i--)////
- {
- exist = strstr(stock[i], str);/////////////////////////////////////////////////////////////
- if (exist == NULL)
- {
- printf("%s", stock[i]);
- insert_line_in_stock(stock[i], stock, size, count);
- count++;
- flag = TRUE;
- }
- }
- }
- else
- {
- for (i = size - 1; (flag == FALSE) && (i >= 0); i--)////
- {
- exist = strstr(stock[i], str);/////////////////////////////////////////////////////////////
- if (exist == NULL)
- {
- printf("%s", stock[i]);
- insert_line_in_stock(stock[i], stock, size, count);
- count++;
- flag = TRUE;
- }
- }
- }
- if (flag == FALSE)
- printf("line doesn't excist");///
- return(count);
- }
- int print_last_start_with_str(char **stock, char *str, int size, int count)
- {
- int i;
- BOOL different = TRUE;
- char *exist;
- if (count < size)
- {
- for (i = count - 1; (different == TRUE) && (i >= 0); i--)
- {
- exist = strstr(stock[i], str);
- if (exist == stock[i])////
- {
- printf("%s", stock[i]);
- insert_line_in_stock(stock[i], stock, size, count);
- count++;
- different = FALSE;
- }
- }
- }
- else
- {
- for (i = size - 1; (different == TRUE) && (i >= 0); i--)////
- {
- //printf("stock[%d] %s str %s\n", i, stock[i], str);
- exist = strstr(stock[i], str);
- if (stock[i] == exist)
- {
- printf("%s", stock[i]);
- insert_line_in_stock(stock[i], stock, size, count);
- count++;
- different = FALSE;
- }
- }
- }
- if (different == TRUE)
- printf("line doesn't excist");///
- return(count);
- /*for (i = count; (i >= 0) && (different == TRUE); i--)
- {
- excisit = strstr(stock[i], str);
- if (stock[i] == excisit)
- {
- printf("%s", stock[i]);
- insert_line_in_stock(stock[i], stock, size, count);
- count++;
- different = FALSE;
- }
- }
- if (different == TRUE)
- printf("error");
- return(count);*/
- }
- void print_stock(char** stock, int size, int count)
- {
- int i, j = 0;
- for (i = 0; (j < count) && (i<size); i++)//=
- {
- j = count - size + i + 1;
- printf("%d. %s\n", j, stock[i]);
- }
- return;
- }
- int print_n_line(char **stock, int size, int count, int n)
- {
- if ((count < size) && (n <= count))
- {
- printf("%s", stock[n - 1]);
- insert_line_in_stock(stock[n - 1], stock, size, count);
- count++;
- }
- else if ((n>count) || (n <= count - size))
- printf("Could not restore line number %d, currently lines %d-%d are in the storage", n, count - size + 1, count);
- else
- {
- printf("%s", stock[size - (count - n + 1)]);
- insert_line_in_stock(stock[size - (count - n + 1)], stock, size, count);
- count++;
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement