complexnumbersmodule.cpp 3.67 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 13
#include <ccomplex>
#include "stdlib/kumirstdlib.hpp"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

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
39

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

45

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


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


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


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


Victor Yacovlev's avatar
Victor Yacovlev committed
79
Complex ComplexNumbersModule::runOperatorSLASH(const Complex& x, const Complex& y)
80
{
81 82 83 84 85 86 87 88 89
    Complex result;
    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