Exemplo n.º 1
0
 bool isNumber(const char *s) {
     while (*s==' ') ++s;
     const char* e=s;
     while(*e!='\0' && *e!=' ') ++e;
     for (const char* t=e;*t!='\0';++t) if (*t!=' ') return false;
     return isExp(s,e);
 }
Exemplo n.º 2
0
/*inline*/ bool CExpression::isExp(const std::string& str) { return isExp(str.c_str()); }
Exemplo n.º 3
0
CExpression& CExpression::rebild(const char* CExpression)
{
    delete[] walkthroughArray;
    delete[] numArray;
    walkthroughArray = 0;
    numArray = 0;
    fine = true;
    if (!isExp(CExpression))
    {
        fine = false;
        return *this;
    }
    int len = static_cast<int>(strlen(CExpression));
    char* sourseString = new char[len * 3 + 1];
    for (int i = 0, j = 0; i < len; ++i)
        sourseString[j++] = CExpression[i];
    sourseString[len] = 0;

    sintAdaptation(sourseString);//Делает строку регистронезависимой, удаляет пробелы/'\t'/'\n', приводит скобочки к однообразию
    addCode(sourseString);//Заменяет ссылки на функции символами от -128 для удобства дальнейшего анализа
    std::cout << sourseString << std::endl;
    for (int i(0); sourseString[i]; ++i) {
        if (sourseString[i] == 't')
            sourseString[i] = 'y';
    }
    std::cout << sourseString << std::endl;
    if (expForm) {
        toExpForm(sourseString);//Преобразовывает записи типа -3.0e-2 так,
    }//чтобы они вычислялись как экспоненциальная форма числа.
    addMult(sourseString);//Добавляет * и 0 для реализации унарного минуса. Функции объеденены по историческим причинам.
    sortPoland(setArray(sourseString));/*При вызове преобразует строку в массив sintElem и высылает его для
                                       сортировки в польскую нотацию*/

    short i = 0,
          numbersCount = 0,
          opCount = 1;
    while (walkthroughArray[i].lexeme != 127)
    {
        if (walkthroughArray[i].lexeme == 'x' ||
            walkthroughArray[i].lexeme == 'y' ||
           !walkthroughArray[i].lexeme) {
            ++numbersCount;//Посчитать максимально возможное количество чисел в стеке(тоесть все числа вобще)
        } else {
            opCount += !isUnary(walkthroughArray[i].lexeme);
        }
        ++i;
    }
    //qDebug() << opCount << '<' << numbersCount;
    if (opCount > numbersCount)
    {
        delete[] walkthroughArray;
        delete[] numArray;
        walkthroughArray = 0;
        numArray = 0;
        fine = false;
        return *this;
    }

    numArray = new double[numbersCount + 1];//1 лишний элемент нужен для оптимизации алгаритма вычисления
    //std::cout << "объект создан.\n";
    return *this;
}
Exemplo n.º 4
0
Func1& newProdFunction(Func1& f1, Func1& f2)
{
    if (isOne(f1)) {
        delete &f1;
        return f2;
    }
    if (isOne(f2)) {
        delete &f2;
        return f1;
    }
    if (isZero(f1) || isZero(f2)) {
        delete &f1;
        delete &f2;
        return *(new Const1(0.0));
    }
    if (isConstant(f1) && isConstant(f2)) {
        doublereal c1c2 = f1.c() * f2.c();
        delete &f1;
        delete &f2;
        return *(new Const1(c1c2));
    }
    if (isConstant(f1)) {
        doublereal c = f1.c();
        delete &f1;
        return newTimesConstFunction(f2, c);
    }
    if (isConstant(f2)) {
        doublereal c = f2.c();
        delete &f2;
        return newTimesConstFunction(f1, c);
    }

    if (isPow(f1) && isPow(f2)) {
        Func1& p = *(new Pow1(f1.c() + f2.c()));
        delete &f1;
        delete &f2;
        return p;
    }

    if (isExp(f1) && isExp(f2)) {
        Func1& p = *(new Exp1(f1.c() + f2.c()));
        delete &f1;
        delete &f2;
        return p;
    }

    bool tc1 = isTimesConst(f1);
    bool tc2 = isTimesConst(f2);

    if (tc1 || tc2) {
        doublereal c1 = 1.0, c2 = 1.0;
        Func1* ff1 = 0, *ff2 = 0;
        if (tc1) {
            c1 = f1.c();
            ff1 = &f1.func1_dup();
            delete &f1;
        } else {
            ff1 = &f1;
        }
        if (tc2) {
            c2 = f2.c();
            ff2 = &f2.func1_dup();
            delete &f2;
        } else {
            ff2 = &f2;
        }
        Func1& p = newProdFunction(*ff1, *ff2);

        if (c1* c2 != 1.0) {
            return newTimesConstFunction(p, c1*c2);
        } else {
            return p;
        }
    } else {
        return *(new Product1(f1, f2));
    }
}