From 9078d3a3c155bc5813d03a03ffface15d48a1a9f Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 21 Oct 2020 14:15:29 +0000 Subject: [PATCH 01/10] Update 'a2/program.c' --- a2/program.c | 1 - 1 file changed, 1 deletion(-) diff --git a/a2/program.c b/a2/program.c index 20a34b9..a02d772 100644 --- a/a2/program.c +++ b/a2/program.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include From 69f2c4959674dc6f9142ecda33f441046aaaea4c Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 21 Oct 2020 14:18:07 +0000 Subject: [PATCH 02/10] Update 'a2/program.c' --- a2/program.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/a2/program.c b/a2/program.c index a02d772..a7e2c41 100644 --- a/a2/program.c +++ b/a2/program.c @@ -86,7 +86,7 @@ int main(){ } else if(!find_pair(gettop(), line[i])){ - printf("Crossed bracket %c in %d, expected %c \n", line[i], i, r_bracket[j]); + printf("Crossed bracket %c in %d, expected %c \n", line[i], i, r_bracket[f]); return 0; } else{ @@ -100,7 +100,6 @@ int main(){ int begin = 0; int end = strlen(r_bracket); int temp; - //printf("%s", b); while(begin < end){ temp = r_bracket[begin]; r_bracket[begin++] = r_bracket[--end]; @@ -113,7 +112,9 @@ int main(){ else { printf("Missing closing brackets: %s\n", r_bracket); } + free(l_bracket); free(r_bracket); + return 0; } From 27ce4ed1176bd6f98bcc1105e968d05df1e1f749 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 21 Oct 2020 14:19:37 +0000 Subject: [PATCH 03/10] Update 'cv3/program.c' --- cv3/program.c | 110 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 44 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 9f39b2b..12e1807 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,9 +1,9 @@ -#include #include #include #include +#include -#define STACK_SIZE 100 +#define STACK_SIZE 10 struct stack { float values[STACK_SIZE]; @@ -11,17 +11,29 @@ struct stack { }; void push(struct stack* stack,float values){ - assert(stack->size < STACK_SIZE); - stack->values[stack->size] = values; - stack->size += 1; + if(stack->size < 10){ + stack->values[stack->size] = values; + stack->size += 1; + } + else{ + printf("full stack\n"); + exit(0); + + } } float pop(struct stack *stack){ - assert(stack->size > 0); - float values = stack->values[stack->size-1]; - stack->size -= 1; - return values; + if(stack->size > 0){ + float value = stack->values[stack->size-1]; + stack->size -= 1; + return value; + } + else{ + printf("not enough operands\n"); + exit(0); + } } + void print_stack(struct stack *stack){ int i; int len = stack->size - 1; @@ -30,9 +42,8 @@ void print_stack(struct stack *stack){ } if(stack->size != 0){ printf("%0.2f ", stack->values[i]); - } - printf("\n"); - + printf("\n"); + } } @@ -40,44 +51,55 @@ int main() { struct stack mystack; memset(&mystack, 0, sizeof(struct stack)); int i = 0; - float z; - for(i = 0; i < 10000; i++){ + for(i = 0; i < 100; i++){ + char line[10]; char *x = fgets(line, 10, stdin); float r = 0; - - if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){ - printf("no input"); - return 0; - } + float z = 0; - if(line[0] == '+'){ - r = pop(&mystack) + pop(&mystack); - } - else if(line[0] == '-'){ - z = pop(&mystack); - r = pop(&mystack) - z; - } - else if(line[0] == '*'){ - r = pop(&mystack) * pop(&mystack); - } - else if(line[0] == '/'){ - z = pop(&mystack); - if(z != 0.0){ - r = pop(&mystack) / z; + if(line[1] != 0 && x != NULL){ + + + if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){ + printf("bad input\n"); + return 0; + } + + if(line[0] == '+'){ + r = pop(&mystack) + pop(&mystack); } + else if(line[0] == '-'){ + z = pop(&mystack); + r = pop(&mystack) - z; + } + else if(line[0] == '*'){ + r = pop(&mystack) * pop(&mystack); + } + else if(line[0] == '/'){ + z = pop(&mystack); + if(z == 0){ + printf("division by zero\n"); + return 0; + } + else { + r = pop(&mystack) / z; + } + } + + + else { + r = strtof(line,&x); + } + + push(&mystack,r); + print_stack(&mystack); } - - else { - r = strtof(line,&x); - } - - push(&mystack,r); - print_stack(&mystack); - + else{ + printf("no input\n"); + return 0; + } } - - - + return 0; } From c7eaa875079364c631aec53c25f82638fcd015ec Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 21 Oct 2020 14:20:21 +0000 Subject: [PATCH 04/10] Update 'cv2/program.c' --- cv2/program.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index 9798c01..2a45013 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -2,7 +2,6 @@ #include #include - #define LINESIZE 100 struct pizza{ @@ -63,8 +62,6 @@ int main(){ qsort(jedalny_listok, counter, sizeof(struct pizza), compareprize); - //qsort(jedalny_listok, counter, sizeof(struct pizza), comparename); - int i = 0; for(i = 0; i < counter; i++){ printf("%s", jedalny_listok[i].name); From adc28499849d8b76a9dedead8b82b4155bec8575 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 21 Oct 2020 14:48:39 +0000 Subject: [PATCH 05/10] Update 'cv2/program.c' --- cv2/program.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index 2a45013..e4ad9a0 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -29,20 +29,14 @@ int read_pizza(struct pizza* item){ return 1; } } -int comparename(const void *a, const void *b){ - - struct pizza* pizza_a = (void*) a; - struct pizza* pizza_b = (void*) b; +int compare(const void *a, const void *b){ - return strcmp(pizza_a->name, pizza_b->name); - -} -int compareprize(const void *a, const void *b){ - struct pizza* pizza_a = (void*) a; struct pizza* pizza_b = (void*) b; - float r = (pizza_a->prize > pizza_b->prize) - (pizza_a->prize < pizza_b->prize); + if (r == 0){ + r = strcmp(pizza_a->name, pizza_b->name); + } return r; } @@ -60,7 +54,7 @@ int main(){ counter++; } - qsort(jedalny_listok, counter, sizeof(struct pizza), compareprize); + qsort(jedalny_listok, counter, sizeof(struct pizza), compare); int i = 0; for(i = 0; i < counter; i++){ @@ -68,7 +62,5 @@ int main(){ printf("%f", jedalny_listok[i].prize); printf("\n"); } - - return 0; } From ffb35f129d327a5f09ae06c048c5af4142148682 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sat, 24 Oct 2020 16:50:10 +0000 Subject: [PATCH 06/10] Update 'a2/program.c' --- a2/program.c | 55 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/a2/program.c b/a2/program.c index a7e2c41..c44595b 100644 --- a/a2/program.c +++ b/a2/program.c @@ -9,6 +9,8 @@ struct stack{ int size; }stack; +///add bracket to stack + void push(char ch){ if(stack.size < 100){ stack.size++; @@ -16,7 +18,9 @@ void push(char ch){ } else exit(0); -} +} + +///if i have opening bracket in stack, then decrement size void pop(){ if(stack.size >= 0) @@ -25,6 +29,8 @@ void pop(){ exit(0); } +///compare brackets + bool find_pair(char a, char b){ if(a == '(' && b == ')') return true; @@ -36,8 +42,10 @@ bool find_pair(char a, char b){ return true; return false; - } + +///function to find right pair + char right_pair(char a){ char b; if(a == '(') @@ -52,6 +60,8 @@ char right_pair(char a){ return b; } +///find the last opening bracket in the stack + char gettop(){ return stack.data[stack.size]; } @@ -68,53 +78,60 @@ int main(){ int l = strlen(line); int i, j = -1, f = -1;; - char *l_bracket = calloc(l, sizeof(char)); - char *r_bracket = calloc(l, sizeof(char)); + char *o_bracket = calloc(l, sizeof(char)); + char *c_bracket = calloc(l, sizeof(char)); for(i = 0; i < l; i++){ if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){ push(line[i]); j++; f++; - l_bracket[j] = gettop(); - r_bracket[f] = right_pair(l_bracket[j]); + o_bracket[j] = gettop(); //get opening bracket from the stack + c_bracket[f] = right_pair(o_bracket[j]); //get closing bracket } - if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){ - if(stack.size == -1){ + if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){ + + if(stack.size == -1){ //if there is no opening bracket in the stack printf("Unexpected closing bracket %c in %d\n", line[i], i); return 0; } - else if(!find_pair(gettop(), line[i])){ - printf("Crossed bracket %c in %d, expected %c \n", line[i], i, r_bracket[f]); + else if(!find_pair(gettop(), line[i])){ //if there is another closing bracket + printf("Crossed bracket %c in %d, expected %c \n", line[i], i, c_bracket[f]); return 0; } else{ - r_bracket[f] = 0; - f--; + c_bracket[f] = 0; //if closing bracket is correct then reducee array + f--; //if i didn't find a correct bracket, then keep it in the array pop(); } } } int begin = 0; - int end = strlen(r_bracket); + int end = strlen(c_bracket); int temp; + + //now i have all missing closing brackets in array + + //flip array while(begin < end){ - temp = r_bracket[begin]; - r_bracket[begin++] = r_bracket[--end]; - r_bracket[end] = temp; + temp = c_bracket[begin]; + c_bracket[begin++] = c_bracket[--end]; + c_bracket[end] = temp; } + //if program pop all opening brackets from the stack, then stack size must be -1; if(stack.size == -1){ printf("All brackets OK\n"); } + //if no, then program didn't find closing bracket else { - printf("Missing closing brackets: %s\n", r_bracket); + printf("Missing closing brackets: %s\n", c_bracket); } - free(l_bracket); - free(r_bracket); + free(o_bracket); + free(c_bracket); return 0; } From fe62adf6845fe3eacfad2c4e63a675c45248ead2 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sat, 24 Oct 2020 16:59:41 +0000 Subject: [PATCH 07/10] Update 'cv3/program.c' --- cv3/program.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 12e1807..73ca78b 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -10,6 +10,8 @@ struct stack { int size; }; +//push number to the stack + void push(struct stack* stack,float values){ if(stack->size < 10){ stack->values[stack->size] = values; @@ -22,6 +24,8 @@ void push(struct stack* stack,float values){ } } +//pop number from the stack + float pop(struct stack *stack){ if(stack->size > 0){ float value = stack->values[stack->size-1]; @@ -58,9 +62,10 @@ int main() { float r = 0; float z = 0; + //if string is not empty if(line[1] != 0 && x != NULL){ - + //if there is a wrong symbol if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){ printf("bad input\n"); return 0; @@ -70,8 +75,8 @@ int main() { r = pop(&mystack) + pop(&mystack); } else if(line[0] == '-'){ - z = pop(&mystack); - r = pop(&mystack) - z; + z = pop(&mystack); //swap numbers(e.g input "3 4 -" + r = pop(&mystack) - z; //output 3 - 4, not 4 - 3) } else if(line[0] == '*'){ r = pop(&mystack) * pop(&mystack); @@ -86,13 +91,14 @@ int main() { r = pop(&mystack) / z; } } - - + //if symbol is not operation, than it is number + //change string to float else { r = strtof(line,&x); } - + //push result to stack push(&mystack,r); + print_stack(&mystack); } else{ From 594d3cd491450ae71178a3b6a9b94747a121b673 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sat, 24 Oct 2020 17:06:11 +0000 Subject: [PATCH 08/10] Update 'cv2/program.c' --- cv2/program.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index e4ad9a0..fe26dbb 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -9,6 +9,9 @@ struct pizza{ float prize; }; +//read the name and prize +//push them to stack + int read_pizza(struct pizza* item){ char line1[LINESIZE]; @@ -29,12 +32,15 @@ int read_pizza(struct pizza* item){ return 1; } } + +//compare 2 elements + int compare(const void *a, const void *b){ struct pizza* pizza_a = (void*) a; struct pizza* pizza_b = (void*) b; float r = (pizza_a->prize > pizza_b->prize) - (pizza_a->prize < pizza_b->prize); - if (r == 0){ + if (r == 0){ //if prizes are identical, than check if names are in alphabetical order r = strcmp(pizza_a->name, pizza_b->name); } return r; @@ -48,12 +54,13 @@ int main(){ struct pizza item; int counter = 0; + //write menu to array while(stdin, read_pizza(&item)){ strcpy(jedalny_listok[counter].name, item.name); jedalny_listok[counter].prize = item.prize; counter++; } - + qsort(jedalny_listok, counter, sizeof(struct pizza), compare); int i = 0; From 7a3744754128376c34385d974efeef4fa64286f3 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Mon, 26 Oct 2020 11:21:05 +0000 Subject: [PATCH 09/10] Update 'cv1/program.c' --- cv1/program.c | 61 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/cv1/program.c b/cv1/program.c index 8076ce1..c8ce704 100644 --- a/cv1/program.c +++ b/cv1/program.c @@ -30,26 +30,31 @@ int read_pizza(struct pizza* item){ } int search_string(const char* heap, const char* needle){ + int H = strlen(heap); int N = strlen(needle); - int i; + int i = 0; for(i = 0; i <= H - N; i++){ int j; + for(j = 0; j < N; j++){ if(heap[i+j] != needle[j]){ - return -1; - } - if(j = N){ - return i; + break; } } - } + if(j == N){ + return i; + + } + } + return -1; } + char hacker_script(char c){ if(isupper(c)){ - c = tolower(c); - } + c = tolower(c); + } char numbers[] = "0123456789"; char letters[] = "oizeasbtbq"; int i; @@ -59,48 +64,56 @@ char hacker_script(char c){ return c; } } + return c; } + int main(){ printf("Zadaj hladanu surovinu:"); char key[LINESIZE]; memset(key,0,LINESIZE); - char* r = fgets(key,LINESIZE,stdin); - + char* r = fgets(key,LINESIZE,stdin); + key[strlen(key)-1] = '\0'; + int k = 0; + for(k = 0; k < strlen(key); k++){ + key[k] = hacker_script(key[k]); + } + printf("Zadaj jedalny listok:"); struct pizza jedalny_listok[100]; struct pizza pomocny[100]; memset(pomocny, 0, sizeof(struct pizza)*100); - memset(jedalny_listok, 0,sizeof(struct pizza)*100); + memset(jedalny_listok, 0,sizeof(struct pizza)*100); struct pizza item; int counter = 0; + - while(read_pizza(&item)){ + while(stdin,read_pizza(&item)){ strcpy(jedalny_listok[counter].name, item.name); jedalny_listok[counter].prize = item.prize; counter++; } + int i, j, f; + int i, j; for(i = 0; i < counter; i++){ for(j = 0; j < strlen(jedalny_listok[i].name); j++){ pomocny[i].name[j] = hacker_script(jedalny_listok[i].name[j]); - //printf("%c", pomocny[i].name[j]); - - } - search_string(pomocny[i].name, key); - if(search_string(pomocny[i].name, key) != -1){ + + } + int result = search_string(pomocny[i].name, key); + if(result != -1){ printf("%s", jedalny_listok[i].name); printf("%.2f\n", jedalny_listok[i].prize); - printf("Nacitano %d poloziek.\n", counter); - return 1; - } - + } + } - printf("Nacitano %d poloziek\n", counter); - return 1; - + printf("Nacitanych %d poloziek.\n", counter); + return 0; + + } From e9625e2598b1d81a9040332b5ae9e1731951c771 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Mon, 26 Oct 2020 13:56:05 +0000 Subject: [PATCH 10/10] Delete 'program.c' --- program.c | 104 ------------------------------------------------------ 1 file changed, 104 deletions(-) delete mode 100644 program.c diff --git a/program.c b/program.c deleted file mode 100644 index a65ec21..0000000 --- a/program.c +++ /dev/null @@ -1,104 +0,0 @@ -#include -#include -#include -#include - -#define LINESIZE 100 -struct pizza{ - char name[LINESIZE]; - float prize; -}; - -int read_pizza(struct pizza* item){ - - char line1[LINESIZE]; - char line2[LINESIZE]; - - memset(line1, 0, LINESIZE); - memset(line2, 0, LINESIZE); - char *l1 = fgets(line1,LINESIZE,stdin); - char *l2 = fgets(line2,LINESIZE,stdin); - float value = strtof(line2, &l2); - if(value == 0.0F){ - return 0; - } - if(l1 != NULL && line1[1] != 0){ - item->prize = value; - strcpy(item->name, line1); - return 1; - } -} - -int search_string(const char* heap, const char* needle){ - int H = strlen(heap); - int N = strlen(needle); - int i; - for(i = 0; i <= H - N; i++){ - int j; - for(j = 0; j < N; j++){ - if(heap[i+j] != needle[j]){ - return -1; - } - if(j = N){ - return i; - } - } - } -} - -char hacker_script(char c){ - if(isupper(c)){ - c = tolower(c); - } - char numbers[] = "0123456789"; - char letters[] = "oizeasbtbq"; - int i; - for (i = 0; i < 10; i++){ - if (c == numbers[i]){ - c = letters[i]; - return c; - } - } -} - -int main(){ - - char key[LINESIZE]; - memset(key,0,LINESIZE); - char* r = fgets(key,LINESIZE,stdin); - - struct pizza jedalny_listok[100]; - struct pizza pomocny[100]; - memset(pomocny, 0, sizeof(struct pizza)*100); - memset(jedalny_listok, 0,sizeof(struct pizza)*100); - struct pizza item; - int counter = 0; - - - while(read_pizza(&item)){ - strcpy(jedalny_listok[counter].name, item.name); - jedalny_listok[counter].prize = item.prize; - counter++; - } - int i, j, f; - for(i = 0; i < counter; i++){ - for(j = 0; j < strlen(jedalny_listok[i].name); j++){ - pomocny[i].name[j] = hacker_script(jedalny_listok[i].name[j]); - //printf("%c", pomocny[i].name[j]); - - } - search_string(pomocny[i].name, key); - if(search_string(pomocny[i].name, key) != -1){ - printf("%s", jedalny_listok[i].name); - printf("%.2f\n", jedalny_listok[i].prize); - printf("Nacitano %d poloziek\n", counter); - return 1; - } - - } - printf("Nacitano %d poloziek\n", counter); - return 1; - -} - -