#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??

String comparisons in C using the standard library functions can't directly be used in a switch, and you are basically doing the most efficient way from the perspective of the CPU, which is why as a C program its always best to avoid using strings if you can use enumerators instead. But many times this can't be helped and run-time string parsing is required, so in these cases there are a few alternatives instead of a bunch of if statements with str*()functions.

Instead of:

if (strcmp(str,"Test1"))
    funct(1,2,3,4);
  if (strcmp(str,"Test2"))
    funct2(5,6,7);

You can try this:

typedef enum {
  PARSE_NONE, PARSE_TEST1, PARSE_TEST2
} PARSE_ENUMS;

PARSE_ENUMS parse() {
  if (strcmp(str,"Test1")) return PARSE_TEST1;
  if (strcmp(str,"Test1")) return PARSE_TEST2;
  return PARSE_NONE;
}
switch (parse(str)) {
    case PARSE_TEST1:
      funct(1,2,3,4);
    break;
    case PARSE_TEST2:
      funct2(5,6,7);
    break;
  }

While this may be nearly the exact same code, this actually allows you to use a switch to contain blocks of relevant code, also, its technically takes a few less clock cycles to process the second example than the first.

By the way, if a comparison fails early in the str*() functions, they abort, so comparing "test" with "two" will break on the second character, so its an efficient function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.