Skip to main content

DSL C23

Data Structure Lab : 


Practical C23 :

Write C++ program for storing binary number using doubly linked lists. Write functions

a) To compute 1‘s and 2‘s complement 

b) Add two binary numbers

The code for above problem is as follows:

   
   
 //Author : SPPU CSE GURU
 
 #include<iostream>  
   
 using namespace std;  
   
 struct dnode  
 {  
      int data;  
      dnode *next;  
      dnode *prev;  
 };  
   
 dnode *nnode;  
 int bit,bits,flag;  
   
 class Binary{  
        
      public:  
           dnode *head,*tail;  
             
           Binary(){  
                head=tail=NULL;  
           }  
             
      void create();  
      void display();  
      void rdisplay();  
      void OneCom();  
      void TwoCom();  
      void AddNode(int x);  
      void Addition(Binary B2);  
 };  
   
 void Binary::create(){  
      cout<<"Enter total no. of bits"<<endl;  
      cin>>bits;  
      cout<<"***Enter bits from left to right***"<<endl;  
      for(int i=1;i<=bits;i++)  
      {  
           nnode=new dnode;  
           nnode->next=NULL;  
           nnode->prev=NULL;  
           cout<<"Enter "<<i<<" bit "<<endl;  
           cin>>bit;  
           nnode->data=bit;  
           if(head==NULL||tail==NULL)  
           {  
                head=tail=nnode;  
           }  
           else  
           {  
                tail->next=nnode;  
                nnode->prev=tail;  
                tail=nnode;  
           }  
             
      }  
 }  
   
   
 void Binary::display()  
 {  
      dnode *temp;  
      temp=head;  
      cout<<"Number Is :"<<endl;  
      while(temp!=NULL)  
      {  
           cout<<temp->data<<" ";  
           temp=temp->next;  
      }  
 }  
 
   
 void Binary::OneCom()  
 {  
      dnode *temp;  
      temp=head;  
      cout<<"\n1's Complement : "<<endl;  
      while(temp!=NULL)  
      {  
           if (temp->data==0)  
           {  
                cout<<"1 ";  
           }  
           else  
           {  
                cout<<"0 ";  
           }  
           temp=temp->next;  
      }  
        
 }  
   
   
 void Binary::TwoCom()  
 {  
      dnode *temp;  
      temp=tail;  
      flag=0;  
      while(temp!=NULL)  
      {  
           if(flag==0)  
           {  
                  
             if(temp->data==1)  
                {  
                flag=1;  
              }  
                
           }  
           else  
           {  
                if (temp->data==0)  
                {  
                temp->data=1;  
             }  
           else  
             {  
                temp->data=0;  
             }  
                  
           }  
             
           temp=temp->prev;  
      }  
        
 }  
   
 void Binary::AddNode(int x)  
 {  
      nnode=new dnode;  
      nnode->next=NULL;  
      nnode->prev=NULL;  
      nnode->data=x;  
      if(head==NULL)  
      {  
           head=tail=nnode;  
      }  
      else  
      {  
           head->prev=nnode;  
           nnode->next=head;  
           head=nnode;       
      }  
        
   
 }  
   
 void Binary::Addition(Binary B2)  
 {  
      Binary B3;  
   dnode *temp1,*temp2;  
   int a,b,c=0;  
   temp1=tail;  
   temp2=B2.tail;  
   while (temp1!=NULL && temp2!=NULL)  
      {  
        a=temp1->data;  
        b=temp2->data;  
        if (a==0 && b==0 && c==0)  
           {  
             B3.AddNode(0);  
             c=0;  
           }  
             
           else if (((a==0 && b==1) || (a==1 && b==0)) && c==0)  
           {  
                B3.AddNode(1);  
             c=0;  
           }  
             
           else if (a==1 && b==1 && c==0)  
           {  
                B3.AddNode(0);  
             c=1;       
           }  
             
           else if (a==0 && b==0 && c==1)  
           {  
             B3.AddNode(1);  
             c=0;  
           }  
             
           else if (((a==0 && b==1) || (a==1 && b==0)) && c==1)  
           {  
                B3.AddNode(0);  
             c=1;  
           }  
             
           else if (a==1 && b==1 && c==1)  
           {  
                B3.AddNode(1);  
             c=1;  
           }  
             
           temp1=temp1->prev;  
           temp2=temp2->prev;  
             
      }  
        
      while (temp1!=NULL){  
           a=temp1->data;  
           if (a==0 && c==0)  
           {  
                B3.AddNode(0);  
                c=0;  
           }  
             
           else if ((a==0 && c==1)||(a==1 && c==0))  
           {  
                B3.AddNode(1);  
                c=0;  
           }  
             
           else if (a==1 && c==1)  
           {  
                B3.AddNode(0);  
                c=1;  
           }  
             
        temp1=temp1->prev;       
      }  
        
      while (temp2!=NULL)  
      {  
           b=temp2->data;  
           if (b==0 && c==0)  
           {  
                B3.AddNode(0);  
                c=0;  
           }  
             
           else if ((b==0 && c==1)||(b==1 && c==0))  
           {  
                B3.AddNode(1);  
                c=0;  
           }  
             
           else if (b==1 && c==1)  
           {  
                B3.AddNode(0);  
                c=1;  
           }  
             
           temp2=temp2->prev;  
      }  
        
      if (c==1)  
      {  
           B3.AddNode(1);  
      }  
        
      cout<<"\nBinary Addition ";  
   B3.display();  
 }  
   
 int main()  
 {  
        
      Binary B,B1,B2;  
        
      int ch;  
        
      do{  
           cout<<"\n 0.Exit\n 1.create\n 2.display\n 3. 1's Complement\n 4. 2's Complement\n 5.Addition\n ";  
           cout<<"Enter your Choice : "<<endl;  
           cin>>ch;  
           switch(ch)  
           {  
                case 0:    
            break;  
                case 1:  
                     B.create();  
                     break;  
                case 2:  
                     B.display();  
                     break;  
                case 3:  
                     B.OneCom();  
                     break;  
                case 4:  
                     B.TwoCom();  
                     cout<<"\n2's Complement ";  
                     B.display();  
                     break;  
                case 5:  
                     cout<<"\nEnter two Binary Numbers for Addition : "<<endl;  
                     B1.create();  
                     B2.create();  
                     cout<<"\n1. ";  
                     B1.display();  
                     cout<<"\n2. ";  
                     B2.display();  
                     B1.Addition(B2);  
                     break;  
                default:
	             cout<<"Wrong choice";
           }  
                        
      }while(ch!=0);  
        
        
      return 0;  
 }  

Comments

Popular posts from this blog

Bouncing Ball

Group B   Practical 4 a   Problem Statement :   Write a C++ program to implement bouncing ball using sine wave form. Apply the concept of polymorphism. Check Out Code Here  ðŸ‘‡ Outputs :  Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

OOP 4

  Object Oriented Programming Group B - Practical : 4 Problem Statement :  Write a C++ program that creates an output file, writes information to it, closes the file, open it again as an input file and read the information from the file. Check Out Code Here  ðŸ‘‡ Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

OOP 2

Object Oriented Programming Group A - Practical : 2 Problem Statement :  Develop a program in C++ to create a database of student’s information system containing the following information: Name, Roll number, Class, Division, Date of Birth, Blood group, Contact address, Telephone number, Driving license no. and other. Construct the database with suitable member functions. Make use of constructor, default constructor, copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete as well as exception handling. Check Out Code Here  ðŸ‘‡ Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊