fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<iostream>
  4. //using namespace std;
  5.  
  6. struct list{
  7. int coef;
  8. int expon;
  9. struct list *next;
  10. };
  11. typedef struct list node;
  12. typedef node *link;
  13.  
  14. void createpoly(link *ptr);
  15. link mul(link poly1,link poly2);
  16. link add(link poly1,link poly2);
  17. int compare(int,int); // 比較兩多項式的指數大小
  18. void attach(int,int,link *ptr); // 將一個節點附加在串列的結尾
  19. void printfresult( link );
  20. link freelist( link temp );
  21.  
  22. int main()
  23. {
  24. link *ptr=NULL;
  25. link poly1 = NULL; // intially there are no nodes
  26. link poly2 = NULL;
  27. link answer = NULL; // 放置多項式的運算結果
  28. link temp;
  29.  
  30. //input polynomial 1
  31. printf("Enter the polynomial 1: ");
  32. createpoly(&poly1);
  33.  
  34. //input polynomial 2
  35. printf("Enter the polynomial 2: ");
  36. createpoly(&poly2);
  37.  
  38.  
  39. printf("Answer \n");
  40. answer = mul(poly1,poly2);
  41. printfresult(answer);
  42.  
  43. system("pause");
  44.  
  45. }
  46. void createpoly(link *ptr)
  47. {
  48. link head=NULL; // pointer to head
  49. link current; // pointer to current
  50.  
  51. int coef,expon ;
  52. int sign = 1 ;
  53. char token ;
  54.  
  55. do {
  56. scanf("%d", &coef ); // 接收係數
  57. getchar(); // 接收 ^
  58. scanf("%d", &expon ); // 接收指數
  59. scanf("%c", &token ); // 接收下一項的正負號
  60.  
  61. if( !head ){ // 如head為NULL時
  62. head =(node*)malloc( sizeof ( node )); //creat node
  63. head->coef = coef * sign; // place 係數值 in node
  64. head->expon = expon; // place 指數值 in node
  65. head->next = NULL; // node does not link to another node
  66.  
  67. }
  68. else{
  69. current = head; // 指到串列之首
  70. while( current->next !=NULL ){ // 此while是在找串列的最後一個節點 就是current->next 為 NULL時
  71. current = current->next;
  72. }
  73. current->next = (node *)malloc( sizeof( node)); // 在串列最後再creat一個node
  74. current->next->coef = coef * sign ; // place 係數值 in node
  75. current->next->expon = expon; // place 指數值 in node
  76. current->next->next = NULL; // node does not link to another node
  77. }
  78.  
  79. // 判定下一項的正負號
  80. if( token == '+') // 如為正
  81. sign = 1;
  82. else if( token == '-') // 如為負
  83. sign = -1;
  84.  
  85. }while( token != '\n'); // 當token為enter時就結束while
  86.  
  87. *ptr = head; // head assign 給*sptr 指到串列之首
  88.  
  89.  
  90. }
  91.  
  92.  
  93. // 多項式的加法
  94. link add(link poly1,link poly2)
  95. {
  96. link front , rear, temp;
  97. int sum;
  98. rear = ( node *)malloc( sizeof( node )); //creat node
  99. front = rear;
  100.  
  101.  
  102. while( poly1, poly2 )
  103. switch( compare( poly1->expon, poly2->expon )){
  104.  
  105. case -1: // poly1->expon 小於 poly2->expon
  106. attach( poly2->coef, poly2->expon, &rear);
  107. poly2 = poly2->next;
  108. break;
  109.  
  110. case 0: // poly1->expon 等於 poly2->expon
  111. sum = poly1->coef + poly2->coef ;
  112. if( sum )
  113. attach( sum, poly1->expon, &rear);
  114. poly1 = poly1->next;
  115. poly2 = poly2->next;
  116. break;
  117.  
  118. case 1: // poly1->expon 大於 poly2->expon
  119. attach( poly1->coef, poly1->expon, &rear);
  120. poly1 = poly1->next;
  121. break;
  122. }
  123.  
  124. /*copy rest of list head1 and then head2*/
  125. for( ; poly1 ; poly1 = poly1->next)
  126. attach( poly1->coef, poly1->expon, &rear);
  127. for( ; poly2 ; poly2 = poly2->next)
  128. attach( poly2->coef, poly2->expon, &rear);
  129. rear->next = NULL;
  130.  
  131. /* delete extra initial node */
  132. temp = front;
  133. front = front->next;
  134. free( temp );
  135. return front;
  136. }
  137.  
  138. link mul(link poly1,link poly2)
  139. {
  140. link temp = NULL , sum = NULL;
  141. link first;
  142. int coef , expon;
  143.  
  144. first = poly1;
  145. while(poly2 != NULL){
  146. while( poly1 != NULL){
  147. coef = (poly1->coef) * (poly2->coef);
  148. expon = (poly1->expon) * (poly2->expon);
  149. attach( coef, expon, &temp ); // 將一個節點附加在串列的結尾
  150. poly1 = poly1->next; // poly1 下一個節點
  151. }
  152. sum = add(temp,sum);
  153. poly2 = poly2->next;
  154. poly1 = first; // poly1 回到最首項
  155. temp = freelist(temp); // 釋回節點串列空間
  156. }
  157. return sum;
  158.  
  159. }
  160. int compare(int a,int b)
  161. {
  162. if(a < b) return -1;
  163. else if(a > b) return 1;
  164. else return 0;
  165. }
  166.  
  167. // 將一個節點附加在串列的結尾
  168. void attach( int coefficient , int exponent, link *ptr)
  169. {
  170. link temp;
  171. temp = (node*)malloc(sizeof(node)); //creat node
  172. temp->coef = coefficient ;
  173. temp->expon = exponent ;
  174. (*ptr)->next=temp;
  175. *ptr = temp;
  176. }
  177.  
  178. // 把運算之後的多項式printf出來
  179. void printfresult( link node )
  180. {
  181. while( node != NULL ){
  182. if(node->coef > 0 ){ // 係數為正時
  183. printf( "+%dx^%d", node->coef, node->expon);
  184. }
  185. else{ // 係數為負時
  186. printf("%dx^%d", node->coef, node->expon);
  187. }
  188. node = node->next; // 到下一個node
  189. }
  190. printf( "\n" );
  191.  
  192. }
  193.  
  194. // 釋回節點串列空間
  195. link freelist( link head )
  196. {
  197. link temp;
  198.  
  199. while(temp != NULL){
  200. temp = head->next;
  201. free(head);
  202. head = temp;
  203. }
  204.  
  205. head = NULL;
  206. return head;
  207. }
  208.  
  209.  
  210.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:3:19: error: iostream: No such file or directory
prog.c: In function ‘main’:
prog.c:28: warning: unused variable ‘temp’
prog.c:24: warning: unused variable ‘ptr’
prog.c:43: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result
prog.c: In function ‘createpoly’:
prog.c:56: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:58: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:59: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c: In function ‘add’:
prog.c:102: warning: left-hand operand of comma expression has no effect
prog.c: In function ‘main’:
prog.c:45: warning: control reaches end of non-void function
stdout
Standard output is empty