#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char textMessage[512];
char stringResult[256];
char stringFirst[32];
char stringSecond[32];
char symbolOperation[] = "";
double operandFirst = 0, operandSecond = 0, operandResult = 0;
int identifierOperation = 0;
bool existenceFirstOperand = false, existenceSecondOperand = false, existenceResultOperand = false;
void onClickNumber(int num);
void onClickOperation(int id);
void onClickResult(void);
void Cleaning(void);
int main(int argc, char* argv[])
{
onClickNumber(5);
onClickOperation(13);
onClickNumber(10);
onClickResult();
printf("%s%s%s = %s\n", stringFirst, symbolOperation, stringSecond, stringResult);
printf("\n");
system("pause");
return EXIT_SUCCESS;
}
void onClickNumber(int num)
{
char str[32];
sprintf(str, "%d", num);
if (existenceResultOperand)
{
Cleaning();
onClickNumber(num);
return;
}
if (identifierOperation == 0)
{
if (operandFirst == 0) strcpy(stringFirst, str);
else strcat(stringFirst, str);
existenceFirstOperand = true;
operandFirst = atof(str);
}
else
{
if (operandSecond == 0) strcpy(stringSecond, str);
else strcat(stringSecond, str);
existenceSecondOperand = true;
operandSecond = atof(str);
}
existenceResultOperand = false;
strcpy(textMessage, stringFirst);
strcat(textMessage, symbolOperation);
strcat(textMessage, stringSecond);
}
void onClickOperation(int id)
{
if (existenceFirstOperand)
{
identifierOperation = id;
switch (id)
{
case 10: strcpy(symbolOperation, "/"); break;
case 11: strcpy(symbolOperation, "*"); break;
case 12: strcpy(symbolOperation, "-"); break;
case 13: strcpy(symbolOperation, "+"); break;
}
}
if (existenceResultOperand)
{
strcpy(stringFirst, stringResult);
operandSecond = 0; strcpy(stringSecond, ""); existenceSecondOperand = false;
}
operandResult = 0; strcpy(stringSecond, ""); existenceResultOperand = false;
strcpy(textMessage, stringFirst);
strcat(textMessage, symbolOperation);
strcat(textMessage, stringSecond);
}
void onClickResult(void)
{
if (existenceFirstOperand && existenceSecondOperand)
{
switch (identifierOperation)
{
case 10: operandResult = operandFirst /= operandSecond; break;
case 11: operandResult = operandFirst *= operandSecond; break;
case 12: operandResult = operandFirst -= operandSecond; break;
case 13: operandResult = operandFirst += operandSecond; break;
}
if ((int)operandResult == operandResult)
sprintf(stringResult, "%d", (int)operandResult);
else
sprintf(stringResult, "%f", operandResult);
existenceResultOperand = true;
}
}
void Cleaning(void)
{
strcpy(textMessage, "");
strcpy(stringResult, "");
strcpy(stringFirst, "");
strcpy(stringSecond, "");
strcpy(symbolOperation, "");
operandFirst = operandSecond = operandResult = 0;
identifierOperation = 0;
existenceFirstOperand = existenceSecondOperand = existenceResultOperand = false;
}