Thursday, 5 January 2012

C++ Program adds two numbers in binary using class and object.





C++ Program adds two numbers in binary using class and object.

#include<iostream.h>
#include<conio.h>
//using namespace std; 

class adder{ 
public: 
   int number1[8]; 
   int number2[8]; 
   int carry[8]; 
   int sum[8]; 
   int number_bits; 
   char choice; 
    
    

   void input(){ 
      cout<<"Input the number of bits you want to enter\n"; 
      cout<<"For the first number \n"; 
      cin>>number_bits; 


      if(number_bits<0||number_bits>8){ 
         cout<<"Error, the number of bits cannot be allowed to be \n"; 
         cout<<"greater than 8 or less than 0\n"; 
         cout<<"Please,Input the number correct entry\n"; 
          cout<<"For the first number \n"; 
          cin>>number_bits; 
      } 

      if(number_bits<8){ 
         for(int k=1;k<=(8-number_bits);k++){ 
            number1[k]=0; 
         } 
      } 
      cout<<"Enter the bits as 1 or 0 from the most significant bit\n"; 
      for(int i=(9-number_bits);i<=8;i++){ 
         cout<<"Enter bit "<<i-(8-number_bits)<<" : "; 
         cin>>number1[i]; 
      } 

      cout<<"Input the number of bits you want to enter\n"; 
      cout<<"For the second number \n"; 
      cin>>number_bits; 


      if(number_bits<0||number_bits>8){ 
         cout<<"Error, the number of bits cannot be allowed to be \n"; 
         cout<<"greater than 8 or less than 0\n"; 
         cout<<"Please,Input the number correct entryr\n"; 
          cout<<"For the second number \n"; 
          cin>>number_bits; 
      } 

      if(number_bits<8){ 
         for(int k=1;k<=(8-number_bits);k++){ 
            number2[k]=0; 
         } 
      } 
       
      cout<<"Enter the bits as 1 or 0 from the most significant bit\n"; 
      for(i=(9-number_bits);i<=8;i++){ 
         cout<<"Enter bit "<<i-(8-number_bits)<<" : "; 
         cin>>number2[i]; 
      } 

      cout<<"THE FIRST NUMBER YOU HAVE ENTERED \n"; 
      cout<<"NUMBER ONE:  "; 
      for(int l=1;l<=8;l++){ 
         cout<<number1[l]; 
         cout<<" "; 
      } 
      cout<<endl; 
      cout<<"THE SECOND NUMBER YOU HAVE ENTERED \n"; 
      cout<<"NUMBER TWO: "; 
      for(l=1;l<=8;l++){ 
         cout<<number2[l]; 
         cout<<" "; 
      } 
      cout<<endl; 

       
   } 

   void check(int array[],int size){ 

      for(int b=1;b<=size;b++){ 
         array[b]; 
         } 
      for(b=1;b<=size;b++){ 
       if((array[b]!=0)&&(array[b]!=1)){ 
         cout<<"One of the entry is not 1 0r 0\n"; 
         cout<<"please update it, go back to A on the menu\n"; 
            break; 
       } 
       else{ 
          cout<<"The entries are in binary, ok\n"; 
          break; 
       } 
      } 

   } 

char uppercase(char ch){ 
   switch(ch){ 


case'a':return'A';break; 
case'b':return 'B';break; 
case'c':return 'C';break; 
case'd':return 'D';break; 
case'e':return 'E';break; 
case'f':return 'F';break; 
case'g':return 'G';break; 
case'h':return 'H';break; 
case'i':return 'I';break; 
case'j':return 'J';break; 
case'k':return 'K';break; 
case'l':return 'L';break; 
case'm':return 'M';break; 
case'n':return 'N';break; 
case'o':return 'O';break; 
case'p':return 'P';break; 
case'q':return 'Q';break; 
case'r':return 'R';break; 
case's':return 'S';break; 
case't':return 'T';break; 
case'u':return 'U';break; 
case'v':return 'V';break; 
case'w':return 'W';break; 
case'x':return 'X';break; 
case'y':return 'Y';break; 
     
default:return ch;break;                         
   } 
}       

void adding(int x[],int j,int y[],int k){ 
    carry[8]=0; 
   for(int g=8;g>=1;g--){ 
      if((x[g]+y[g]+carry[g])==0){ 
         sum[g]=0; 
         carry[g-1]=0; 
      } 
      if((x[g]+y[g]+carry[g])==1){ 
         sum[g]=1; 
         carry[g-1]=0; 
      } 
      if((x[g]+y[g]+carry[g])==2){ 
         sum[g]=0; 
         carry[g-1]=1; 
      } 
      if((x[g]+y[g]+carry[g])==3){ 
         sum[g]=1; 
         carry[g-1]=1; 
      } 
   } 
    
   cout<<"THE SUM OF NUMBERS ENTERED IS \n"; 
   for(int r=1;r<=8;r++){ 
      cout<<sum[r]; 
      cout<<" "; 
   } 
   if(carry[1]==1){ 
      cout<<"over flow of "; 
      cout<<carry[1]; 
      cout<<endl; 
   } 
} 


}; 

int main(){
clrscr();
      adder sum;
      sum.choice='Y';

      while(sum.choice=='Y'){
     cout<<"\n";
     cout<<" Choose from the following \n";
     cout<<" A: Input of two numbers in binary \n ";
     cout<<"B: Check the entry of numbers \n";
     cout<<" C: Adding the two numbers \n";
     cout<<" ";
     cin>>sum.choice;
     sum.choice=sum.uppercase(sum.choice);

      switch(sum.choice)
      {

    case 'A':
     sum.input();
     break;

    case 'B':
     cout<<"Checking the first number\n";
     sum.check(sum.number1,8);
     cout<<"Checking the second number\n";
        sum.check(sum.number2,8);
     break;
      case 'C':
     sum.adding(sum.number1,8,sum.number2,8);
     break;


    default: cout<<"Sorry, your selection is not right \a\n";break;
      }

     cout<<"\n";
      cout<<"Do you want to experiment some more [Y/N] ?\n";
      cin>>sum.choice;
     cout<<"\n";
      sum.choice=sum.uppercase(sum.choice);

      if(sum.choice=='N'){
     cout<<"END.......\n";
     cout<<endl;
      }
      clrscr();
      }

      return 0; 
} 
 

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More