结构体存储多项式
#include<bits/stdc++.h>
using namespace std;
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
struct Polynomial{//多项式
int cof, ind;//系数 指数
};
typedef Polynomial ElemType;
typedef int Status;
typedef struct LNode{
ElemType data;
LNode *next;
}*Linklist;
Status ListCreate_L(Linklist &L){
LNode *curPtr, *rearPtr;
L = (LNode*)malloc(sizeof(LNode));
if(!L) exit(OVERFLOW);
L->next = NULL;
rearPtr = L;
int cof, ind;
while(scanf("%d%d",&cof, &ind)!=EOF){
curPtr = (LNode*)malloc(sizeof(LNode));
if(!curPtr) exit(OVERFLOW);
curPtr->data.cof = cof, curPtr->data.ind = ind;
curPtr->next = NULL;//最后一个节点值 赋空
rearPtr->next = curPtr;
rearPtr = curPtr;
}
return OK;
}
void ListDelet_L(Linklist &L, LNode *p){//本题指数递降 无需调用
}
void Derivate(Linklist &L){
LNode *p, *q;//q用于记录前驱 以便实现删除操作
p = L->next; q = L;
while(p){
int cof = p->data.cof, ind = p->data.ind;
if(ind == 0){//删除
q->next = p->next;
//free(p);
}
else{
p->data.cof *= ind;
p->data.ind -= 1;
}
q = p;
p = p->next;
}
}
void ListPrint_L(Linklist L){
LNode *p = L->next;
if(!p) {printf("0 0"); return ;}
while(p){
if(p->next == NULL) printf("%d %d", p->data.cof, p->data.ind);
else printf("%d %d ",p->data.cof, p->data.ind);
p = p->next;
}
}
int main(){
Linklist L;
ListCreate_L(L);
Derivate(L);
ListPrint_L(L);
//system("pause");
return 0;
}```
仅供参考 切勿抄袭
hang hang hang !!!