编了两天,希望楼主打赏点财富值
#include
#include
#include
struct polylink
{
x09char ch; /*变量名*/
x09float coef; /* 系数域*/
x09int expn; /* 指数域*/
x09struct polylink *next; /* 链域*/
};
/*初始化链表*/
polylink *initpoly()
{
x09polylink *p;
x09p = (polylink *)malloc(sizeof(polylink));
x09if(p == NULL)
x09{
x09x09printf("memory error!");
x09x09exit(0);
x09}
x09p->next = NULL;
x09return p;
}
/*销毁链表*/
void destroy_link(polylink *p)
{
x09polylink *q;
x09while(p != NULL)
x09{
x09x09q = p;
x09x09p = p->next;
x09x09free(q);
x09}
}
/*将多项式存入链表*/
polylink * creat_ploy(polylink *p)
{
x09char c[1];
x09int i = 0, n=0;
x09polylink *p1;
x09p = initpoly();
x09printf("input variable's name 输入变量名:n");
x09scanf("%s", c);x09x09
x09printf("input the polynomial's number 输入多项式项数:n");
x09scanf("%d", &n);
x09for(i = 0; i < n; i++)
x09{
x09x09p1 = (polylink *)malloc(sizeof(polylink));
x09x09if(p1 == NULL)/*分配不成功时*/
x09x09{
x09x09x09printf("memory error!");
x09x09x09exit(0);
x09x09}
x09x09printf("input the %dst's coefficient 输入第%d项的系数:n", i+1, i+1);
x09x09scanf("%f", &p1->coef);
x09x09printf("input the variable's index 输入变量的指数:n");
x09x09scanf("%d", &p1->expn);
x09x09p1->ch = c[0];
x09x09p1->next = p->next;
x09x09p->next = p1;
x09}
x09return p;
}
/*删除节点函数*/
polylink *delnode(polylink * h , polylink * maxp) /*删除一个节点,返回剩下的链表首地址*/
{
x09polylink *t;
x09t = h;
while(t->next != maxp )
x09x09t=t->next;x09x09x09x09 /*找到maxp 的前节点t */
x09t->next = maxp->next ; /*删除maxp,maxp后面的接到t后面*/
maxp->next = NULL;
return h; /*链首依然是h ,返回 */
}
/*求表长函数*/
void link_lenth(polylink *p, int &a)/* &是引用*/
{
x09while(p->next != NULL)
x09{
p = p->next;
x09x09a++;
x09}
}
/*排序*/
polylink *sort(polylink *h)
{
x09int min; /*保存指数最小的值*/
x09polylink *t=NULL, *minp=NULL, *head=NULL;
x09if(h ->next == NULL)
x09{
x09x09printf("don't exist number");
x09x09exit(0);
x09}
while(h->next !=NULL) /*只要当前链中不为空就循环 */
{
t= h->next; // t :临时指针
min=t->expn; minp=t; /*把当前t中的值作为最小*/
while (t->next !=NULL) /*只要t后面还有节点就循环*/
{
x09x09x09t=t->next ; /*t往后移动一个*/
if (t->expn < min) /*如果t中的值大于maxn,则记下其值和位置*/
{
x09x09x09x09min = t->expn;
minp=t;
x09x09x09}
} /*找出当前头开始在链中最大节点 maxp*/
h = delnode(h, minp); /*删除minp节点,返回剩下的链表*/
minp->next=head; /*将每次的最小节点接在头结点之前*/
x09x09head = minp;x09 /*head 重新回到该链的开头*/
x09}
x09h->next = head; /*将head 接在头节点之后*/
x09return h;
}
/*对一元多项式求导*/
polylink *poly_derivation(polylink *p)
{
x09polylink *n, *q;
x09q= p;
x09if(p->next == NULL)
x09{
x09x09printf("polynomial don't exist!n");
x09}
x09while(p->next != NULL)
x09{
x09x09p = p->next;
x09x09if(p->expn == 0 || p->coef == 0) /*指数为零或者系数为零就删除*/
x09x09{
x09x09x09q = delnode(q, p);
x09x09}
x09}
x09n = q; /*保存删除无用节点的链表之后的头结点, 用于返回*/
x09if(q->next == NULL)
x09{
x09x09printf("polynomial don't exist!n");
x09}
x09while(q->next != NULL)
x09{
x09x09q = q->next;
x09x09q->coef = q->coef * q->expn; /*系数乘指数*/
x09x09q->expn = q->expn - 1;x09 /*指数减一*/
x09}
x09return n;
}
/*显示多项式*/
void display_poly(polylink *p)
{
x09int a;
x09p = sort(p); /*排序*/
x09while(p->next != NULL )
x09{
x09x09p = p->next;
x09x09a = 1;
x09x09if(p->coef < 0) a = 0;
x09x09a ? printf(" +"): printf(" ");
x09x09printf("%.1f%c^%d", p->coef, p->ch, p->expn);
x09}
x09printf("n");
}
/*查找函数*/
polylink *locate_link(polylink *p, int index)
{
x09
x09p = p->next;
x09while(p != NULL && p->expn != index)
x09x09p = p->next;
x09if(p == NULL)
x09x09return NULL;
x09else
x09 return p;
}
/*为了代码重用*/
void creat(polylink *&p1, polylink *&p2)
{
x09p1 = creat_ploy(p1);x09x09x09x09/*创建多项式*/
x09printf("another polynomial building:n");
x09p2 = creat_ploy(p2);x09x09x09x09/*创建多项式*/
x09printf("first :n");
x09display_poly(p1);x09x09x09x09x09/*显示创建的多项式*/
x09printf("the second:n");
display_poly(p2);x09x09x09x09x09/*显示创建的多项式*/
}
/*多项式相加*/
polylink * poly_add(polylink *head1, polylink *head2, char a)
{
x09char ch[1];
x09polylink *p1, *p2, *p, *r;
x09p1 = head1;
x09p2 = head2;
x09if(a == '+')x09x09/*其他函数调用时不执行*/
x09{
x09x09system("cls");
x09x09creat(p1, p2);x09/*创建p1 p2*/
x09}
x09r = p1;
x09while(r->next != NULL)
x09{
x09x09p1 = r->next;
x09x09r = delnode(r, p1);x09x09x09x09/*删除p1*/
x09x09p = locate_link(p2, p1->expn); /*查找p2中是否有 系数等于p1->expn的节点,有就返回该点,否则返回NULL*/
x09x09if(p != NULL)
x09x09x09p->coef = p->coef + p1->coef;
x09x09elsex09x09x09/*把节点p1插入到链表p2*/
x09x09{
x09x09x09
x09x09x09p1->next = p2->next;
x09x09x09p2->next = p1;
x09x09}
x09}
x09if(a == '+') /*被减或者乘运算时不调用*/
x09{
x09x09printf("add result:n");
x09 display_poly(p2); /*显示*/
x09x09printf("whether to derivat:y/nn");
x09x09scanf("%s", ch);
x09x09if( ch[0] == 'Y' || ch[0] == 'y')
x09x09{
p2 = poly_derivation(p2);
x09x09 display_poly(p2);
x09x09x09destroy_link(p2); /*销毁链表*/
x09x09}
x09x09else
x09x09x09destroy_link(p2);/*销毁链表*/
x09x09getchar();/*让屏幕显示停在运算结果*/
x09x09return NULL;
x09}
x09return p2;
}
/*多项式相减*/
void poly_sub(polylink *p1, polylink *p2)
{
x09char ch[1];
polylink *n, *p;
x09system("cls"); /*清屏*/
x09creat(p1, p2);
x09p = p2;
x09while(p->next != NULL)
x09{
x09x09p = p->next;
x09x09p->coef = -1 * p->coef;
x09}
x09printf("nsub 1st - 2stn");
x09n = poly_add(p1, p2, '-'); /*调用加运算函数*/
x09display_poly(n);
x09printf("whether to derivat:y/nn");
x09scanf("%s", ch);
x09if( ch[0] == 'Y' || ch[0] == 'y')x09
x09{
n = poly_derivation(n); /*求导*/
x09 display_poly(n);
x09x09destroy_link(n);
x09}
x09else destroy_link(n);
x09getchar();
}
/*多项式相乘*/
polylink *multiply(polylink *p, float coef, int index)
{x09x09x09x09x09x09/*传入系数和指数,分别与p的每个节点运算 最后返回该链*/
x09polylink *q, *n;
x09n = (polylink *)malloc(sizeof(polylink));
x09n->next = NULL;x09x09x09/*n作为头节点*/
x09while(p->next != NULL)
x09{
x09x09p = p->next;
x09x09q = (polylink *)malloc(sizeof(polylink));
x09x09q->ch = p->ch;
x09x09q->coef = coef * p->coef;
x09x09q->expn = index + p->expn;
x09x09q->next = n->next;
x09x09n->next = q;x09x09x09
x09}
x09return n;
}
/*多项式相乘所用,另外为了提高代码重用性*/
void run_mul(polylink *p1, polylink *p2, int num)
{
x09int i = 0;
x09char ch[1];
x09polylink **p, *q; /*二维指针存每次multiply()函数返回的链表*/
x09p = (polylink **)malloc( num *sizeof(polylink));
while(p1->next != NULL)
x09{
x09x09p1 = p1->next;
x09x09p[i++] = multiply(p2, p1->coef, p1->expn);
x09}
x09q = poly_add(p[0], p[1], '*');
x09for(i = 2; i < num; i++)
x09{
x09 q = poly_add(q, p[i], '*');
x09}
printf("mul result:n");
x09display_poly(q);
x09printf("whether to derivat:y/nn");
x09scanf("%s", ch);
x09if( ch[0] == 'Y' || ch[0] == 'y')
x09{
q = poly_derivation(q);
x09x09 display_poly(q);
x09x09 destroy_link(q);
x09}
x09else destroy_link(q);
x09getchar();
}
void poly_mul(polylink *p1, polylink *p2)
{
x09int num1 =0 , num2 = 0;x09x09/*用于存放p1 ,p2的表长*/
x09system("cls");
x09creat(p1, p2);
link_lenth(p1, num1);
x09link_lenth(p2, num2);
x09if(num2 >= num1) /*根据链表*/
x09{
x09x09run_mul(p1, p2, num1);
x09}
x09else
x09{
x09x09run_mul(p2, p1, num2);
x09}
}
/*选择函数*/
int menu_select()
{
x09int n;
x09printf("press enter key to enter the menu."); /*按任一键进入主菜单*/
x09getchar(); /*从键盘读取一个字符,但不显示于屏幕*/
system("cls"); /*清屏*/
printf("nnnnnn");
x09printf("tttIt just for a variablesnn");
x09printf("tnn");
x09do{
x09x09x09printf("nt输入你的选择Enter your choice(1~4):");
x09x09x09scanf("%d",&n);
x09}while(n < 1 || n > 4); /*如果选择项不在1~4之间则重输*/
x09return n; /*返回选择项,主函数根据该数调用相应的函数*/
}
void main()
{
x09polylink *head1=NULL, *head2=NULL, *head3=NULL; /*head3的作用不是很大*/
x09for(;;) /*循环无限次*/
x09{
x09x09switch( menu_select() )
x09x09{
x09x09x09case 1: head3 = poly_add(head1, head2, '+'); break;
x09x09x09case 2: poly_sub(head1, head2); break;
x09x09x09case 3: poly_mul(head1, head2); break;
x09x09x09case 4: exit(0); /*如菜单返回值为4则程序结束*/
x09x09}
x09}
}