fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6. #include <stdbool.h>
  7. typedef struct node_s{
  8. int data;
  9. struct node_s *next;
  10. } Node;
  11. typedef Node* Node_p;
  12. Node_p create(int data){
  13. Node_p newNode=(Node_p)malloc(sizeof(Node));
  14. newNode->data=data;
  15. newNode->next=NULL;
  16. return newNode;
  17. }
  18. int counting_length(Node_p p){
  19. Node_p current=p;
  20. int length=0;
  21. while(current!=NULL){
  22. length+=1;
  23. current=current->next;
  24. }
  25. return length-1;
  26. }
  27. void printing(Node_p p){
  28. int length=counting_length(p);
  29. int max=length;
  30. Node_p current=p;
  31. while(current!=NULL){
  32. if(length==max){
  33. if(current->data==0){
  34. if(length==0) printf("0");
  35. else{
  36. length--;
  37. max--;
  38. }
  39. }
  40. else {
  41. if(current->data==1){
  42. printf("x^%d",length--);
  43. }
  44. else if(current->data==-1){
  45. printf("-x^%d",length--);
  46. }
  47. else printf("%dx^%d",current->data,length--);
  48. }
  49. }
  50. else if(length==1){
  51. if(current->data>0){
  52. if(current->data==1) printf("+x");
  53. else printf("+%dx",current->data);
  54. }
  55. else if(current->data<0){
  56. if(current->data==-1) printf("-x");
  57. else printf("%dx",current->data);
  58. }
  59. else length--;
  60. length=0;
  61. }
  62. else if(length>0){
  63. if(current->data>0){
  64. if(current->data==1) printf("+x^%d",length--);
  65. else printf("+%dx^%d",current->data,length--);
  66. }
  67. else if(current->data<0){
  68. if(current->data==-1) printf("-x^%d",length--);
  69. else printf("%dx^%d",current->data,length--);
  70. }
  71. else length--;
  72. }
  73. else{
  74. if(current->data>0){
  75. printf("+%d",current->data);
  76. }
  77. else if(current->data<0){
  78. printf("%d",current->data);
  79. }
  80. else length--;
  81. }
  82. current=current->next;
  83. }
  84. printf("\n");
  85. }
  86. void insertNodeback(Node_p *p,int data){
  87. Node_p newNode=create(data);
  88. if((*p)==NULL) (*p)=newNode;
  89. else{
  90. Node_p current=(*p);
  91. while(current->next!=NULL) current=current->next;
  92. current->next=newNode;
  93. }
  94. }
  95. void insertNodefront(Node_p *p,int data){
  96. Node_p newNode=create(data);
  97. if((*p)==NULL) (*p)=newNode;
  98. else{
  99. newNode->next=(*p);
  100. (*p)=newNode;
  101. }
  102. }
  103. void separate_to_data(char equation[], Node_p *p) {
  104. char *temp = strtok(equation, " ");
  105. while (temp != NULL) {
  106. int number = atoi(temp);
  107. insertNodefront(p, number);
  108. temp = strtok(NULL, " ");
  109. }
  110. }
  111.  
  112. void getdata(Node_p *p){
  113. char equation[100];
  114. scanf("%[^\n]%*c",equation);
  115. separate_to_data(equation,p);
  116. }
  117. Node_p add(Node_p p_1,Node_p p_2){
  118. Node_p equa_1=p_1;
  119. Node_p equa_2=p_2;
  120. Node_p answer=NULL;
  121. int length_1=counting_length(equa_1);
  122. int length_2=counting_length(equa_2);
  123. if(length_1>length_2){
  124. for(int i=0;i<length_1-length_2;i++) insertNodeback(&equa_2,0);
  125. }
  126. else if(length_1<length_2){
  127. for(int i=0;i<length_2-length_1;i++) insertNodeback(&equa_1,0);
  128. }
  129. while(equa_1!=NULL&&equa_2!=NULL){
  130. int temp=(equa_1->data)+(equa_2->data);
  131. insertNodefront(&answer,temp);
  132. equa_1=equa_1->next;
  133. equa_2=equa_2->next;
  134. }
  135. return answer;
  136. }
  137. Node_p minus(Node_p p_1,Node_p p_2){
  138. Node_p equa_1=p_1;
  139. Node_p equa_2=p_2;
  140. Node_p answer=NULL;
  141. int length_1=counting_length(equa_1);
  142. int length_2=counting_length(equa_2);
  143. if(length_1>length_2){
  144. for(int i=0;i<length_1-length_2;i++) insertNodeback(&equa_2,0);
  145. }
  146. else if(length_1<length_2){
  147. for(int i=0;i<length_2-length_1;i++) insertNodeback(&equa_1,0);
  148. }
  149. while(equa_1!=NULL&&equa_2!=NULL){
  150. int temp=(equa_1->data)-(equa_2->data);
  151. insertNodefront(&answer,temp);
  152. equa_1=equa_1->next;
  153. equa_2=equa_2->next;
  154. }
  155. return answer;
  156. }
  157. Node_p times(Node_p p_1,Node_p p_2){
  158. Node_p equa_1=p_1;
  159. Node_p equa_2=p_2;
  160. Node_p answer=NULL;
  161. int length_1=counting_length(equa_1);
  162. int length_2=counting_length(equa_2);
  163. int max_length=length_1+length_2+1;
  164. for(int i=0;i<max_length;i++) insertNodefront(&answer,0);
  165. Node_p origin=answer;
  166. while(equa_1!=NULL){
  167. Node_p current=answer;
  168. while(equa_2!=NULL){
  169. int temp=(equa_1->data)*(equa_2->data);
  170. (current->data)+=temp;
  171. current=current->next;
  172. equa_2=equa_2->next;
  173. }
  174. answer=answer->next;
  175. equa_1=equa_1->next;
  176. equa_2=p_2;
  177. }
  178. Node_p final_answer=NULL;
  179. while(origin!=NULL){
  180. insertNodefront(&final_answer,origin->data);
  181. origin=origin->next;
  182. }
  183. return final_answer;
  184. }
  185. int main() {
  186. Node_p equation_1=NULL;
  187. Node_p equation_2=NULL;
  188. Node_p answer=NULL;
  189. getdata(&equation_1);
  190. getdata(&equation_2);
  191. printing(add(equation_1,equation_2));
  192. printing(minus(equation_1,equation_2));
  193. printing(times(equation_1,equation_2));
  194. }
Success #stdin #stdout 0s 5548KB
stdin
2 3 0 1 -1
1 0 -1 4 -3 2
stdout
x^5+2x^4+2x^3+4x^2-2x+1
-x^5+2x^4+4x^3-4x^2+4x-3
2x^9+3x^8-2x^7+6x^6+5x^5-6x^4+11x^3-7x^2+5x-2