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