Second try
This commit is contained in:
parent
7dcd74f698
commit
83e0ac56af
@ -23,37 +23,52 @@ return value;
|
|||||||
}
|
}
|
||||||
void print_stack(struct stack* stack){
|
void print_stack(struct stack* stack){
|
||||||
for(int i = 0; i < stack->size; i++)
|
for(int i = 0; i < stack->size; i++)
|
||||||
printf("%g ", stack->values[i]);
|
printf("%.2f ", stack->values[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_operations(struct stack* stack){
|
void process_operations(struct stack* stack){
|
||||||
char line[STACK_SIZE];
|
char line[STACK_SIZE];
|
||||||
if(!fgets(line, STACK_SIZE, stdin)){
|
int could_read = 0;
|
||||||
printf("Chyba\n");
|
|
||||||
|
while(fgets(line, sizeof(line), stdin)){
|
||||||
|
line[strcspn(line, "\n")] = '\0';
|
||||||
|
if(line[0] == '\0'){
|
||||||
|
printf("no input\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(int i = 0; line[i] != '\0'; i++){
|
could_read = 1;
|
||||||
if(line[i] >= '0' && line[i] <= '9'){
|
|
||||||
char *endptr;
|
if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || strcmp(line, "*") == 0 || strcmp(line, "/") == 0)
|
||||||
float val = strtof(&line[i], &endptr);
|
|
||||||
push_stack(stack, val);
|
|
||||||
print_stack(stack);
|
|
||||||
i = endptr - line - 1;
|
|
||||||
}
|
|
||||||
else if ((line[i] == '*' || line[i] == '+' || line[i] == '-' || line[i] == '/')
|
|
||||||
&& stack->size >= 2){
|
&& 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[i] == '+') ? a + b :
|
float res = (line[0] == '+') ? a + b :
|
||||||
(line[i] == '-') ? a - b :
|
(line[0] == '-') ? a - b :
|
||||||
(line[i] == '*') ? a * b :
|
(line[0] == '*') ? a * b :
|
||||||
(line[i] == '/') ? a / b : 0;
|
(line[0] == '/') ? a / b : 0;
|
||||||
push_stack(stack, res);
|
push_stack(stack, res);
|
||||||
print_stack(stack);
|
print_stack(stack);
|
||||||
}
|
}
|
||||||
|
else if ((strcmp(line, "+") == 0 || strcmp(line, "-") == 0 || strcmp(line, "*") == 0 || strcmp(line, "/") == 0)
|
||||||
|
&& stack->size <2){
|
||||||
|
printf("no input\n");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
else{
|
||||||
|
char *endptr;
|
||||||
|
float val = strtof(line, &endptr);
|
||||||
|
push_stack(stack, val);
|
||||||
|
print_stack(stack);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} }
|
||||||
|
if (!could_read){
|
||||||
|
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