From 58039d83f54c840fba7c1afa662cf4166b33554f Mon Sep 17 00:00:00 2001 From: kr820js Date: Sat, 23 Mar 2024 18:11:50 +0100 Subject: [PATCH] submition1 --- a1/program.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 a1/program.c diff --git a/a1/program.c b/a1/program.c new file mode 100644 index 0000000..cf55ba9 --- /dev/null +++ b/a1/program.c @@ -0,0 +1,148 @@ +#include +#include + +#define SIZE 1000 + +typedef struct{ + char* p_na_line; + struct link* p_na_next_struct; +}link; + +int main() +{ + link* pointer_to_start_of_chain = malloc(sizeof(link)); + memset(pointer_to_start_of_chain,0,sizeof(link)); + reading_strings_and_storing_them_in_memory(pointer_to_start_of_chain); + int result=0; + link* next_link = pointer_to_start_of_chain; + while(next_link != NULL){ + result=analizing_string(next_link->p_na_line); + if(result==0){ + printf("ZLE\n"); + }else if(result==1){ + printf("OK\n"); + }else{ + printf("CHYBA\n"); + } + next_link=next_link->p_na_next_struct; + } + return 0; +} + +int reading_strings_and_storing_them_in_memory(link* pointer_to_start_of_chain){ + link* next_link = pointer_to_start_of_chain; + char buffer[SIZE]; + link* ancestor=NULL; + while(1){ + if(fgets(buffer, SIZE, stdin)==NULL){ + if(ancestor!=NULL){ + free(ancestor->p_na_next_struct); + ancestor->p_na_next_struct=NULL; + } + break; + } + + int size=strlen(buffer)+1; + + next_link->p_na_line=malloc(size); + next_link->p_na_next_struct=malloc(sizeof(link)); + + memcpy(next_link->p_na_line, buffer,size); + ancestor=next_link; + next_link=next_link->p_na_next_struct; + } +} +int analizing_string(char* pointer){ + float number1, number2, result3; + + + int counter=0; + char operant; + int i=0; + + while(pointer[i]!='\0'){ + if(pointer[i]=='+'||pointer[i]=='/'||pointer[i]=='*'||pointer[i]=='-'){ + operant=pointer[i]; + counter++; + if(counter>1){ + return -1; + } + } + i++; + } + + char delim[] =" +=-*/\n"; + + char *first_number_cucumber=strtok(pointer, delim); + + if(first_number_cucumber==NULL||is_number_question_mark(first_number_cucumber)==0){ + + return -1; + } + + char *second_number=strtok(NULL, delim); + + if(second_number==NULL||is_number_question_mark(second_number)==0){ + return -1; + } + + + char *result=strtok(NULL, delim); + if(result==NULL||is_number_question_mark(result)==0){ + return -1; + } + + char *token = strtok(NULL, delim); + if(token!=NULL){ + return -1; + } + + + number1=atof(first_number_cucumber); + number2=atof(second_number); + result3=atof(result); + + if(operant=='+' &&result3==number1+number2){ + return 1; + } + if(operant=='-'&&(result3==number1-number2)){ + return 1; + } + if(operant=='*'&&(result3==number1*number2)){ + return 1; + } + if(operant=='/'&&(result3==number1/number2)){ + return 1; + } + return 0; +} + + +int is_number_question_mark(char *pointer){ + for(int i=0; pointer[i]!='\0'; i++){ + if (!isdigit(pointer[i])&&pointer[i]!='.'&&pointer[i]!=',') { + return 0; + } + } + return 1; +} + + + + + + + + + + + + + + + + + + + +