149 lines
2.9 KiB
C
149 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#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;
|
|
}
|
|
|
|
void 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||buffer[0]=='\n'){
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|