Exemplo n.º 1
0
/**
 * Display a polynomial
 *
 * @param poly Pointer on the polynomial.
 * @param dev Polynomial form (dev: 1, fac: 0).
 */
void displayPolynomial(polynomial poly, int form) {
    if (!poly
            || (form == DEV_FORM && !poly->dev)
            || (form == FACT_FORM && !poly->fact)) {
        return;
    }

    list node = form ? poly->dev : poly->fact;

    if (form == FACT_FORM) {
        printf("Not implemented yet");
        return;
    }

    double real = 0;
    double imaginary = 0;

    while (node) {
        real = getRealPart(node->number);
        imaginary = getImaginaryPart(node->number);

        if (node->prev) {
            if (real < 0 && !imaginary) {
                real = -real;
                printf(" - ");
            } else if (imaginary < 0 && !real) {
                imaginary = -imaginary;
                printf(" - ");
            } else
                printf(" + ");
        }

        if (!imaginary) {
            printf("%f", real);
        } else if (!real) {
            printf("%fi", imaginary);
        } else {
            if (imaginary < 0) {
                imaginary = -imaginary;
                printf("(%f - %fi)", real, imaginary);
            } else
                printf("(%f + %fi)", real, imaginary);
        }

        if (node->exponent > 0)
            printf("z^%d", node->exponent);

        node = node->succ;
    }

    printf("\n");
}
Exemplo n.º 2
0
/**
 * Display a complex number ( real and imaginary complex part )
 *
 * @param number The number to dysplay
 */
void displayComplex(const complexNumber number) {
    double real = getRealPart(number);
    double imaginary = getImaginaryPart(number);

    if (!imaginary) {
        printf("%f\n", real);
    } else if (!real) {
        printf("%fi\n", imaginary);
    } else {
        if (imaginary < 0) {
            imaginary = -imaginary;
            printf("%f - %fi\n", real, imaginary);
        } else
            printf("%f + %fi\n", real, imaginary);
    }
}
Exemplo n.º 3
0
/**
 * Compute an addition or a substraction of polynomial
 *
 * @param poly1 The first polynomial
 * @param poly2 The second polynomial
 * @param operation Type of computation
 * @return Result of the computation
 */
polynomial addSubPoly(polynomial poly1, polynomial poly2, int operation) {
    // We work on dev form
    if (!poly1 || !poly2 || !poly1->dev || !poly2->dev)
        return NULL;

    list firstPoly = poly1->dev;
    list secondPoly = poly2->dev;
    polynomial newPoly = malloc(sizeof (polynomialElem));

    while (firstPoly || secondPoly) {
        /**
         * We try to get the value of the exponent of the monomial.
         * If the monomial is not defined (we are at the end of the polynomial)
         * we set the exponent to a infinit value so that the monomial of the
         * other polynomial will automatically be added/substracted at the end
         * of the new polynomial.
         */
        int firstExponent = firstPoly ? firstPoly->exponent : INT_MAX;
        int secondExponent = secondPoly ? secondPoly->exponent : INT_MAX;

        if (firstExponent < secondExponent) {
            addNode(newPoly, firstPoly->number, firstPoly->exponent, DEV_FORM);
            firstPoly = firstPoly->succ;
        } else if (secondExponent < firstExponent) {
            complexNumber tmpCplx = secondPoly->number;
            if (operation == SUB_OP) {
                tmpCplx.real = -getRealPart(secondPoly->number);
                tmpCplx.imaginary = getImaginaryPart(secondPoly->number);
            }
            addNode(newPoly, tmpCplx, secondPoly->exponent, DEV_FORM);
            secondPoly = secondPoly->succ;
        } else {
            if (operation == ADD_OP)
                addNode(newPoly, add(firstPoly->number, secondPoly->number), firstPoly->exponent, DEV_FORM);
            else
                addNode(newPoly, substract(firstPoly->number, secondPoly->number), firstPoly->exponent, DEV_FORM);
            firstPoly = firstPoly->succ;
            secondPoly = secondPoly->succ;
        }
    }

    return newPoly;
}
Exemplo n.º 4
0
//nonbasic operator functions
float Complex::operator[](int i){
    if (i==0)
        return getRealPart();
    //default return is imaginary number (if not 0 or 1)
   else return getImaginaryPart();
}
Exemplo n.º 5
0
//subtraction function
Complex Complex::subtract(Complex& c){
    //follows imaginary number equation

    return Complex(getRealPart()-c.getRealPart(),getImaginaryPart()-c.getImaginaryPart());

}
Exemplo n.º 6
0
//adding function
Complex Complex::add(Complex& c){
    //follows imaginary number equation
    return Complex(getRealPart()+c.getRealPart(),getImaginaryPart()+c.getImaginaryPart());
}
Exemplo n.º 7
0
//sets the object into a string output, sort of
string Complex::toString(){
cout<< "(" << getRealPart()<< " + " << getImaginaryPart()<<"i) ";
    return "";
    
}
Exemplo n.º 8
0
//absolute value function
float Complex::abs(){
    return sqrt(getRealPart()*getRealPart()+getImaginaryPart()*getImaginaryPart());
    
}
Exemplo n.º 9
0
//division function
Complex Complex::divide(Complex& c){
    return Complex((getRealPart()*c.getRealPart()+getImaginaryPart()*c.getImaginaryPart())/(c.getRealPart()*c.getRealPart()+c.getImaginaryPart()*c.getImaginaryPart() ),( getImaginaryPart()*c.getRealPart()-getRealPart()*c.getImaginaryPart())/(c.getRealPart()*c.getRealPart()+c.getImaginaryPart()*c.getImaginaryPart() ));
}
Exemplo n.º 10
0
//multiplication function
Complex Complex::multiply(Complex& c){
    return Complex((getRealPart()*c.getRealPart()-getImaginaryPart()*c.getImaginaryPart()),( getImaginaryPart()*c.getRealPart()+getRealPart()*c.getImaginaryPart()));

}