二进制数学编程

Binary math programming

本文关键字:编程 二进制数      更新时间:2023-10-16

我被要求创建一个程序,该程序接受2个二进制数,然后输出加,减和乘的结果。我的代码适用于加法,但当我尝试做减法或乘法时,输出不像预期的那样。cout语句显示每个数字,并且有几个数字不正确。有人能指出我代码中的缺陷吗?

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
    long num1, num2;
    int choice;
    int i = 0, r = 0, sum[20];
    cout << "Enter the first binary number: ";   //prompts user to input binary values to perform arithmetic on
    cin >> num1;
    cout << "Enter the second binary number: ";
    cin >> num2;
    cout << "To add the numbers press 1, to subtract the numbers press 2," << endl;  //prompts user to choose which operation to perform
    cout << "to multiply the numbers press 3" << endl;
    cin >> choice;
    if (choice != 1 & choice != 2 & choice != 3)  //if statement to test for valid input
    {
        cout << "Please enter a valid choice: ";
        cin >> choice;
    }
    if (choice == 1) 
    {
        while (num1 != 0 || num2 != 0)     //if user chooses 1, perform binary arithmetic
        {
            sum[i++] = (num1 % 10 + num2 % 10 + r) % 2;
            r = (num1 % 10 + num2 % 10 + r) / 2;
            num1 = num1 / 10;
            num2 = num2 / 10;
        }
        if (r != 0)
            sum[i++] = r;
        --i;
        cout << "The sum of the two numbers is: ";

        while (i >= 0)
            cout << sum[i--];
        cout << ". ";
    }
    if (choice == 2)
    {
        while (num1 != 0 || num2 != 0)     //if user chooses 2, perform binary subtraction
        {
            int i = 0, r = 0, diff[20];
            diff[i++] = (num1 % 10 - num2 % 10 + r) % 2;
            r = (num1 % 10 - num2 % 10 + r) / 2;
            num1 = num1 / 10;
            num2 = num2 / 10;
            if (r != 0)
                diff[i++] = r;
            --i;
            cout << "The difference of the two numbers is: ";
            while (i >= 0)
                cout << diff[i--];
            cout << ". ";
        }
    }
    if (choice == 3)   //if user chooses 3, perform binary multiplication
    {
        while (num1 != 0 || num2 != 0)
        {
            int i = 0, r = 0, product[20];
            product[i++] = (num1 % 10 * num2 % 10 + r) % 2;
            r = (num1 % 10 * num2 % 10 + r) / 2;
            num1 = num1 / 10;
            num2 = num2 / 10;
            if (r != 0)
                product[i++] = r;
            --i;
            cout << "The product of the two numbers is: ";
            while (i >= 0)
                cout << product[i--];
            cout << ". ";
        }
    }

    system("pause");
    return 0;
}

你可能想尝试一下

int binary2decimal(int n) /* Function to convert binary to decimal.*/
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(2,i);
        ++i;
    }
    return decimal;
}
int main()
{
    ....
    ....
    if(!num1 || !num2)
    {
        cout << "Result = 0";
        exit(0);
    }
    .....
    switch(choice)
    {
        case 1:                   
             int r = binary2decimal(num1) + binary2decimal(num2);
             cout << "Result = " << r;
             break;
         case 2: 
             int r = binary2decimal(num1) - binary2decimal(num2);
             cout << "Result = " << (r>=0) ? r : -r;
             break;
         case 3: 
             int r = (num1 & num2) ? 
                 binary2decimal(num1) * binary2decimal(num2) : 0;
             cout << "Result = " << r;
             break; 
         default:
             cout << "Enter a valid choice next time";
             break;
     }
    .....
}