我必须完成以下任务:

实现两个两位二进制数的写入乘法 而不使用+-*/和位运算.逻辑上的 运算符&&||!只能应用于逻辑表达式AND 而不是数字本身.数组和循环的使用不是 这里是允许的.同样,通过区分所有 这里不允许装16箱.

代码的 struct 应该是这样的: 较大位数的数字很容易实现.

这两个数字是从控制台或通过逐位输入的 从文件重定向(正好是四位数).对于个位数 数字,还必须输入前导零.

为此,使用两个函数add(),该函数将两个二进制位相加 不带进位(即返回0或1)和carry(),这决定了 将两位二进制数相加时进位.可以使用其他功能.

输出应使这两个因素相邻 如果没有前导零,则后面跟一个等号,乘积 没有前导零.

我的方法是Booth的二进制数乘法算法.然而,我不知道如何在没有循环的情况下实现这一点.

我有正确的算法吗?

上述算法的代码示例:

#include <stdio.h>

// Function to display a binary number
void displayBinary(int n) {
    if (n == 0) {
        printf("0");
        return;
    }

    int binary[32];
    int i = 0;

    while (n > 0) {
        binary[i] = n % 2;
        n /= 2;
        i++;
    }

    for (i--; i >= 0; i--) {
        printf("%d", binary[i]);
    }
}

// Function to perform Booth multiplication
int boothMultiplication(int multiplicand, int multiplier) {
    int m = multiplicand;
    int q = multiplier;
    int ac = 0; // Accumulator
    int q0 = 0; // Least significant bit of q
    int q1 = 0; // Next least significant bit of q

    int n = sizeof(int) * 8; // Number of bits in an integer 

    printf("Step\t\tA\t\tQ\t\tQ(-1)\tQ(0)\tOperation\n");

    for (int step = 1; step <= n; step++) {
        int q0_q1 = (q0 << 1) | q1;
        int operation = 0;

        if ((q0_q1 & 0b11) == 0b01) {
            ac += m;
            operation = 1;
        } else if ((q0_q1 & 0b11) == 0b10) {
            ac -= m;
            operation = -1;
        }

        if (q & 1) {
            q1 = q0;
        }
        q0 = q & 1;
        q >>= 1;

        printf("%d\t\t\t", step);
        displayBinary(ac);
        printf("\t");
        displayBinary(q);
        printf("\t");
        displayBinary(q1);
        printf("\t");
        displayBinary(q0);
        printf("\t");

        if (operation == 1) {
            printf("Addition (+%d)\n", m);
        } else if (operation == -1) {
            printf("Subtraction (-%d)\n", m);
        } else {
            printf("No Operation\n");
        }
    }

    return ac;
}

int main() {
    int multiplicand, multiplier;
    
    printf("Enter the multiplicand: ");
    scanf("%d", &multiplicand);
    
    printf("Enter the multiplier: ");
    scanf("%d", &multiplier);

    int product = boothMultiplication(multiplicand, multiplier);

    printf("\nResult: %d * %d = %d\n", multiplicand, multiplier, product);

    return 0;
}

The original task in German

推荐答案

这应该可以满足要求.

#include <stdio.h>

int add(int b1, int b0) { return b1 != b0; }

int carry(int b1, int b0) { return b1==1 && b0==1; }

void print(int b3, int b2, int b1, int b0, char *s)
{
    int c = 0;
    if (b3==1 || c) printf("%i", b3), c = 1;
    if (b2==1 || c) printf("%i", b2), c = 1;
    if (b1==1 || c) printf("%i", b1), c = 1;
    printf("%i%s", b0, s), c = 1;
}

int main()
{
    int x1, x0, y1, y0, z3, z2, z1, z0, a1, a0;
    scanf("%i", &x1),   scanf("%i", &x0); 
    scanf("%i", &y1),   scanf("%i", &y0); 
    z1 = y0 ? x1 : 0,   z0 = y0 ? x0 : 0;
    a1 = y1 ? x1 : 0,   a0 = y1 ? x0 : 0;
    z2 = carry(z1, a0), z1 = add(z1, a0);
    z3 = carry(z2, a1), z2 = add(z2, a1);
    print( 0,  0, x1, x0, " * "),
    print( 0,  0, y1, y0, " = "),
    print(z3, z2, z1, z0, "\n");
}

C++相关问答推荐

C中的ATONE会扰乱SEN/CLUTE GMS应用程序中的其他字符串

为什么已经设置的值在C中被重置为for循环条件中的新值?

字符数组,字符指针,在一种情况下工作,但在另一种情况下不工作?

如何使fputs功能提示错误输入并要求用户重新输入.程序停止而不是请求新的输入

C中的指针增量和减量(*--*++p)

向上强制转换C中的数值类型总是可逆的吗?

限制不同类型的限定符

平均程序编译,但结果不好

为什么中断函数会以这种方式影响数组?

如何在GET_STRING输入后对少数几个特定字符串进行C判断?

在进程之间重定向输出和输入流的问题

Go和C中的数据 struct 对齐差异

链表删除 node 错误

将某些内容添加到链接列表时,列表中的其他项将使用最后添加的项的名称

OMP并行嵌套循环

如何在MSVC中使用intSafe.h函数?

分支预测和UB(未定义的行为)

C 错误:对 int 数组使用 typedef 时出现不兼容的指针类型问题

UEFI 应用程序中的计时器回调仅在 AMI BIOS 中挂起

如何在 C 中编辑 struct 体中的多个变量