回 帖 发 新 帖 刷新版面

主题:[讨论]数据结构简单题

#include<iostream>
using namespace std;
class list
{
      public:
        int coef,exp;//系数和指数 
        class list *next;     
};
typedef class list node;
typedef node *link;
link creat_link(int data[4]);
void print_link(link head);
link sum_link(link a,link b);
link creat_link(int data[4])
{
     link head,newnode,ptr;
     for(int i=0;i<4;i++)
         {
             newnode=new node;
             if(!newnode)
               {
                       cout<<"!!!Error!!!"<<endl;
                       exit(1);  
                }
              if(i==0)//建立表头 
              {
                    newnode->coef=data[i];
                    newnode->exp=3-i;
                    newnode->next=NULL;
                    head=newnode;
                    ptr=head;  
              }
             else if(data[i]!=0)//建立表 
              {
                  newnode->coef=data[i];
                  newnode->exp=3-i;
                  newnode->next=NULL;
                  ptr->next=newnode;//连接各部分 
                  ptr=newnode;
              }  
         }
         return head;
 }
void print_link(link head)
{
     while(head!=NULL)
     {
          if(head->exp==1 && head->coef!=0)
             cout<<head->coef<<"X + ";
     else if(head->exp!=0 && head->coef!=0)
             cout<<head->coef<<"X^"<<head->exp<<" + ";
     else if(head->coef!=0)
             cout<<head->coef;
          head=head->next;                        
     }
     cout<<endl;
 }
link sum_link(link a,link b)
{
     int sum[4],i=0;
     link ptr;
     ptr=b;
     while(a!=NULL)
     {
          b=ptr;
          while(b!=NULL)
          {
              if(a->exp==b->exp)
                 {
                     sum[i]=a->coef+b->coef;
                     i++;
                     a=a->next;
                     b=b->next;             
                 }
              else if(b->exp > a->exp)
              {
                   sum[i]=b->coef;
                   b=b->next;
                   i++;
              }
              else if(a->exp > b->exp)
               {
                   sum[i]=a->coef;
                   a=a->exp;
                   i++;
               }               
          }         
     }
     return creat_link(sum);
 } 

int main()
{
    link a,b,c;
    int data1[4]={3,0,4,2};
    int data2[4]={6,8,6,9};
    cout<<"原始多项式"<<endl<<"A= ";
    a=creat_link(data1);
    b=creat_link(data2);
    print_link(a);
    cout<<"B= ";
    print_link(b);
    cout<<"多项式相加结果\nC= ";
    c=sum_link(a,b);
    print_link(c);
    system("pause");
    return 0;
}
出了一点问题。。。找不出。。。。。求指教

回复列表 (共3个回复)

沙发

将原来的print函数改为
void print_link(link head)
{
     while(head!=NULL)
     { 
     if(head->ex==0 && head->coef!=0)
     cout<<head->coef;
      else  if(head->ex==1 && head->coef!=0)
             cout<<head->coef<<"X + ";
     else
     if(head->coef!=0)
             cout<<head->coef<<"X^"<<head->ex<<" + ";
          head=head->next;                        
     }
     cout<<endl;
 }
这样就行了,可能原来条件混淆有问题。。但我不知道具体问题是什么。。。有谁能解释一下吗

板凳

说可能哪里错了,写出理由和自己的分析,这样有针对性。。。
错的地方有2(算法方面的没看,这是得自己实现的)

1:笔误,line 84 a = a->next;
2: 由于格式不对,注意编写时的规范,编译器无歧义,人也容易读。。
if(head->exp == 1 && head->coef != 0)  
             cout<<head->coef<<"X + ";
     else if(head->exp != 0 && head->coef != 0)  //这里看下。

另:有空希望多交流。。

3 楼

谢谢。。。我感到总算有人回复了。。。。。

我来回复

您尚未登录,请登录后再回复。点此登录或注册