This commit is contained in:
Maryna Kravtsova 2020-10-26 14:58:37 +01:00
parent 82a772622a
commit 73d6bd42c2
4 changed files with 161 additions and 109 deletions

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <ctype.h> #include <ctype.h>
@ -10,6 +9,8 @@ struct stack{
int size; int size;
}stack; }stack;
///add bracket to stack
void push(char ch){ void push(char ch){
if(stack.size < 100){ if(stack.size < 100){
stack.size++; stack.size++;
@ -19,6 +20,8 @@ void push(char ch){
exit(0); exit(0);
} }
///if i have opening bracket in stack, then decrement size
void pop(){ void pop(){
if(stack.size >= 0) if(stack.size >= 0)
stack.size--; stack.size--;
@ -26,6 +29,8 @@ void pop(){
exit(0); exit(0);
} }
///compare brackets
bool find_pair(char a, char b){ bool find_pair(char a, char b){
if(a == '(' && b == ')') if(a == '(' && b == ')')
return true; return true;
@ -37,8 +42,10 @@ bool find_pair(char a, char b){
return true; return true;
return false; return false;
} }
///function to find right pair
char right_pair(char a){ char right_pair(char a){
char b; char b;
if(a == '(') if(a == '(')
@ -53,6 +60,8 @@ char right_pair(char a){
return b; return b;
} }
///find the last opening bracket in the stack
char gettop(){ char gettop(){
return stack.data[stack.size]; return stack.data[stack.size];
} }
@ -69,52 +78,60 @@ int main(){
int l = strlen(line); int l = strlen(line);
int i, j = -1, f = -1;; int i, j = -1, f = -1;;
char *l_bracket = calloc(l, sizeof(char)); char *o_bracket = calloc(l, sizeof(char));
char *r_bracket = calloc(l, sizeof(char)); char *c_bracket = calloc(l, sizeof(char));
for(i = 0; i < l; i++){ for(i = 0; i < l; i++){
if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){ if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){
push(line[i]); push(line[i]);
j++; j++;
f++; f++;
l_bracket[j] = gettop(); o_bracket[j] = gettop(); //get opening bracket from the stack
r_bracket[f] = right_pair(l_bracket[j]); c_bracket[f] = right_pair(o_bracket[j]); //get closing bracket
} }
if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){ if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){
if(stack.size == -1){
if(stack.size == -1){ //if there is no opening bracket in the stack
printf("Unexpected closing bracket %c in %d\n", line[i], i); printf("Unexpected closing bracket %c in %d\n", line[i], i);
return 0; return 0;
} }
else if(!find_pair(gettop(), line[i])){ 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, r_bracket[j]); printf("Crossed bracket %c in %d, expected %c \n", line[i], i, c_bracket[f]);
return 0; return 0;
} }
else{ else{
r_bracket[f] = 0; c_bracket[f] = 0; //if closing bracket is correct then reducee array
f--; f--; //if i didn't find a correct bracket, then keep it in the array
pop(); pop();
} }
} }
} }
int begin = 0; int begin = 0;
int end = strlen(r_bracket); int end = strlen(c_bracket);
int temp; int temp;
//printf("%s", b);
//now i have all missing closing brackets in array
//flip array
while(begin < end){ while(begin < end){
temp = r_bracket[begin]; temp = c_bracket[begin];
r_bracket[begin++] = r_bracket[--end]; c_bracket[begin++] = c_bracket[--end];
r_bracket[end] = temp; c_bracket[end] = temp;
} }
//if program pop all opening brackets from the stack, then stack size must be -1;
if(stack.size == -1){ if(stack.size == -1){
printf("All brackets OK\n"); printf("All brackets OK\n");
} }
//if no, then program didn't find closing bracket
else { 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; return 0;
} }

View File

@ -30,22 +30,27 @@ int read_pizza(struct pizza* item){
} }
int search_string(const char* heap, const char* needle){ int search_string(const char* heap, const char* needle){
int H = strlen(heap); int H = strlen(heap);
int N = strlen(needle); int N = strlen(needle);
int i; int i = 0;
for(i = 0; i <= H - N; i++){ for(i = 0; i <= H - N; i++){
int j; int j;
for(j = 0; j < N; j++){ for(j = 0; j < N; j++){
if(heap[i+j] != needle[j]){ if(heap[i+j] != needle[j]){
return -1; break;
} }
if(j = N){ }
if(j == N){
return i; return i;
} }
} }
} return -1;
} }
char hacker_script(char c){ char hacker_script(char c){
if(isupper(c)){ if(isupper(c)){
c = tolower(c); c = tolower(c);
@ -59,14 +64,21 @@ char hacker_script(char c){
return c; return c;
} }
} }
return c;
} }
int main(){ int main(){
printf("Zadaj hladanu surovinu:"); printf("Zadaj hladanu surovinu:");
char key[LINESIZE]; char key[LINESIZE];
memset(key,0,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:"); printf("Zadaj jedalny listok:");
struct pizza jedalny_listok[100]; struct pizza jedalny_listok[100];
@ -77,30 +89,29 @@ int main(){
int counter = 0; int counter = 0;
while(read_pizza(&item)){
while(stdin,read_pizza(&item)){
strcpy(jedalny_listok[counter].name, item.name); strcpy(jedalny_listok[counter].name, item.name);
jedalny_listok[counter].prize = item.prize; jedalny_listok[counter].prize = item.prize;
counter++; counter++;
} }
int i, j, f; int i, j, f;
int i, j;
for(i = 0; i < counter; i++){ for(i = 0; i < counter; i++){
for(j = 0; j < strlen(jedalny_listok[i].name); j++){ for(j = 0; j < strlen(jedalny_listok[i].name); j++){
pomocny[i].name[j] = hacker_script(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); int result = search_string(pomocny[i].name, key);
if(search_string(pomocny[i].name, key) != -1){ if(result != -1){
printf("%s", jedalny_listok[i].name); printf("%s", jedalny_listok[i].name);
printf("%.2f\n", jedalny_listok[i].prize); printf("%.2f\n", jedalny_listok[i].prize);
printf("Nacitano %d poloziek.\n", counter);
return 1;
} }
} }
printf("Nacitano %d poloziek\n", counter); printf("Nacitanych %d poloziek.\n", counter);
return 1; return 0;
} }

View File

@ -2,7 +2,6 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#define LINESIZE 100 #define LINESIZE 100
struct pizza{ struct pizza{
@ -10,6 +9,9 @@ struct pizza{
float prize; float prize;
}; };
//read the name and prize
//push them to stack
int read_pizza(struct pizza* item){ int read_pizza(struct pizza* item){
char line1[LINESIZE]; char line1[LINESIZE];
@ -30,20 +32,17 @@ int read_pizza(struct pizza* item){
return 1; return 1;
} }
} }
int comparename(const void *a, const void *b){
//compare 2 elements
int compare(const void *a, const void *b){
struct pizza* pizza_a = (void*) a; struct pizza* pizza_a = (void*) a;
struct pizza* pizza_b = (void*) b; struct pizza* pizza_b = (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); float r = (pizza_a->prize > pizza_b->prize) - (pizza_a->prize < pizza_b->prize);
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; return r;
} }
@ -55,15 +54,14 @@ int main(){
struct pizza item; struct pizza item;
int counter = 0; int counter = 0;
//write menu to array
while(stdin, read_pizza(&item)){ while(stdin, read_pizza(&item)){
strcpy(jedalny_listok[counter].name, item.name); strcpy(jedalny_listok[counter].name, item.name);
jedalny_listok[counter].prize = item.prize; jedalny_listok[counter].prize = item.prize;
counter++; counter++;
} }
qsort(jedalny_listok, counter, sizeof(struct pizza), compareprize); qsort(jedalny_listok, counter, sizeof(struct pizza), compare);
//qsort(jedalny_listok, counter, sizeof(struct pizza), comparename);
int i = 0; int i = 0;
for(i = 0; i < counter; i++){ for(i = 0; i < counter; i++){
@ -71,7 +69,5 @@ int main(){
printf("%f", jedalny_listok[i].prize); printf("%f", jedalny_listok[i].prize);
printf("\n"); printf("\n");
} }
return 0; return 0;
} }

View File

@ -1,27 +1,43 @@
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define STACK_SIZE 100 #define STACK_SIZE 10
struct stack { struct stack {
float values[STACK_SIZE]; float values[STACK_SIZE];
int size; int size;
}; };
//push number to the stack
void push(struct stack* stack,float values){ void push(struct stack* stack,float values){
assert(stack->size < STACK_SIZE); if(stack->size < 10){
stack->values[stack->size] = values; stack->values[stack->size] = values;
stack->size += 1; stack->size += 1;
}
else{
printf("full stack\n");
exit(0);
}
} }
//pop number from the stack
float pop(struct stack *stack){ float pop(struct stack *stack){
assert(stack->size > 0); if(stack->size > 0){
float values = stack->values[stack->size-1]; float value = stack->values[stack->size-1];
stack->size -= 1; stack->size -= 1;
return values; return value;
}
else{
printf("not enough operands\n");
exit(0);
}
} }
void print_stack(struct stack *stack){ void print_stack(struct stack *stack){
int i; int i;
int len = stack->size - 1; int len = stack->size - 1;
@ -30,9 +46,8 @@ void print_stack(struct stack *stack){
} }
if(stack->size != 0){ if(stack->size != 0){
printf("%0.2f ", stack->values[i]); printf("%0.2f ", stack->values[i]);
}
printf("\n"); printf("\n");
}
} }
@ -40,14 +55,19 @@ int main() {
struct stack mystack; struct stack mystack;
memset(&mystack, 0, sizeof(struct stack)); memset(&mystack, 0, sizeof(struct stack));
int i = 0; int i = 0;
float z; for(i = 0; i < 100; i++){
for(i = 0; i < 10000; i++){
char line[10]; char line[10];
char *x = fgets(line, 10, stdin); char *x = fgets(line, 10, stdin);
float r = 0; 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] != '-'){ if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){
printf("no input"); printf("bad input\n");
return 0; return 0;
} }
@ -55,29 +75,37 @@ int main() {
r = pop(&mystack) + pop(&mystack); r = pop(&mystack) + pop(&mystack);
} }
else if(line[0] == '-'){ else if(line[0] == '-'){
z = pop(&mystack); z = pop(&mystack); //swap numbers(e.g input "3 4 -"
r = pop(&mystack) - z; r = pop(&mystack) - z; //output 3 - 4, not 4 - 3)
} }
else if(line[0] == '*'){ else if(line[0] == '*'){
r = pop(&mystack) * pop(&mystack); r = pop(&mystack) * pop(&mystack);
} }
else if(line[0] == '/'){ else if(line[0] == '/'){
z = pop(&mystack); z = pop(&mystack);
if(z != 0.0){ if(z == 0){
printf("division by zero\n");
return 0;
}
else {
r = pop(&mystack) / z; r = pop(&mystack) / z;
} }
} }
//if symbol is not operation, than it is number
//change string to float
else { else {
r = strtof(line,&x); r = strtof(line,&x);
} }
//push result to stack
push(&mystack,r); push(&mystack,r);
print_stack(&mystack); print_stack(&mystack);
} }
else{
printf("no input\n");
return 0;
}
}
return 0; return 0;
} }