Write C++ program for storing binary number using doubly linked lists. Write functions- a) to compute 1s and 2s complement b) add two binary numbers

Write C++ program for storing binary number using doubly linked lists. Write functions-
a) to compute 1s and 2s complement b) add two binary numbers.
OUTPUT FOR THE PROGRAM
#include<iostream>
using namespace std;
struct node
{  int x;
    node *next,*prev;
};
class binary
{    node *head=NULL,*temp=NULL, *head1=NULL,*temp1=NULL, *head2=NULL,*temp2=NULL,*temp3=NULL,*head3=NULL;
     int c,i;
      public:
       node *create();
       void insert();
       void binary1();
       void binary2();
       void add();
       void com();
       void comp();
       void display();
};
node *binary::create()
{   node *p=new(struct node);
     cout<<"enter binary";
     cin>>c;
     p->x=c;
     p->next=NULL;
     p->prev=NULL;
     return  p;
  }
  void binary::insert()
  {
       node *p=create();

     if(head==NULL)
     {    head=p;
     }
    else
    {      temp=head;
          while(temp->next!=NULL)
          {    temp=temp->next;   }
              temp->next=p;
              p->prev=temp;
         
     }    
     
   }
   void binary:: binary1()
    { int a;
       cout<<"enter the no. of bits";
        cin>>a;
         head=NULL;
        for(i=0;i<a;i++)
        {  insert();      }
          head1=head;
         display();
         head=NULL;
         temp1=head1;
     }
      void binary:: binary2()
    {     int a;
       cout<<"enter the no. of bits";
        cin>>a;
         head=NULL;
        for(i=0;i<a;i++)
        {  insert();      }
           head2=head;
         display();
         head=NULL;
      }
 
      void binary::add()
      {   int carry=0;
         temp1=head1;
         while(temp1->next!=NULL)
         {   temp1=temp1->next;     }
  
          temp2=head2;
         while(temp2->next!=NULL)
         {   temp2=temp2->next;     }
    
         while(temp1!=NULL)
          {      node *p=new(struct node);
                 p->next=NULL;
                 p->prev=NULL;
                 if(temp1->x==0&&temp2->x==0)
                 {      p->x=0+carry;  carry=0;      }
                 if(temp1->x==0&&temp2->x==1)
                 {      if(carry==0)
                        {p->x=1;     carry=0; }
                         else
                         {  p->x=0;  carry=1;
                         }
                 }
                   if(temp1->x==1&&temp2->x==0)
                 {       if(carry==0)
                         {p->x=1;     carry=0; }
                         else
                         {  p->x=0;  carry=1;
                         }
                 }
                   if(temp1->x==1&&temp2->x==1)
                 {        if(carry==0)
                        {p->x=0;     carry=1; }
                         else
                         {  p->x=1;  carry=1; }
                 }
               if(temp3==NULL)
               {   temp3=p;     }
               else
               {
                  p->next=temp3;
                  temp3=p;
               }
          
              temp1=temp1->prev;
              temp2=temp2->prev;   
         }
      node *p=new(struct node);
                p->x=carry;
                 p->next=NULL;
                 p->prev=NULL;
           if(temp3==NULL)
               {   temp3=p;     }
               else
               {
                  p->next=temp3;
                  temp3=p;
               }
               head3=temp3;
     temp3=head3;  cout<<"\n";
      while(temp3->next!=NULL)
      { cout<<" "<<temp3->x;;
        temp3=temp3->next;
      } cout<<" "<<temp3->x<<"\n";
 
}
void binary::com()
       { while(temp1!=NULL)
        {  node *p=new(struct node);
           p->next=NULL;
           p->prev=NULL;
           if(temp1->x==0)
             p->x=1;
            else
             p->x=0;
       
         if(head==NULL)
         {    head=p;
         }
         else
         {      temp=head;
                while(temp->next!=NULL)
                {    temp=temp->next;   }
                temp->next=p;
                p->prev=temp;
         
          }    
           temp1=temp1->next;
        }  cout<<"\n1's compliment of  binary no.";
         display();
 
        int f=0;
        while(temp!=NULL)
        {   if(temp->x==1)
            {   temp->x=0;   }
            else
            {   temp->x=1;   f=1;
                break;
            } temp=temp->prev;
        }
        if(f==0)
        {  node *p=new(struct node);
           p->next=NULL;
           p->prev=NULL;
           p->x=1;
           temp=head;
           head=p;
           head->next=temp;
           temp->prev=head;
         }
       cout<<"\n2's compliment of  binary no.";
         display();
      }
   void binary::comp()
   {
  
      cout<<"\n for first binary no.";
         temp1=head1;
         com();
         head=NULL;
      cout<<"\n for second binary no. ";
         temp1=head2;
         com();
   }
           




    void binary::display()
   {  temp=head;  cout<<"\n";
      while(temp->next!=NULL)
      { cout<<" "<<temp->x;;
        temp=temp->next;
      } cout<<" "<<temp->x<<"\n";
   }
          int main()
          {  binary b;
              int m;
              char ch;
             do{    cout<<"\nenter the choice";
                         cout<<"\n 1.insert binary1";
                         cout<<"\n 2.insert binary 2";
                         cout<<"\n 3.add binary no.s";
                         cout<<"\n 4.1's and 2's compliment of binary no.";
                         cin>>m;
                         switch(m)
                         {   case 1:   b.binary1();
                                                 break;
                              case 2:   b.binary2();
                                                 break;
                              case 3:   b.add();
                                                 break;
                              case 4:   b.comp();
                                                 break;
                               default: cout<<"unknown choice";
                            }
                            cout<<" \ndo you want to continue press y";                
                           cin>>ch;
                   }while(ch=='y'||ch=='Y');   
             
         
              return 0;
              }
For more such posts click the link:-http://svencrai.com/G8W


Comments


Comments

Popular posts from this blog

A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required. If the requested copies book details and requests for the number of copies required. If the requested copies are available, the total cost of the requested copies is displayed; otherwise the message ?Required copies not in stock? is displayed. Design a system using a class called books with suitable member functions and Constructors. Use new operator in constructors to allocate memory space required. Implement C++ program for the system

Write C++ program for storing appointment schedule for day. Appointments are booked randomly using linked list. Set start and end time and min and max duration for visit slot. Write functions for- a)Display free slots b)Book appointment c)Cancel appointment ( check validity, time bounds, availability etc) d)Sort list based on time e)Sort list based on time using pointer manipulation

Imagine a publishing company which does marketing for book and audiocassette versions. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Write a program that instantiates the book and tape classes, allows user to enter data and displays the data members.