Update 'cv3/program.c'

This commit is contained in:
Daniel Ryzhuk 2020-10-14 16:57:47 +00:00
parent 0ac9d77192
commit 2f3fc5ad8a

View File

@ -1,84 +1,87 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define STACK_SIZE 10 #define STACK_SIZE 10
typedef struct { typedef struct {
float values[STACK_SIZE]; float values[STACK_SIZE];
int size; int size;
} stack; } stack;
void stack_print (stack* stack); void stack_print (stack* stack);
void stack_push (stack* stack,float value); void stack_push (stack* stack,float value);
float stack_pop (stack* stack); float stack_pop (stack* stack);
void my_exit(int flag, const char* input); void my_exit(int flag, const char* input);
int main(void) { int main(void) {
stack* p_stack = (stack*) malloc(sizeof(stack)); stack* p_stack = (stack*) malloc(sizeof(stack));
memset(p_stack, 0, sizeof(stack)); memset(p_stack, 0, sizeof(stack));
while(1) { while(1) {
char str[128] = { 0 }; char str[128] = { 0 };
char* tres = fgets(str, 128, stdin); char* tres = fgets(str, 128, stdin);
if(tres == NULL || str[0] == 0 || (str[1] == 0 && str[0] == '\n')) if(tres == NULL || str[0] == 0 || (str[1] == 0 && str[0] == '\n'))
my_exit(1, "no_input"); my_exit(1, "no_input");
str[strlen(str) - 1] = 0; str[strlen(str) - 1] = 0;
// if number // if number
if(strlen(str) > 1 || (str[0] >= '0' && str[0] <= '9')) { if(strlen(str) > 1 || (str[0] >= '0' && str[0] <= '9')) {
char *p; my_exit(str[0] == '.', "bad input");
errno = 0; float res = 0.f;
float res = strtod(str,&p); int dots = 0;
my_exit(str[0] == '.' || errno || (*p) != 0, "bad input"); for(int i = 0; i < strlen(str); i++)
stack_push(p_stack, res); if(str[i]=='.') dots++;
} else my_exit(str[i] < '0' || str[i] > '9', "bad input");
else { res = atof(str);
my_exit(p_stack->size < 2, "not enough operands"); stack_push(p_stack, res);
float b = stack_pop(p_stack); }
float a = stack_pop(p_stack); else {
char op = str[0]; my_exit(p_stack->size < 2, "not enough operands");
float res; float b = stack_pop(p_stack);
switch (op) { float a = stack_pop(p_stack);
case '+': char op = str[0];
res = a + b; float res;
break; switch (op) {
case '-': case '+':
res = a - b; res = a + b;
break; break;
case '*': case '-':
res = a * b; res = a - b;
break; break;
case '/': case '*':
my_exit(b == 0.f, "division by zero"); res = a * b;
res = a / b; break;
break; case '/':
default: my_exit(b == 0.f, "division by zero");
my_exit(1, "bad input"); res = a / b;
} break;
stack_push(p_stack, res); default:
} my_exit(1, "bad input");
stack_print(p_stack); }
} stack_push(p_stack, res);
free(p_stack); }
} stack_print(p_stack);
}
void stack_print(stack* st) { free(p_stack);
for(int i = 0; i < st->size; i++) }
printf("%.2f ",st->values[i]);
printf("\n"); void stack_print(stack* st) {
} for(int i = 0; i < st->size; i++)
printf("%.2f ",st->values[i]);
void stack_push(stack* st, float push_value) { printf("\n");
if(st->size < STACK_SIZE) }
st->values[st->size++] = push_value;
} void stack_push(stack* st, float push_value) {
if(st->size < STACK_SIZE)
float stack_pop(stack* st) { st->values[st->size++] = push_value;
if(st->size > 0) }
return st->values[--st->size];
} float stack_pop(stack* st) {
if(st->size > 0)
void my_exit(int flag, const char* input) { return st->values[--st->size];
if (flag) { }
printf("%s\n", input);
exit(0); void my_exit(int flag, const char* input) {
} if (flag) {
printf("%s\n", input);
exit(0);
}
} }