八进制加法链表

#include<iostream>
using namespace std;


struct node
{
   int data;
   node*next;
};


class Add_8
{
       public:
      Add_8(){flag=0;}
           void func();
      void add(node *pf,node *qf);
   
       private:
      node*p;
  node*q;
      node*first;
      int m,flag;
};


void Add_8::func()
{         
     node *s;
          first=new node;
          first->next=NULL;
          while(cin>>m)
 {
 s=new node;
              s->data=m;
              s->next=first->next;
              first->next=s;
 }
 while(first->next!=NULL)
 {
 cout<<first->next->data<<endl;
          first->next=first->next->next;
 }
 //return first;
}


void Add_8::add(node *pf,node*qf)
{       
node*temp;
         p=pf->next;
         q=qf->next;
         while(p->next!=NULL&&q->next!=NULL)
{
               p->data=(p->data+q->data+flag)%8;
               flag=(p->data+q->data+flag)/8;
               p=p->next;
               q=q->next;
}
           p->data=(p->data+q->data+flag)%8;
          flag=(p->data+q->data+flag)/8;


         if(q->next==NULL)
{
            while(p->next!=NULL)
{
             p=p->next;
             p->data=(p->data+flag)%8;
             flag=(p->data+flag)/8;
}
p->data=(p->data+flag)%8;
             flag=(p->data+flag)/8;
            if(flag>0)
{
 temp=new node;
 temp->next=NULL;
 temp->data=flag;
 p->next=temp;
}
}
         if(p->next==NULL)
{
    while(q->next!=NULL)
{  
temp=new node;
   temp->data=(q->data+flag)%8;
flag=(q->data+flag)/8;
p->next=temp;
temp->next=NULL;
}
    if(flag>0)
{
      temp=new node;
      temp->next=NULL;
      temp->data=flag;
      p->next=temp;
}
}
p=first->next;
       while(p->next!=NULL)
  cout<<p->data;
        cout<<p->next->data;
}
int main()
{
  node *pf,*qf;
  Add_8 obj1,obj2,obj3;
  obj1.func();
  cout<<"hello"<<endl;
  obj2.func();
 
 
  
  // if(pf==qf)
//  cout<<"nima"<<endl;
cout<<qf->next->data<<endl;
  //cout<<pf->next->data<<endl;
  


  cout<<"hello"<<endl;


 //obj3.add(pf,qf);
return 0;
}









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值