Advertisement
Guest User

Main

a guest
Nov 27th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.86 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////
  2. //  File: main.c
  3. //  Desc: main file to run the program and initiate test cases
  4. //  
  5. //  Author: Group 3
  6. //  ID: 13131567 13133799 13153889 13104195
  7. //  Date: 24/11/14
  8. /////////////////////////////////////////////////////////////////////////////////
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. #include "polynomial.h"
  13.  
  14. int main(){
  15.   //test cases
  16.   polynomial a, b, c, d;
  17.  
  18.   //result of arithmetic operations
  19.   polynomial sum, difference, product, quotient, normal;
  20.  
  21.   //used as dividing/multiplying factor in later polynomials
  22.   double coeff;
  23.  
  24.   //these will be converted into dynamic arrays later on.
  25.   //they just store the coeffs to be used for the polynomials.
  26.   double data1[3] = {20, -4, 0};
  27.   double data2[3] = {5.2, -2, 0};
  28.   double data3[7] = {5.0, 2.0, 20, 0, 5, 2, 0};
  29.  
  30.   //clear the screen to focus on this single session's tests
  31.   system("clear");
  32.  
  33.   //////////////////////////////////////////////////////////////////
  34.   //creating base polynomials we'll be using for the entirity of
  35.   //these test cases.
  36.   //
  37.   //NOTE: second parameter of initalisePolynomial(poly, size)
  38.   //      MUST match the test case arrays above.
  39.   //////////////////////////////////////////////////////////////////  
  40.   createPolynomial(&a, 3, data1);
  41.   createPolynomial(&b, 3, data2);
  42.   createPolynomial(&c, 7, data3);
  43.  
  44.   printf("--------------------------------------------------------------------------------\n");
  45.   printf("These are the starting polynomials which will be used for the later operations\n");
  46.   printf("--------------------------------------------------------------------------------\n");
  47.   printf("Polynomial (A): ");
  48.   printPolynomial(&a);
  49.   printf("Polynomial (B): ");
  50.   printPolynomial(&b);
  51.   printf("Polynomial (C): ");
  52.   printPolynomial(&c);
  53.   printf("Polynomial (D) [invalid]: ");
  54.   printPolynomial(&d);
  55.  
  56.   //////////////////////////////////////////////////////////////////
  57.   //testing add/subtract polynomials of different length  
  58.   //////////////////////////////////////////////////////////////////
  59.   printf("--------------------------------------------------------------------------------\n");
  60.   printf("testing add/subtract polynomials of different length\n");
  61.   printf("--------------------------------------------------------------------------------\n");
  62.   printf("Polynomial (A): ");
  63.   printPolynomial(&a);
  64.  
  65.   printf("Polynomial (C): ");
  66.   printPolynomial(&c);
  67.  
  68.   printf("A + C =  ");
  69.   add(&a, &c, &sum);
  70.   printPolynomial(&sum);
  71.  
  72.   printf("A - C =  ");
  73.   subtract(&a, &c, &difference);
  74.   printPolynomial(&difference);
  75.  
  76.   deletePolynomial(&sum);
  77.   deletePolynomial(&difference);
  78.  
  79.   //////////////////////////////////////////////////////////////////
  80.   //testing multiplcation/division by a scalar  
  81.   //////////////////////////////////////////////////////////////////
  82.   printf("--------------------------------------------------------------------------------\n");
  83.   printf("testing multiplcation/division by a scalar\n");
  84.   printf("--------------------------------------------------------------------------------\n");
  85.   printf("Polynomial (A): ");
  86.   printPolynomial(&a);
  87.   printf("Polynomial (C): ");
  88.   printPolynomial(&c);
  89.  
  90.   coeff = 5;
  91.  
  92.   //multiply
  93.   printf("A * %.2lf =  ", coeff);
  94.   multiply(&a, coeff, &product);
  95.   printPolynomial(&product);
  96.  
  97.   //divide
  98.   printf("C / %.2lf =  ", coeff);
  99.   divide(&c, coeff, &quotient);
  100.   printPolynomial(&quotient);
  101.  
  102.   deletePolynomial(&product);
  103.   deletePolynomial(&quotient);
  104.  
  105.   //////////////////////////////////////////////////////////////////
  106.   //testing for normalising polynomials
  107.   //////////////////////////////////////////////////////////////////
  108.   printf("--------------------------------------------------------------------------------\n");
  109.   printf("testing for normalising polynomials\n");
  110.   printf("--------------------------------------------------------------------------------\n");
  111.   printf("Polynomial (A): ");
  112.   printPolynomial(&a);
  113.   printf("Polynomial (C): ");
  114.   printPolynomial(&c);
  115.  
  116.   //test1
  117.   printf("Noramlised (A) =  ");
  118.   normalise(&a, &normal);
  119.   printPolynomial(&normal);
  120.  
  121.   deletePolynomial(&normal);
  122.  
  123.   //test2  
  124.   printf("Noramlised (C) =  ");
  125.   normalise(&c, &normal);
  126.   printPolynomial(&normal);
  127.  
  128.   deletePolynomial(&normal);
  129.        
  130.   //////////////////////////////////////////////////////////////////
  131.   //testing for order of poly
  132.   //////////////////////////////////////////////////////////////////
  133.   printf("--------------------------------------------------------------------------------\n");
  134.   printf("testing for order of poly\n");
  135.   printf("--------------------------------------------------------------------------------\n");
  136.   printf("Polynomial (A): ");
  137.   printPolynomial(&a);
  138.   printf("Order: %d\n", getOrder(&a));
  139.  
  140.   //////////////////////////////////////////////////////////////////
  141.   //testing operations with invalid polynomials
  142.   //////////////////////////////////////////////////////////////////
  143.   printf("--------------------------------------------------------------------------------\n");
  144.   printf("testing for operations with invalid polynomials\n");
  145.   printf("--------------------------------------------------------------------------------\n");
  146.   printf("Polynomial (A): ");
  147.   printPolynomial(&a);
  148.   printf("Polynomial (D) [invalid]: ");
  149.   printPolynomial(&d);
  150.  
  151.   //ADDITION SUBTRACTION
  152.   printf("ADD/SUBTRACT\n");
  153.   printf("A + D =  ");
  154.   add(&a, &d, &sum);
  155.   printPolynomial(&sum);
  156.  
  157.   printf("A - D =  ");
  158.   subtract(&a, &d, &difference);
  159.   printPolynomial(&difference);
  160.  
  161.   //these polynomials were never created as the above operations return an error
  162.   //this will cause deletePolynomial to give a warning to the user.
  163.   deletePolynomial(&sum);
  164.   deletePolynomial(&difference);
  165.  
  166.   //MULTIPLCATION DIVISION
  167.   printf("MULTIPLY/DIVIDE\n");
  168.   coeff = 5;
  169.  
  170.   printf("A * %.2lf =  ", coeff);
  171.   multiply(&a, coeff, &product);
  172.   printPolynomial(&product);
  173.  
  174.   printf("D / %.2lf =  ", coeff);
  175.   divide(&d, coeff, &quotient);
  176.   printPolynomial(&quotient);
  177.  
  178.   deletePolynomial(&product);
  179.   deletePolynomial(&quotient); //quotient delete should return an error as it was never created
  180.  
  181.  
  182.   //NORMALISATION
  183.   printf("NORMALISE\n");
  184.   //test1
  185.   printf("Noramlised (A) =  ");
  186.   normalise(&a, &normal);
  187.   printPolynomial(&normal);
  188.  
  189.   deletePolynomial(&normal);
  190.  
  191.   //test2  
  192.   printf("Noramlised (D) =  ");
  193.   normalise(&d, &normal);
  194.   printPolynomial(&normal);
  195.  
  196.   deletePolynomial(&normal); //will return a warning
  197.  
  198.   //////////////////////////////////////////////////////////////////
  199.   //delete original polynomials
  200.   //end of testing.
  201.   //////////////////////////////////////////////////////////////////
  202.   deletePolynomial(&a);
  203.   deletePolynomial(&b);
  204.   deletePolynomial(&c);
  205.  
  206.   return EXIT_SUCCESS;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement