Изменил(а) на 'cv3/program.c'
This commit is contained in:
parent
03aedc72b7
commit
3034a490df
@ -1 +1,84 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user