Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void sort(int* , int* , char* , int* , int );
- void power(int , int , int* , char* , int* , char* );
- void transf(int* , int* , int* , int );
- void coefficient(int , int* , int , char* , int* );
- int gcd(int , int );
- int isnumber(char );
- int isletter(char );
- int main(void) {
- char str[100002] = "\0", sign[50002] = "\0";
- char arg = 'x';
- int coef[50002] = {0};
- int den[50002] = {0};
- int pow[25002] = {0};
- int i = 0, j = 0, len = 0, count = 0;
- scanf("%c", &str[len]);
- while ((str[len] != '\n') && (str[len] != '\0')) {
- if ((str[len] != ' ') && (str[len] != '*')) {
- if ((str[len] == '+') || (str[len] == '-')) {
- count++;
- }
- len++;
- }
- scanf("%c", &str[len]);
- }
- if ((str[0] != '-') && (len != 0)) {
- count++;
- sign[0] = '+';
- }
- for (int k = 0; k < count; k++) {
- if ((str[j] == '+') || (str[j] == '-')) {
- sign[k] = str[j];
- j++;
- }
- if (str[j] != 0) {
- if (isnumber(str[j])) {
- coefficient(len, &j, k, str, coef);
- }
- else {
- coef[k] = 1;
- }
- if (j < len) {
- if (isletter(str[j])) {
- arg = str[j];
- j++;
- if (j < len) {
- if (str[j] == '^') {
- j++;
- power(len, k, &j, str, pow, sign);
- }
- else {
- pow[k] = 2;
- }
- }
- else
- pow[k] = 2;
- }
- else {
- pow[k] = 1;
- }
- }
- else
- pow[k] = 1;
- transf(coef, den, pow, k);
- }
- else {
- while ((str[j] != '+') && (str[j] != '-') && (j < len))
- j++;
- }
- }
- if (count > 1)
- sort(coef, den, sign, pow, count);
- if (count == 0)
- printf("C\n");
- else {
- j = 0;
- while (j < count) {
- if (coef[j] != 0) {
- if ((j == i) && (sign[j] == '-'))
- printf("%c", sign[j]);
- if (j != i)
- printf(" %c ", sign[j]);
- if (pow[j] != 0) {
- if (den[j] != 0)
- printf("%d/%d*%c", coef[j], den[j], arg);
- else if (coef[j] != 1)
- printf("%d*%c", coef[j], arg);
- else
- printf("%c", arg);
- }
- else {
- if (den[j] != -1)
- printf("%d/%d", coef[j], den[j]);
- else
- printf("%d", coef[j]);
- }
- if (pow[j] > 1) {
- printf("^%d", pow[j]);
- }
- j++;
- }
- else {
- j++;
- i++;
- }
- }
- if (i != count)
- printf(" + C\n");
- else
- printf("C\n");
- }
- return 0;
- }
- void transf(int *coef, int *den, int *pow, int count) {
- if (coef[count] % pow[count] == 0) {
- coef[count] /= pow[count];
- den[count] = 0;
- }
- else {
- int nod = gcd(coef[count], pow[count]);
- den[count] = pow[count] / nod;
- coef[count] /= nod;
- }
- }
- void coefficient(int len, int *j, int k, char *str, int *coef) {
- while ((isnumber(str[*j])) && (*j < len)) {
- coef[k] = (int) coef[k] * 10 + str[*j] - '0';
- *j += 1;
- }
- }
- void power(int len, int k, int *j, char *str, int *pow, char *sign) {
- while ((isnumber(str[*j])) && (*j < len)) {
- pow[k] = (int) pow[k] * 10 + str[*j] - '0';
- *j += 1;
- }
- pow[k]++;
- }
- int gcd(int x, int y) {
- if (y == 0)
- return x;
- return gcd(y, x % y);
- }
- int isletter(char c) {
- if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
- return 1;
- return 0;
- }
- int isnumber(char c) {
- if ((c >= '0') && (c <= '9'))
- return 1;
- return 0;
- }
- void sort(int *coef, int *den, char *sign, int *pow, int n) {
- char c;
- for (int i = 0; i < n; i++) {
- for (int j = i; j < n; j++) {
- if (pow[i] < pow[j]) {
- pow[i] += pow[j];
- pow[j] = pow[i] - pow[j];
- pow[i] -= pow[j];
- coef[i] += coef[j];
- coef[j] = coef[i] - coef[j];
- coef[i] -= coef[j];
- den[i] += den[j];
- den[j] = den[i] - den[j];
- den[i] -= den[j];
- c = sign[i];
- sign[i] = sign[j];
- sign[j] = c;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement