0

このプログラムは、基数4の数値を基数2の数値に変換するためのものであり、その場で実行する必要があります。

#include<stdio.h>
#include<string.h>

void shiftr(char num[],int i)
{
     memmove(num+i,num+i+1,strlen(num)-i);
}

char* convert4to2(char num[])
{
int i=0,len;
char ch;

    while(num[i]!='\0')
    {
        ch=num[i];
        shiftr(num,i);
        switch(ch)
        {
            case '0':num[i++]='0';
                 num[i++]='0';
                 break;

            case '1':num[i++]='0';
                 num[i++]='1';
                 break;

            case '2':num[i++]='1';
                 num[i++]='0';
                 break;

            case '3':num[i++]='1';
                 num[i++]='1';
                 break;

            default:printf("Error");
        }
    }

    num[i]='\0';
    return(num);
}



void main()
{
    char num[20];
    printf("Enter the Base 4 Number:");
    scanf("%s",&num);
    printf("The Binary Equivalent is:%s\n",convert4to2(num));   
}

121(基数4の数値)の入力の出力は011001である必要がありますが、01のみが表示されます。12101のような大きな数値の場合、最初と最後の1つの数字をとって0100が表示されます。何が問題なのですか?

4

3 に答える 3

1

入力を積極的に破棄しています。たとえば、最初の反復では、数値を1桁シフトし、その後、0桁目と1桁目(4桁の次の2桁を含む)のデータを1桁目のバイナリ出力で上書きします。

于 2012-10-27T13:28:21.333 に答える
1

文字を使用して基数4から基数2に直接変換する代わりに、strtol任意の基数の整数文字列をに変換する関数に文字列を挿入することができますlong。そこから、バイナリ表現を印刷するのは非常に簡単です。

編集:例:

char* convert4to2(const char input[], char *output, const size_t output_length)
{
    /* First make sure the output string is long enough */
    if (output_length < (sizeof(long) * 8 + 1))  /* +1 for '\0' */
        return NULL;  /* Not enouth space */

    /* Convert from the input string to a `long` */
    long value = strtol(input, NULL, 4);  /* The last `4` is the base of the input string */

    /* Convert the number to binary */
    char *output_ptr = output;

    /* Multiply with 8 to get the number of bits */
    /* Subtract 1 because bits a numbered from zero */
    for (int bit = sizeof(long) * 8 - 1; bit >= 0; bit--)
    {
        /* `value >> bit` make the current bit the lowest bit */
        /* `& 1` to mask out all but the lowest bit */
        /* `+ '0'` to make it a proper character digit */
        *output_ptr++ = ((value >> bit) & 1) + '0';
    }

    /* Terminate the string */
    *output_ptr = '\0';

    /* Return the converted string */
    return output;
}

このように呼んでください:

const char num[] = "121";
char output[65];  /* `long` can be 64 bits, plus one for the string terminator */

printf("%s in base 4 is %s in base 2\n",
    num, convert4to2(num, output, sizeof(output)));
于 2012-10-27T13:45:42.160 に答える
0

上にシフトする必要はありません。後ろに移動するだけです。

#include <stdio.h>
#include <string.h>

void convert4to2(char *str)
{
size_t idx;
char ch;
static char *conv[4] = {"00", "01","10", "11" };

idx = strlen(str);
str[idx*2] = 0;

while(idx--) {
    ch=str[idx];
    if (ch < '0' || ch > '3') return; /* replace by relevant error handler */
    memcpy(str+2*idx, conv[ ch - '0' ], 2 );
    }

return;
}

int main(void)
{
char quad[21] = "0123321023" ;

printf("Old:%s\n", quad);
convert4to2(quad);
printf("New:%s\n", quad);

return 0;
}
于 2012-10-27T14:10:41.907 に答える