pvjc24/a1/program.c

143 lines
3.4 KiB
C
Raw Normal View History

2024-03-23 17:11:50 +00:00
#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));
2024-03-23 17:45:12 +00:00
int read_lines = reading_strings_and_storing_them_in_memory(pointer_to_start_of_chain);
if(read_lines==0){
return 0;
}
2024-03-23 17:11:50 +00:00
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");
}
2024-03-23 17:36:27 +00:00
free(next_link->p_na_line);
link* ptr=next_link;
2024-03-23 17:11:50 +00:00
next_link=next_link->p_na_next_struct;
2024-03-23 17:36:27 +00:00
free(ptr);
2024-03-23 17:11:50 +00:00
}
return 0;
}
2024-03-23 17:45:12 +00:00
int reading_strings_and_storing_them_in_memory(link* pointer_to_start_of_chain){
2024-03-23 17:11:50 +00:00
link* next_link = pointer_to_start_of_chain;
char buffer[SIZE];
link* ancestor=NULL;
2024-03-23 17:45:12 +00:00
int counter=0;
2024-03-23 17:11:50 +00:00
while(1){
2024-03-23 17:21:21 +00:00
if(fgets(buffer, SIZE, stdin)==NULL||buffer[0]=='\n'){
2024-03-23 17:45:12 +00:00
free(next_link);
2024-03-23 17:11:50 +00:00
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;
2024-03-23 17:45:12 +00:00
counter++;
2024-03-23 17:11:50 +00:00
}
2024-03-23 17:45:12 +00:00
return counter;
2024-03-23 17:11:50 +00:00
}
int analizing_string(char* pointer){
2024-03-23 18:10:08 +00:00
char token1[SIZE];
char token2[SIZE];
char token3[SIZE];
2024-03-23 17:11:50 +00:00
float number1, number2, result3;
2024-03-23 17:55:17 +00:00
char buffer_for_token[SIZE];
strcpy(buffer_for_token,pointer);
2024-03-23 17:11:50 +00:00
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";
2024-03-23 17:55:17 +00:00
char *first_number_cucumber=strtok(buffer_for_token, delim);
2024-03-23 17:59:31 +00:00
if(first_number_cucumber==NULL) {//||is_number_question_mark(first_number_cucumber)==0){
2024-03-23 17:11:50 +00:00
return -1;
}
2024-03-23 18:10:08 +00:00
strcpy(token1, first_number_cucumber);
2024-03-23 17:11:50 +00:00
char *second_number=strtok(NULL, delim);
2024-03-23 17:59:31 +00:00
if(second_number==NULL) { //||is_number_question_mark(second_number)==0){
2024-03-23 17:11:50 +00:00
return -1;
}
2024-03-23 18:10:08 +00:00
strcpy(token2, second_number);
2024-03-23 17:11:50 +00:00
2024-03-23 18:10:08 +00:00
char *result_token=strtok(NULL, delim);
if(result_token==NULL) { //||is_number_question_mark(result)==0){
2024-03-23 17:11:50 +00:00
return -1;
}
2024-03-23 18:10:08 +00:00
strcpy(token3, result_token);
2024-03-23 17:11:50 +00:00
char *token = strtok(NULL, delim);
if(token!=NULL){
return -1;
}
2024-03-23 18:10:08 +00:00
number1=atof(token1);
number2=atof(token2);
result3=atof(token3);
2024-03-23 17:11:50 +00:00
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;
}
2024-03-23 17:59:31 +00:00
/*int is_number_question_mark(char *pointer){
2024-03-23 17:11:50 +00:00
for(int i=0; pointer[i]!='\0'; i++){
if (!isdigit(pointer[i])&&pointer[i]!='.'&&pointer[i]!=',') {
return 0;
}
}
return 1;
2024-03-23 17:59:31 +00:00
}*/
2024-03-23 18:10:08 +00:00