#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)); int read_lines = reading_strings_and_storing_them_in_memory(pointer_to_start_of_chain); if(read_lines==0){ return 0; } 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"); } free(next_link->p_na_line); link* ptr=next_link; next_link=next_link->p_na_next_struct; free(ptr); } 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; int counter=0; while(1){ if(fgets(buffer, SIZE, stdin)==NULL||buffer[0]=='\n'){ free(next_link); if(ancestor!=NULL){ 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; counter++; } return counter; } int analizing_string(char* pointer){ 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); double number1, number2, result3; char operant; int i = 0; int counter=0; while(pointer[i]!='\0'){ if(pointer[i]=='+'||pointer[i]=='/'||pointer[i]=='*'){ operant=pointer[i]; counter++; if(counter>1){ return -1; } } i++; } i = 0; // Look for the start of the first number. i = skip_spaces(i, pointer); // Copy first number to its destination. i = copy_number(first_number, pointer, i); // 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); // 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); number1 = round(number1 *100) / 100; number2 = round(number2 *100) / 100; result3 = round(result3 *100) / 100; if(operant=='+' &&result3==number1+number2){ return 1; } if(operant=='-'&&(result3==number1-number2)){ return 1; } if(operant=='*'&&(result3==round((number1*number2) * 100) / 100)){ return 1; } if(operant=='/'&&(result3==round((number1/number2) * 100) / 100)){ return 1; } 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]!=','&&pointer[i]!='-') { break; } if (pointer[i] == '-' && destination_i > 0) { break; } number[destination_i] = pointer[i]; destination_i++; i++; } return i; }