added comments and aesthetic changes
This commit is contained in:
parent
0361617c0f
commit
1aff928a0d
171
cv3/program.c
171
cv3/program.c
@ -1,121 +1,106 @@
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define STACK_SIZE 10
|
||||
|
||||
struct stack{
|
||||
struct stack{ //definition of STACK struct
|
||||
float values[STACK_SIZE];
|
||||
int size;
|
||||
};
|
||||
|
||||
//function which prints contents of stack
|
||||
void print_stack(struct stack* stack);
|
||||
|
||||
//function to append element into stack
|
||||
void push_stack(struct stack* stack, float value);
|
||||
|
||||
//function to delete last element of stack
|
||||
float pop_stack(struct stack* stack);
|
||||
|
||||
//function to sum last 2 operands from stack
|
||||
void sum(struct stack* stack);
|
||||
|
||||
//function to substract last 2 operands from stack
|
||||
void substract(struct stack* stack);
|
||||
|
||||
//function to multiply last 2 operands from stack
|
||||
void multip(struct stack* stack);
|
||||
|
||||
//function to divide last 2 operands from stack
|
||||
void divis(struct stack* stack);
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
char char_value[STACK_SIZE]; //definition of input array
|
||||
|
||||
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));
|
||||
memset(&char_value,0,STACK_SIZE); //inicialization of input array
|
||||
|
||||
float float_value; //variable to which we convert number from input array (char_value[])
|
||||
char* ptr; //return pointer of fgets
|
||||
struct stack myStack; //struct myStach of type stack
|
||||
memset(&myStack,0,sizeof(struct stack)); //initialization of myStruct structure
|
||||
while(1){
|
||||
if(myStack.size == 10){
|
||||
//print_stack(&myStack);
|
||||
if(myStack.size == 10){ //condition to break the cycle if we have full stack
|
||||
puts("full stack");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//load input into char_value
|
||||
ptr = fgets(char_value, STACK_SIZE-1, stdin);
|
||||
if(ptr != NULL){
|
||||
|
||||
if(char_value[0] == '\n'){
|
||||
if(ptr != NULL){ //check if it run ok
|
||||
if(char_value[0] == '\n'){ //if first char is \n, break the cycle, its the end
|
||||
puts("no input");
|
||||
break;
|
||||
}
|
||||
|
||||
/*if(char_value[0] == '0'){
|
||||
push_stack(&myStack,0);
|
||||
print_stack(&myStack);
|
||||
printf("\n");
|
||||
continue;
|
||||
}*/
|
||||
|
||||
//sum
|
||||
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");
|
||||
sum(&myStack);
|
||||
}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");
|
||||
}
|
||||
|
||||
//substraction
|
||||
else if(char_value[0] == '-'){
|
||||
if(myStack.size > 1){
|
||||
substract(&myStack);
|
||||
}else{puts("not enough operands"); break;}
|
||||
} else if(isdigit(char_value[0]) != 0 ){
|
||||
}
|
||||
|
||||
//multiplication
|
||||
else if(char_value[0] == '*'){
|
||||
if(myStack.size > 1){
|
||||
multip(&myStack);
|
||||
}else{puts("not enough operands"); break;}
|
||||
}
|
||||
|
||||
//division
|
||||
else if(char_value[0] == '/'){
|
||||
if(myStack.size > 1){
|
||||
divis(&myStack);
|
||||
}else{puts("not enough operands"); break;}
|
||||
}
|
||||
|
||||
//if its a number
|
||||
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");
|
||||
puts("bad input"); //if its not digit nor symbol of operation, break cycle, bad input
|
||||
break;
|
||||
}
|
||||
|
||||
//if theres no further input
|
||||
} else {
|
||||
//push_stack(&myStack, float_value);
|
||||
//print_stack(&myStack);
|
||||
//printf("\n");
|
||||
puts("no input");
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -141,3 +126,45 @@ float pop_stack(struct stack* stack){
|
||||
return value;
|
||||
}
|
||||
|
||||
void sum(struct stack* stack){
|
||||
float a = pop_stack(stack);
|
||||
float b = pop_stack(stack);
|
||||
float c = a+b;
|
||||
push_stack(stack, c);
|
||||
print_stack(stack);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void substract(struct stack* stack){
|
||||
float a = pop_stack(stack);
|
||||
float b = pop_stack(stack);
|
||||
float c = b-a;
|
||||
push_stack(stack, c);
|
||||
print_stack(stack);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void multip(struct stack* stack){
|
||||
float a = pop_stack(stack);
|
||||
float b = pop_stack(stack);
|
||||
float c = a*b;
|
||||
push_stack(stack, c);
|
||||
print_stack(stack);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
void divis(struct stack* stack){
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user