-2

バイナリ コードで除算を行う方法を示すプログラムを実行しています。

#include <stdio.h>
#include <stdlib.h>

unsigned int division(unsigned int a, unsigned int b) // fonction division
{
    int i;
    b = (b << 16);
    for(i = 0; i <= 15; i++)
    {
        a = (a << 1);
        if(a >= b)
        {
            a = a - b;
            a = a + 1;
        }
    }
    return a;
}

int main()
{
    unsigned int i, a, b, d, N;
    unsigned short c;

    FILE* rep;
    rep = fopen("reponse.txt", "w"); /* ouverture du fichier */

    printf("Entrer le nombre de division a effectuer");
    scanf("%i", &N);

    printf("Veuillez inserer la ou les divisions a effectuer\n");
    printf("de la facon suivante : a/b\n");

    for(i = 1; i <= N; i++)
    {
        scanf("%i/%i", &a, &b); /* il suffira d'entrer a/b */
            d = division(a, b); /* la division de a par b */
            c = unsigned short(d); /* les 16 premiers bits */
            d = (d >> 16); /* les 16 premiers bits */
            fprintf(rep, "division %i : %i/%i = %d reste %i\n", i, a, b, c, d);
    }
    fclose(rep); /* fermeture du fichier */
    return 0;
}

error: expected expression before 'unsigned'この行でc = unsigned short(d); 、何が問題なのか正確にはわかりません! 誰か助けてくれませんか?私はCode::BlocksでLinux Ubuntu 12.10コーディングを行っています

4

1 に答える 1

4
c = unsigned short(d);

これは C ではありません。

ある型から別の型に変換するには、キャスト演算子を使用できます。

c = (unsigned short) d;

unsigned intと の間に暗黙的な変換があるunsigned shortため、キャストは必要なく、これは同等であることに注意してください。

c = d;
于 2013-03-16T21:26:35.650 に答える