pvjc24/a1/program.c

175 lines
4.2 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:49:38 +00:00
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);
2024-03-23 19:12:52 +00:00
double number1, number2, result3;
2024-03-23 17:11:50 +00:00
char operant;
2024-03-23 18:49:38 +00:00
int i = 0;
int counter=0;
2024-03-23 17:11:50 +00:00
while(pointer[i]!='\0'){
2024-03-23 18:56:38 +00:00
if(pointer[i]=='+'||pointer[i]=='/'||pointer[i]=='*'){
2024-03-23 17:11:50 +00:00
operant=pointer[i];
counter++;
if(counter>1){
return -1;
}
}
i++;
}
2024-03-23 18:49:38 +00:00
i = 0;
// Look for the start of the first number.
i = skip_spaces(i, pointer);
2024-03-23 17:11:50 +00:00
2024-03-23 18:49:38 +00:00
// Copy first number to its destination.
i = copy_number(first_number, pointer, i);
2024-03-23 17:11:50 +00:00
2024-03-23 18:49:38 +00:00
// Look for the start of the operation;
i = skip_spaces(i, pointer);
// Processing operation.
operant = pointer[i];
i++;
if (operant != '+' && operant != '-' && operant != '*' && operant != '/') {
2024-03-23 17:11:50 +00:00
return -1;
}
2024-03-23 18:49:38 +00:00
// Look for the start of the second number;
i = skip_spaces(i, pointer);
2024-03-23 17:11:50 +00:00
2024-03-23 18:49:38 +00:00
// 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] != '=') {
2024-03-23 17:11:50 +00:00
return -1;
}
2024-03-23 18:49:38 +00:00
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);
2024-03-23 17:11:50 +00:00
2024-03-23 18:49:38 +00:00
// Look for the start of the "\n";
i = skip_spaces(i, pointer);
if ('\n' != pointer[i]) {
2024-03-23 17:11:50 +00:00
return -1;
}
2024-03-23 18:49:38 +00:00
number1=atof(first_number);
number2=atof(second_number);
result3=atof(result_nubmer);
2024-03-23 17:11:50 +00:00
2024-03-23 19:15:04 +00:00
2024-03-23 19:18:17 +00:00
if(operant=='+' &&result3==round((number1+number2) * 100) / 100){
2024-03-23 17:11:50 +00:00
return 1;
}
2024-03-23 19:18:17 +00:00
if(operant=='-'&&(result3==round((number1-number2) * 100) / 100)){
2024-03-23 17:11:50 +00:00
return 1;
}
2024-03-23 19:12:52 +00:00
if(operant=='*'&&(result3==round((number1*number2) * 100) / 100)){
2024-03-23 17:11:50 +00:00
return 1;
}
2024-03-23 19:12:52 +00:00
if(operant=='/'&&(result3==round((number1/number2) * 100) / 100)){
2024-03-23 17:11:50 +00:00
return 1;
}
return 0;
}
2024-03-23 18:49:38 +00:00
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'){
2024-03-23 19:22:13 +00:00
if (!isdigit(pointer[i])&&pointer[i]!='.'&&pointer[i]!='-') {
2024-03-23 18:56:38 +00:00
break;
}
if (pointer[i] == '-' && destination_i > 0) {
2024-03-23 18:49:38 +00:00
break;
}
number[destination_i] = pointer[i];
destination_i++;
i++;
}
return i;
}
2024-03-23 17:11:50 +00:00