3
This commit is contained in:
parent
23fd7ac3c3
commit
e99adaa6a4
@ -11,7 +11,7 @@ int stack_top = -1;
|
|||||||
bool is_number(char *string) {
|
bool is_number(char *string) {
|
||||||
char *end;
|
char *end;
|
||||||
strtof(string, &end);
|
strtof(string, &end);
|
||||||
return end != string && *end == '\0';
|
return end != string && *end == '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_operation(char *string) {
|
bool is_operation(char *string) {
|
||||||
@ -33,7 +33,7 @@ float calculator(float n1, float n2, char operation) {
|
|||||||
return n1 / n2;
|
return n1 / n2;
|
||||||
} else {
|
} else {
|
||||||
printf("Error: division by zero\n");
|
printf("Error: division by zero\n");
|
||||||
exit(1);
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -44,7 +44,6 @@ void push(float number) {
|
|||||||
stack[++stack_top] = number;
|
stack[++stack_top] = number;
|
||||||
} else {
|
} else {
|
||||||
printf("Error: stack overflow\n");
|
printf("Error: stack overflow\n");
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,7 +52,7 @@ float pop() {
|
|||||||
return stack[stack_top--];
|
return stack[stack_top--];
|
||||||
} else {
|
} else {
|
||||||
printf("Error: stack is empty\n");
|
printf("Error: stack is empty\n");
|
||||||
exit(1);
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,12 +69,12 @@ int main() {
|
|||||||
if (is_number(arr)) {
|
if (is_number(arr)) {
|
||||||
float number = strtof(arr, &pend);
|
float number = strtof(arr, &pend);
|
||||||
push(number);
|
push(number);
|
||||||
printf("Number added: %.2f\n", number);
|
printf("%.2f ", number);
|
||||||
}
|
}
|
||||||
else if (is_operation(arr)) {
|
else if (is_operation(arr)) {
|
||||||
if (stack_top < 1) {
|
if (stack_top < 1) {
|
||||||
printf("Error: not enough numbers in the stack for operation\n");
|
printf("no input\n");
|
||||||
return 1;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
float n2 = pop();
|
float n2 = pop();
|
||||||
@ -83,18 +82,18 @@ int main() {
|
|||||||
char operation = arr[0];
|
char operation = arr[0];
|
||||||
float result = calculator(n1, n2, operation);
|
float result = calculator(n1, n2, operation);
|
||||||
push(result);
|
push(result);
|
||||||
printf("Operation result: %.2f\n", result);
|
printf("%.2f ", result);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
printf("Error: invalid input\n");
|
printf("no input\n");
|
||||||
return 1;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stack_top == 0) {
|
if (stack_top == 0) {
|
||||||
printf("Final result: %.2f\n", pop());
|
printf("\n%.2f\n", pop());
|
||||||
} else {
|
} else {
|
||||||
printf("Error: stack contains more than one number after operations\n");
|
printf("\nno input\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user