一元多项式

2017-09-24  本文已影响0人  star_night
#include<stdio.h>
#include<math.h>

typedef struct Node{
  int a;
  int b;
  struct Node *next;
}node;

#define SIZE sizeof(node)

node *initList();
void creatList(node *head, int n);
void printList(node *head);
node *listAdd(node *A, node *B);
node *listSub(node *A, node *B);
node *listTime(node *A, node *B);
int listIs(node *A,int x);
node *listDer(node *A);
void funa(node *head);
void funb(node *head);
void func(node *a, node *b);

int main(){
  int n;
// 相减
  scanf("%d\n", &n);
  node *h1 = initList();
  creatList(h1,n);

  scanf("%d\n", &n);
  node *h2 = initList();
  creatList(h2,n);

  printf("sub:\n");
  printList(h1);
  printList(h2);
  node *h3 = listSub(h1,h2);
  printList(h3);
// 相乘
  scanf("%d\n", &n);
  node *h4 = initList();
  creatList(h4,n);

  scanf("%d\n", &n);
  node *h5 = initList();
  creatList(h5,n);

  node *h6 = listTime(h4,h5);
  printf("time:\n");
  printList(h4);
  printList(h5);
  printList(h6);
// 求导
  scanf("%d\n", &n);
  node *h7 = initList();
  creatList(h7,n);

  node *h8 = listDer(h7);
  printf("der:\n");
  printList(h7);
  printList(h8);

  return 0;
}

node *initList(){
  node *p = (node*)malloc(SIZE);
  p->next = NULL;
  return p;
}
void creatList(node *head, int n){
  node *pre = head;
  while (n--) {
    node *new = (node*)malloc(SIZE);
    scanf("(%d,%d)", &new->a, &new->b);
    pre->next = new;
    pre = new;
  }
  pre->next = NULL;
}
void printList(node *head){
  node *cur = head->next;
  while (cur != NULL) {
    int a = cur->a;
    int b = cur->b;
    if(cur != head->next){
      if(a > 0)
        printf("+");
    }
    printf("%d", a);
    if(b != 0){
      printf("X");
      if (b != 1)
        printf("^%d", b);
    }
    cur = cur->next;
  }
  if (!head->next){
    printf("0");
  }
  printf("\n");
}
node *listAdd(node *A, node *B){
  node *list = initList();
  node *pre = list;
  node *Acur = A->next;
  node *Bcur = B->next;
  int a1,a2,b1,b2;
  while (Acur && Bcur) {
    a1 = Acur->a;
    b1 = Acur->b;
    a2 = Bcur->a;
    b2 = Bcur->b;
    node *new = (node*)malloc(SIZE);
    if(b1 == b2){
      if (a1 != -a2){
        new->a = a1 + a2;
        new->b = b1;
        pre->next = new;
        pre = new;
        Acur = Acur->next;
        Bcur = Bcur->next;
      }else{
        Acur = Acur->next;
        Bcur = Bcur->next;
      }
    }else if (b1 < b2) {
      new->a = a1;
      new->b = b1;
      pre->next = new;
      pre = new;
      Acur = Acur->next;
    }else if (b1 > b2) {
      new->a = a2;
      new->b = b2;
      pre->next = new;
      pre = new;
      Bcur = Bcur->next;
    }
  }
  if(!Acur){
    pre->next = Bcur;
  }else{
    pre->next = Acur;
  }
  return list;
}
node *listSub(node *A, node *B){
  node *list = initList();
  node *pre = list;
  node *Acur = A->next;
  node *Bcur = B->next;
  int a1,a2,b1,b2;
  while(Bcur){
    Bcur->a *= -1;
    Bcur = Bcur->next;
  }
  Bcur = B->next;
  while (Acur && Bcur) {
    a1 = Acur->a;
    b1 = Acur->b;
    a2 = Bcur->a;
    b2 = Bcur->b;
    node *new = (node*)malloc(SIZE);
    if(b1 == b2){
      if (a1 != -a2){
        new->a = a1 + a2;
        new->b = b1;
        pre->next = new;
        pre = new;
        Acur = Acur->next;
        Bcur = Bcur->next;
      }else{
        Acur = Acur->next;
        Bcur = Bcur->next;
      }
    }else if (b1 < b2) {
      new->a = a1;
      new->b = b1;
      pre->next = new;
      pre = new;
      Acur = Acur->next;
    }else if (b1 > b2) {
      new->a = a2;
      new->b = b2;
      pre->next = new;
      pre = new;
      Bcur = Bcur->next;
    }
  }
  if(!Acur){
    pre->next = Bcur;
  }else{
    pre->next = Acur;
  }
  return list;
}
node *listTime(node *A, node *B){
  node *list = initList();
  node *Acur = A->next;
  node *Bcur = B->next;
  node *pre = list;
  while (Acur) {
    while (Bcur) {
      int a1 = Acur->a;
      int a2 = Bcur->a;
      int b1 = Acur->b;
      int b2 = Bcur->b;
      node *new = (node*)malloc(SIZE);
      new->a = a1 * a2;
      new->b = b1 + b2;
      pre->next = new;
      pre = new;
      Bcur = Bcur->next;
    }
    Bcur = B->next;
    Acur = Acur->next;
  }
  pre->next = NULL;
  //合并同类项
  funa(list);
  //排序
  funb(list);
  return list;
}
void funa(node *head){
  node *pre = head;
  node *cur = head->next;

  while (cur) {
    int b1 = cur->b;
    node *Npre = cur;
    node *Ncur = cur->next;
    while (Ncur) {
      int b2 = Ncur->b;
      if (b1 == b2) {
        // printf("a1=%d a2=%d\n",b1,b2);
        Npre->next = Ncur->next;
        cur->a = cur->a + Ncur->a;
        Ncur = Ncur->next;
      }
      Npre = Npre->next;
      Ncur = Ncur->next;
    }
    cur = cur->next;
  }
}
void funb(node *head){
  node *pre = head;
  node *cur = pre->next;
  while (cur) {
    node *Npre = cur;
    node *Ncur = Npre->next;
    while (Ncur) {
      int b1 = cur->b;
      int b2 = Ncur->b;
      if(b1 > b2){
        func(cur, Ncur);
      }
      Ncur = Ncur->next;
    }
    cur = cur->next;
  }
}
// void funb(node *head){
//   node *cur = head->next;
//   node *pre = cur;
//   while (cur) {
//     node *Npre = cur;
//     node *Ncur = Npre->next;
//     while (Ncur) {
//       int b1 = cur->b;
//       int b2 = Ncur->b;
//       if (b1 > b2){
//         node *new = Npre->next;
//         pre->next = Ncur;
//         Npre->next = cur;
//         Ncur->next = cur->next;
//         cur->next = new;
//
//         node *p = Ncur;
//         Ncur = cur;
//         cur = p;
//       }
//       Ncur = Ncur->next;
//     }
//     cur = cur->next;
//   }
// }
void func(node *a, node *b){
  int na = a->a;
  int nb = a->b;

  a->a = b->a;
  a->b = b->b;

  b->a = na;
  b->b = nb;
}
int listIs(node *A, int x){
  int sum = 0;
  node *cur = A->next;
  while (cur != NULL) {
    int a = cur->a;
    int b = cur->b;
    sum += a * pow(x,b);
    cur = cur->next;
  }
  return sum;
}
node *listDer(node *head){
  node *pre = head;
  node *cur = pre->next;
  node *list = initList();
  node *Npre = list;
  while (cur) {
    if (cur->b != 0){
      node *new = (node*)malloc(SIZE);
      int a = cur->a;
      int b = cur->b;
      new->a = a * b;
      new->b = b-1;
      Npre->next = new;
      Npre = new;
    }
    cur = cur->next;
  }
  Npre->next = NULL;
  return list;
}

上一篇 下一篇

猜你喜欢

热点阅读