From a663a883786ad31b892fac521693f6b99129cf15 Mon Sep 17 00:00:00 2001 From: Marto Date: Sun, 12 Jan 2025 22:35:16 +0100 Subject: [PATCH] minor fixes --- main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index 9aa9910..3ca303f 100644 --- a/main.c +++ b/main.c @@ -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);