minor fixes

This commit is contained in:
Martin Vrhovšek 2025-01-12 22:35:16 +01:00
parent 5e55e32bf1
commit a663a88378

9
main.c
View File

@ -20,7 +20,7 @@ int main(const int argc, char *argv[]) {
//manipulate string by cutting of trailing zeros
char* final_string = manipulate_str(res);
printf("%s %s %s = %s", argv[1], argv[2], argv[3], final_string);
printf("%s %s %s = %s\n", argv[1], argv[2], argv[3], final_string);
free(final_string);
return 0;
}
@ -67,6 +67,8 @@ double calculate(const CalcStr calc) {
return calc.first * calc.second;
case '/':
return calc.first / calc.second;
case '%':
return (int)calc.first % (int)calc.second;
case '^':
double r = 1;
for (int i = (int)calc.second; i > 0; i--) {
@ -81,10 +83,11 @@ double calculate(const CalcStr calc) {
}
char* manipulate_str(const double input) {
const int str_basesize = (int)log10(input) + 16 + 1;
const int digit = abs((int)input) == 0 ? 1 : abs((int)input);
const int str_basesize = (int)log10(digit) + 16 + 1;
char *str = malloc(str_basesize * sizeof(char));
if (str == nullptr) {
printf("Failed to allocate the memory");
printf("Failed to allocate the memory\n");
exit(EXIT_FAILURE);
}
memset(str, 0, str_basesize);