bool calcMul(double &result, int &curLex, char lexemeString []) { bool isNegative = false; if (curLex == add || curLex == take) { isNegative = curLex == take; nextLexeme(curLex, lexemeString); } switch (curLex) { case number: { result = toNum(lexemeString); nextLexeme(curLex, lexemeString); break; } case open:{ nextLexeme(curLex, lexemeString); if (!calcExp(result, curLex, lexemeString)) return false; if (curLex != close) return false; nextLexeme(curLex, lexemeString); break; } } result *= isNegative ? -1 : 1; return true; }
int main() { int curLex = start; char lexemeString[10] = { '/0' }; double result = 0.0; nextLexeme(curLex, lexemeString); if (calcExp(result, curLex, lexemeString)) printf("%0.3f\n", result); else printf("wrong expression\n"); return 0; }
void evaluate( const double phiA, const double phiB, const double phiX, const bool calculateGradient, double& f, double& fa, double& fb, double& fx) const { (*numFunctionEvaluations) += 1; if (calculateGradient) { (*numGradientEvaluations) += 1; } const double expPhiA = std::exp(phiA); const double expPhiB = std::exp(phiB); const double expPhiX = std::exp(phiX); f = 0.; fa = 0.; fb = 0.; fx = 0.; for (int k = q + 1; k >= 1; --k) { int cSmaller1 = jointStatistic.getSmaller1Count(k); int cLarger1 = jointStatistic.getLarger1Count(k); int cSmaller2 = jointStatistic.getSmaller2Count(k); int cLarger2 = jointStatistic.getLarger2Count(k); int cEqual = jointStatistic.getEqualCount(k); double ya = 0, za = 0, xaya = 0; double yb = 0, zb = 0, xbyb = 0; double yx = 0, zx = 0, xxyx = 0; if (cSmaller1 > 0 || cEqual > 0 || cLarger1 > 0) { double xa = expPhiA * pow2k[k]; calcExp(xa, ya, za, xaya); } if (cSmaller2 > 0 || cEqual > 0 || cLarger2 > 0) { double xb = expPhiB * pow2k[k]; calcExp(xb, yb, zb, xbyb); } if (cSmaller1 > 0 || cEqual > 0 || cSmaller2 > 0) { double xx = expPhiX * pow2k[k]; calcExp(xx, yx, zx, xxyx); } if (cSmaller1 > 0) { double arg = zx + yx * za; f += cSmaller1 * std::log(arg); if (calculateGradient) { double tmp = cSmaller1 / arg; fa += tmp * yx * xaya; fx += tmp * ya * xxyx; } } if (cLarger1 > 0) { f += cLarger1 * std::log(za); if (calculateGradient) fa += cLarger1 * xaya / za; } if (cSmaller2 > 0) { double arg = zx + yx * zb; f += cSmaller2 * std::log(arg); if (calculateGradient) { double tmp = cSmaller2 / arg; fb += tmp * yx * xbyb; fx += tmp * yb * xxyx; } } if (cLarger2 > 0) { f += cLarger2 * std::log(zb); if (calculateGradient) fb += cLarger2 * xbyb / zb; } if (cEqual > 0) { double arg = za * zb * yx + zx; f += cEqual * std::log(arg); if (calculateGradient) { double tmp = cEqual / arg; double tmpyx = tmp * yx; fa += tmpyx * zb * xaya; fb += tmpyx * za * xbyb; fx += tmp * (ya + yb * za) * xxyx; } } } double linTermExpPhiA = linTermA * expPhiA; double linTermExpPhiB = linTermB * expPhiB; double linTermExpPhiX = linTermX * expPhiX; f -= linTermExpPhiA + linTermExpPhiB + linTermExpPhiX; fa -= linTermExpPhiA; fb -= linTermExpPhiB; fx -= linTermExpPhiX; }