From 27d34610aea39dd22e2f93bc6de854dec526a8b6 Mon Sep 17 00:00:00 2001 From: kr820js Date: Sat, 23 Mar 2024 19:49:38 +0100 Subject: [PATCH] submition8 --- a1/program.c | 106 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 34 deletions(-) diff --git a/a1/program.c b/a1/program.c index 3d1271e..e59b2d7 100644 --- a/a1/program.c +++ b/a1/program.c @@ -62,17 +62,19 @@ int reading_strings_and_storing_them_in_memory(link* pointer_to_start_of_chain){ return counter; } int analizing_string(char* pointer){ - char token1[SIZE]; - char token2[SIZE]; - char token3[SIZE]; - - float number1, number2, result3; - char buffer_for_token[SIZE]; - strcpy(buffer_for_token,pointer); - int counter=0; - char operant; - int i=0; + char first_number[SIZE]; + char second_number[SIZE]; + char result_nubmer[SIZE]; + memset(first_number, 0, SIZE); + memset(second_number, 0, SIZE); + memset(result_nubmer, 0, SIZE); + + float number1, number2, result3; + char operant; + + int i = 0; + int counter=0; while(pointer[i]!='\0'){ if(pointer[i]=='+'||pointer[i]=='/'||pointer[i]=='*'||pointer[i]=='-'){ operant=pointer[i]; @@ -84,36 +86,51 @@ int analizing_string(char* pointer){ i++; } - char delim[] =" +=-*/\n"; + i = 0; + // Look for the start of the first number. + i = skip_spaces(i, pointer); - char *first_number_cucumber=strtok(buffer_for_token, delim); - if(first_number_cucumber==NULL) {//||is_number_question_mark(first_number_cucumber)==0){ - return -1; - } - strcpy(token1, first_number_cucumber); + // Copy first number to its destination. + i = copy_number(first_number, pointer, i); - char *second_number=strtok(NULL, delim); - if(second_number==NULL) { //||is_number_question_mark(second_number)==0){ - return -1; - } - strcpy(token2, second_number); - - - char *result_token=strtok(NULL, delim); - if(result_token==NULL) { //||is_number_question_mark(result)==0){ - return -1; - } - strcpy(token3, result_token); - - char *token = strtok(NULL, delim); - if(token!=NULL){ + // Look for the start of the operation; + i = skip_spaces(i, pointer); + // Processing operation. + operant = pointer[i]; + i++; + if (operant != '+' && operant != '-' && operant != '*' && operant != '/') { return -1; } + // Look for the start of the second number; + i = skip_spaces(i, pointer); - number1=atof(token1); - number2=atof(token2); - result3=atof(token3); + // Copy second number to its destination. + i = copy_number(second_number, pointer, i); + + // Look for the start of the "="; + i = skip_spaces(i, pointer); + // Processing "=" + if (pointer[i] != '=') { + return -1; + } + i++; + + // Look for the start of the result number; + i = skip_spaces(i, pointer); + + // Copy result number to its destination. + i = copy_number(result_nubmer, pointer, i); + + // Look for the start of the "\n"; + i = skip_spaces(i, pointer); + if ('\n' != pointer[i]) { + return -1; + } + + number1=atof(first_number); + number2=atof(second_number); + result3=atof(result_nubmer); if(operant=='+' &&result3==number1+number2){ return 1; @@ -130,6 +147,27 @@ int analizing_string(char* pointer){ return 0; } +int skip_spaces(int i, char *pointer) { + while(pointer[i]==' '){ + i++; + } + + return i; +} + +int copy_number(char number[], char *pointer, int i) { + int destination_i = 0; + while(pointer[i]!='\0'){ + if (!isdigit(pointer[i])&&pointer[i]!='.'&&pointer[i]!=',') { + break; + } + number[destination_i] = pointer[i]; + destination_i++; + i++; + } + + return i; +} /*int is_number_question_mark(char *pointer){ for(int i=0; pointer[i]!='\0'; i++){