Изменил(а) на 'cv3/program.c'
This commit is contained in:
parent
3034a490df
commit
71bc6751f4
121
cv3/program.c
121
cv3/program.c
@ -11,74 +11,73 @@ int currentlyInBuffer = 0;
|
|||||||
|
|
||||||
bool calculatorLogic(char buffer[SIZE][SIZE]){
|
bool calculatorLogic(char buffer[SIZE][SIZE]){
|
||||||
if(isdigit(buffer[currentlyInBuffer][0])){
|
if(isdigit(buffer[currentlyInBuffer][0])){
|
||||||
if(currentlyInBuffer == 9) {
|
if(currentlyInBuffer == 9) {
|
||||||
printf("full stack\n");
|
printf("full stack\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for(int i = 0; i <= currentlyInBuffer; i++){
|
for(int i = 0; i <= currentlyInBuffer; i++){
|
||||||
if(i == currentlyInBuffer)
|
if(i == currentlyInBuffer)
|
||||||
printf("%0.2f \n", roundf(atof(buffer[i])*100)/100);
|
printf("%0.2f \n", roundf(atof(buffer[i])*100)/100);
|
||||||
else
|
else
|
||||||
printf("%0.2f ", roundf(atof(buffer[i])*100)/100);
|
printf("%0.2f ", roundf(atof(buffer[i])*100)/100);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if(strchr("+-/*", buffer[currentlyInBuffer][0]) != NULL){
|
else if(strchr("+-/*", buffer[currentlyInBuffer][0]) != NULL){
|
||||||
if(currentlyInBuffer < 2){
|
if(currentlyInBuffer < 2){
|
||||||
printf("not enough operands\n");
|
printf("not enough operands\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
double temporaryDecimal = 0;
|
double temporaryDecimal = 0;
|
||||||
|
|
||||||
switch(buffer[currentlyInBuffer][0]){
|
switch(buffer[currentlyInBuffer][0]){
|
||||||
case '+':
|
case '+':
|
||||||
temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) + atof(buffer[currentlyInBuffer-2]))*100)/100;
|
temporaryDecimal = round((atof(buffer[currentlyInBuffer-2]) + atof(buffer[currentlyInBuffer-1]))*100)/100;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) - atof(buffer[currentlyInBuffer-2]))*100)/100;
|
temporaryDecimal = round((atof(buffer[currentlyInBuffer-2]) - atof(buffer[currentlyInBuffer-1]))*100)/100;
|
||||||
break;
|
break;
|
||||||
case '*':
|
case '*':
|
||||||
temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) * atof(buffer[currentlyInBuffer-2]))*100)/100;
|
temporaryDecimal = round((atof(buffer[currentlyInBuffer-2]) * atof(buffer[currentlyInBuffer-1]))*100)/100;
|
||||||
break;
|
break;
|
||||||
case '/':
|
case '/':
|
||||||
if(atof(buffer[currentlyInBuffer-2]) == 0.0)
|
if(atof(buffer[currentlyInBuffer-1]) == 0.0)
|
||||||
printf("division by zero\n");
|
printf("division by zero\n");
|
||||||
else
|
else
|
||||||
temporaryDecimal = round((atof(buffer[currentlyInBuffer-1]) / atof(buffer[currentlyInBuffer-2]))*100)/100;
|
temporaryDecimal = round((atof(buffer[currentlyInBuffer-2]) / atof(buffer[currentlyInBuffer-1]))*100)/100;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = currentlyInBuffer-3; currentlyInBuffer > i; currentlyInBuffer--)
|
for(int i = currentlyInBuffer-3; currentlyInBuffer > i; currentlyInBuffer--)
|
||||||
memset(buffer[currentlyInBuffer], SIZE, '\0');
|
memset(buffer[currentlyInBuffer], SIZE, '\0');
|
||||||
|
|
||||||
gcvt(temporaryDecimal, 10, buffer[currentlyInBuffer]);
|
gcvt(temporaryDecimal, 10, buffer[currentlyInBuffer++]);
|
||||||
|
|
||||||
for(int i = 0; i <= currentlyInBuffer; i++){
|
for(int i = 0; i <= currentlyInBuffer; i++){
|
||||||
if(i == currentlyInBuffer)
|
if(i == currentlyInBuffer)
|
||||||
printf("%0.2f \n", roundf(atof(buffer[i]) * 100) / 100);
|
printf("%0.2f \n", roundf(atof(buffer[i]) * 100) / 100);
|
||||||
else
|
else
|
||||||
printf("%0.2f ", roundf(atof(buffer[i]) * 100) / 100);
|
printf("%0.2f ", roundf(atof(buffer[i]) * 100) / 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
printf("bad input\n");
|
printf("bad input\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char buffer[SIZE][SIZE];
|
char buffer[SIZE][SIZE];
|
||||||
for(int i = 0; i < SIZE; i++)
|
for(int i = 0; i < SIZE; i++)
|
||||||
memset(buffer[i], '\0', SIZE);
|
memset(buffer[i], '\0', SIZE);
|
||||||
|
|
||||||
for(; fgets(buffer[currentlyInBuffer], SIZE, stdin); currentlyInBuffer++)
|
for(; fgets(buffer[currentlyInBuffer], SIZE, stdin); currentlyInBuffer++)
|
||||||
if(!calculatorLogic(buffer))
|
if(!calculatorLogic(buffer))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if(buffer[currentlyInBuffer][0] == '\0')
|
if(buffer[currentlyInBuffer][0] == '\0')
|
||||||
printf("no input\n");
|
printf("no input\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user