2024-03-28 23:51:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
struct uloha{
|
|
|
|
|
|
|
|
float result;
|
|
|
|
char Output[25];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char* ClearString(const char* str) {
|
|
|
|
const char* separator = " "; // Declare separator as const pointer
|
|
|
|
char* novy_string = malloc(strlen(str) + 1); // Allocate memory for new string
|
|
|
|
if (novy_string == NULL) {
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
novy_string[0] = '\0'; // Initialize the new string
|
|
|
|
|
|
|
|
char* token;
|
|
|
|
char* kopie_str = strdup(str); // Make a copy of the original string
|
|
|
|
if (kopie_str == NULL) {
|
|
|
|
free(novy_string); // Free memory allocated for new string
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the first token
|
|
|
|
token = strtok(kopie_str, separator);
|
|
|
|
|
|
|
|
// Iterate through the remaining tokens
|
|
|
|
while (token != NULL) {
|
|
|
|
strcat(novy_string, token); // Append token to the new string
|
|
|
|
token = strtok(NULL, separator); // Get the next token
|
|
|
|
}
|
|
|
|
|
|
|
|
free(kopie_str); // Free the copied string as it's no longer needed
|
|
|
|
|
|
|
|
return novy_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
char pole[100];
|
|
|
|
memset(pole,0,100);
|
|
|
|
|
|
|
|
char CistePole[100];
|
|
|
|
|
|
|
|
float cislo1;
|
|
|
|
float cislo2;
|
|
|
|
char znamienko;
|
|
|
|
|
|
|
|
char* token;
|
|
|
|
int priklad = 0;
|
|
|
|
|
|
|
|
int chyba = 0;
|
|
|
|
|
|
|
|
struct uloha excel[100];
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
char* a = fgets(pole, 100, stdin);
|
|
|
|
|
|
|
|
if (pole[0] == '\n' || a == NULL ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//CHECK
|
|
|
|
for(int i = 0; i<strlen(pole); i++){
|
|
|
|
|
|
|
|
if(isdigit(pole[i]==0)){
|
|
|
|
|
|
|
|
if(pole[i] == '.'){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strcpy(excel[priklad].Output, "CHYBA");
|
|
|
|
chyba = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chyba == 0){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* ptrc = ClearString(pole);
|
|
|
|
strcpy(CistePole, ptrc);
|
|
|
|
char znamienka[] ="+-*/";
|
|
|
|
char* endptr;
|
|
|
|
|
|
|
|
cislo1 = strtof(CistePole, &endptr);
|
|
|
|
|
|
|
|
znamienko = *endptr;
|
|
|
|
|
|
|
|
cislo2 = strtof(endptr + 1, &endptr);
|
|
|
|
|
|
|
|
excel[priklad].result = strtof(endptr + 1, NULL);
|
|
|
|
//printf("STRUCT RESULT: %f\n",excel[priklad].result);
|
|
|
|
|
|
|
|
//CHECK ZNAMIENKA
|
|
|
|
for(int i = 0; i<4;i++){
|
|
|
|
|
|
|
|
if(strchr(znamienka,znamienko)==NULL){
|
|
|
|
chyba=2;
|
|
|
|
strcpy(excel[priklad].Output, "CHYBA");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chyba!=2){
|
|
|
|
|
|
|
|
|
|
|
|
float realny = 0;
|
|
|
|
|
|
|
|
if (znamienko == '+') {
|
|
|
|
realny = cislo1 + cislo2;
|
|
|
|
} else if (znamienko == '-') {
|
|
|
|
realny = cislo1 - cislo2;
|
|
|
|
} else if (znamienko == '*') {
|
|
|
|
realny = cislo1 * cislo2;
|
|
|
|
} else if (znamienko == '/') {
|
|
|
|
realny = cislo1 / cislo2;
|
|
|
|
} else {
|
|
|
|
strcpy(excel[priklad].Output, "CHYBA");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
realny = round(realny * 100) / 100;
|
|
|
|
|
|
|
|
if (realny == excel[priklad].result){
|
|
|
|
strcpy(excel[priklad].Output, "OK"); //kopiruje vysledok do pola vysledky
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
strcpy(excel[priklad].Output, "ZLE");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
priklad++;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
//vypis vysledkov
|
|
|
|
for (int i = 0; i < priklad; i++) {
|
|
|
|
printf("%s\n",excel[i].Output);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2024-03-28 18:44:42 +00:00
|
|
|
}
|