void Calculator::getResult()
{
while(!vectorExp.isEmpty())
{
char ch = vectorExp.first();
vectorExp.removeFirst();
getDigital(ch);
getSign(ch);
}
}
void Calculator::getDigital(char digital)
{
if(digital == '0' || digital == '1' || digital == '2'
|| digital == '3' || digital == '4' || digital == '5'
|| digital == '6' || digital == '7' || digital == '8'
|| digital == '9' )
{
if(isPoint)
i *= 10;//i初值是1
j = 0;//j标记是否连续字符输入
sum = sum * 10 + (digital - '0');
isLArc = false;
}
else if(digital == '.')
{
isPoint = true;
isLArc = false;
}
}
void Calculator::getSign(char ch)
{
if(isLArc && ch != '(' && ch != ')')
{
sign = ch;
}
else if(ch == '+' || ch == '-'
|| ch == '*' || ch == '/'
|| ch == '=' || ch == '('
|| ch == ')')
{
if(ch == '(')
isLArc = true;
else
isLArc = false;
j++;
if(j <=1)
{
if(sign == '-')
sum = sum * (-1);
sign = ' ';
vectorOpnd.append(sum / i);
isLArc = false;
i = 1;
sum = 0;
isPoint = false;
}
pickSign(ch);
}
}
bool Calculator::pickSign(char ch)
{
char theta;
double a, b;
if(ch == '=')
{
while(vectorOptr.last() != '#')
{
do{
theta = vectorOptr.last();
vectorOptr.pop_back();
}while(theta == '(' || theta == ')');
if(theta != '#')//void 8(,9),8()
{
b = vectorOpnd.last();vectorOpnd.pop_back();
a = vectorOpnd.last();vectorOpnd.pop_back();
vectorOpnd.append(operate(a, theta, b));
}
else if(theta == '#')
vectorOptr.append('#');
}
result = vectorOpnd.last();
screenTextEdit->insertPlainText(QString("%1").arg(result));
vectorOpnd.clear();
vectorOptr.clear();
initVector();
isEnter = false;
return true;
}
pushVector(ch);
return true;
}
bool Calculator::pushVector(char optr)
{
char theta;
double a, b;
if(optr == ')')
{
do{
theta = vectorOptr.last();
vectorOptr.pop_back();
}while(theta == '(' || theta == ')');
if(theta != '#')// void 8(,9),8()
{
b = vectorOpnd.last();vectorOpnd.pop_back();
a = vectorOpnd.last();vectorOpnd.pop_back();
vectorOpnd.append(operate(a, theta, b));
}
else
{
vectorOptr.append('#');
}
return true;
}
switch(precedeSign(vectorOptr.last(),optr))
{
case '<':
vectorOptr.append(optr);
break;
case '=':
vectorOptr.pop_back();
break;
case '>':
theta = vectorOptr.last();
vectorOptr.pop_back();
b = vectorOpnd.last();
vectorOpnd.pop_back();
a = vectorOpnd.last();
vectorOpnd.pop_back();
vectorOpnd.append(operate(a, theta, b));
vectorOptr.append(optr);
break;
}
return true;
}
void Calculator::clearScreen()
{
screenTextEdit->clear();
result = 0;
isEnter = true;
}