2024-10-14 12:20:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#define STACK_SIZE 50
|
|
|
|
|
|
|
|
struct stack
|
|
|
|
{
|
|
|
|
float values[STACK_SIZE];
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
void add(struct stack* stack, float num)
|
|
|
|
{
|
|
|
|
assert(stack->size < STACK_SIZE);
|
|
|
|
stack->values[stack->size] = num;
|
|
|
|
stack->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getnpop(struct stack* stack)
|
|
|
|
{
|
|
|
|
assert(stack->size > 0);
|
|
|
|
float value = stack->values[stack->size-1];
|
|
|
|
stack->size -= 1;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-14 12:48:27 +00:00
|
|
|
int calculate(struct stack* stack, char operator)
|
2024-10-14 12:20:08 +00:00
|
|
|
{
|
2024-10-14 13:47:58 +00:00
|
|
|
if(stack->size < 2)
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2024-10-14 12:20:08 +00:00
|
|
|
float num1 = getnpop(stack);
|
|
|
|
float num2 = getnpop(stack);
|
|
|
|
float res = 0;
|
|
|
|
|
|
|
|
switch(operator)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
res = num1+num2;
|
|
|
|
break;
|
|
|
|
case '-':
|
2024-10-14 12:24:03 +00:00
|
|
|
res = num2-num1;
|
2024-10-14 12:20:08 +00:00
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
res = num1*num2;
|
|
|
|
break;
|
|
|
|
case '/':
|
2024-10-14 12:48:27 +00:00
|
|
|
if(num1 == 0)
|
|
|
|
{
|
|
|
|
//res = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
2024-10-14 12:24:03 +00:00
|
|
|
res = num2/num1;
|
2024-10-14 12:20:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
add(stack, res);
|
2024-10-14 12:48:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int check_op(char symb)
|
|
|
|
{
|
|
|
|
if(symb == '+' || symb == '-' || symb == '*' || symb == '/')
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2024-10-14 12:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char const *argv[])
|
|
|
|
{
|
|
|
|
char buf[50];
|
|
|
|
struct stack calc_stack;
|
|
|
|
float num = 0;
|
|
|
|
int s = 0;
|
|
|
|
memset(buf, 0, 50);
|
|
|
|
memset(&calc_stack,0,sizeof(struct stack));
|
|
|
|
|
|
|
|
while (fgets(buf, 50, stdin) != NULL)
|
|
|
|
{
|
|
|
|
if(buf[0] == '\n')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2024-10-14 13:44:28 +00:00
|
|
|
if(buf[0] == '.')
|
|
|
|
{
|
|
|
|
printf("bad input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:42:05 +00:00
|
|
|
for (int i = 0; i < strcspn(buf, "\n"); i++)
|
2024-10-14 13:36:31 +00:00
|
|
|
{
|
2024-10-14 13:42:05 +00:00
|
|
|
if(isalpha(buf[i]) != 0)
|
|
|
|
{
|
|
|
|
printf("bad input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:36:31 +00:00
|
|
|
}
|
2024-10-14 13:42:05 +00:00
|
|
|
|
2024-10-14 12:20:08 +00:00
|
|
|
buf[strcspn(buf, "\n")] = '\0';
|
2024-10-14 12:48:27 +00:00
|
|
|
if(!check_op(buf[0]))
|
2024-10-14 12:20:08 +00:00
|
|
|
{
|
2024-10-14 12:48:27 +00:00
|
|
|
num = atof(buf);
|
2024-10-14 12:20:08 +00:00
|
|
|
add(&calc_stack, num);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-10-14 12:48:27 +00:00
|
|
|
if(calculate(&calc_stack, buf[0]) == 1)
|
|
|
|
{
|
|
|
|
printf("division by zero\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:47:58 +00:00
|
|
|
else if(calculate(&calc_stack, buf[0]) == 2)
|
|
|
|
{
|
|
|
|
printf("not enough operands\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 12:20:08 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < calc_stack.size; i++)
|
|
|
|
{
|
2024-10-14 12:29:01 +00:00
|
|
|
printf("%.2f ", calc_stack.values[i]);
|
2024-10-14 12:20:08 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2024-10-14 12:31:07 +00:00
|
|
|
printf("no input\n");
|
2024-10-14 12:20:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|