visual2015 2017-06-14 13:57 采纳率: 80%
浏览 1253
已结题

C语言,帮忙修改一元多项式的乘法,谢谢啦

#include
#include
typedef struct polynomial{ //项的数据类型
float coef; //系数
int expn; //指数
}polynomial,*polynomialptr; //类型名

typedef struct polynode{ //一元多项式的结点,即项
polynomial data; //数据域为polynomial型
struct Node *next; //指针域指向下一个项结点
}polynode,*polynodeptr; //结点类型名
polynodeptr producehead(polynodeptr head); //建立头结点
void creatpolynomial(polynodeptr head,polynomial n,int m); //生成一元多项式
void Display(polynodeptr head); //显示一元多项式
void addpolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2); //两个多项式相加
void minuspolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2); //两个多项式相减
//void multiplypolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2); //两个多项式相乘

int main()
{
polynodeptr head1,head2; //head为头结点,是全局变量
polynomial p1,p2;
int m1,m2; //m1,m2分别为两个多项式的最大指数
head1=producehead(head1);
head2=producehead(head2);

printf("请输入第一个一元多项式的最大指数:");
scanf("%d",&m1);
creatpolynomial(head1,p1,m1);
printf("\n");
Display(head1);

printf("\n\n请输入第二个一元多项式的最大指数:");
scanf("%d",&m2);
creatpolynomial(head2,p2,m2);
printf("\n");
Display(head2);
printf("\n\n");

int n;
printf("请问要执行什么运算?\n执行加法运算请输入1\n执行减法运算请输入2\n执行加法运算请输入3\n");
scanf("%d",&n);
printf("\n");
while(n<4&&n>0)
{
  switch(n)
  {
    case 1:{addpolynomial(head1,head2,m1,m2);printf("\n\n");break;}
    case 2:{minuspolynomial(head1,head2,m1,m2);printf("\n\n");break;}
    //case 3:{multiplypolynomial(head1,head2,m1,m2);printf("\n\n");break;}

  }
    getch();
    printf("请问要执行什么运算?\n执行加法运算请输入1\n执行减法运算请输入2\n执行乘法运算请输入3\n");
    scanf("%d",&n);
    printf("\n");
}
return 0;

}

polynodeptr producehead(polynodeptr head){ //建立头结点
head=(polynodeptr)malloc(sizeof(polynode));
head->next=NULL;
return head;
}

void creatpolynomial(polynodeptr head,polynomial n,int m){ //生成一元多项式
int i;
for(i=0;i<=m;i++){
printf("请输入该一元多项式的指数为%d的项的指数与对应系数:",m-i);
scanf("%d%f",&n.expn,&n.coef);
polynodeptr p;
p=(polynodeptr)malloc(sizeof(polynode));
p->data=n;
p->next=head->next;
head->next=p;
}
}

void Display(polynodeptr head) //显示一元多项式
{
head=head->next;
if(head==NULL)
{
printf("无多项式\n");return;
}
printf("%.3fX(%d)",head->data.coef,head->data.expn); //处理首项无+的问题
head=head->next;
while(head)
{
if(head&&head->data.coef>0) printf("+");
if(head->data.coef!=0) printf("%.3fX(%d)",head->data.coef,head->data.expn);
head=head->next;
}
}

void addpolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2){ //两个多项式相加
int i;
float coef;//系数
head1=head1->next;
head2=head2->next;
if(m1==m2){//m1,m2分别为两个多项式的最大指数
for(i=0;i<=m1;i++){
coef=head1->data.coef+head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
}
if(m1 for(i=0;i coef=head1->data.coef+head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m2;i++){
if(i!=(m2-1)&&head2->data.coef>0) printf("+");
if(head2->data.coef!=0) printf("%.3fX(%d)",head2->data.coef,i);
head2=head2->next;
}
}
if(m1>m2){
for(i=0;i<=m2;i++){
coef=head1->data.coef+head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m1;i++){
if(i!=(m1-1)&&head1->data.coef>0) printf("+");
if(head1->data.coef!=0) printf("%.3fX(%d)",head1->data.coef,i);
head1=head1->next;
}
}
}

void minuspolynomial(polynodeptr head1,polynodeptr head2,int m1,int m2){ //两个多项式相减
int i;
float coef;
head1=head1->next;
head2=head2->next;
if(m1==m2){
for(i=0;i<=m1;i++){
coef=head1->data.coef-head2->data.coef;
if(i!=0&&coef>0) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
}
if(m1 for(i=0;i coef=head1->data.coef-head2->data.coef;
if(i!=0&&coef>0&&i!=m1) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m2;i++){
if(i!=(m2-1)&&head2->data.coef>0) printf("-");
if(head2->data.coef!=0) printf("%.3fX(%d)",head2->data.coef,i);
head2=head2->next;
}
}
if(m1>m2){
for(i=0;i<=m2;i++){
coef=head1->data.coef-head2->data.coef;
if(i!=0&&coef>0&&i!=m2) printf("+");
if(coef!=0) printf("%.3fX(%d)",coef,i);
head1=head1->next;
head2=head2->next;
}
for(;i<=m1;i++){
if(i!=(m1-1)&&head1->data.coef>0) printf("+");
if(head1->data.coef!=0) printf("%.3fX(%d)",head1->data.coef,i);
head1=head1->next;
}
}
}

  • 写回答

1条回答 默认 最新

  • devmiao 2017-06-14 16:11
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog