void operations0(char* s) { int t=binary_to_decimal(s+3); char temp[9]; memset(temp,0,sizeof(temp)); for(int i=0;i<8;++i) temp[i]='0'; decimal_to_binary(accumulator,temp); strcpy(contents[t],temp); }
int send_string_stdin(void) { char *theNumber = NULL; // Used for strtok char line[256]; double decimalResult = 0; int pCount = 0; while( fgets(line, 256, stdin) != NULL ) { /* * We'll use strtok to tokenize the string, number by number, using the * space character as a delimiter */ theNumber = strtok(line, " "); while( theNumber != NULL ) { /* If the user wants slightly prettier output */ if( lineSpacing == 1 && pCount > 0 && textMode == 0) { printf("\n"); } if( textMode == 1 ) number_to_string( theNumber ); else { decimalResult = binary_to_decimal( theNumber ); string_printer( &decimalResult ); } theNumber = strtok(NULL, " "); // Re-run strtok, grab next number ++pCount; } } return(0); }
/* * ========================================================================== * MAIN FUNCTION * ========================================================================== */ int main( int argc, char *argv[] ) { if( argc > 1 ) { /* If they want help */ if( strcmp( "-h", argv[1]) == 0 || strcmp( "--help", argv[1]) == 0 ) { print_help(); return(0); } /* If they want version / author info */ if( strcmp( "--version", argv[1]) == 0 ) { print_version(); return(0); } } /* Initialize variables */ bin2bin = 0; bin2dec = 0; bin2hex = 0; bin2phex = 0; bin2oct = 0; sections = 0; hexCaps = 0; phexCaps = 0; verbose = 0; textMode = 0; bigEndian = 1; /* Get options */ int opt = getopt( argc, argv, optString ); while ( opt != -1 ) { switch( opt ) { case 'h': // Help print_help(); return(0); break; case 'b': // Binary bin2bin = 1; break; case 'd': // Decimal bin2dec = 1; break; case 'v': // Verbosity verbose = 1; break; case 'x': // Hexadecimal bin2hex = 1; break; case 'X': // Hexadecimal with caps hexCaps = 1; break; case 'a': // Extra precise hexadecimal bin2phex = 1; break; case 'A': // Same, but also with caps phexCaps = 1; break; case 'o': // Octal bin2oct = 1; break; case 's': // Use sections sections = 4; break; case 'l': // Print newlines between sections of output lineSpacing = 1; break; case 't': textMode = 1; break; case 'e': bigEndian = 0; break; case 'E': bigEndian = 1; break; default: // Do nuttin' break; } opt = getopt( argc, argv, optString ); } /* Shift / adjust argc and argv */ argv += ( optind - 1 ); argc -= ( optind - 1 ); /* With no args, we default to decimal */ if( bin2dec == 0 && bin2hex == 0 && bin2phex == 0 && bin2oct == 0 && hexCaps == 0 && phexCaps == 0 && bin2bin == 0 ) { bin2dec = 1; } /* Set the total number of conversions */ totalConversions = ( bin2dec + bin2hex + bin2phex + bin2oct + hexCaps + phexCaps + bin2bin ); /* If they're reading from stdin */ if( argc == 1 || (strcmp( "-", argv[1] ) == 0) ) { /* Grab string */ if( send_string_stdin() == 1 ) return(1); else return(0); } int pCount = 0; // Random counter! while( argc > 1 ) { if( textMode == 1 ) { number_to_string( argv[1] ); --argc; ++argv; ++pCount; continue; } /* Check to make sure it's a binary number */ if( check_for_binary( argv[1] ) != 0 ) { /* If it isn't, tell them but don't kill the program */ fprintf(stderr, "WARNING: Not a binary number:\t%s\n", argv[1] ); --argc; ++argv; ++pCount; continue; } /* Get our decimal number from the binary to decimal converter */ double decimalResult = binary_to_decimal( argv[1] ); /* If the user wants prettier output, give it to them */ if( lineSpacing == 1 && pCount > 0 ) printf("\n"); /* Handle binary output */ if( bin2bin == 1 ) { if( verbose == 1 ) printf( "BIN\t" ); print_number_string( argv[1] ); } /* Print that sumbitch out */ if( string_printer( &decimalResult ) == 1 ) return(1); /* Increment or decrement our various lists and counters */ --argc; ++argv; ++pCount; } return(0); }
/* * Convert a binary number (string) to a series of characters */ int number_to_string( char *string ) { int sLen = strlen( string ); // Length of original string int ch = 0; // Integer to receive character value int stringMoved = 0; // Number of times string was incremeneted int counter; // Generic counter /* Create our bin string */ char *binStr = malloc( (BIN_STR_LENGTH) ); if( binStr == NULL ) { mem_error("In: number_to_string"); return(1); } /* A mutable copy of the original string */ char fromStringStack[ sLen ]; char *fromString = fromStringStack; /* Null out strings */ memset( binStr, '\0', (BIN_STR_LENGTH) ); memset( fromString, '\0', ( sLen + 1 )); /* Strip whitespace */ strip_whitespace( fromString, string ); /* * Go through the string, grabbing 8-bit (or fewer) chunks of it and * converting said 'chunks' to ASCII character values, printing each * along the way. */ while( 1 ) { /* Check if we're only working with one character */ if( sLen <= (BIN_STR_LENGTH) + 1) { ch = (int)binary_to_decimal( fromString ); if( ch != 0 ) { text_mode_printer( ch, fromString ); if( lineSpacing == 1 && totalConversions < 2 ) printf("\n"); } break; } else { for( counter = 0; counter <= (BIN_STR_LENGTH) ; ++counter ) { binStr[counter] = fromString[counter]; } fromString += counter; stringMoved += counter; sLen -= counter; ch = (int)binary_to_decimal( binStr ); text_mode_printer( ch, binStr ); memset( binStr, '\0', (BIN_STR_LENGTH) ); if( lineSpacing == 1 ) printf("\n"); else if( verbose == 1 ) printf("\t"); } } if( lineSpacing == 0 ) printf("\n"); /* Reset the original string */ fromString -= stringMoved; /* Free / null binStr */ free( binStr ); binStr = NULL; return(0); }
double BinaryChromosome::evaluate(void) { return binary_to_decimal(bitstring); }
void operations6(char* s) { pc=binary_to_decimal(s+3); }
void operations2(char* s) { if(accumulator==0) pc=binary_to_decimal(s+3); }
void operations1(char* s) { accumulator=binary_to_decimal(contents[binary_to_decimal(s+3)]); }
int main(int argc, char *argv[]) { int a; int b; scanf("%d", &a); // printf("%d\n", a); while(a != 0) { int i; int num_digits_in_binary = 0; int start = 0, end = 0; for(i=0; i<20; i++) { binary[i] = 0; } num_digits_in_binary = get_binary_from_decimal(a); // printf("n digits : %d\n", num_digits_in_binary); // for(i=0; i<num_digits_in_binary; i++) // printf("%d", binary[i]); // // printf("\n"); for(i=0; i< num_digits_in_binary; i++) { if(binary[i] == 1) { start = i; break; } } for(i=start; i< num_digits_in_binary; i++) { if(i == num_digits_in_binary -1) { end = i; num_digits_in_binary++; break; } if(binary[i+1] == 0 ) { end = i; break; } } // printf("%d, %d\n", start, end); if(start == end) { binary[start + 1] = 1; binary[start] = 0; } else { int x = start; while(x <= end) { binary[x] = 0; x++; } binary[end+1] = 1; x = 0; while(x < (end-start)) { binary[x] = 1; x++; } } // for(i=0; i<num_digits_in_binary; i++) // printf("%d", binary[i]); // // printf("\n"); int n = binary_to_decimal(num_digits_in_binary); printf("%d\n", n); // printf("\n\n"); scanf("%d", &a); // printf("%d\n", a); } return 0; }