| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:多项式加法:
取消只看楼主 加入收藏
我的手机哦
Rank: 1
等 级:新手上路
帖 子:7
专家分:1
注 册:2011-5-16
结帖率:66.67%
收藏
已结贴  问题点数:5 回复次数:0 
多项式加法:
   
#include"stdio.h"
#include"stdlib.h"
struct node
{
  int cof,exp;
  struct node *next;
};
struct node *create()
{
  struct node *head,*p,*q;
  int i,j;
  head=(struct node *)malloc(sizeof(struct node));
  q=head;
  printf("input i,j:");
  scanf("%d%d",&i,&j);
  while(i!=-999&&j!=-999)
  {
    p=(struct node *)malloc(sizeof(struct node));
    p->cof=i;
    p->exp=j;
    q->next=p;
    q=p;
    scanf("%d%d",&i,&j);
  }
  q->next=NULL;
  return(head);
}
void outline(struct node *head)
{
   struct node *p;
   p=head->next;
   while(p!=NULL)
   {
     printf("%5d%5d",p->cof,p->exp);
     p=p->next;
 
}
void lineadd(struct node *head1,struct node *head2)
{
  struct node *p,*q,*r;
  int x;
  int  cof,exp;
  p=head1->next;
  q=head2->next;
  r=head1;
  while(p!=NULL&&q!=NULL)
  {
    if(p->exp==q->exp)
    {
     x=p->cof+q->cof;
     if(x!=0)
     {
       p->cof=x;
       r->next=p;
       r=p;
     }
       p=p->next;
       q=q->next;

    }
    else if(p->exp>q->exp)
     {
       r->next=p;
       r=p;
       p=p->next;
     }
     else
     {
      r->next=q;
      r=q;
      q=q->next;
     }
    }
    if(p==NULL)
      r->next=q;
     if(q==NULL)
      r->next=p;
}

main()
{
 struct node *head1,*head2;
 head1=(struct node *)malloc(sizeof(struct node));
 head2=(struct node *)malloc(sizeof(struct node));
 head1=create();
 head2=create();
lineadd(head1,head2);
 outline(head1);
}
高手指点我的程序有没有可能可以改的更好一点。
搜索更多相关主题的帖子: include outline return create 多项式 
2011-10-20 23:22
快速回复:多项式加法:
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014722 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved