Something strange with server
This commit is contained in:
parent
83e0ac56af
commit
1a8ad13b45
@ -27,6 +27,7 @@ void print_stack(struct stack* stack){
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void process_operations(struct stack* stack) {
|
void process_operations(struct stack* stack) {
|
||||||
char line[STACK_SIZE];
|
char line[STACK_SIZE];
|
||||||
int could_read = 0;
|
int could_read = 0;
|
||||||
@ -39,8 +40,9 @@ void process_operations(struct stack* stack){
|
|||||||
}
|
}
|
||||||
could_read= 1;
|
could_read= 1;
|
||||||
|
|
||||||
if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || strcmp(line, "*") == 0 || strcmp(line, "/") == 0)
|
if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 ||
|
||||||
&& stack->size >= 2){
|
strcmp(line, "*") == 0 || strcmp(line, "/") == 0)) {
|
||||||
|
if (stack->size >= 2) {
|
||||||
float b = pop_stack(stack);
|
float b = pop_stack(stack);
|
||||||
float a = pop_stack(stack);
|
float a = pop_stack(stack);
|
||||||
float res = (line[0] == '+') ? a + b :
|
float res = (line[0] == '+') ? a + b :
|
||||||
@ -49,26 +51,26 @@ void process_operations(struct stack* stack){
|
|||||||
(line[0] == '/') ? a / b : 0;
|
(line[0] == '/') ? a / b : 0;
|
||||||
push_stack(stack, res);
|
push_stack(stack, res);
|
||||||
print_stack(stack);
|
print_stack(stack);
|
||||||
}
|
} else {
|
||||||
else if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || strcmp(line, "*") == 0 || strcmp(line, "/") == 0)
|
printf("bad input\n");
|
||||||
&& stack->size <2){
|
|
||||||
printf("no input\n");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
else{
|
|
||||||
char *endptr;
|
char *endptr;
|
||||||
float val = strtof(line, &endptr);
|
float val = strtof(line, &endptr);
|
||||||
|
if (endptr == line) {
|
||||||
|
printf("bad input\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
push_stack(stack, val);
|
push_stack(stack, val);
|
||||||
print_stack(stack);
|
print_stack(stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} }
|
|
||||||
if (!could_read) {
|
if (!could_read) {
|
||||||
printf("no input\n");
|
printf("no input\n");
|
||||||
} }
|
}
|
||||||
|
}
|
||||||
int main(void){
|
int main(void){
|
||||||
memset(&mystack,0,sizeof(struct stack));
|
memset(&mystack,0,sizeof(struct stack));
|
||||||
process_operations(&mystack);
|
process_operations(&mystack);
|
||||||
|
Loading…
Reference in New Issue
Block a user