#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct Node{
float coef; // 系数
int exp; // 指数
struct Node *next;
}Node, *Polynomial;
// 创建新节点
Node* createNode(float coef, int exp){
Node *node = (Node*)malloc(sizeof(Node));
node->coef = coef;
node->exp = exp;
node->next = NULL;
return node;
}
// 创建多项式
void createPolynomial(Polynomial *p){
int n;
float coef;
int exp;
printf("请输入待插入多项式的项数:");
scanf("%d", &n);
for(int i=0; i<n; i++){
printf("请插入新第%d项的系数和指数:(按降幂输入)", i+1);
scanf("%f %d", &coef, &exp);
if(coef != 0){
Node *node = createNode(coef, exp);
if(*p == NULL){
*p = node;
}else{
Node *last = *p;
while(last->next != NULL){
last = last->next;
}
last->next = node;
}
}
else
{
printf("系数不应为0!\n");
}
}
}
// 输出多项式
void printPolynomial(Polynomial p){
if(p == NULL){
printf("0\n");
}else{
while(p != NULL){
printf("%.2f", p->coef);
if(p->exp != 0){
printf("x^%d", p->exp);
}
if(p->next != NULL && p->next->coef > 0){
printf("+");
}
p = p->next;
}
printf("\n");
}
}
//多项式赋值
void assignPolynomial(Polynomial *a, Polynomial b){
Node *p = NULL;
Node *q = b;
//int k;
// 如果b为空链表,直接返回
if (q == NULL) {
printf("赋值多项式为空!\n");
return;
}
// 如果a不为空链表,先删除a的所有节点
while(*a != NULL){
Node *temp = *a;
*a = (*a)->next;
free(temp);
}
/*k = (p == NULL);
printf("%d\n",k);*/
// 复制b的所有节点到a
while(q != NULL){
Node *node = createNode(q->coef, q->exp);
if(p == NULL){
//printf("成功");
*a = node;
p = *a;
}else{
p->next = node;
p = p->next;
}
q = q->next;
}
}
// 多项式相加
Polynomial addPolynomial(Polynomial a, Polynomial b){
Node *pa = a;
Node *pb = b;
Polynomial c = NULL, pc = NULL;
while(pa != NULL && pb != NULL){
if(pa->exp > pb->exp){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
}else if(pa->exp < pb->exp){
if(pb->coef != 0){
Node *node = createNode(pb->coef, pb->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pb = pb->next;
}else{
float coef_sum = pa->coef + pb->coef;
if(coef_sum != 0){
Node *node = createNode(coef_sum, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
pb = pb->next;
}
}
while(pa != NULL){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
pc->next = node;
pc = pc->next;
}
pa = pa->next;
}
while(pb != NULL){
if(pb->coef != 0){
Node *node = createNode(pb->coef, pb->exp);
pc->next = node;
pc = pc->next;
}
pb = pb->next;
}
return c;
}
// 多项式相减
Polynomial subPolynomial(Polynomial a, Polynomial b){
Node *pa = a;
Node *pb = b;
Polynomial c = NULL, pc = NULL;
while(pa != NULL && pb != NULL){
if(pa->exp > pb->exp){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
}else if(pa->exp < pb->exp){
if(-pb->coef != 0){
Node *node = createNode(-pb->coef, pb->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pb = pb->next;
}else{
float coef_diff = pa->coef - pb->coef;
if(coef_diff != 0){
Node *node = createNode(coef_diff, pa->exp);
if(c == NULL){
c = node;
pc = c;
}else{
pc->next = node;
pc = pc->next;
}
}
pa = pa->next;
pb = pb->next;
}
}
while(pa != NULL){
if(pa->coef != 0){
Node *node = createNode(pa->coef, pa->exp);
pc->next = node;
pc = pc->next;
}
pa = pa->next;
}
while(pb != NULL){
if(-pb->coef != 0){
Node *node = createNode(-pb->coef, pb->exp);
pc->next = node;
pc = pc->next;
}
pb = pb->next;
}
return c;
}
// 多项式求值
float evalPolynomial(Polynomial p, float x){
float sum = 0;
while(p != NULL){
float term = p->coef * pow(x, p->exp);
sum += term;
p = p->next;
}
return sum;
}
// 多项式求导
Polynomial derPolynomial(Polynomial p){
Node *q = NULL, *last = NULL;
while(p != NULL){
if(p->exp > 0){
float coef_der = p->coef * p->exp;
Node *node = createNode(coef_der, p->exp-1);
if(q == NULL){
q = node;
last = q;
}else{
last->next = node;
last = last->next;
}
}
p = p->next;
}
return q;
}
//更新多项式
void updatePolynomial(Polynomial *p)
{
while(*p != NULL){
Node *temp = *p;
*p = (*p)->next;
free(temp);
}
int n;
float coef;
int exp;
printf("请输入待插入多项式的项数:");
scanf("%d", &n);
for(int i=0; i<n; i++){
printf("请插入新第%d项的系数和指数:(按降幂输入)", i+1);
scanf("%f %d", &coef, &exp);
if(coef != 0){
Node *node = createNode(coef, exp);
if(*p == NULL){
*p = node;
}else{
Node *last = *p;
while(last->next != NULL){
last = last->next;
}
last->next = node;
}
}
else
{
printf("系数不应为0!\n");
}
}
}
int main(){
Polynomial A = NULL, B = NULL;
int choice;
float x;
while(1){
printf("请选择操作:\n");
printf("1.向多项式A插入\t\t");
printf("2.向多项式B插入\n");
printf("3.输出多项式A\t\t");
printf("4.输出多项式B\n");
printf("5.将A赋值给B\t\t");
printf("6.将B赋值给A\n");
printf("7.计算A+B\t\t");
printf("8.计算A-B\n");
printf("9.计算多项式的值\n");
printf("10.求多项式的导数\n");
printf("11.更新多项式A\t\t");
printf("12.更新多项式B\n");
printf("13.退出\n");
scanf("%d", &choice);
switch(choice){
case 1:
createPolynomial(&A);
break;
case 2:
createPolynomial(&B);
break;
case 3:
printf("A = ");
printPolynomial(A);
break;
case 4:
printf("B = ");
printPolynomial(B);
break;
case 5:
assignPolynomial(&B, A);
/*if (B==NULL){
assignPolynomial(&B,A);
}*/
break;
case 6:
assignPolynomial(&A, B);
/*if (A==NULL){
assignPolynomial(&A,B);
}*/
break;
case 7:
printf("A + B = ");
printPolynomial(addPolynomial(A, B));
break;
case 8:
printf("A - B = ");
printPolynomial(subPolynomial(A, B));
break;
case 9:
printf("请输入x的值:");
scanf("%f", &x);
printf("A(%g) = %g\n", x, evalPolynomial(A, x));
printf("B(%g) = %g\n", x, evalPolynomial(B, x));
break;
case 10:
printf("A' = ");
printPolynomial(derPolynomial(A));
printf("B' = ");
printPolynomial(derPolynomial(B));
break;
case 11:
updatePolynomial(&A);
break;
case 12:
updatePolynomial(&B);
break;
case 13:
return 0;
default:
printf("无效操作,请重新选择。\n");
}
}
return 0;
}