Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void sort(int* , int* , char* , int* , int );
  5. void power(int , int , int* , char* , int* , char* );
  6. void transf(int* , int* , int* , int );
  7. void coefficient(int , int* , int , char* , int* );
  8. int gcd(int , int );
  9. int isnumber(char );
  10. int isletter(char );
  11.  
  12. int main(void) {
  13. char str[100002] = "\0", sign[50002] = "\0";
  14. char arg = 'x';
  15. int coef[50002] = {0};
  16. int den[50002] = {0};
  17. int pow[25002] = {0};
  18. int i = 0, j = 0, len = 0, count = 0;
  19. scanf("%c", &str[len]);
  20. while ((str[len] != '\n') && (str[len] != '\0')) {
  21. if ((str[len] != ' ') && (str[len] != '*')) {
  22. if ((str[len] == '+') || (str[len] == '-')) {
  23. count++;
  24. }
  25. len++;
  26. }
  27. scanf("%c", &str[len]);
  28. }
  29. if ((str[0] != '-') && (len != 0)) {
  30. count++;
  31. sign[0] = '+';
  32. }
  33. for (int k = 0; k < count; k++) {
  34. if ((str[j] == '+') || (str[j] == '-')) {
  35. sign[k] = str[j];
  36. j++;
  37. }
  38. if (str[j] != 0) {
  39. if (isnumber(str[j])) {
  40. coefficient(len, &j, k, str, coef);
  41. }
  42. else {
  43. coef[k] = 1;
  44. }
  45. if (j < len) {
  46. if (isletter(str[j])) {
  47. arg = str[j];
  48. j++;
  49. if (j < len) {
  50. if (str[j] == '^') {
  51. j++;
  52. power(len, k, &j, str, pow, sign);
  53. }
  54. else {
  55. pow[k] = 2;
  56. }
  57. }
  58. else
  59. pow[k] = 2;
  60. }
  61. else {
  62. pow[k] = 1;
  63. }
  64. }
  65. else
  66. pow[k] = 1;
  67. transf(coef, den, pow, k);
  68. }
  69. else {
  70. while ((str[j] != '+') && (str[j] != '-') && (j < len))
  71. j++;
  72. }
  73. }
  74. if (count > 1)
  75. sort(coef, den, sign, pow, count);
  76. if (count == 0)
  77. printf("C\n");
  78. else {
  79. j = 0;
  80. while (j < count) {
  81. if (coef[j] != 0) {
  82. if ((j == i) && (sign[j] == '-'))
  83. printf("%c", sign[j]);
  84. if (j != i)
  85. printf(" %c ", sign[j]);
  86. if (pow[j] != 0) {
  87. if (den[j] != 0)
  88. printf("%d/%d*%c", coef[j], den[j], arg);
  89. else if (coef[j] != 1)
  90. printf("%d*%c", coef[j], arg);
  91. else
  92. printf("%c", arg);
  93. }
  94. else {
  95. if (den[j] != -1)
  96. printf("%d/%d", coef[j], den[j]);
  97. else
  98. printf("%d", coef[j]);
  99. }
  100. if (pow[j] > 1) {
  101. printf("^%d", pow[j]);
  102. }
  103. j++;
  104. }
  105. else {
  106. j++;
  107. i++;
  108. }
  109. }
  110. if (i != count)
  111. printf(" + C\n");
  112. else
  113. printf("C\n");
  114. }
  115. return 0;
  116. }
  117.  
  118. void transf(int *coef, int *den, int *pow, int count) {
  119. if (coef[count] % pow[count] == 0) {
  120. coef[count] /= pow[count];
  121. den[count] = 0;
  122. }
  123. else {
  124. int nod = gcd(coef[count], pow[count]);
  125. den[count] = pow[count] / nod;
  126. coef[count] /= nod;
  127. }
  128. }
  129. void coefficient(int len, int *j, int k, char *str, int *coef) {
  130. while ((isnumber(str[*j])) && (*j < len)) {
  131. coef[k] = (int) coef[k] * 10 + str[*j] - '0';
  132. *j += 1;
  133. }
  134. }
  135. void power(int len, int k, int *j, char *str, int *pow, char *sign) {
  136. while ((isnumber(str[*j])) && (*j < len)) {
  137. pow[k] = (int) pow[k] * 10 + str[*j] - '0';
  138. *j += 1;
  139. }
  140. pow[k]++;
  141. }
  142. int gcd(int x, int y) {
  143. if (y == 0)
  144. return x;
  145. return gcd(y, x % y);
  146. }
  147. int isletter(char c) {
  148. if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
  149. return 1;
  150. return 0;
  151. }
  152. int isnumber(char c) {
  153. if ((c >= '0') && (c <= '9'))
  154. return 1;
  155. return 0;
  156. }
  157. void sort(int *coef, int *den, char *sign, int *pow, int n) {
  158. char c;
  159. for (int i = 0; i < n; i++) {
  160. for (int j = i; j < n; j++) {
  161. if (pow[i] < pow[j]) {
  162. pow[i] += pow[j];
  163. pow[j] = pow[i] - pow[j];
  164. pow[i] -= pow[j];
  165. coef[i] += coef[j];
  166. coef[j] = coef[i] - coef[j];
  167. coef[i] -= coef[j];
  168. den[i] += den[j];
  169. den[j] = den[i] - den[j];
  170. den[i] -= den[j];
  171. c = sign[i];
  172. sign[i] = sign[j];
  173. sign[j] = c;
  174. }
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement