int main(void)
{
    /* 0(bin) have length 1, so r[0] should be 1 */
    int bin_max[18] = { 1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 };
    int bin_len[18] = { 0, };

    for(int i = 0; i < 131072; i++)
    {
        char bin[18] = {0, };
        int len = convert_decimal_to_binary(i, bin);

        if(strstr(bin, "00") != NULL) bin_len[len]++;
    }

    for(int i = 0; i < 18; i++)
        printf("%d: %d\n", i, bin_max[i] - bin_len[i]);

    return 0;
}
示例#2
0
文件: p1.c 项目: ajgreenman/cs452-1
/**************************************************************************
 * Main driver of the program.                                            *
 **************************************************************************/
int main(int argc, char **argv)
{
  if(argc != 3) {
    printf("Usage: $ %s input_a input_b\n", argv[0]);
    return -1;
  }

  #ifdef DIAG
    printf("Starting program...\n");
    sleep(1);
  #endif

  // Declare variables.
  pid_t pid;
  FILE *vector_a, *vector_b, *output;
  int pipe1_fd[2], pipe2_fd[2], length;;
  long long decimal_value, decimal_value2;
  char input_a[MAX_VECTOR_SIZE], input_b[MAX_VECTOR_SIZE];

  // Open input and output files.
  vector_a = fopen(argv[1], "r");
  vector_b = fopen(argv[2], "r");
  output = fopen("output.txt", "w");

  // Create pipes.
  if(pipe(pipe1_fd)) {
    perror("Plumbing problem.\n");
    exit(1);
  }

  if(pipe(pipe2_fd)) {
    perror("Plumbing problem.\n");
    exit(1);
  }

  pid = fork();
  if(pid < 0) {
    fprintf(stderr, "First fork() failed.\n");
    return(1);
  } else if(pid) {
    pid = fork();
    if(pid < 0) {
      fprintf(stderr, "Second fork() failed.\n");
      return(1);
    } else if(pid) {
      // Process 2

      #ifdef DIAG
        printf("Process 2 created.\n");
        sleep(2);
      #endif

      close(pipe1_fd[PIPE_READ]); // Close the read portion of pipe 1.
      close(pipe1_fd[PIPE_WRITE]); // Close the write portion of pipe 1.
      close(pipe2_fd[PIPE_WRITE]); // Close the write portion of pipe 2.

      while(fgets(input_a, MAX_VECTOR_SIZE, vector_a) != NULL) {
        // Reads in and process input_a line by line.

        char *position;
        if((position = strchr(input_a, '\n')) != NULL) {
          *position = '\0'; // Removes newline character from end of buffer.
        }

        #ifdef DIAG
          printf("Process 2: Read line '%s' from input_a file.\n", input_a);
        #endif

        length = strlen(input_a); // Get the length in bits of input_a.
        decimal_value = convert_binary_to_decimal(input_a); // Convert the binary number to decimal for easier math.

        #ifdef DIAG
          printf("Process 2: Converted '%s' to integer value %lld.\n", input_a, decimal_value);
        #endif

        read(pipe2_fd[PIPE_READ], input_b, sizeof(input_b)); // Reads in the two's complimented input_b from the pipe.
        #ifdef DIAG
          printf("Process 2: Read '%s' from pipe 2.\n", input_b);
        #endif

        decimal_value2 = strtoll(input_b,  NULL, 10); // Convert the binary number to decimal for easier math.
        #ifdef DIAG
          printf("Process 2: Converted '%s' to integer value %lld.\n", input_b, decimal_value2);
        #endif

        decimal_value += decimal_value2; // Add input_a with input_b.

        #ifdef DIAG
          printf("Process 2: Added %lld with %lld to get %lld.\n", decimal_value - decimal_value2, decimal_value2, decimal_value);
        #endif

        convert_decimal_to_binary(output, decimal_value, length); // Convert the decimal number back to a binary string.

        #ifdef DIAG
          printf("Process 2: Converting %lld to binary and writing to file \"output.txt\".\n", decimal_value);
        #endif

        #ifdef DIAG
          sleep(3);
        #endif
      }

      #ifdef DIAG
        printf("Process 2: No more input, closing pipe and exiting.\n");
      #endif

      close(pipe2_fd[PIPE_READ]); // Close the read portion of pipe 2.

      exit(0);
    } else {
      // Process 1

      #ifdef DIAG
        printf("Process 1 created.\n");
        sleep(2);
      #endif

      close(pipe1_fd[PIPE_WRITE]); // Close the write portion of pipe 1.
      close(pipe2_fd[PIPE_READ]); // Close the read portion of pipe 2.

      while(read(pipe1_fd[PIPE_READ], input_b, sizeof(input_b))) {
        #ifdef DIAG
          printf("Process 1: Read '%s' from pipe 1.\n", input_b);
        #endif

        decimal_value = convert_binary_to_decimal(input_b); // Convert the binary number to decimal for easier math.
        #ifdef DIAG
          printf("Process 1: Converted '%s' to integer value %lld.\n", input_b, decimal_value);
        #endif

        decimal_value++; // Increments the decimal value by 1.
        #ifdef DIAG
          printf("Process 1: Incremented by 1 to %lld.\n", decimal_value);
        #endif

        sprintf(input_b, "%lld", decimal_value); // Convert from long long to string.

        #ifdef DIAG
          printf("Process 1: Writing to pipe 2.\n");
        #endif
        // Write the incremented number to the pipe.
        write(pipe2_fd[PIPE_WRITE], (const void *) input_b, (size_t) strlen(input_b) + 1);

        #ifdef DIAG
          sleep(3);
        #endif
      }

      #ifdef DIAG
        printf("Process 1: No more input, closing pipes and exiting.\n");
      #endif

      close(pipe1_fd[PIPE_READ]); // Close the read portion of pipe 1.
      close(pipe2_fd[PIPE_WRITE]); // Close the write portion of pipe 2.

      exit(0);
    }
  } else {
    // Process 0

    #ifdef DIAG
      printf("Process 0 created.\n");
      sleep(2);
    #endif

    close(pipe1_fd[PIPE_READ]); // Close the read portion of pipe 1.
    close(pipe2_fd[PIPE_READ]); // Close the read portion of pipe 2.
    close(pipe2_fd[PIPE_WRITE]); // Close the write portion of pipe 2.

    while(fgets(input_b, MAX_VECTOR_SIZE, vector_b) != NULL) {
      // Reads in and processes input_b line by line.

      char *position;
      if((position = strchr(input_b, '\n')) != NULL) {
        *position = '\0'; // Removes newline character from end of buffer.
      }

      #ifdef DIAG
        printf("Process 0: Read line '%s' from input_b file.\n", input_b);
      #endif

      complementer(input_b); // Bitwise complement on the binary number.

      #ifdef DIAG
        printf("Process 0: Complemented input to '%s'.\n", input_b);
      #endif

      // Write the complemented number to the pipe.

      #ifdef DIAG
        printf("Process 0: Writing to pipe 1.\n");
      #endif

      write(pipe1_fd[PIPE_WRITE], (const void *) input_b, (size_t) strlen(input_b) + 1);

      #ifdef DIAG
        sleep(3);
      #endif
    }

    #ifdef DIAG
      printf("Process 0: No more input, closing pipe and exiting.\n");
    #endif

    close(pipe1_fd[PIPE_WRITE]); // Close the write portion of pipe 1.

    exit(0);
  }

  return 0;
}