With a pre-driven number, it works according to a simple algorithm (for example, 4 characters)

#include <stdio.h> #include <stdlib.h> main () { int number = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a10 = 0; printf("Please input char 4 "); scanf("%d", &number); a1 = number / 1000; a2 = (number - (a1 * 1000)) / 100; a3 = (number - ((a1 * 1000) + (a2 * 100))) / 10; a4 = (number - ((a1 * 1000) + (a2 * 100) + (a3 * 10))); a10 = a1 * 8 + a2 * 4 + a3 * 2 + a4 * 1; printf("%d", a10); system("PAUSE"); } 

But if the numbers are more than 4 or less, I can not figure out how to implement. I thought through the algorithm, it is necessary to count the number of characters in a given number and with a bunch of conditions to implement it, but it is very cumbersome and I can not figure out how to make it shorter. Given that I can only use WHILE and IF ELSE conditions. Who can tell the algorithm?

    4 answers 4

    I am not sure that I understood the task, but if you need to enter the toe and one from the keyboard, and then print them as a decimal number, then it is more convenient to type in a cycle character-by-character and calculate the result, which you can type later.

    The program is based simply on the representation of a number in a certain number system (in this case, binary).

    I don’t know if you can (as part of the assignment) use shifts, so just multiply by the base of the number system.

    There must be something in the spirit

     int c, sum = 0; while ((c = getchar()) != EOF) { if (!(c == '0' || c == '1') break; sum = sum * 2 + (c-'0'); } printf ("%d\n",sum); 

    To understand why 0 is subtracted from c look at the ASCII character table.

    • Is there an opportunity to do without getchar? Just enter characters in turn 1 or 0? Or something different? - Stee1House

    For a start, it would be nice to get the numbers from the end:

     nextDigit = currNumber % 10; currNumber = currNumber / 10; 

    and pack them into an array. You will need to maintain the current index in the array in a variable. (If the selected array size is not enough, to add the next digit there, you will need to increase the size of the array using realloc . Or from the very beginning, select the larger array, it will come down for the learning task.)

    Then, go over the array and "collect" the numbers in a new number on a new basis:

     currNumber = currNumber * 8 + nextDigit; 

    In C ++, it would be easier to cope with the task, since there are standard containers that take care of low-level things like memory allocation.

    Notice, however, that the code from your question translates from an 8-bit to a 10-bit record (and not from a 2-bit). For binary, the constant 8 must be replaced by 2 .

    (Since the task is educational, I do not provide the full code.)


    By the way, you can not use an intermediate array, but calculate a new value on the fly:

     int nextDigit = inputNumber % 10; inputNumber = inputNumber / 10; targetNumber = targetNumber * 8 + nextDigit; 

    (The idea is torn from the @avp answer)

    • You can use only WHILE and ELSE IF to use other control structures, arrays, etc. prohibited. I do not think that this decision, even if it is the right one for me. Anyway, thanks for responding. - Stee1House
    • @steelhouse: updated the answer, now without cycles :) That is, ugh, without arrays! - VladD 2:32

    Well, if you enter a number as a decimal, then. First of all, for larger numbers it is better to use the long / unsigned long type (on some architectures this is identical to an int / unsigned int). Secondly, there are operations / and%. and will be approximately

     a10 = 0; factor = 1; while(number) { a10 += number%10 *factor; number /= 10; factor *= 2; // или factor <<= 1; } 

    Something like this: take the lower digit. The value of a10 is increased by this digit multiplied by the power of two for this digit (accumulated in factor). Start with the youngest digit and up to the older one. The number of d. positive.

      Here are some options:

      1. to stuff everything not in a1, a2, a3, a4 but into array a [20]. ie, instead of a1 - a [0], a2 - a [1] ... an array of size 20 (so that the maximum number of 8 bytes would fit) fill in all the excess with zeroes or "-1"

      2. to use associations http://www.programmers.kz/2009/07/14/-obedineniya.html

      • The class. Associations are after all union. For such advice, the vehicle has the right to fill the adviser's face. Explain why? - alexlz 2:19
      • You can use only WHILE and ELSE IF to use other control structures, arrays, etc. prohibited. I do not think that this decision, even if it is the right one for me. Anyway, thanks for responding. - Stee1House