当前位置:文档之家› 一元多项式的运算(C程序)

一元多项式的运算(C程序)


r->next=s;
s->coef=q->coef*t->coef;
s->expn=q->expn+t->expn;
s->next=NULL;
h=Add_Poly(r,h); //把每项相乘结果加起来
}
}
return h;
}
PolyNode * Sub_Poly(PolyNode *f,PolyNode *g) {
{
PolyNode *fg;
PolyNode *t,*q,*s,*r;
float m;
t=f->next;
q=g->next;
fg=r=(PolyNode*)malloc(sizeof(PolyNode));
fg->next=NULL;
while(t&&q)
{
if(t->expn==q->expn)
void Readme() {
printf("**********Help***********\n"); printf("1.Please input the coefficient and exponent only,end with 0 0.\n"); printf("2.Please input with ascending sort.\n"); printf("3.For example: \"1 1 2 2 0 0\" means \"1*X^1+2*X^2\"\n"); }
PolyNode *t; t=g->next; while(t) { t->coef=-t->coef; t=t->next; } Add_Poly(f,g); }
void Open() {
printf("*****************************\n");//用户选择界面 printf(" Polynomial Operation\n");
case 4: Readme(); i=-1; Open(); break;
default: printf("输入有误!请重新选择操作!\n"); //选择错误,返回选择界面 Open(); } } }
//用指针实现一元多项式的加法、减法和乘法#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <conio.h>
typedef struct node {
float coef;//系数 int expn;//指数 struct node *next; }PolyNode;
s->coef=x; s->expn=y; s->next=NULL; if(p->next==NULL) { p->next=s;
r=s; }
else {
r->next=s; r=s;
} scanf("%f %d",&x,&y);
} return p; }
PolyNode * Add_Poly(PolyNode *f,PolyNode *g) //多项式相加
PolyNode *t; t=f->next; if(!f->next) { printf("0\n"); return; }
//输入多项式
while(t) { if(t->coef>0&&f->next!=t) printf("+"); if(t->expn==0)
printf("%f",t->coef); else printf("%5.2f*X^%d",t->coef,t->expn); t=t->next; } printf("\n"); }
s->coef=q->coef;
s->expn=q->expn;
s->next=NULL;
q=q->next;
}
}

if(fg->next==NULL)
{
fg->next=s;
r=s;
}
else
{
r->next=s;
r=s;
}
}
r->next=t?t:q;
return fg;
}
void Out_Poly(PolyNode * f) {
PolyNode * Create_Poly(char ch) //输入多项式 {
PolyNode *p,*s,*r; float x; int y; p=(PolyNode *)malloc(sizeof(PolyNode)); p->next=NULL; printf("please input the Polynomial %c:\n",ch); scanf("%f %d",&x,&y); while(x!=0 && y!=0) { s=(PolyNode *)malloc(sizeof(PolyNode));
}
else
//指数小的加到结果中去再后移
{
if(t->expn<q->expn)
{
s=(PolyNode *)malloc(sizeof(PolyNode));
s->coef=t->coef;
s->expn=t->expn;
s->next=NULL;
t=t->next;
}
else
{
s=(PolyNode *)malloc(sizeof(PolyNode));
r->next=NULL;
for(t=f->next;t;t=t->next)
//相乘时把第一项多项式的每一项
{
for(q=g->next;q;q=q->next) //用双重循环实现
for(q=g->next;q;q=q->next)
//用双重循环实现
{
s=(PolyNode *)malloc(sizeof(PolyNode));
void main() {
int i=-1; //设置标志 PolyNode *f,*g,*fg; clrscr(); Open(); while(i!=0) { scanf("%d",&i); getchar(); switch(i) { case 0: return;//退出 case 1: printf("You have chosen the polynomial's addition:\n"); f=Create_Poly('A'); //输入多项式A printf("A="); Out_Poly(f); g=Create_Poly('B');//输入多项式B printf("B="); Out_Poly(g); printf("A+B="); fg=Add_Poly(f,g); Out_Poly(fg); i=-1; Open(); break; case 2: printf("You have chosen the polynomial's subtration:\n"); f=Create_Poly('A'); //输入多项式A printf("A="); Out_Poly(f); g=Create_Poly('B'); //输入多项式B printf("B="); Out_Poly(g);
printf("*****************************\n"); printf("Operatin:\n"); printf("0.Exit\n"); printf("1.Polynomial Addition\n"); printf("2.Polynomial Subtration\n"); printf("3.Polynomial Multiplication\n"); printf("4.Help\n"); }
printf("A-B="); fg=Sub_Poly(f,g); Out_Poly(fg); i=-1; Open(); //回复用户选择界面 break;
case 3: printf("You have chosen the polynomial's multiplication:\n"); f=Create_Poly('A'); printf("A="); Out_Poly(f); g=Create_Poly('B'); printf("B="); Out_Poly(g); printf("A*B="); fg=Mul_Poly(f,g); Out_Poly(fg); i=-1; Open(); break;
//指数相等时系数相加
{
m=t->coef+q->coef; //系数为不0时加到结果中去
if(m!=0)
{
相关主题