diff --git a/du4/program.c b/du4/program.c index f0aeabc..dc7790e 100644 --- a/du4/program.c +++ b/du4/program.c @@ -1,5 +1,113 @@ #include +#include +#include +#include +#include +#include -int main(){ +int main() { + char* input = (char*) calloc(100, sizeof(char)); + char symbol; + int counter; + int operation; + char* number1 = (char*) calloc(25, sizeof(char)); + char* number2 = (char*) calloc(25, sizeof(char)); + char* result = (char*) calloc(30, sizeof(char)); + char* ptr; + bool equals = false; -} + for(int i = 0; fgets(input, 100, stdin) != NULL; i++){ + if(!input || !strcmp(input, "") || input[0] == EOF || input[0] == '\n' || input[0] == '\0') + break; + counter = 0; + operation = 0; + equals = false; + memset(number1, '\0', 25); + memset(number2, '\0', 25); + memset(result, '\0', 30); + while(input[counter] != '\0' && input[counter] != '\n'){ + symbol = input[counter++]; + while (isspace(symbol)) + symbol = input[counter++]; + if(symbol == '\0' || symbol == '\n') + break; + switch (symbol) { + case '+': + operation = 1; + break; + case '-': + if(operation == 0) + operation = 2; + else{ + result[strlen(result)] = symbol; + result[strlen(result)] = '\0'; + } + break; + case '*': + operation = 3; + break; + case '/': + operation = 4; + break; + case '=': + equals = true; + break; + default: + if (isdigit(symbol) || symbol == '.'){ + if (operation == 0) { + number1[strlen(number1)] = symbol; + number1[strlen(number1)] = '\0'; + } + else if(!equals){ + number2[strlen(number2)] = symbol; + number2[strlen(number2)] = '\0'; + } + else{ + result[strlen(result)] = symbol; + result[strlen(result)] = '\0'; + } + } + else{ + printf("CHYBA\n"); + goto LABEL; + } + break; + } + } + + switch(operation){ + case 1: + if((strtof(number1, &ptr) != roundf(strtof(number1, &ptr))) || (strtof(number2, &ptr) != roundf(strtof(number2, &ptr))) || (strtof(result, &ptr) != roundf(strtof(result, &ptr)))) + printf("%s\n", (roundf((strtof(number1, &ptr) + strtof(number2, &ptr))*100)/100 > strtof(result, &ptr) - 0.001 && roundf((strtof(number1, &ptr) + strtof(number2, &ptr))*100)/100 < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + else + printf("%s\n", (strtof(number1, &ptr) + strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) + strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + break; + case 2: + if((strtof(number1, &ptr) != roundf(strtof(number1, &ptr))) || (strtof(number2, &ptr) != roundf(strtof(number2, &ptr))) || (strtof(result, &ptr) != roundf(strtof(result, &ptr)))) + printf("%s\n", (roundf((strtof(number1, &ptr) - strtof(number2, &ptr))*100)/100 > strtof(result, &ptr) - 0.001 && roundf((strtof(number1, &ptr) - strtof(number2, &ptr))*100)/100 < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + else + printf("%s\n", (strtof(number1, &ptr) - strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) - strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + break; + case 3: + if((strtof(number1, &ptr) != roundf(strtof(number1, &ptr))) || (strtof(number2, &ptr) != roundf(strtof(number2, &ptr))) || (strtof(result, &ptr) != roundf(strtof(result, &ptr)))) + printf("%s\n", (roundf((strtof(number1, &ptr) * strtof(number2, &ptr))*100)/100 > strtof(result, &ptr) - 0.001 && roundf((strtof(number1, &ptr) * strtof(number2, &ptr))*100)/100 < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + else + printf("%s\n", (strtof(number1, &ptr) * strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) * strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + break; + case 4: + if((strtof(number1, &ptr) != roundf(strtof(number1, &ptr))) || (strtof(number2, &ptr) != roundf(strtof(number2, &ptr))) || (strtof(result, &ptr) != roundf(strtof(result, &ptr)))) + printf("%s\n", (roundf((strtof(number1, &ptr) / strtof(number2, &ptr))*100)/100 > strtof(result, &ptr) - 0.001 && roundf((strtof(number1, &ptr) / strtof(number2, &ptr))*100)/100 < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + else + printf("%s\n", (strtof(number1, &ptr) / strtof(number2, &ptr) > strtof(result, &ptr) - 0.001 && strtof(number1, &ptr) / strtof(number2, &ptr) < strtof(result, &ptr) + 0.001) ? "OK" : "ZLE"); + break; + default: + printf("CHYBA\n"); + } + LABEL: + i = i; + } + + free(input); + + return 0; +} \ No newline at end of file diff --git a/du5/program.c b/du5/program.c index f0aeabc..3fcd60e 100644 --- a/du5/program.c +++ b/du5/program.c @@ -1,5 +1,122 @@ #include +#include +#include +#include +#include -int main(){ +struct entry{ + char* name; + int votes; +}; +struct entry* createEntry(char* name, int votes){ + struct entry* newEntry = (struct entry*)calloc(1, sizeof(struct entry)); + newEntry->name = (char*)calloc(25, sizeof(char)); + strcpy(newEntry->name, name); + newEntry->votes = votes; + return newEntry; } + +bool addPoints(struct entry* myEntries, int votes, char* name){ + if(!myEntries) + return false; + if(!strcmp(myEntries->name, name)) { + myEntries->votes += votes; + return true; + } + + return false; +} + +void bsortDesc(struct entry* list[80], int s){ + int i, j; + struct entry* temp; + int count = 0; + + for (i = 0; i < s - 1; i++) + { + for (j = 0; j < (s - 1-i); j++) + { + if (list[j]->votes < list[j + 1]->votes) + { + temp = list[j]; + list[j] = list[j + 1]; + list[j + 1] = temp; + } + else if (list[j]->votes == list[j + 1]->votes){ + while (list[j]->name[count] == list[j + 1]->name[count]) + count++; + if (list[j]->name[count] > list[j + 1]->name[count]){ + temp = list[j]; + list[j] = list[j + 1]; + list[j + 1] = temp; + } + } + count = 0; + } + } +} + +int main() { + struct entry* myEntries[100]; + for(int i = 0; i < 100; i++) + myEntries[i] = (struct entry*) calloc(1, sizeof(struct entry)); + char inputs[100][50]; //input + strcpy(inputs[0], "('-_-)"); + int counter; //counter + char symbol; //current symbol + char* number1 = (char*) calloc(25, sizeof(char)); //temporary points + char* name = (char*) calloc(25, sizeof(char)); //temporary name + char* ptr; + bool checked = false; + int position = 0; + + for(int i = 0; fgets(inputs[i], 50, stdin) != NULL; i++){ + counter = 0; + checked = false; + memset(number1, '\0', 25); + memset(name, '\0', 25); + symbol = inputs[i][counter++]; + if(!isdigit(symbol)){ + if (i == 0){ + printf("Nepodarilo nacitat nic\n"); + return 0; + } + break; + } + while(symbol != '\0' && symbol != '\n'){ + if(isspace(symbol) && isdigit(inputs[i][counter-2])) + symbol = inputs[i][counter++]; + + if(isdigit(symbol)) { + number1[strlen(number1)] = symbol; + number1[strlen(number1)] = '\0'; + } + else{ + name[strlen(name)] = symbol; + name[strlen(name)] = '\0'; + } + symbol = inputs[i][counter++]; + } + + for(int j = 0; myEntries[j]->name != NULL; j++) { + if(addPoints(myEntries[j], (int)strtol(number1, &ptr, 10), name)) + checked = true; + position = j+1; + } + + if(!checked) + myEntries[position] = createEntry(name, (int)strtol(number1, &ptr, 10)); + } + if(!strcmp(inputs[0], "('-_-)")){ + printf("Nepodarilo nacitat nic\n"); + return 0; + } + bsortDesc(myEntries, position+1); + printf("Vysledky:\n"); + for(int i = 0; myEntries[i]->name != NULL; i++) { + printf("%d %s\n", myEntries[i]->votes, myEntries[i]->name); + } + + return 0; +} \ No newline at end of file