2020-10-11 12:27:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-10-21 14:19:37 +00:00
|
|
|
#include <ctype.h>
|
2020-10-11 12:27:21 +00:00
|
|
|
|
2020-10-21 14:19:37 +00:00
|
|
|
#define STACK_SIZE 10
|
2020-10-11 12:27:21 +00:00
|
|
|
|
|
|
|
struct stack {
|
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2020-10-24 16:59:41 +00:00
|
|
|
//push number to the stack
|
|
|
|
|
2020-10-11 12:27:21 +00:00
|
|
|
void push(struct stack* stack,float values){
|
2020-10-21 14:19:37 +00:00
|
|
|
if(stack->size < 10){
|
|
|
|
stack->values[stack->size] = values;
|
|
|
|
stack->size += 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
printf("full stack\n");
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
}
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:59:41 +00:00
|
|
|
//pop number from the stack
|
|
|
|
|
2020-10-11 12:27:21 +00:00
|
|
|
float pop(struct stack *stack){
|
2020-10-21 14:19:37 +00:00
|
|
|
if(stack->size > 0){
|
|
|
|
float value = stack->values[stack->size-1];
|
|
|
|
stack->size -= 1;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
printf("not enough operands\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
2020-10-21 14:19:37 +00:00
|
|
|
|
2020-10-11 12:27:21 +00:00
|
|
|
void print_stack(struct stack *stack){
|
|
|
|
int i;
|
|
|
|
int len = stack->size - 1;
|
|
|
|
for(i = 0; i < len; i++){
|
2020-10-11 12:43:09 +00:00
|
|
|
printf("%0.2f ", stack->values[i]);
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
if(stack->size != 0){
|
2020-10-11 12:43:09 +00:00
|
|
|
printf("%0.2f ", stack->values[i]);
|
2020-10-21 14:19:37 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2020-10-11 12:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct stack mystack;
|
|
|
|
memset(&mystack, 0, sizeof(struct stack));
|
|
|
|
int i = 0;
|
2020-10-21 14:19:37 +00:00
|
|
|
for(i = 0; i < 100; i++){
|
|
|
|
|
2020-10-11 12:27:21 +00:00
|
|
|
char line[10];
|
|
|
|
char *x = fgets(line, 10, stdin);
|
|
|
|
float r = 0;
|
2020-10-21 14:19:37 +00:00
|
|
|
float z = 0;
|
2020-10-11 12:27:21 +00:00
|
|
|
|
2020-10-24 16:59:41 +00:00
|
|
|
//if string is not empty
|
2020-10-21 14:19:37 +00:00
|
|
|
if(line[1] != 0 && x != NULL){
|
2020-10-11 12:27:21 +00:00
|
|
|
|
2020-10-24 16:59:41 +00:00
|
|
|
//if there is a wrong symbol
|
2020-10-21 14:19:37 +00:00
|
|
|
if(!isdigit(line[0]) && line[0] != '+' && line[0] != '*' && line[0] != '/' && line[0] != '-'){
|
|
|
|
printf("bad input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-11 12:27:21 +00:00
|
|
|
|
2020-10-21 14:19:37 +00:00
|
|
|
if(line[0] == '+'){
|
|
|
|
r = pop(&mystack) + pop(&mystack);
|
|
|
|
}
|
|
|
|
else if(line[0] == '-'){
|
2020-10-24 16:59:41 +00:00
|
|
|
z = pop(&mystack); //swap numbers(e.g input "3 4 -"
|
|
|
|
r = pop(&mystack) - z; //output 3 - 4, not 4 - 3)
|
2020-10-21 14:19:37 +00:00
|
|
|
}
|
|
|
|
else if(line[0] == '*'){
|
|
|
|
r = pop(&mystack) * pop(&mystack);
|
|
|
|
}
|
|
|
|
else if(line[0] == '/'){
|
|
|
|
z = pop(&mystack);
|
|
|
|
if(z == 0){
|
|
|
|
printf("division by zero\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r = pop(&mystack) / z;
|
|
|
|
}
|
|
|
|
}
|
2020-10-24 16:59:41 +00:00
|
|
|
//if symbol is not operation, than it is number
|
|
|
|
//change string to float
|
2020-10-21 14:19:37 +00:00
|
|
|
else {
|
|
|
|
r = strtof(line,&x);
|
|
|
|
}
|
2020-10-24 16:59:41 +00:00
|
|
|
//push result to stack
|
2020-10-21 14:19:37 +00:00
|
|
|
push(&mystack,r);
|
2020-10-24 16:59:41 +00:00
|
|
|
|
2020-10-21 14:19:37 +00:00
|
|
|
print_stack(&mystack);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 12:27:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|