美文网首页
一元多项式

一元多项式

作者: star_night | 来源:发表于2017-09-24 17:53 被阅读0次
#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;
}

相关文章

  • PAT-B 1010 一元多项式求导(C语言)

    题目 链接:PAT (Basic Level) Practice 1010 一元多项式求导 设计函数求一元多项式的...

  • 机器学习实践系列2——多项式回归

    摘要:本文主要结合实际案例介绍多项式回归, 多项式回归主要包括一元多项式回归和多元多项式回归,本文主要介绍的是一元...

  • 高等代数理论基础3:一元多项式

    一元多项式 定义:形式表达式称为系数在数域P上的一元多项式 其中 项与系数 多项式中 定义:称为i次项,称为i次项...

  • 线性表及其实现

    多项式的表示(以一元多项式为例)一元多项式:顺序存储结构直接表示含义:每个数组元素蕴含两个信息:该元素的值表示系数...

  • 1010

    /1010 一元多项式求导 (25)(25 分)//设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为...

  • 用单链表表示一元多项式及其简单计算

    github源码 一元多项式 在数学上,一个一元多项式Pn(x)可以按升幂写为:Pn(x) = p0 + p1x ...

  • 【乙】1010 一元多项式求导

    1010. 一元多项式求导 (25) 设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为n*xn-1。...

  • 1010. 一元多项式求导

    原题链接一元多项式求导: 设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为n*xn-1。) 输入格式...

  • 第2章 线性表及其实现

    一元多项式的表示 分析:多项式的关键数据:多项式项数n,各项系数ai 及指数 i 方法1:顺序存储结构直接表示 利...

  • 一元多项式

    1 、问题描述: 功能:设计一个一元多项式加法器。 输入并建立多项式,实现两个多项式的加法运算。 要求: 1) 界...

网友评论

      本文标题:一元多项式

      本文链接:https://www.haomeiwen.com/subject/cstlextx.html