zoukankan      html  css  js  c++  java
  • 多项式求导

    #include<iostream>
    using namespace std;
    class poly{
    public:
    int coef;
    int expon;
    poly *next;
    };
    poly*input(); /*输入函数*/
    poly*attach(poly*rear, int c, int e); /*链接函数,连接求导后的结果*/
    poly*func(poly*head); /*实现求导的函数*/
    int main(){
    poly*p;
    p = input();
    p = func(p);
    while (p){
    cout << p->coef << " " << p->expon << (p->next==NULL?' ':' ');
    p = p->next;
    }
    return 0;
    }
    poly*input(){
    poly*p = new poly; /*申请新空间*/
    poly*head = p, *p1 = p;
    while (cin >> p->coef >> p->expon){
    char ch = getchar();
    p1 = p;
    p = new poly;
    p1->next = p;
    if (ch == ' ') /*碰到换行键跳出*/
    break;
    }
    delete p; /*删除最后末尾的空节点*/
    p1->next = NULL;
    return head; /*返回头结点*/
    }
    poly * attach(poly*rear, int c, int e){
    poly*p = new poly; /*插入先申请空间*/
    p->coef = c;
    p->expon = e;
    rear->next = p;
    rear = p;
    return rear; /*返回末尾指针*/
    }
    poly*func(poly*p){
    poly*head = new poly; /*申请一个空节点*/
    poly*rear = head;
    if (p->expon == 0){ /*如果只有常数项,分开做处理*/
    p->coef = 0;
    return p;
    }
    while (p&&p->expon){ /*跳出条件为p为空或者p的指数为o,对于求导来说,指数为0的可以省略*/
    p->coef = p->coef*p->expon;
    p->expon--;
    rear = attach(rear, p->coef, p->expon);
    p = p->next;
    }
    rear->next = NULL; /*扫尾工作*/
    poly*temp = head;
    head = head->next; //头结点移到下一位
    delete temp; //删除空的头结点
    return head;
    }

  • 相关阅读:
    vmware磁盘空间扩展
    Winrar发现损坏的压缩文件头
    java ASM动态生成类
    使用ffmpeg将任意格式视频转MP4格式
    mongodb导入csv结构化数据
    Vmware黑屏解决方法
    mysql命令行导入结构化数据
    mysql导入慢解决方法
    CategoryPanelGroup动态生成节点
    delphi XE7 判断手机返回键
  • 原文地址:https://www.cnblogs.com/td15980891505/p/4426343.html
Copyright © 2011-2022 走看看