complexnumbersmodule.cpp 3.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
This file is generated, but you can safely change it
until you run "gen_actor_source.py" with "--project" flag.

Generated file is just a skeleton for module contents. 
You should change it corresponding to functionality.
*/

#include <QtCore>
#include <QtGui>
#include "complexnumbersmodule.h"
12
#include "stdlib/kumirstdlib.hpp"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

namespace ActorComplexNumbers {

ComplexNumbersModule::ComplexNumbersModule(ExtensionSystem::KPlugin * parent)
    : ComplexNumbersModuleBase(parent)
{
    /* TODO 
    implement class Constructor
    */
}

void ComplexNumbersModule::reset()
{
    /* TODO
    This method is called when actor resets its state before program starts.
    */
}

void ComplexNumbersModule::setAnimationEnabled(bool enabled)
{
    /* TODO
    Set internal flag for animation
    */
}

Victor Yacovlev's avatar
Victor Yacovlev committed
38

Victor Yacovlev's avatar
Victor Yacovlev committed
39 40
qreal ComplexNumbersModule::runRe(const Complex& x)
{
41
    return x.re;
Victor Yacovlev's avatar
Victor Yacovlev committed
42
}
Victor Yacovlev's avatar
Victor Yacovlev committed
43

44

Victor Yacovlev's avatar
Victor Yacovlev committed
45
qreal ComplexNumbersModule::runIm(const Complex& x)
46
{
47
    return x.im;
48 49 50
}


Victor Yacovlev's avatar
Victor Yacovlev committed
51
Complex ComplexNumbersModule::runOperatorPLUS(const Complex& x, const Complex& y)
52
{
53 54 55 56
    Complex result;
    result.re = x.re + y.re;
    result.im = x.im + y.im;
    return result;
57 58 59
}


Victor Yacovlev's avatar
Victor Yacovlev committed
60
Complex ComplexNumbersModule::runOperatorMINUS(const Complex& x, const Complex& y)
61
{
62 63 64 65
    Complex result;
    result.re = x.re - y.re;
    result.im = x.im - y.im;
    return result;
66 67 68
}


Victor Yacovlev's avatar
Victor Yacovlev committed
69
Complex ComplexNumbersModule::runOperatorASTERISK(const Complex& x, const Complex& y)
70
{
71 72 73 74
    Complex result;
    result.re = x.re*y.re - x.im*y.im;
    result.im = y.im*x.re + x.re*y.im;
    return result;
75 76 77
}


Victor Yacovlev's avatar
Victor Yacovlev committed
78
Complex ComplexNumbersModule::runOperatorSLASH(const Complex& x, const Complex& y)
79
{
80
    Complex result;
Victor Yacovlev's avatar
Victor Yacovlev committed
81
    result.im = result.re = 0.0;
82 83 84 85 86 87 88 89
    if (y.re==0 && y.im==0) {
        setError(QString::fromUtf8("Деление на комплексный нуль"));
        return result;
    }
    qreal factor = y.re*y.re + y.im*y.im;
    result.re = (x.re*y.re+x.im*y.im) / factor;
    result.im = (x.im*y.re-x.re*y.im) / factor;
    return result;
90 91 92
}


Victor Yacovlev's avatar
Victor Yacovlev committed
93
Complex ComplexNumbersModule::runOperatorASSIGN(const int x)
94
{
95 96 97 98
    Complex result;
    result.re = x;
    result.im = 0;
    return result;
99 100
}

Victor Yacovlev's avatar
Victor Yacovlev committed
101

Victor Yacovlev's avatar
Victor Yacovlev committed
102
Complex ComplexNumbersModule::runOperatorASSIGN(const qreal x)
Victor Yacovlev's avatar
Victor Yacovlev committed
103
{
104 105 106 107
    Complex result;
    result.re = x;
    result.im = 0;
    return result;
Victor Yacovlev's avatar
Victor Yacovlev committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
}


Complex ComplexNumbersModule::runOperatorINPUT(const QString& x, bool& ok)
{
    static const QRegExp rxPlusMinus("[+-]");
    int p = x.indexOf(rxPlusMinus);
    QString re = "0.0", im = "0.0";
    if (p==-1) {
        if (x.trimmed().endsWith(QChar('i'), Qt::CaseInsensitive))
            im = x.trimmed();
        else
            re = x.trimmed();
    }
    else {
        QString a = x.left(p).trimmed();
        QString b = x.mid(p+1).trimmed();
        if (a.endsWith(QChar('i'), Qt::CaseInsensitive)) {
            im = a;
            re = x[p]+b;
        }
        else {
            im = x[p]+b;
            re = a;
        }
    }
    bool ok1, ok2;
    Complex result;
    im.remove(QChar('i'), Qt::CaseInsensitive);
    im = im.trimmed();
    if (im.endsWith("*")) {
        im = im.left(im.length()-1);
        im = im.trimmed();
    }
142 143 144
    if (im.isEmpty()) {
        im = "1.0";
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
145 146 147 148
    result.re = re.toDouble(&ok1);
    result.im = im.toDouble(&ok2);
    ok = ok1 && ok2;
    return result;
Victor Yacovlev's avatar
Victor Yacovlev committed
149 150 151
}


Victor Yacovlev's avatar
Victor Yacovlev committed
152
QString ComplexNumbersModule::runOperatorOUTPUT(const Complex& x)
Victor Yacovlev's avatar
Victor Yacovlev committed
153
{
154 155 156 157 158 159 160 161 162 163 164
    QString result;
    const QString re = QString::fromStdWString(
                Kumir::Converter::sprintfReal(x.re,'.',false,0,0,0)
                );
    const QString im = QString::fromStdWString(
                Kumir::Converter::sprintfReal(x.im,'.',false,0,0,0)
                );
    result = re;
    result += im.startsWith('-')? "" : "+";
    result += im + "i";
    return result;
Victor Yacovlev's avatar
Victor Yacovlev committed
165 166
}

167 168
    
} // $namespace