complexnumbersmodule.cpp 3.84 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
    */
}

38 39 40 41
QList<ExtensionSystem::CommandLineParameter> ComplexNumbersModule::acceptableCommandLineParameters()
{
    return QList<ExtensionSystem::CommandLineParameter>();
}
Victor Yacovlev's avatar
Victor Yacovlev committed
42

Victor Yacovlev's avatar
Victor Yacovlev committed
43 44
qreal ComplexNumbersModule::runRe(const Complex& x)
{
45
    return x.re;
Victor Yacovlev's avatar
Victor Yacovlev committed
46
}
Victor Yacovlev's avatar
Victor Yacovlev committed
47

48

Victor Yacovlev's avatar
Victor Yacovlev committed
49
qreal ComplexNumbersModule::runIm(const Complex& x)
50
{
51
    return x.im;
52 53 54
}


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


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


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


Victor Yacovlev's avatar
Victor Yacovlev committed
82
Complex ComplexNumbersModule::runOperatorSLASH(const Complex& x, const Complex& y)
83
{
84
    Complex result;
Victor Yacovlev's avatar
Victor Yacovlev committed
85
    result.im = result.re = 0.0;
86 87 88 89 90 91 92 93
    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;
94 95 96
}


Victor Yacovlev's avatar
Victor Yacovlev committed
97
Complex ComplexNumbersModule::runOperatorASSIGN(const int x)
98
{
99 100 101 102
    Complex result;
    result.re = x;
    result.im = 0;
    return result;
103 104
}

Victor Yacovlev's avatar
Victor Yacovlev committed
105

Victor Yacovlev's avatar
Victor Yacovlev committed
106
Complex ComplexNumbersModule::runOperatorASSIGN(const qreal x)
Victor Yacovlev's avatar
Victor Yacovlev committed
107
{
108 109 110 111
    Complex result;
    result.re = x;
    result.im = 0;
    return result;
Victor Yacovlev's avatar
Victor Yacovlev committed
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 142 143 144 145
}


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();
    }
146 147 148
    if (im.isEmpty()) {
        im = "1.0";
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
149 150 151 152
    result.re = re.toDouble(&ok1);
    result.im = im.toDouble(&ok2);
    ok = ok1 && ok2;
    return result;
Victor Yacovlev's avatar
Victor Yacovlev committed
153 154 155
}


Victor Yacovlev's avatar
Victor Yacovlev committed
156
QString ComplexNumbersModule::runOperatorOUTPUT(const Complex& x)
Victor Yacovlev's avatar
Victor Yacovlev committed
157
{
158 159 160 161 162 163 164 165 166 167 168
    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
169 170
}

171 172
    
} // $namespace