144 lines
4.2 KiB
C
144 lines
4.2 KiB
C
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
|
|
#define STACK_SIZE 10
|
|
|
|
struct stack{
|
|
float values[STACK_SIZE];
|
|
int size;
|
|
};
|
|
|
|
void print_stack(struct stack* stack);
|
|
void push_stack(struct stack* stack, float value);
|
|
float pop_stack(struct stack* stack);
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
char char_value[STACK_SIZE];
|
|
memset(&char_value,0,STACK_SIZE);
|
|
float float_value;
|
|
char* ptr;
|
|
struct stack myStack;
|
|
memset(&myStack,0,sizeof(struct stack));
|
|
while(1){
|
|
if(myStack.size == 9){
|
|
//print_stack(&myStack);
|
|
puts("full stack");
|
|
break;
|
|
}
|
|
|
|
|
|
|
|
|
|
ptr = fgets(char_value, STACK_SIZE-1, stdin);
|
|
if(ptr != NULL){
|
|
|
|
if(char_value[0] == '\n'){
|
|
puts("no input");
|
|
break;
|
|
}
|
|
|
|
/*if(char_value[0] == '0'){
|
|
push_stack(&myStack,0);
|
|
print_stack(&myStack);
|
|
printf("\n");
|
|
continue;
|
|
}*/
|
|
|
|
if(char_value[0] == '+'){
|
|
if(myStack.size > 1){
|
|
float a = pop_stack(&myStack);
|
|
float b = pop_stack(&myStack);
|
|
float c = a+b;
|
|
push_stack(&myStack, c);
|
|
print_stack(&myStack);
|
|
printf("\n");
|
|
}else{puts("not enough operands"); break;}
|
|
}else if(char_value[0] == '-'){
|
|
if(myStack.size > 1){
|
|
float a = pop_stack(&myStack);
|
|
float b = pop_stack(&myStack);
|
|
float c = b-a;
|
|
push_stack(&myStack, c);
|
|
print_stack(&myStack);
|
|
printf("\n");
|
|
}else{puts("not enough operands"); break;}
|
|
}else if(char_value[0] == '*'){
|
|
if(myStack.size > 1){
|
|
float a = pop_stack(&myStack);
|
|
float b = pop_stack(&myStack);
|
|
float c = a*b;
|
|
push_stack(&myStack, c);
|
|
print_stack(&myStack);
|
|
printf("\n");
|
|
}else{puts("not enough operands"); break;}
|
|
} else if(char_value[0] == '/'){
|
|
if(myStack.size > 1){
|
|
float a = pop_stack(&myStack);
|
|
if(a == 0){
|
|
printf("division by zero\n");
|
|
break;
|
|
}else{
|
|
float b = pop_stack(&myStack);
|
|
float c = b/a;
|
|
push_stack(&myStack, c);
|
|
print_stack(&myStack);
|
|
printf("\n");
|
|
}
|
|
}else{puts("not enough operands"); break;}
|
|
} else if(isdigit(char_value[0]) != 0 ){
|
|
float_value = strtof(char_value, NULL);
|
|
//printf("float value: %f\n",float_value);
|
|
/*if(float_value == 0){
|
|
puts("bad input");
|
|
break;*/
|
|
//} else {
|
|
push_stack(&myStack,float_value);
|
|
print_stack(&myStack);
|
|
printf("\n");
|
|
continue;
|
|
|
|
} else {
|
|
puts("bad input");
|
|
break;
|
|
}
|
|
} else {
|
|
//push_stack(&myStack, float_value);
|
|
//print_stack(&myStack);
|
|
//printf("\n");
|
|
puts("no input");
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
void print_stack(struct stack* stack){
|
|
for(int i = 0; i < stack->size; i++){
|
|
printf("%.2f ",stack->values[i]);
|
|
}
|
|
}
|
|
|
|
void push_stack(struct stack* stack, float value){
|
|
//assert(stack->size < STACK_SIZE);
|
|
stack->values[stack->size] = value;
|
|
stack->size += 1;
|
|
}
|
|
|
|
float pop_stack(struct stack* stack){
|
|
//assert(stack->size > 0);
|
|
float value = stack->values[stack->size-1];
|
|
stack->size -= 1;
|
|
return value;
|
|
}
|
|
|