#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int bin2dec1(char *afterx1);
int bin2dec2(char *afterx2);
void displayBits(unsigned value);
/* function main begins program execution */
int main()
{
int opcode,dec1,dec2;
char oprand1[13]="",oprand2[13]="";
char ones[13]="111111111111";
char zeros[13]="000000000000";
char afterx1[13]="",afterx2[13]="";
printf("Instruction : ");
scanf("%d%s%s",&opcode,oprand1,oprand2);
if((opcode == 1||opcode == 0) && strlen(oprand1)<=12 && strlen(oprand2)<=12)
{
if(strncmp(oprand1,ones,1)==0)
{
strncpy(afterx1,ones,12-strlen(oprand1));
}
if(strncmp(oprand1,zeros,1)==0)
{
strncpy(afterx1,zeros,12-strlen(oprand1));
}
if(strncmp(oprand2,ones,1)==0)
{
strncpy(afterx2,ones,12-strlen(oprand2));
}
if(strncmp(oprand2,zeros,1)==0)
{
strncpy(afterx2,zeros,12-strlen(oprand2));
}
printf("After extension : %d %s %s\n",opcode,strcat(afterx1,oprand1),strcat(afterx2,oprand2));
if(strncmp(afterx1,ones,1)==0)
{
dec1 = bin2dec1(afterx1);
}
if(strncmp(afterx1,zeros,1)==0)
{
dec1 = bin2dec1(afterx1);
}
if(strncmp(afterx2,ones,1)==0)
{
dec2 = bin2dec2(afterx2);
}
if(strncmp(afterx2,zeros,1)==0)
{
dec2 = bin2dec2(afterx2);
}
if(opcode == 1)
{
printf("Result : ");
displayBits(dec1+dec2);
printf("\nResult in Decimal : %d\n\n",dec1+dec2);
}
if(opcode == 0)
{
printf("Result : ");
displayBits(dec1-dec2);
printf("\nResult in Decimal : %d\n\n",dec1-dec2);
}
} /* end if */
else
{
printf("\aInvalid instruction.\n");
} /* end else */
system("PAUSE");
return 0; /* indicates successful termination */
} /* end main */
/* convert a binary string to a decimal number, returns decimal value */
int bin2dec1(char *afterx1)
{
int a, b, p, q;
int length1, sum1 = 0;
length1 = strlen(afterx1) - 1;
for(a = 0; a <= length1; a++)
{
p = (afterx1[a] - '0'); /* char to numeric value */
for(b = 1, q = length1; q > a; q--)
{
b *= 2; /* 1 2 4 8 16 32 64 ... place-values, reversed here */
}
sum1 = sum1 + p * b; /* sum it up */
}
return(sum1);
} /* end function bin2dec1 */
int bin2dec2(char *afterx2)
{
int c, d, r, s;
int length2, sum2 = 0;
length2 = strlen(afterx2) - 1;
for(c = 0; c <= length2; c++)
{
r = (afterx2[c] - '0'); /* char to numeric value */
for(d = 1, s = length2; s > c; s--)
{
d *= 2; /* 1 2 4 8 16 32 64 ... place-values, reversed here */
}
sum2 = sum2 + r * d; /* sum it up */
}
return(sum2);
} /* end function bin2dec2 */
/* display bits of an unsigned integer value */
void displayBits(unsigned value)
{
unsigned i; /* counter */
/* declare displayMask and left shift 11 bits */
unsigned displayMask = 1 << 11;
/* loop through bits */
for(i=1; i <= 12; i++)
{
putchar(value & displayMask ? '1' : '0');
value <<= 1; /* shift value left by 1 */
} /* end for */
} /* end function displayBits */
does it possible change that if part to switch case??