Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int coe[50],pow[50];
  4. void sort(int start,int end)
  5. {
  6. int i,j,temp;
  7. for(i=start;i<end;i++)
  8. {
  9. for(j=start;j<end;j++)
  10. {
  11. if(pow[i] > pow [j])
  12. {
  13. temp=pow[i];
  14. pow[i]=pow[j];
  15. pow[j]=temp;
  16. temp=coe[i];
  17. coe[i]=coe[j];
  18. coe[j]=temp;
  19. }
  20. }
  21. }
  22. }
  23. void combine(int start,int end)
  24. {
  25. int i,j;
  26. for(i=start;i<end;i++)
  27. {
  28. for(j=i+1;j<=end;j++)
  29. {
  30. if(pow[i] == pow[j])
  31. {
  32. coe[i]+=coe[j];
  33. pow[j]=0;
  34. coe[j]=0;
  35. }
  36. }
  37. }
  38. }
  39. void Print(int start,int end)
  40. {
  41. int i,j=0;
  42. for(i=start;i<=end;i++)
  43. {
  44. if(coe[i] != 0)//係數不為0
  45. {
  46. if(pow[i] != 0)
  47. {
  48. if(coe[i] == -1 )
  49. {
  50. printf("-x^%d",pow[i]);
  51. j=1;
  52. }
  53. else if(coe[i] == 1)
  54. {
  55. printf("x^%d",pow[i]);
  56. j=1;
  57. }
  58. else
  59. {
  60. printf("%dx^%d",coe[i],pow[i]);
  61. j=1;
  62. }
  63.  
  64. }
  65. else
  66. {
  67. printf("%d",coe[i]);
  68. j=0;
  69. }
  70. }
  71. if(j == 1)//係數為0
  72. {
  73. if(coe[i] >0)
  74. {
  75. printf("+");
  76. }
  77. }
  78. j=1;
  79. }
  80. }
  81. int main()
  82. {
  83. int i,j;
  84. int startA=0,startB,finishA=0,finishB=0,avail;
  85.  
  86. while(1)
  87. {
  88. printf("請輸入多項式1 (係數 次方):");
  89. scanf("%d %d",&coe[finishA],&pow[finishA]);
  90.  
  91. if(coe[finishA] == 0 && pow[finishA] == 0)
  92. {
  93. finishA--;
  94. startB = finishA+1;
  95. break;
  96. }
  97. else
  98. {
  99. finishA++;
  100. }
  101. }
  102. combine(startA,finishA);
  103. sort(startA,finishA);
  104. printf("\n多項式1為:\n");
  105. Print(startA,finishA);
  106.  
  107. finishB = startB;
  108. printf("\n");
  109. while(1)
  110. {
  111. printf("請輸入多項式2 (係數 次方):");
  112. scanf("%d %d",&coe[finishB],&pow[finishB]);
  113.  
  114. if(coe[finishB] == 0 && pow[finishB] == 0)
  115. {
  116. finishB--;
  117. avail = finishB+1;
  118. break;
  119. }
  120. else
  121. {
  122. finishB++;
  123. }
  124. }
  125. combine(startB,finishB);
  126. sort(startB,finishB);
  127. printf("\n多項式2為:\n");
  128. Print(startB,finishB);
  129.  
  130. for(i=startA;i<=finishA;i++)
  131. {
  132. for(j=startB;j<=finishB;j++)
  133. {
  134. coe[avail] = coe[i]*coe[j];
  135. pow[avail] = pow[i]+pow[j];
  136. avail++;
  137. }
  138. }
  139. combine(finishB+1,avail);
  140. sort(finishB+1,avail);
  141. printf("\n");
  142. printf("相乘過後之多項式為:\n");
  143. Print(finishB+1,avail);
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement