Problem Type : Math
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
int i,j,k,l1,l2,len1,len2,len3=13,count,sum,p=0,carry,n,l,x,z,arr1,arr2;
char cp ,c1[]="0001",c2[]="0010",c3[]="0011",c4[]="0100",
c0[]="0000",c5[]="0101",c6[]="0110",c7[]="0111",c8[]="1000",c9[]="1001",
cA[]="1010",cB[]="1011",cC[]="1100",cD[]="1101",cE[]="1110",cF[]="1111",
a[100],b[100],c[100],d[100],add[100];
scanf("%d",&n);
{
for(x=0; x<n; x++)
{
scanf("%s %c %s",a,&cp,b);
len1=strlen(a);
len2=strlen(b);
count=0;
for(i=0; i<len1; i++)
{
if(a[i]=='1')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c1[k];
}
count=j;
}
else if (a[i]=='0')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c0[k];
}
count=j;
}
else if (a[i]=='2')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c2[k];
}
count=j;
}
else if (a[i]=='3')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c3[k];
}
count=j;
}
else if (a[i]=='4')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c4[k];
}
count=j;
}
else if (a[i]=='5')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c5[k];
}
count=j;
}
else if (a[i]=='6')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c6[k];
}
count=j;
}
else if (a[i]=='7')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c7[k];
}
count=j;
}
else if (a[i]=='8')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c8[k];
}
count=j;
}
else if (a[i]=='9')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c9[k];
}
count=j;
}
else if (a[i]=='A')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cA[k];
}
count=j;
}
else if (a[i]=='B')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cB[k];
}
count=j;
}
else if (a[i]=='C')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cC[k];
}
count=j;
}
else if (a[i]=='D')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cD[k];
}
count=j;
}
else if (a[i]=='E')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cE[k];
}
count=j;
}
else if (a[i]=='F')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cF[k];
}
count=j;
}
}
c[count]='\0';
l1=strlen(c);
if(l1<len3)
{
reverse(c,c+l1);
for(i=l1; i<len3; i++)
{
c[i]=0+48;
}
c[i]='\0';
reverse(c,c+len3);
}
count=0;
for(i=0; i<len2; i++)
{
if(b[i]=='1')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c1[k];
}
count=j;
}
else if (b[i]=='2')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c2[k];
}
count=j;
}
else if (b[i]=='3')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c3[k];
}
count=j;
}
else if (b[i]=='4')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c4[k];
}
count=j;
}
else if (b[i]=='5')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c5[k];
}
count=j;
}
else if (b[i]=='6')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c6[k];
}
count=j;
}
else if (b[i]=='7')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c7[k];
}
count=j;
}
else if (b[i]=='8')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c8[k];
}
count=j;
}
else if (b[i]=='9')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c9[k];
}
count=j;
}
else if (b[i]=='A')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cA[k];
}
count=j;
}
else if (b[i]=='B')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cB[k];
}
count=j;
}
else if (b[i]=='C')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cC[k];
}
count=j;
}
else if (b[i]=='D')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cD[k];
}
count=j;
}
else if (b[i]=='E')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cE[k];
}
count=j;
}
else if (b[i]=='F')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cF[k];
}
count=j;
}
}
d[count]='\0';
l2=strlen(d);
if(l2<len3)
{
reverse(d,d+l2);
for(i=l2; i<len3; i++)
{
d[i]=0+48;
}
d[i]='\0';
reverse(d,d+len3);
}
if(cp=='+')
{
reverse(c,c+len3);
reverse(d,d+len3);
carry=0;
for(i=0; i<len3; i++)
{
if(c[i]=='0' && d[i]=='0' && carry==0)
{
add[i]=0+48;
carry=0;
}
else if(c[i]=='0' && d[i]=='0' && carry==1)
{
add[i]=1+48;
carry = 0;
}
else if(c[i]=='0' && d[i]=='1' && carry==0)
{
add[i]=1+48;
carry = 0;
}
else if(c[i]=='0' && d[i]=='1' && carry==1)
{
add[i]=0+48;
carry = 1;
}
else if(c[i]=='1' && d[i]=='0' && carry==0)
{
add[i]=1+48;
carry = 0;
}
else if(c[i]=='1' && d[i]=='0' && carry==1)
{
add[i]=0+48;
carry =1;
}
else if(c[i]=='1' && d[i]=='1' && carry==0)
{
add[i]=0+48;
carry = 1;
}
else if(c[i]=='1' && d[i]=='1' && carry==1)
{
add[i]=1+48;
carry = 1;
}
}
if(carry==1)
{
add[i-1]='1';
}
add[i]='\0';
reverse(c,c+len3);
reverse(d,d+len3);
reverse(add,add+i);
sum=0;
reverse(add,add+len3);
for(j=0; j<len3; j++)
{
z=add[j]-48;
sum+=z*pow(2,j);
}
printf("%s + %s = %d\n",c ,d,sum);
}
else if(cp=='-')
{
arr1=0;
arr2=0;
reverse(c,c+len3);
reverse(d,d+len3);
for(i=0; i<len3; i++)
{
z=c[i]-48;
arr1+=z*pow(2,i);
}
for(i=0; i<len3; i++)
{
z=d[i]-48;
arr2+=z*pow(2,i);
}
sum=arr1-arr2;
reverse(c,c+len3);
reverse(d,d+len3);
printf("%s - %s = %d\n",c ,d,sum);
}
}
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
int i,j,k,l1,l2,len1,len2,len3=13,count,sum,p=0,carry,n,l,x,z,arr1,arr2;
char cp ,c1[]="0001",c2[]="0010",c3[]="0011",c4[]="0100",
c0[]="0000",c5[]="0101",c6[]="0110",c7[]="0111",c8[]="1000",c9[]="1001",
cA[]="1010",cB[]="1011",cC[]="1100",cD[]="1101",cE[]="1110",cF[]="1111",
a[100],b[100],c[100],d[100],add[100];
scanf("%d",&n);
{
for(x=0; x<n; x++)
{
scanf("%s %c %s",a,&cp,b);
len1=strlen(a);
len2=strlen(b);
count=0;
for(i=0; i<len1; i++)
{
if(a[i]=='1')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c1[k];
}
count=j;
}
else if (a[i]=='0')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c0[k];
}
count=j;
}
else if (a[i]=='2')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c2[k];
}
count=j;
}
else if (a[i]=='3')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c3[k];
}
count=j;
}
else if (a[i]=='4')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c4[k];
}
count=j;
}
else if (a[i]=='5')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c5[k];
}
count=j;
}
else if (a[i]=='6')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c6[k];
}
count=j;
}
else if (a[i]=='7')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c7[k];
}
count=j;
}
else if (a[i]=='8')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c8[k];
}
count=j;
}
else if (a[i]=='9')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=c9[k];
}
count=j;
}
else if (a[i]=='A')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cA[k];
}
count=j;
}
else if (a[i]=='B')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cB[k];
}
count=j;
}
else if (a[i]=='C')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cC[k];
}
count=j;
}
else if (a[i]=='D')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cD[k];
}
count=j;
}
else if (a[i]=='E')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cE[k];
}
count=j;
}
else if (a[i]=='F')
{
for(j=count,k=0; k<4; j++,k++)
{
c[j]=cF[k];
}
count=j;
}
}
c[count]='\0';
l1=strlen(c);
if(l1<len3)
{
reverse(c,c+l1);
for(i=l1; i<len3; i++)
{
c[i]=0+48;
}
c[i]='\0';
reverse(c,c+len3);
}
count=0;
for(i=0; i<len2; i++)
{
if(b[i]=='1')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c1[k];
}
count=j;
}
else if (b[i]=='2')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c2[k];
}
count=j;
}
else if (b[i]=='3')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c3[k];
}
count=j;
}
else if (b[i]=='4')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c4[k];
}
count=j;
}
else if (b[i]=='5')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c5[k];
}
count=j;
}
else if (b[i]=='6')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c6[k];
}
count=j;
}
else if (b[i]=='7')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c7[k];
}
count=j;
}
else if (b[i]=='8')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c8[k];
}
count=j;
}
else if (b[i]=='9')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=c9[k];
}
count=j;
}
else if (b[i]=='A')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cA[k];
}
count=j;
}
else if (b[i]=='B')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cB[k];
}
count=j;
}
else if (b[i]=='C')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cC[k];
}
count=j;
}
else if (b[i]=='D')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cD[k];
}
count=j;
}
else if (b[i]=='E')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cE[k];
}
count=j;
}
else if (b[i]=='F')
{
for(j=count,k=0; k<4; j++,k++)
{
d[j]=cF[k];
}
count=j;
}
}
d[count]='\0';
l2=strlen(d);
if(l2<len3)
{
reverse(d,d+l2);
for(i=l2; i<len3; i++)
{
d[i]=0+48;
}
d[i]='\0';
reverse(d,d+len3);
}
if(cp=='+')
{
reverse(c,c+len3);
reverse(d,d+len3);
carry=0;
for(i=0; i<len3; i++)
{
if(c[i]=='0' && d[i]=='0' && carry==0)
{
add[i]=0+48;
carry=0;
}
else if(c[i]=='0' && d[i]=='0' && carry==1)
{
add[i]=1+48;
carry = 0;
}
else if(c[i]=='0' && d[i]=='1' && carry==0)
{
add[i]=1+48;
carry = 0;
}
else if(c[i]=='0' && d[i]=='1' && carry==1)
{
add[i]=0+48;
carry = 1;
}
else if(c[i]=='1' && d[i]=='0' && carry==0)
{
add[i]=1+48;
carry = 0;
}
else if(c[i]=='1' && d[i]=='0' && carry==1)
{
add[i]=0+48;
carry =1;
}
else if(c[i]=='1' && d[i]=='1' && carry==0)
{
add[i]=0+48;
carry = 1;
}
else if(c[i]=='1' && d[i]=='1' && carry==1)
{
add[i]=1+48;
carry = 1;
}
}
if(carry==1)
{
add[i-1]='1';
}
add[i]='\0';
reverse(c,c+len3);
reverse(d,d+len3);
reverse(add,add+i);
sum=0;
reverse(add,add+len3);
for(j=0; j<len3; j++)
{
z=add[j]-48;
sum+=z*pow(2,j);
}
printf("%s + %s = %d\n",c ,d,sum);
}
else if(cp=='-')
{
arr1=0;
arr2=0;
reverse(c,c+len3);
reverse(d,d+len3);
for(i=0; i<len3; i++)
{
z=c[i]-48;
arr1+=z*pow(2,i);
}
for(i=0; i<len3; i++)
{
z=d[i]-48;
arr2+=z*pow(2,i);
}
sum=arr1-arr2;
reverse(c,c+len3);
reverse(d,d+len3);
printf("%s - %s = %d\n",c ,d,sum);
}
}
}
return 0;
}
No comments:
Post a Comment