run.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ** polynomial_adt_test.c
  3. ** Test (minimalistic) for the polynomial module
  4. * More of a display of functionality
  5. * Copyright (c) 2009 I. Soule
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. ** iasoule32@gmail.com
  30. */
  31. #include <stdio.h>
  32. #include "../polynomial_adt.h"
  33. #include "../polynomial_adt.c"
  34. int main(void)
  35. {
  36. PolyAdt *p = create_adt(5), *q = create_adt(4);
  37. PolyAdt *sum, *diff, *prod;
  38. insert_term(p,1,5);
  39. insert_term(p,3,4);
  40. insert_term(p,1,3);
  41. insert_term(p,9,2);
  42. insert_term(p,8,1);
  43. insert_term(q,2,4);
  44. insert_term(q,8,3);
  45. insert_term(q,7,2);
  46. insert_term(q,6,1);
  47. printf("Displaying Polynomials ...\n");
  48. display_poly(p);
  49. display_poly(q);
  50. sum = add(p,q);
  51. printf("P(x) + Q(x) = ");
  52. display_poly(sum);
  53. diff = subtract(p,q);
  54. printf("P(x) - Q(x) = ");
  55. display_poly(diff);
  56. prod = multiply(p,q);
  57. printf("P(x)*Q(x) = ");
  58. display_poly(prod);
  59. PolyAdt *quad = create_adt(2);
  60. insert_term(quad, 10, 2);
  61. insert_term(quad, 30, 1);
  62. insert_term(quad, 2, 0);
  63. quadratic_roots(quad, NULL, NULL); //print out the roots
  64. float real, cplx;
  65. quadratic_roots(quad, &real, &cplx);
  66. printf("X1 = %f, X2 = %f\n\n", real, cplx);
  67. PolyAdt *deriv, *integral;
  68. deriv = derivative(p);
  69. printf("The derivitive of p = ");
  70. display_poly(deriv);
  71. integral = integrate(q);
  72. printf("The integral of q = ");
  73. display_poly(integral);
  74. printf("\n Computing P(x)^3\n");
  75. PolyAdt *expo;
  76. expo = exponentiate(p, 3);
  77. display_poly(expo);
  78. printf("\n");
  79. printf("Computing Integral[Q(x)^2]\n");
  80. expo = exponentiate(q, 2);
  81. integral = integrate(expo);
  82. display_poly(integral);
  83. printf(" Differentiating and Integrating P\n");
  84. display_poly(integrate(derivative(p)));
  85. PolyAdt *L, *M;
  86. L = create_adt(3), M = create_adt(2);
  87. insert_term(L, 4, 3);
  88. insert_term(L, 10, 2);
  89. insert_term(L, 15, 1);
  90. insert_term(M, 4, 2);
  91. printf("L = ");
  92. display_poly(L);
  93. printf("M = ");
  94. display_poly(M);
  95. printf("Computing composition L(M(X))\n");
  96. display_poly(compose(L, M));
  97. printf("Freed memory back to heap for allocated polys'\n");
  98. destroy_poly(sum);
  99. destroy_poly(diff);
  100. destroy_poly(prod);
  101. destroy_poly(L); destroy_poly(M);
  102. destroy_poly(q); destroy_poly(p);
  103. return 0;
  104. }