minor fixes
This commit is contained in:
parent
5e55e32bf1
commit
a663a88378
9
main.c
9
main.c
@ -20,7 +20,7 @@ int main(const int argc, char *argv[]) {
|
|||||||
//manipulate string by cutting of trailing zeros
|
//manipulate string by cutting of trailing zeros
|
||||||
char* final_string = manipulate_str(res);
|
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);
|
free(final_string);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -67,6 +67,8 @@ double calculate(const CalcStr calc) {
|
|||||||
return calc.first * calc.second;
|
return calc.first * calc.second;
|
||||||
case '/':
|
case '/':
|
||||||
return calc.first / calc.second;
|
return calc.first / calc.second;
|
||||||
|
case '%':
|
||||||
|
return (int)calc.first % (int)calc.second;
|
||||||
case '^':
|
case '^':
|
||||||
double r = 1;
|
double r = 1;
|
||||||
for (int i = (int)calc.second; i > 0; i--) {
|
for (int i = (int)calc.second; i > 0; i--) {
|
||||||
@ -81,10 +83,11 @@ double calculate(const CalcStr calc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char* manipulate_str(const double input) {
|
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));
|
char *str = malloc(str_basesize * sizeof(char));
|
||||||
if (str == nullptr) {
|
if (str == nullptr) {
|
||||||
printf("Failed to allocate the memory");
|
printf("Failed to allocate the memory\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
memset(str, 0, str_basesize);
|
memset(str, 0, str_basesize);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user