67 048
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #22174 in C++ for rodea embradura

Question #22174
create a c++ program that performs arithmetic binary subtraction.
Expert's answer
#include<iostream>
#include<string.h>
#include<windows.h>

using namespace std;
class bin
{
char a[32],b[32],c[32];
int s1,s2,m;
char cary;
public:
bin()
{
& cary='0';
& s1=0;s2=0;m=0;
& for(int i=0;i<32;i++)
& {
a[i]='0';
b[i]='0';
c[i]='0';
& }
}
void getdata()
{
& char m[32],n[32];
cout<<"Enter first binary number: ";
& gets(m);
cout<<"Enter second binary number: ";
& gets(n);
& s1=strlen(m);
& s2=strlen(n);
& for(int i=31,j=s1-1;j>=0;)
& {
if(m[j]!='0'&&m[j]!='1')
{
system("cls");
cout<<"\nNot a binary number... Please insert binary numbers next time. See ya :/";
Sleep(4000);
exit (1);
}
a[i]=m[j];
i--;j--;
& }
& for(int i=31,j=s2-1;j>=0;)
& {
if(n[j]!='0'&&n[j]!='1')
{
system("cls");
cout<<"\nNot a binary number... Please Enter valid binary numbers next time...";
for(int z=0;z<=2000;z++)
for(int za=0;za<=2000;za++)
for(int zb=0;zb<=2000;zb++);
exit (1);
}
b[i]=n[j];
i--;j--;
& }
& system("cls");//edit as per compiler
}
void add();
void sub();
void com();
void display();
};
void bin::sub()
{
m=1;
com();
add();
}
void bin::com()
{
int count=0;
for(int i=31;i>=0;i--)
{
& if(count>0)
& {
if(b[i]=='1')
{
b[i]='0';
}
else
{
b[i]='1';
}
& }
& if(b[i]=='1')
& {
count++;
& }
}
}
void bin::add()
{
cout<<"\a";
for(int i=31;i>=0;i--)
{
& if(cary=='0'&&a[i]=='0'&&b[i]=='0')
& {
cary='0';
c[i]='0';
& }
& else if(cary=='0'&&a[i]=='0'&&b[i]=='1')
& {
cary='0';
c[i]='1';
& }
else if(cary=='0'&&a[i]=='1'&&b[i]=='0')
& {
cary='0';
c[i]='1';
& }
& else if(cary=='0'&&a[i]=='1'&&b[i]=='1')
& {
cary='1';
c[i]='0';
& }
& else if(cary=='1'&&a[i]=='1'&&b[i]=='1')
& {
cary='1';
c[i]='1';
& }
& else if(cary=='1'&&a[i]=='0'&&b[i]=='0')
& {
cary='0';
c[i]='1';
& }
& else if(cary=='1'&&a[i]=='0'&&b[i]=='1')
& {
cary='1';
c[i]='0';
& }
& else if(cary=='1'&&a[i]=='1'&&b[i]=='0')
& {
cary='1';
c[i]='0';
& }
& else if(cary=='1'&&a[i]=='1'&&b[i]=='1')
& {
cary='1';
c[i]='1';
& }
}
}

void bin::display()
{
int n;
cout<<"\nUpto how many bits do you want to display\n->";
cin>>n;
cout<<"\nFirst Binary Number is:\n";
for(int i=32-n;i<32;i++)
{
& cout<<a[i];
}
cout<<endl;
if(m==1)
cout<<"\n2's complement of second number is:\n";
else
{
cout<<"\nSecond Binary Number is:\n";
}
for(int i=(32-n);i<32;i++)
{
& cout<<b[i];
}
cout<<endl;
cout<<"Result is: \n";
for(int i=32-n;i<32;i++)
{
& cout<<c[i];
}
cout<<endl;
}

int main()
{
char y;y='y';
bin b;
int ch;
b.getdata();
b.sub();
b.display();
system("PAUSE");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions