2021-10-21 22:12:54 +00:00
|
|
|
#include <ctype.h>
|
2021-10-21 20:16:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-10-21 22:45:57 +00:00
|
|
|
#define STACK_SIZE 10
|
2021-10-21 20:16:34 +00:00
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
struct stack{ //definition of STACK struct
|
2021-10-21 20:16:34 +00:00
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
//function which prints contents of stack
|
|
|
|
void print_stack(struct stack* stack);
|
|
|
|
|
|
|
|
//function to append element into stack
|
2021-10-21 20:16:34 +00:00
|
|
|
void push_stack(struct stack* stack, float value);
|
2021-10-21 23:16:39 +00:00
|
|
|
|
|
|
|
//function to delete last element of stack
|
2021-10-21 20:16:34 +00:00
|
|
|
float pop_stack(struct stack* stack);
|
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
//function to sum last 2 operands from stack
|
|
|
|
void sum(struct stack* stack);
|
|
|
|
|
|
|
|
//function to substract last 2 operands from stack
|
|
|
|
void substract(struct stack* stack);
|
|
|
|
|
|
|
|
//function to multiply last 2 operands from stack
|
|
|
|
void multip(struct stack* stack);
|
|
|
|
|
|
|
|
//function to divide last 2 operands from stack
|
|
|
|
void divis(struct stack* stack);
|
|
|
|
|
2021-10-21 20:16:34 +00:00
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
char char_value[STACK_SIZE]; //definition of input array
|
2021-10-21 20:16:34 +00:00
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
memset(&char_value,0,STACK_SIZE); //inicialization of input array
|
|
|
|
|
|
|
|
float float_value; //variable to which we convert number from input array (char_value[])
|
|
|
|
char* ptr; //return pointer of fgets
|
|
|
|
struct stack myStack; //struct myStach of type stack
|
|
|
|
memset(&myStack,0,sizeof(struct stack)); //initialization of myStruct structure
|
2021-10-21 20:16:34 +00:00
|
|
|
while(1){
|
2021-10-21 23:16:39 +00:00
|
|
|
if(myStack.size == 10){ //condition to break the cycle if we have full stack
|
2021-10-21 22:45:57 +00:00
|
|
|
puts("full stack");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
//load input into char_value
|
2021-10-21 20:16:34 +00:00
|
|
|
ptr = fgets(char_value, STACK_SIZE-1, stdin);
|
2021-10-21 23:16:39 +00:00
|
|
|
if(ptr != NULL){ //check if it run ok
|
|
|
|
if(char_value[0] == '\n'){ //if first char is \n, break the cycle, its the end
|
2021-10-21 21:20:42 +00:00
|
|
|
puts("no input");
|
|
|
|
break;
|
|
|
|
}
|
2021-10-21 23:16:39 +00:00
|
|
|
|
|
|
|
//sum
|
2021-10-21 21:20:42 +00:00
|
|
|
if(char_value[0] == '+'){
|
2021-10-21 22:30:38 +00:00
|
|
|
if(myStack.size > 1){
|
2021-10-21 23:16:39 +00:00
|
|
|
sum(&myStack);
|
2021-10-21 22:30:38 +00:00
|
|
|
}else{puts("not enough operands"); break;}
|
2021-10-21 23:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//substraction
|
|
|
|
else if(char_value[0] == '-'){
|
2021-10-21 22:30:38 +00:00
|
|
|
if(myStack.size > 1){
|
2021-10-21 23:16:39 +00:00
|
|
|
substract(&myStack);
|
2021-10-21 22:30:38 +00:00
|
|
|
}else{puts("not enough operands"); break;}
|
2021-10-21 23:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//multiplication
|
|
|
|
else if(char_value[0] == '*'){
|
2021-10-21 22:30:38 +00:00
|
|
|
if(myStack.size > 1){
|
2021-10-21 23:16:39 +00:00
|
|
|
multip(&myStack);
|
2021-10-21 22:30:38 +00:00
|
|
|
}else{puts("not enough operands"); break;}
|
2021-10-21 23:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//division
|
|
|
|
else if(char_value[0] == '/'){
|
2021-10-21 22:30:38 +00:00
|
|
|
if(myStack.size > 1){
|
2021-10-21 23:16:39 +00:00
|
|
|
divis(&myStack);
|
2021-10-21 22:30:38 +00:00
|
|
|
}else{puts("not enough operands"); break;}
|
2021-10-21 23:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//if its a number
|
|
|
|
else if(isdigit(char_value[0]) != 0 ){
|
2021-10-21 22:12:54 +00:00
|
|
|
float_value = strtof(char_value, NULL);
|
2021-10-21 23:16:39 +00:00
|
|
|
push_stack(&myStack,float_value);
|
|
|
|
print_stack(&myStack);
|
|
|
|
printf("\n");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
puts("bad input"); //if its not digit nor symbol of operation, break cycle, bad input
|
2021-10-21 21:20:42 +00:00
|
|
|
break;
|
2021-10-21 20:16:34 +00:00
|
|
|
}
|
2021-10-21 22:45:57 +00:00
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
//if theres no further input
|
|
|
|
} else {
|
|
|
|
puts("no input");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 20:16:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_stack(struct stack* stack){
|
|
|
|
for(int i = 0; i < stack->size; i++){
|
2021-10-21 20:21:32 +00:00
|
|
|
printf("%.2f ",stack->values[i]);
|
2021-10-21 20:16:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_stack(struct stack* stack, float value){
|
2021-10-21 22:45:57 +00:00
|
|
|
//assert(stack->size < STACK_SIZE);
|
2021-10-21 20:16:34 +00:00
|
|
|
stack->values[stack->size] = value;
|
|
|
|
stack->size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
float pop_stack(struct stack* stack){
|
2021-10-21 22:45:57 +00:00
|
|
|
//assert(stack->size > 0);
|
2021-10-21 20:16:34 +00:00
|
|
|
float value = stack->values[stack->size-1];
|
|
|
|
stack->size -= 1;
|
|
|
|
return value;
|
|
|
|
}
|
2021-10-21 22:30:38 +00:00
|
|
|
|
2021-10-21 23:16:39 +00:00
|
|
|
void sum(struct stack* stack){
|
|
|
|
float a = pop_stack(stack);
|
|
|
|
float b = pop_stack(stack);
|
|
|
|
float c = a+b;
|
|
|
|
push_stack(stack, c);
|
|
|
|
print_stack(stack);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void substract(struct stack* stack){
|
|
|
|
float a = pop_stack(stack);
|
|
|
|
float b = pop_stack(stack);
|
|
|
|
float c = b-a;
|
|
|
|
push_stack(stack, c);
|
|
|
|
print_stack(stack);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void multip(struct stack* stack){
|
|
|
|
float a = pop_stack(stack);
|
|
|
|
float b = pop_stack(stack);
|
|
|
|
float c = a*b;
|
|
|
|
push_stack(stack, c);
|
|
|
|
print_stack(stack);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void divis(struct stack* stack){
|
|
|
|
float a = pop_stack(&myStack);
|
|
|
|
if(a == 0){
|
|
|
|
printf("division by zero\n");
|
|
|
|
break;
|
|
|
|
}else{
|
|
|
|
float b = pop_stack(&myStack);
|
|
|
|
float c = b/a;
|
|
|
|
push_stack(&myStack, c);
|
|
|
|
print_stack(&myStack);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|