Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. #define MEMORY_SIZE (15)
  6.  
  7. typedef struct link {
  8. double coeff;
  9. int pow;
  10. struct link * next;
  11. } poly;
  12.  
  13. poly *polyArray[MEMORY_SIZE];// array of 15 polynomials to play with
  14.  
  15. // /** The function prototypes */
  16. void createPoly(poly **);
  17. void showPoly(poly *);
  18. void deletePoly(poly *);
  19.  
  20.  
  21. int main(void) {
  22.  
  23.  
  24. int a;
  25.  
  26. for( a = 0; a < MEMORY_SIZE; a++ )
  27. {
  28. polyArray[a] = NULL;//Initialise each element of the array of pointers to NULL
  29. }//end for
  30.  
  31. createPoly(&polyArray[0]);
  32. showPoly(polyArray[0]);
  33.  
  34. srand(time(NULL));
  35. createPoly(&polyArray[1]);
  36. showPoly(polyArray[1]);
  37.  
  38. showPoly(polyArray[0]);
  39.  
  40. int count;
  41. for(count = 0; count < MEMORY_SIZE; count++)
  42. {
  43. deletePoly(polyArray[count]);
  44. }
  45.  
  46. showPoly(polyArray[0]);
  47.  
  48. return 0;
  49.  
  50. }// End Main Function
  51.  
  52.  
  53.  
  54. //////////////////////////////////////////////////////////////////////////////////////
  55.  
  56.  
  57. void createPoly(poly **node) {
  58.  
  59. poly *tempnode; //To hold the temporary last address
  60. tempnode = (poly*)malloc( sizeof(poly) ); //create the first node
  61. *node = tempnode; //Store the head address to the reference variable
  62.  
  63. int flag = 1 + rand()%3;; // A flag to control the number of terms
  64. int counter;
  65.  
  66. for( counter = 0; counter <= flag; counter++ )
  67. {
  68. tempnode->pow = ( flag-counter );
  69. tempnode->coeff = ( (double)(rand()%20) )/( (double)(1 + rand()%20) );
  70.  
  71. if( (counter < flag) && (counter >= 0) )
  72. {
  73. tempnode->next = (poly*)malloc( sizeof(poly) ); //Grow the list
  74. }
  75. else if ( counter == flag )
  76. {
  77. tempnode->next = NULL;
  78. }
  79.  
  80. tempnode = tempnode->next;
  81. }
  82.  
  83. }//end function createPoly
  84.  
  85. void deletePoly(poly *node) {
  86.  
  87. poly *temp;//Create pointer to poly called 'temp'
  88.  
  89. while( node->next != NULL );
  90. {
  91. temp = node->next;//Assign the address of the next node to 'temp'
  92. free( node );//Delete the current node
  93. node = temp;//Assign the address of the next node in the list to 'node'
  94. }//end while
  95.  
  96. node = NULL;//Set pointer 'node' to NULL to prevent a "lingering" pointer
  97.  
  98. }//end function 'deletePoly'
  99.  
  100.  
  101. void showPoly(poly * node) {
  102.  
  103. while(node->next != NULL) {
  104.  
  105. if(node->coeff == 0)
  106. {
  107. node = node->next;
  108. }
  109. else if(node->coeff == 1 && node->pow > 1)
  110. {
  111. printf("[x^%i]", node->pow);
  112. node = node->next;
  113. }
  114. else if(node->coeff == 1 && node->pow == 1)
  115. {
  116. printf("[x]");
  117. node = node->next;
  118. }
  119. else if(node->coeff != 0 && node->pow == 0)
  120. {
  121. printf("(%.2lf)", node->coeff);
  122. node = node->next;
  123. }
  124. else if(node->pow == 0 && node->coeff == 0)
  125. {
  126. node = node->next;
  127. }
  128. else if(node->coeff != 1 && node->pow > 1 && node->coeff != 0)
  129. {
  130. printf("(%.2lf)[x^%i]", node->coeff, node->pow);
  131. node = node->next;
  132. }
  133. else if(node->coeff != 1 && node->pow == 1 && node->coeff != 0)
  134. {
  135. printf("(%.2lf)[x]", node->coeff);// no need to print x to the power 0
  136. node = node->next;
  137. }
  138.  
  139. if(node->next != NULL)
  140. {
  141. printf(" + ");
  142. }
  143. }
  144. }//end function showPoly
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement