code format

This commit is contained in:
2022-12-14 14:14:33 +03:00
parent 09e5342956
commit cdb02fc9be
278 changed files with 15371 additions and 12176 deletions

View File

@@ -32,8 +32,8 @@ QPIEvaluatorContent::QPIEvaluatorContent() {
addFunction("rad", 1);
addFunction("deg", 1);
clearCustomVariables();
//addVariable("n", 0.);
//addVariable("x1", 123);
// addVariable("n", 0.);
// addVariable("x1", 123);
}
@@ -64,18 +64,18 @@ void QPIEvaluatorContent::clearCustomVariables() {
void QPIEvaluatorContent::sortVariables() {
var_index.clear();
#if QT_VERSION < QT_VERSION_CHECK(5,13,0)
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
qSort
#else
std::sort
#endif
(variables.begin(), variables.end());
(variables.begin(), variables.end());
for (int i = 0; i < variables.size(); i++)
var_index[variables[i].name] = i;
/*
qDebug() << "---";
for (int i = 0; i < variables.size(); i++) {
qDebug() << variables[i].name;
qDebug() << variables[i].name;
}
*/
}
@@ -120,8 +120,10 @@ const QString & QPIEvaluator::prepare(const QString & string) {
replaceOperators();
removeSpaces();
checkBrackets();
while (fillElements()) checkBrackets();
while (setSignes()) fillElements();
while (fillElements())
checkBrackets();
while (setSignes())
fillElements();
removeJunk();
findUnknownVariables();
return currentString;
@@ -159,7 +161,8 @@ void QPIEvaluator::removeJunk() {
elements.pop_back();
junk = true;
break;
} else break;
} else
break;
}
}
}
@@ -189,7 +192,8 @@ void QPIEvaluator::findUnknownVariables() {
QString cvar;
unknownVars.clear();
for (int i = 0; i < currentString.length(); i++) {
if (elements[i].var_num == -666) cvar += currentString[i];
if (elements[i].var_num == -666)
cvar += currentString[i];
else {
if (cvar.length() == 0) continue;
unknownVars << cvar;
@@ -202,13 +206,8 @@ void QPIEvaluator::findUnknownVariables() {
bool QPIEvaluator::isSign(const QChar & ch) {
return ch == '+' || ch == '-' ||
ch == '*' || ch == '/' ||
ch == '%' || ch == '^' ||
ch == '=' || ch == ':' ||
ch == '>' || ch == '<' ||
ch == '}' || ch == '{' ||
ch == '&' || ch == '|';
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^' || ch == '=' || ch == ':' || ch == '>' || ch == '<' ||
ch == '}' || ch == '{' || ch == '&' || ch == '|';
}
@@ -226,7 +225,8 @@ void QPIEvaluator::checkBrackets() {
if (bcnt == 0) {
currentString.insert(bpos + inserted, "(");
inserted++;
} else bcnt--;
} else
bcnt--;
}
}
if (bcnt > 0) currentString += QString(bcnt, ')');
@@ -234,9 +234,8 @@ void QPIEvaluator::checkBrackets() {
for (int i = 0; i < tmps.length() - 1; i++) {
fc = tmps[i].toLower();
sc = tmps[i + 1].toLower();
if ((fc == ')' && sc == '(') ||
(fc == ')' && sc >= '0' && sc <= '9') ||
(fc == ')' && sc >= 'a' && sc <= 'z') ) tmps.insert(++i, '*');
if ((fc == ')' && sc == '(') || (fc == ')' && sc >= '0' && sc <= '9') || (fc == ')' && sc >= 'a' && sc <= 'z'))
tmps.insert(++i, '*');
}
currentString = tmps;
}
@@ -244,26 +243,26 @@ void QPIEvaluator::checkBrackets() {
bool QPIEvaluator::fillElements() {
int fstart, flen, cnum = 0, cpart = 0, cfunc;
QChar cc, nc, pc, fc = '!';
QChar cc, nc, pc, fc = '!';
bool numFound = false;
QString curfind, tmps = currentString;
elements.resize(tmps.length());
for (int i = 0; i < elements.size(); i++) {
elements[i].type = QPIEvaluatorTypes::etVariable;
elements[i].type = QPIEvaluatorTypes::etVariable;
elements[i].var_num = -666;
}
currentVariables.clear();
//qDebug().nospace() << "search for functions ...";
// qDebug().nospace() << "search for functions ...";
for (int i = 0; i < content.functionsCount(); i++) {
curfind = content.function(i).identifier;
cfunc = i; //(int)content.function(i).type;
flen = curfind.length();
fstart = 0;
cfunc = i; //(int)content.function(i).type;
flen = curfind.length();
fstart = 0;
while (fstart >= 0) {
fstart = tmps.indexOf(curfind, fstart);
if (fstart < 0) break;
if (tmps[fstart + flen] != '(') {
//currentString.insert(fstart + flen, "(");
// currentString.insert(fstart + flen, "(");
fstart++;
continue;
}
@@ -275,11 +274,11 @@ bool QPIEvaluator::fillElements() {
}
}
cnum = 0;
//qDebug().nospace() << "search for variables ...";
// qDebug().nospace() << "search for variables ...";
for (int i = 0; i < content.variablesCount(); i++) {
curfind = content.variable(i).name;
flen = curfind.length();
fstart = 0;
flen = curfind.length();
fstart = 0;
while (fstart >= 0) {
fstart = tmps.indexOf(curfind, fstart);
if (fstart < 0) break;
@@ -291,19 +290,19 @@ bool QPIEvaluator::fillElements() {
}
}
curfind = "";
cnum = 1;
//qDebug().nospace() << "search for numbers ...";
cnum = 1;
// qDebug().nospace() << "search for numbers ...";
for (int i = 0; i < tmps.length(); i++) {
cc = tmps[i];
/*if (cc == " " || cc == "(" || cc == ")") {
curfind = "";
cpart = 0;
numFound = false;
continue;
curfind = "";
cpart = 0;
numFound = false;
continue;
}*/
switch (cpart) {
case 0:
if ((cc >= '0' && cc <= '9')) {// || cc == '-' || cc == '+') {
if ((cc >= '0' && cc <= '9')) { // || cc == '-' || cc == '+') {
curfind += cc;
cpart = 1;
continue;
@@ -365,7 +364,7 @@ bool QPIEvaluator::fillElements() {
break;
}
if (numFound) {
//qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
// qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
currentVariables.push_back(QPIEvaluatorTypes::Variable("tmp" + QString::number(cnum), curfind.toDouble()));
for (int j = i - curfind.length(); j < i; j++) {
elements[j].set(QPIEvaluatorTypes::etNumber, cnum, -cnum);
@@ -373,12 +372,12 @@ bool QPIEvaluator::fillElements() {
}
curfind = "";
cnum++;
cpart = 0;
cpart = 0;
numFound = false;
}
}
if (cpart > 0) {
//qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
// qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
currentVariables.push_back(QPIEvaluatorTypes::Variable("tmp" + QString::number(cnum), curfind.toDouble()));
for (int j = tmps.length() - curfind.length(); j < tmps.length(); j++) {
elements[j].set(QPIEvaluatorTypes::etNumber, cnum, -cnum);
@@ -386,30 +385,34 @@ bool QPIEvaluator::fillElements() {
}
}
cc = nc = fc;
//qDebug().nospace() << "search for signes ...";
// qDebug().nospace() << "search for signes ...";
for (int i = 0; i < tmps.length(); i++) {
cc = tmps[i];
if (i > 0) pc = tmps[i - 1];
else pc = fc;
if (i < tmps.length() - 1) nc = tmps[i + 1];
else nc = fc;
if (i > 0)
pc = tmps[i - 1];
else
pc = fc;
if (i < tmps.length() - 1)
nc = tmps[i + 1];
else
nc = fc;
if (cc == '(' || cc == ')' || cc == ',') {
elements[i].set(QPIEvaluatorTypes::etOperator, -1);
continue;
}
if (cc == '-' || cc == '+') {
elements[i].set(QPIEvaluatorTypes::etOperator, -1);
if (i < tmps.length() - 1) if (elements[i + 1].type == QPIEvaluatorTypes::etVariable ||
elements[i + 1].type == QPIEvaluatorTypes::etFunction) continue;
if (i < tmps.length() - 1)
if (elements[i + 1].type == QPIEvaluatorTypes::etVariable || elements[i + 1].type == QPIEvaluatorTypes::etFunction)
continue;
if ((pc == '(' || isSign(pc) || i == 0) && i < tmps.length() - 1) {
if (elements[i + 1].type != QPIEvaluatorTypes::etOperator) {
cnum = elements[i + 1].num;
elements[i].set(QPIEvaluatorTypes::etNumber, cnum);
tmps.replace(i, 1, fc);
///cout << "found sign " << cc << " :" << cnum - 1 << endl;
if (cc == '-' && currentVariables.size() >= cnum)
currentVariables[cnum - 1].value = -currentVariables[cnum - 1].value;
//i++;
/// cout << "found sign " << cc << " :" << cnum - 1 << endl;
if (cc == '-' && currentVariables.size() >= cnum) currentVariables[cnum - 1].value = -currentVariables[cnum - 1].value;
// i++;
continue;
}
}
@@ -423,17 +426,17 @@ bool QPIEvaluator::fillElements() {
qDebug().nospace() << tmps;
cout << " ";
for (int i = 0; i < elements.size(); i++) {
switch (elements[i].type) {
case etFunction: cout << "f"; break;
case etNumber: cout << "n"; break;
case etOperator: cout << "o"; break;
case etVariable: cout << "v"; break;
}
switch (elements[i].type) {
case etFunction: cout << "f"; break;
case etNumber: cout << "n"; break;
case etOperator: cout << "o"; break;
case etVariable: cout << "v"; break;
}
}
cout << endl;
*/
return false;
//for (int i = 0; i < currentVariables.size(); i++) qDebug() << "var " << i << ": " << currentVariables[i].value.real();
// for (int i = 0; i < currentVariables.size(); i++) qDebug() << "var " << i << ": " << currentVariables[i].value.real();
}
@@ -443,29 +446,45 @@ bool QPIEvaluator::setSignes() {
QString tmps = currentString;
for (int i = 0; i < tmps.length() - 1; i++) {
needInsert = 0;
ni = i + 1;
ni = i + 1;
if (i > 0) pi = i - 1;
fc = tmps[i].toLower();
sc = tmps[ni].toLower();
pc = tmps[pi].toLower();
//if (elements[i].type == etOperator || elements[ni].type == etVariable) continue;
// if (elements[i].type == etOperator || elements[ni].type == etVariable) continue;
if (fc == ',' || sc == ',') continue;
if (elements[i].type == QPIEvaluatorTypes::etOperator && elements[ni].type == QPIEvaluatorTypes::etOperator) continue;
if (fc == ')' && (elements[ni].type == QPIEvaluatorTypes::etNumber || elements[ni].type == QPIEvaluatorTypes::etVariable || elements[ni].type == QPIEvaluatorTypes::etFunction)) needInsert = 1;
if (sc == '(' && (elements[i].type == QPIEvaluatorTypes::etNumber || elements[i].type == QPIEvaluatorTypes::etVariable)) needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etNumber && elements[ni].type == QPIEvaluatorTypes::etNumber && elements[i].num != elements[ni].num) needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etVariable && elements[ni].type == QPIEvaluatorTypes::etVariable && elements[i].num != elements[ni].num) needInsert = 1;
if ((elements[i].type == QPIEvaluatorTypes::etNumber && elements[ni].type == QPIEvaluatorTypes::etVariable) || (elements[i].type == QPIEvaluatorTypes::etVariable && elements[ni].type == QPIEvaluatorTypes::etNumber)) needInsert = 1;
if ((elements[i].type == QPIEvaluatorTypes::etNumber || elements[i].type == QPIEvaluatorTypes::etVariable) && elements[ni].type == QPIEvaluatorTypes::etFunction) needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etFunction && elements[ni].type == QPIEvaluatorTypes::etFunction && elements[i].num != elements[ni].num) needInsert = 2;
if (elements[i].type == QPIEvaluatorTypes::etFunction && elements[ni].type != QPIEvaluatorTypes::etFunction && sc != '(') needInsert = 2;
if (elements[pi].type == QPIEvaluatorTypes::etOperator && (elements[ni].type == QPIEvaluatorTypes::etFunction || elements[ni].type == QPIEvaluatorTypes::etVariable) && fc == '-') needInsert = 3;
if (fc == ')' && (elements[ni].type == QPIEvaluatorTypes::etNumber || elements[ni].type == QPIEvaluatorTypes::etVariable ||
elements[ni].type == QPIEvaluatorTypes::etFunction))
needInsert = 1;
if (sc == '(' && (elements[i].type == QPIEvaluatorTypes::etNumber || elements[i].type == QPIEvaluatorTypes::etVariable))
needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etNumber && elements[ni].type == QPIEvaluatorTypes::etNumber &&
elements[i].num != elements[ni].num)
needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etVariable && elements[ni].type == QPIEvaluatorTypes::etVariable &&
elements[i].num != elements[ni].num)
needInsert = 1;
if ((elements[i].type == QPIEvaluatorTypes::etNumber && elements[ni].type == QPIEvaluatorTypes::etVariable) ||
(elements[i].type == QPIEvaluatorTypes::etVariable && elements[ni].type == QPIEvaluatorTypes::etNumber))
needInsert = 1;
if ((elements[i].type == QPIEvaluatorTypes::etNumber || elements[i].type == QPIEvaluatorTypes::etVariable) &&
elements[ni].type == QPIEvaluatorTypes::etFunction)
needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etFunction && elements[ni].type == QPIEvaluatorTypes::etFunction &&
elements[i].num != elements[ni].num)
needInsert = 2;
if (elements[i].type == QPIEvaluatorTypes::etFunction && elements[ni].type != QPIEvaluatorTypes::etFunction && sc != '(')
needInsert = 2;
if (elements[pi].type == QPIEvaluatorTypes::etOperator &&
(elements[ni].type == QPIEvaluatorTypes::etFunction || elements[ni].type == QPIEvaluatorTypes::etVariable) && fc == '-')
needInsert = 3;
switch (needInsert) {
case 1:
currentString.insert(ni + inserted, "*");
elements.insert(ni + inserted, QPIEvaluatorTypes::Element(QPIEvaluatorTypes::etOperator, -1));
//inserted++;
//i++;
// inserted++;
// i++;
return true;
/*case 2:
currentString.insert(ni + inserted, ")");
@@ -478,17 +497,17 @@ bool QPIEvaluator::setSignes() {
case 3:
currentString.insert(ni + inserted, "1*");
elements.insert(ni + inserted, QPIEvaluatorTypes::Element(QPIEvaluatorTypes::etOperator, -1));
//inserted;
//i++;
// inserted;
// i++;
return true;
}
}
/*if (elements[tmps.length() - 1].type == etFunction) {
currentString.insert(tmps.length() + inserted, ")");
currentString.insert(tmps.length() + inserted, "(");
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
return true;
currentString.insert(tmps.length() + inserted, ")");
currentString.insert(tmps.length() + inserted, "(");
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
return true;
}*/
return false;
}
@@ -507,8 +526,9 @@ void QPIEvaluator::convert() {
j++;
}
currentString.replace(i, j - i, " ");
for (int k = i + 1; k < j; k++) elements.remove(i);
//i++;
for (int k = i + 1; k < j; k++)
elements.remove(i);
// i++;
}
for (int i = 0; i < currentString.length(); i++) {
pe = elements[i];
@@ -520,8 +540,9 @@ void QPIEvaluator::convert() {
j++;
}
currentString.replace(i, j - i, " ");
for (int k = i + 1; k < j; k++) elements.remove(i);
//i++;
for (int k = i + 1; k < j; k++)
elements.remove(i);
// i++;
}
for (int i = 0; i < currentString.length(); i++) {
pe = elements[i];
@@ -533,33 +554,33 @@ void QPIEvaluator::convert() {
j++;
}
currentString.replace(i, j - i, " ");
for (int k = i + 1; k < j; k++) elements.remove(i);
//i++;
for (int k = i + 1; k < j; k++)
elements.remove(i);
// i++;
}
/*qDebug().nospace() << currentString;
cout << " ";
for (int i = 0; i < elements.size(); i++) {
switch (elements[i].type) {
case etFunction: cout << "f"; break;
case etNumber: cout << "n"; break;
case etOperator: cout << "o"; break;
case etVariable: cout << "v"; break;
}
switch (elements[i].type) {
case etFunction: cout << "f"; break;
case etNumber: cout << "n"; break;
case etOperator: cout << "o"; break;
case etVariable: cout << "v"; break;
}
}
cout << endl;*/
}
const QString & QPIEvaluator::preprocess(const QString & string) {
static QString ret;
int lind;
ret = prepare(string);
convert();
instructions.clear();
//qDebug() << preproc->currentString;
// qDebug() << preproc->currentString;
variables = currentVariables;
lind = parse(currentString);
lind = parse(currentString);
if (instructions.size() == 0) {
variables.push_back(QPIEvaluatorTypes::Variable());
instructions.push_back(QPIEvaluatorTypes::Instruction(QPIEvaluatorTypes::oNone, QVector<int>(1, lind), -variables.size()));
@@ -568,17 +589,17 @@ const QString & QPIEvaluator::preprocess(const QString & string) {
/*
cout << endl << "variables:" << endl;
for (int i = 0; i < variables.size(); i++)
cout << i << " value = " << variables[i].value << endl;
cout << i << " value = " << variables[i].value << endl;
cout << endl << "instructions:" << endl;
for (int i = 0; i < instructions.size(); i++) {
cout << i << endl;
cout << " operation " << instructions[i].operation << endl;
cout << " operators: ";
for (int j = 0; j < instructions[i].operators.size(); j++)
cout << instructions[i].operators[j] << "; ";
cout << endl << " function " << instructions[i].function << endl;
cout << " out " << instructions[i].out << endl;
cout << i << endl;
cout << " operation " << instructions[i].operation << endl;
cout << " operators: ";
for (int j = 0; j < instructions[i].operators.size(); j++)
cout << instructions[i].operators[j] << "; ";
cout << endl << " function " << instructions[i].function << endl;
cout << " out " << instructions[i].out << endl;
}
*/
makeOutput(ret);
@@ -616,53 +637,51 @@ int QPIEvaluator::parse(const QString & string, int offset) {
QVector<int> args, atmp;
QVector<QPIEvaluatorTypes::Operation> opers;
///qDebug() << "to parse :" + string;
///cout << " "; for (int i = 0; i < slen; i++) cout << preproc->elements[i + offset].type; cout << endl;
/// qDebug() << "to parse :" + string;
/// cout << " "; for (int i = 0; i < slen; i++) cout << preproc->elements[i + offset].type; cout << endl;
for (int i = 0; i < slen; i++) {
ce = elements[i + offset];
cc = string[i];
switch (ce.type) {
case QPIEvaluatorTypes::etNumber:
args.push_back(ce.var_num);
continue;
case QPIEvaluatorTypes::etVariable:
args.push_back(ce.var_num);
continue;
case QPIEvaluatorTypes::etNumber: args.push_back(ce.var_num); continue;
case QPIEvaluatorTypes::etVariable: args.push_back(ce.var_num); continue;
case QPIEvaluatorTypes::etFunction:
i++;
cfunc = content.function(ce.var_num);
//facnt = cfunc.arguments;
// facnt = cfunc.arguments;
atmp.clear();
bcnt = farg = 1;
///qDebug() << "function: " + cfunc.identifier;
//for (int k = 0; k < facnt; k++) {
carg = "";
k = i + 1;
//if (string.size() <= k || k < 0) return -666;
/// qDebug() << "function: " + cfunc.identifier;
// for (int k = 0; k < facnt; k++) {
carg = "";
k = i + 1;
// if (string.size() <= k || k < 0) return -666;
while (bcnt > 0) {
//if (k < facnt - 1) fcomma = string.indexOf(',', j);
// if (k < facnt - 1) fcomma = string.indexOf(',', j);
cc = string[k];
switch (cc.toLatin1()) {
case '(': bcnt++; break;
case ')':
bcnt--;
if (bcnt == 0) {
///qDebug() << "arument: " << carg;
/// qDebug() << "arument: " << carg;
atmp.push_back(parse(carg, k + offset - carg.length()));
k++;
carg = "";
if (atmp.size() > 0) if (atmp.back() < 0 && farg > 0) farg = atmp.back();
if (atmp.size() > 0)
if (atmp.back() < 0 && farg > 0) farg = atmp.back();
continue;
}
break;
case ',':
if (bcnt == 1) {
///qDebug() << "arument: " << carg;
/// qDebug() << "arument: " << carg;
atmp.push_back(parse(carg, k + offset - carg.length()));
k++;
carg = "";
if (atmp.size() > 0) if (atmp.back() < 0 && farg > 0) farg = atmp.back();
if (atmp.size() > 0)
if (atmp.back() < 0 && farg > 0) farg = atmp.back();
continue;
}
break;
@@ -677,31 +696,73 @@ int QPIEvaluator::parse(const QString & string, int offset) {
}
instructions.push_back(QPIEvaluatorTypes::Instruction(QPIEvaluatorTypes::oFunction, atmp, farg, ce.var_num));
args.push_back(farg);
//for (int i = 0; i < args.size(); i++) cout << preproc->currentVariables[-args[i]].value << endl;
//i = j + 1;
// for (int i = 0; i < args.size(); i++) cout << preproc->currentVariables[-args[i]].value << endl;
// i = j + 1;
continue;
case QPIEvaluatorTypes::etOperator:
//qDebug() << "operator: " << cc;
// qDebug() << "operator: " << cc;
if (cc == '(') {
sbrackets = inBrackets(string.right(slen - i));
args.push_back(parse(sbrackets, i + offset + 1));
i += sbrackets.length() + 1;
continue;
}
if (cc == '+') {opers.push_back(QPIEvaluatorTypes::oAdd); continue;}
if (cc == '-') {opers.push_back(QPIEvaluatorTypes::oSubtract); continue;}
if (cc == '*') {opers.push_back(QPIEvaluatorTypes::oMultiply); continue;}
if (cc == '/') {opers.push_back(QPIEvaluatorTypes::oDivide); continue;}
if (cc == '%') {opers.push_back(QPIEvaluatorTypes::oResidue); continue;}
if (cc == '^') {opers.push_back(QPIEvaluatorTypes::oPower); continue;}
if (cc == '=') {opers.push_back(QPIEvaluatorTypes::oEqual); continue;}
if (cc == ':') {opers.push_back(QPIEvaluatorTypes::oNotEqual); continue;}
if (cc == '}') {opers.push_back(QPIEvaluatorTypes::oGreaterEqual); continue;}
if (cc == '{') {opers.push_back(QPIEvaluatorTypes::oSmallerEqual); continue;}
if (cc == '>') {opers.push_back(QPIEvaluatorTypes::oGreater); continue;}
if (cc == '<') {opers.push_back(QPIEvaluatorTypes::oSmaller); continue;}
if (cc == '&') {opers.push_back(QPIEvaluatorTypes::oAnd); continue;}
if (cc == '|') {opers.push_back(QPIEvaluatorTypes::oOr); continue;}
if (cc == '+') {
opers.push_back(QPIEvaluatorTypes::oAdd);
continue;
}
if (cc == '-') {
opers.push_back(QPIEvaluatorTypes::oSubtract);
continue;
}
if (cc == '*') {
opers.push_back(QPIEvaluatorTypes::oMultiply);
continue;
}
if (cc == '/') {
opers.push_back(QPIEvaluatorTypes::oDivide);
continue;
}
if (cc == '%') {
opers.push_back(QPIEvaluatorTypes::oResidue);
continue;
}
if (cc == '^') {
opers.push_back(QPIEvaluatorTypes::oPower);
continue;
}
if (cc == '=') {
opers.push_back(QPIEvaluatorTypes::oEqual);
continue;
}
if (cc == ':') {
opers.push_back(QPIEvaluatorTypes::oNotEqual);
continue;
}
if (cc == '}') {
opers.push_back(QPIEvaluatorTypes::oGreaterEqual);
continue;
}
if (cc == '{') {
opers.push_back(QPIEvaluatorTypes::oSmallerEqual);
continue;
}
if (cc == '>') {
opers.push_back(QPIEvaluatorTypes::oGreater);
continue;
}
if (cc == '<') {
opers.push_back(QPIEvaluatorTypes::oSmaller);
continue;
}
if (cc == '&') {
opers.push_back(QPIEvaluatorTypes::oAnd);
continue;
}
if (cc == '|') {
opers.push_back(QPIEvaluatorTypes::oOr);
continue;
}
}
}
/*
@@ -711,25 +772,33 @@ int QPIEvaluator::parse(const QString & string, int offset) {
for (int i = 0; i < opers.size(); i++) cout << opers[i] << ", ";
*/
if (opers.size() == 0) {
if (args.size() > 0) return args.back();
else return -666;
if (args.size() > 0)
return args.back();
else
return -666;
}
int oprior = -1;
QVector<QPIEvaluatorTypes::Operation> opv;
while(1) {
while (1) {
operationsByPriority(++oprior, opv);
if (opv.isEmpty()) break;
for (int j = 0; j < opers.size(); j++) {
if (!opv.contains(opers[j])) continue;
atmp.clear();
if (j < args.size() && j >= 0) atmp.push_back(args[j]);
else atmp.push_back(-666);
if (j + 1 < args.size() && j >= -1) atmp.push_back(args[j + 1]);
else atmp.push_back(-666);
if (j < args.size() && j >= 0)
atmp.push_back(args[j]);
else
atmp.push_back(-666);
if (j + 1 < args.size() && j >= -1)
atmp.push_back(args[j + 1]);
else
atmp.push_back(-666);
farg = 1;
if (atmp[0] < 0) farg = atmp[0];
if (atmp[0] < 0)
farg = atmp[0];
else {
if (atmp[1] < 0) farg = atmp[1];
if (atmp[1] < 0)
farg = atmp[1];
else {
variables.push_back(QPIEvaluatorTypes::Variable());
farg = -variables.size();
@@ -745,7 +814,7 @@ int QPIEvaluator::parse(const QString & string, int offset) {
}
}
return instructions.back().out;
///cout << endl;
/// cout << endl;
}
@@ -758,7 +827,7 @@ bool QPIEvaluator::check() {
}
for (int i = 0; i < instructions.size(); i++) {
error = false;
ci = instructions[i];
ci = instructions[i];
switch (ci.operation) {
case QPIEvaluatorTypes::oNone: break;
case QPIEvaluatorTypes::oFunction:
@@ -838,9 +907,10 @@ void QPIEvaluator::operationsByPriority(int p, QVector<QPIEvaluatorTypes::Operat
case 0: ret << QPIEvaluatorTypes::oPower; break;
case 1: ret << QPIEvaluatorTypes::oMultiply << QPIEvaluatorTypes::oDivide << QPIEvaluatorTypes::oResidue; break;
case 2: ret << QPIEvaluatorTypes::oAdd << QPIEvaluatorTypes::oSubtract; break;
case 3: ret << QPIEvaluatorTypes::oEqual << QPIEvaluatorTypes::oNotEqual
<< QPIEvaluatorTypes::oGreaterEqual << QPIEvaluatorTypes::oSmallerEqual
<< QPIEvaluatorTypes::oGreater << QPIEvaluatorTypes::oSmaller; break;
case 3:
ret << QPIEvaluatorTypes::oEqual << QPIEvaluatorTypes::oNotEqual << QPIEvaluatorTypes::oGreaterEqual
<< QPIEvaluatorTypes::oSmallerEqual << QPIEvaluatorTypes::oGreater << QPIEvaluatorTypes::oSmaller;
break;
case 4: ret << QPIEvaluatorTypes::oAnd; break;
case 5: ret << QPIEvaluatorTypes::oOr; break;
default: break;
@@ -858,105 +928,63 @@ inline complexd QPIEvaluator::residue(const complexd & f, const complexd & s) {
inline void QPIEvaluator::execFunction(const QPIEvaluatorTypes::Instruction & ci) {
QPIEvaluatorTypes::Function cfunc = content.function(ci.function);
int oi = -ci.out - 1;
int oi = -ci.out - 1;
complexd tmp, stmp;
double ldtmp;
//qDebug() << "function " << (int)cfunc.type;
// qDebug() << "function " << (int)cfunc.type;
switch (cfunc.type) {
case QPIEvaluatorTypes::bfSin:
tmpvars[oi].value = sin(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfCos:
tmpvars[oi].value = cos(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfTg:
tmpvars[oi].value = tan(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfSin: tmpvars[oi].value = sin(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfCos: tmpvars[oi].value = cos(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfTg: tmpvars[oi].value = tan(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfCtg:
tmp = tan(value(ci.operators[0]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfArcsin:
tmpvars[oi].value = asinc_qpie(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfArccos:
tmpvars[oi].value = acosc_qpie(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfArctg:
tmpvars[oi].value = atanc_qpie(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfArcctg:
tmpvars[oi].value = atanc_qpie(-value(ci.operators[0])) + M_PI_2;
break;
case QPIEvaluatorTypes::bfSh:
tmpvars[oi].value = sinh(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfCh:
tmpvars[oi].value = cosh(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfTh:
tmpvars[oi].value = tanh(value(ci.operators[0]));
if (tmp == complexd_0)
tmpvars[oi].value = 0.;
else
tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfArcsin: tmpvars[oi].value = asinc_qpie(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfArccos: tmpvars[oi].value = acosc_qpie(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfArctg: tmpvars[oi].value = atanc_qpie(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfArcctg: tmpvars[oi].value = atanc_qpie(-value(ci.operators[0])) + M_PI_2; break;
case QPIEvaluatorTypes::bfSh: tmpvars[oi].value = sinh(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfCh: tmpvars[oi].value = cosh(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfTh: tmpvars[oi].value = tanh(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfCth:
tmp = tanh(value(ci.operators[0]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfAbs:
tmpvars[oi].value = abs(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfSqrt:
tmpvars[oi].value = sqrt(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfSqr:
tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[0]);
break;
case QPIEvaluatorTypes::bfExp:
tmpvars[oi].value = exp(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfPow:
tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1]));
break;
case QPIEvaluatorTypes::bfLn:
tmpvars[oi].value = log(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfLg:
tmpvars[oi].value = log10(value(ci.operators[0]));
if (tmp == complexd_0)
tmpvars[oi].value = 0.;
else
tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfAbs: tmpvars[oi].value = abs(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfSqrt: tmpvars[oi].value = sqrt(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfSqr: tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[0]); break;
case QPIEvaluatorTypes::bfExp: tmpvars[oi].value = exp(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfPow: tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1])); break;
case QPIEvaluatorTypes::bfLn: tmpvars[oi].value = log(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfLg: tmpvars[oi].value = log10(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfLog:
tmp = log(value(ci.operators[1]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = log(value(ci.operators[0])) / tmp;
break;
case QPIEvaluatorTypes::bfRe:
tmpvars[oi].value = value(ci.operators[0]).real();
break;
case QPIEvaluatorTypes::bfIm:
tmpvars[oi].value = value(ci.operators[0]).imag();
break;
case QPIEvaluatorTypes::bfArg:
tmpvars[oi].value = arg(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfLen:
tmpvars[oi].value = abs(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfConj:
tmpvars[oi].value = conj(value(ci.operators[0]));
if (tmp == complexd_0)
tmpvars[oi].value = 0.;
else
tmpvars[oi].value = log(value(ci.operators[0])) / tmp;
break;
case QPIEvaluatorTypes::bfRe: tmpvars[oi].value = value(ci.operators[0]).real(); break;
case QPIEvaluatorTypes::bfIm: tmpvars[oi].value = value(ci.operators[0]).imag(); break;
case QPIEvaluatorTypes::bfArg: tmpvars[oi].value = arg(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfLen: tmpvars[oi].value = abs(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfConj: tmpvars[oi].value = conj(value(ci.operators[0])); break;
case QPIEvaluatorTypes::bfSign:
ldtmp = value(ci.operators[0]).real();
ldtmp = value(ci.operators[0]).real();
tmpvars[oi].value = ldtmp >= 0. ? complexd_1 : -complexd_1;
break;
case QPIEvaluatorTypes::bfRad:
tmpvars[oi].value = value(ci.operators[0]) * complexd(deg2rad_qpie, 0.);
break;
case QPIEvaluatorTypes::bfDeg:
tmpvars[oi].value = value(ci.operators[0]) * complexd(rad2deg_qpie, 0.);
break;
case QPIEvaluatorTypes::bfRad: tmpvars[oi].value = value(ci.operators[0]) * complexd(deg2rad_qpie, 0.); break;
case QPIEvaluatorTypes::bfDeg: tmpvars[oi].value = value(ci.operators[0]) * complexd(rad2deg_qpie, 0.); break;
case QPIEvaluatorTypes::bfRandom:
tmp = static_cast<double>(rand()) / RAND_MAX;
stmp = value(ci.operators[1]) - value(ci.operators[0]);
tmp = static_cast<double>(rand()) / RAND_MAX;
stmp = value(ci.operators[1]) - value(ci.operators[0]);
tmpvars[oi].value = value(ci.operators[0]) + tmp * stmp;
break;
default: break;
@@ -969,75 +997,45 @@ inline bool QPIEvaluator::execInstructions() {
int oi;
complexd tmp;
tmpvars = variables;
//cout << "var count " << tmpvars.size() << endl;
// cout << "var count " << tmpvars.size() << endl;
for (int i = 0; i < instructions.size(); i++) {
ci = instructions[i];
oi = -ci.out - 1;
//cout << value(ci.operators[0]) << operationChar(ci.operation) << value(ci.operators[1]) << ", " << oi << endl;
// cout << value(ci.operators[0]) << operationChar(ci.operation) << value(ci.operators[1]) << ", " << oi << endl;
switch (ci.operation) {
case QPIEvaluatorTypes::oAdd:
tmpvars[oi].value = value(ci.operators[0]) + value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oSubtract:
tmpvars[oi].value = value(ci.operators[0]) - value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oMultiply:
tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oAdd: tmpvars[oi].value = value(ci.operators[0]) + value(ci.operators[1]); break;
case QPIEvaluatorTypes::oSubtract: tmpvars[oi].value = value(ci.operators[0]) - value(ci.operators[1]); break;
case QPIEvaluatorTypes::oMultiply: tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[1]); break;
case QPIEvaluatorTypes::oDivide:
tmp = value(ci.operators[1]);
if (tmp == complexd(0., 0.)) tmpvars[oi].value = 0.;
else tmpvars[oi].value = value(ci.operators[0]) / tmp;
break;
case QPIEvaluatorTypes::oResidue:
tmpvars[oi].value = residue(value(ci.operators[0]), value(ci.operators[1]));
break;
case QPIEvaluatorTypes::oPower:
tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1]));
break;
case QPIEvaluatorTypes::oEqual:
tmpvars[oi].value = value(ci.operators[0]) == value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oNotEqual:
tmpvars[oi].value = value(ci.operators[0]) != value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oGreaterEqual:
tmpvars[oi].value = value(ci.operators[0]).real() >= value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oSmallerEqual:
tmpvars[oi].value = value(ci.operators[0]).real() <= value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oGreater:
tmpvars[oi].value = value(ci.operators[0]).real() > value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oSmaller:
tmpvars[oi].value = value(ci.operators[0]).real() < value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oAnd:
tmpvars[oi].value = value(ci.operators[0]).real() > 0. && value(ci.operators[1]).real() > 0.;
break;
case QPIEvaluatorTypes::oOr:
tmpvars[oi].value = value(ci.operators[0]).real() > 0. || value(ci.operators[1]).real() > 0.;
break;
case QPIEvaluatorTypes::oFunction:
execFunction(ci);
break;
case QPIEvaluatorTypes::oNone:
tmpvars[oi].value = value(ci.operators[0]);
if (tmp == complexd(0., 0.))
tmpvars[oi].value = 0.;
else
tmpvars[oi].value = value(ci.operators[0]) / tmp;
break;
case QPIEvaluatorTypes::oResidue: tmpvars[oi].value = residue(value(ci.operators[0]), value(ci.operators[1])); break;
case QPIEvaluatorTypes::oPower: tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1])); break;
case QPIEvaluatorTypes::oEqual: tmpvars[oi].value = value(ci.operators[0]) == value(ci.operators[1]); break;
case QPIEvaluatorTypes::oNotEqual: tmpvars[oi].value = value(ci.operators[0]) != value(ci.operators[1]); break;
case QPIEvaluatorTypes::oGreaterEqual: tmpvars[oi].value = value(ci.operators[0]).real() >= value(ci.operators[1]).real(); break;
case QPIEvaluatorTypes::oSmallerEqual: tmpvars[oi].value = value(ci.operators[0]).real() <= value(ci.operators[1]).real(); break;
case QPIEvaluatorTypes::oGreater: tmpvars[oi].value = value(ci.operators[0]).real() > value(ci.operators[1]).real(); break;
case QPIEvaluatorTypes::oSmaller: tmpvars[oi].value = value(ci.operators[0]).real() < value(ci.operators[1]).real(); break;
case QPIEvaluatorTypes::oAnd: tmpvars[oi].value = value(ci.operators[0]).real() > 0. && value(ci.operators[1]).real() > 0.; break;
case QPIEvaluatorTypes::oOr: tmpvars[oi].value = value(ci.operators[0]).real() > 0. || value(ci.operators[1]).real() > 0.; break;
case QPIEvaluatorTypes::oFunction: execFunction(ci); break;
case QPIEvaluatorTypes::oNone: tmpvars[oi].value = value(ci.operators[0]); break;
}
}
if (!instructions.isEmpty())
out = value(instructions.back().out);
if (!instructions.isEmpty()) out = value(instructions.back().out);
return true;
}
bool QPIEvaluator::check(const QString & string) {
currentString = preprocess(string);
correct = check();
if (!correct)
return false;
correct = check();
if (!correct) return false;
lastError = "Correct";
return true;
}