Beispiel #1
0
/*
 * The overall architecture is to repeat the same process,
 * terminating only if a negative degree is entered.
 *
 * Thus, we repeat the process READ->COMPUTE->PRINT.
 *
 */
int main(void)
{
    int maxDegree;                  /* the maximum degree of the polynomial the user wants to enter */
    int *polynomial = NULL;  /* the polynomial is represented by its coefficients, stored here */
    int *derivative = NULL;    /* the reciprocal polynomial is represented by the coefficients stored here */
    
    while (1) {
        /*
         * Clear the data structures from previous iteration
         * NB It's safe to free a NULL pointer
         */
        free(polynomial);
        free(derivative);
        polynomial = derivative = NULL;
        
        /*
         * Read data
         */
        printf("Please enter the maximum degree of the polynomial: ");
        scanf("%d", &maxDegree);
        
        // SENTINEL: Quit program if user inputs negative degree
        if (maxDegree < 0) break;
        
        /*
         * Allocate memory
         */
        polynomial = calloc(maxDegree+1, sizeof(int) );
        derivative = calloc(maxDegree, sizeof(int) );
        if (!polynomial || !derivative) {
            fprintf(stderr, "Error: unable to allocate memory.\n");
            exit(EXIT_FAILURE);
        }
        
        printf("Please enter the coefficients: ");
        ReadPolynomial(polynomial, maxDegree);
        
        /*
         * Compute the derivative
         */
        CalculateDerivative(polynomial, derivative, maxDegree);
        
        /*
         * Print the result
         */
        PrintPolynomial("The polynomial is", polynomial, maxDegree);
        PrintPolynomial("Its derivative is", derivative, maxDegree);
        printf("\n");
    }
    
    return 0;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
    Polynomial Poly, Poly1, Poly2, PolySum, PolyProd;
    int i;
    printf("\n#### Test Basic Operations ...\n");
    Poly = MakeEmpty(NULL);
    PrintPolynomial(Poly);

    printf("insert Coefficient: %d, Exponent: %d\n", 3, 4);
    Insert(Poly, 3, 4);
    PrintPolynomial(Poly);
    for (i = 0; i < 10; ++i)
    {
        printf("insert Coefficient: %d, Exponent: %d\n", i, i*2);
        Insert(Poly, i, i*2);
    }
    PrintPolynomial(Poly);
    DeletePolynomial(Poly);
    PrintPolynomial(Poly);
    for (i = 0; i < 10; ++i)
    {
        printf("insert Coefficient: %d, Exponent: %d\n", i, i*2);
        Insert(Poly, i, i*2);
    }
    PrintPolynomial(Poly);

    Poly1 = MakeEmpty(NULL);
    Poly2 = MakeEmpty(NULL);

    printf("\n#### Test AddPolynomial ...\n");
    for (i = 0; i < 10; ++i)
    {
        Insert(Poly1, i + 1, i*2);
        Insert(Poly2, i + 2, i*3);
    }
    PrintPolynomial(Poly1);
    PrintPolynomial(Poly2);
    PolySum = AddPolynomial(Poly1, Poly2);
    PrintPolynomial(PolySum);

    printf("\n#### Multiply AddPolynomial ...\n");
    Poly1 = MakeEmpty(NULL);
    Poly2 = MakeEmpty(NULL);

    for (i = 0; i < 10; ++i)
    {
        Insert(Poly1, i + 1, i*2);
        Insert(Poly2, i + 2, i*3);
    }
    PrintPolynomial(Poly1);
    PrintPolynomial(Poly2);
    PolyProd = MultPolynomial(Poly1, Poly2);
    PrintPolynomial(PolyProd);
    return 0;
}