usaa21/cv3/program.c
2021-10-22 01:21:30 +02:00

171 lines
4.8 KiB
C

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STACK_SIZE 10
struct stack{ //definition of STACK struct
float values[STACK_SIZE];
int size;
};
//function which prints contents of stack
void print_stack(struct stack* stack);
//function to append element into stack
void push_stack(struct stack* stack, float value);
//function to delete last element of stack
float pop_stack(struct stack* stack);
//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, float a);
int main(){
char char_value[STACK_SIZE]; //definition of input array
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
while(1){
if(myStack.size == 10){ //condition to break the cycle if we have full stack
puts("full stack");
break;
}
//load input into char_value
ptr = fgets(char_value, STACK_SIZE-1, stdin);
if(ptr != NULL){ //check if it run ok
if(char_value[0] == '\n'){ //if first char is \n, break the cycle, its the end
puts("no input");
break;
}
//sum
if(char_value[0] == '+'){
if(myStack.size > 1){
sum(&myStack);
}else{puts("not enough operands"); break;}
}
//substraction
else if(char_value[0] == '-'){
if(myStack.size > 1){
substract(&myStack);
}else{puts("not enough operands"); break;}
}
//multiplication
else if(char_value[0] == '*'){
if(myStack.size > 1){
multip(&myStack);
}else{puts("not enough operands"); break;}
}
//division
else if(char_value[0] == '/'){
if(myStack.size > 1){
float a = pop_stack(&myStack);
if(a == 0){
printf("division by zero\n");
break;
}else{
divis(&myStack, a);
}
}else{puts("not enough operands"); break;}
}
//if its a number
else if(isdigit(char_value[0]) != 0 ){
float_value = strtof(char_value, NULL);
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
break;
}
//if theres no further input
} else {
puts("no input");
break;
}
}
return 0;
}
void print_stack(struct stack* stack){
for(int i = 0; i < stack->size; i++){
printf("%.2f ",stack->values[i]);
}
}
void push_stack(struct stack* stack, float value){
//assert(stack->size < STACK_SIZE);
stack->values[stack->size] = value;
stack->size += 1;
}
float pop_stack(struct stack* stack){
//assert(stack->size > 0);
float value = stack->values[stack->size-1];
stack->size -= 1;
return value;
}
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){
float b = pop_stack(stack);
float c = b/a;
push_stack(stack, c);
print_stack(stack);
printf("\n");
}