From 2f3fc5ad8a374367b86964e2b23056cc45b943e3 Mon Sep 17 00:00:00 2001 From: Daniel Ryzhuk Date: Wed, 14 Oct 2020 16:57:47 +0000 Subject: [PATCH] Update 'cv3/program.c' --- cv3/program.c | 169 +++++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 83 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 38ade7b..128512d 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,84 +1,87 @@ -#include -#include -#include -#define STACK_SIZE 10 - -typedef struct { - float values[STACK_SIZE]; - int size; -} stack; - -void stack_print (stack* stack); -void stack_push (stack* stack,float value); -float stack_pop (stack* stack); -void my_exit(int flag, const char* input); - -int main(void) { - stack* p_stack = (stack*) malloc(sizeof(stack)); - memset(p_stack, 0, sizeof(stack)); - while(1) { - char str[128] = { 0 }; - char* tres = fgets(str, 128, stdin); - if(tres == NULL || str[0] == 0 || (str[1] == 0 && str[0] == '\n')) - my_exit(1, "no_input"); - str[strlen(str) - 1] = 0; - // if number - if(strlen(str) > 1 || (str[0] >= '0' && str[0] <= '9')) { - char *p; - errno = 0; - float res = strtod(str,&p); - my_exit(str[0] == '.' || errno || (*p) != 0, "bad input"); - stack_push(p_stack, res); - } - else { - my_exit(p_stack->size < 2, "not enough operands"); - float b = stack_pop(p_stack); - float a = stack_pop(p_stack); - char op = str[0]; - float res; - switch (op) { - case '+': - res = a + b; - break; - case '-': - res = a - b; - break; - case '*': - res = a * b; - break; - case '/': - my_exit(b == 0.f, "division by zero"); - res = a / b; - break; - default: - my_exit(1, "bad input"); - } - stack_push(p_stack, res); - } - stack_print(p_stack); - } - free(p_stack); -} - -void stack_print(stack* st) { - for(int i = 0; i < st->size; i++) - printf("%.2f ",st->values[i]); - printf("\n"); -} - -void stack_push(stack* st, float push_value) { - if(st->size < STACK_SIZE) - st->values[st->size++] = push_value; -} - -float stack_pop(stack* st) { - if(st->size > 0) - return st->values[--st->size]; -} - -void my_exit(int flag, const char* input) { - if (flag) { - printf("%s\n", input); - exit(0); - } +#include +#include +#include +#define STACK_SIZE 10 + +typedef struct { + float values[STACK_SIZE]; + int size; +} stack; + +void stack_print (stack* stack); +void stack_push (stack* stack,float value); +float stack_pop (stack* stack); +void my_exit(int flag, const char* input); + +int main(void) { + stack* p_stack = (stack*) malloc(sizeof(stack)); + memset(p_stack, 0, sizeof(stack)); + while(1) { + char str[128] = { 0 }; + char* tres = fgets(str, 128, stdin); + if(tres == NULL || str[0] == 0 || (str[1] == 0 && str[0] == '\n')) + my_exit(1, "no_input"); + str[strlen(str) - 1] = 0; + // if number + if(strlen(str) > 1 || (str[0] >= '0' && str[0] <= '9')) { + my_exit(str[0] == '.', "bad input"); + float res = 0.f; + int dots = 0; + for(int i = 0; i < strlen(str); i++) + if(str[i]=='.') dots++; + else my_exit(str[i] < '0' || str[i] > '9', "bad input"); + res = atof(str); + stack_push(p_stack, res); + } + else { + my_exit(p_stack->size < 2, "not enough operands"); + float b = stack_pop(p_stack); + float a = stack_pop(p_stack); + char op = str[0]; + float res; + switch (op) { + case '+': + res = a + b; + break; + case '-': + res = a - b; + break; + case '*': + res = a * b; + break; + case '/': + my_exit(b == 0.f, "division by zero"); + res = a / b; + break; + default: + my_exit(1, "bad input"); + } + stack_push(p_stack, res); + } + stack_print(p_stack); + } + free(p_stack); +} + +void stack_print(stack* st) { + for(int i = 0; i < st->size; i++) + printf("%.2f ",st->values[i]); + printf("\n"); +} + +void stack_push(stack* st, float push_value) { + if(st->size < STACK_SIZE) + st->values[st->size++] = push_value; +} + +float stack_pop(stack* st) { + if(st->size > 0) + return st->values[--st->size]; +} + +void my_exit(int flag, const char* input) { + if (flag) { + printf("%s\n", input); + exit(0); + } } \ No newline at end of file