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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题