From 3034a490dfa6058469fb6499143d657f54bd3ae3 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Fri, 22 Oct 2021 11:02:29 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'cv3/program.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv3/program.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/cv3/program.c b/cv3/program.c index 53c5fdf..2356201 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1 +1,84 @@ #include +#include +#include +#include +#include +#include + +#define SIZE 50 + +int currentlyInBuffer = 0; + +bool calculatorLogic(char buffer[SIZE][SIZE]){ + if(isdigit(buffer[currentlyInBuffer][0])){ + if(currentlyInBuffer == 9) { + printf("full stack\n"); + return false; + } + for(int i = 0; i <= currentlyInBuffer; i++){ + if(i == currentlyInBuffer) + printf("%0.2f \n", roundf(atof(buffer[i])*100)/100); + else + printf("%0.2f ", roundf(atof(buffer[i])*100)/100); + } + return true; + } + else if(strchr("+-/*", buffer[currentlyInBuffer][0]) != NULL){ + if(currentlyInBuffer < 2){ + printf("not enough operands\n"); + return false; + } + + double temporaryDecimal = 0; + + switch(buffer[currentlyInBuffer][0]){ + case '+': + temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) + atof(buffer[currentlyInBuffer-2]))*100)/100; + break; + case '-': + temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) - atof(buffer[currentlyInBuffer-2]))*100)/100; + break; + case '*': + temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) * atof(buffer[currentlyInBuffer-2]))*100)/100; + break; + case '/': + if(atof(buffer[currentlyInBuffer-2]) == 0.0) + printf("division by zero\n"); + else + temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) / atof(buffer[currentlyInBuffer-2]))*100)/100; + } + + for(int i = currentlyInBuffer-3; currentlyInBuffer > i; currentlyInBuffer--) + memset(buffer[currentlyInBuffer], SIZE, '\0'); + + gcvt(temporaryDecimal, 10, buffer[currentlyInBuffer]); + + for(int i = 0; i <= currentlyInBuffer; i++){ + if(i == currentlyInBuffer) + printf("%0.2f \n", roundf(atof(buffer[i]) * 100) / 100); + else + printf("%0.2f ", roundf(atof(buffer[i]) * 100) / 100); + } + + return true; + } + else{ + printf("bad input\n"); + } + } + + int main() { + char buffer[SIZE][SIZE]; + for(int i = 0; i < SIZE; i++) + memset(buffer[i], '\0', SIZE); + + for(; fgets(buffer[currentlyInBuffer], SIZE, stdin); currentlyInBuffer++) + if(!calculatorLogic(buffer)) + return 0; + + if(buffer[currentlyInBuffer][0] == '\0') + printf("no input\n"); + + return 0; + } + \ No newline at end of file