二进制数学编程
Binary math programming
我被要求创建一个程序,该程序接受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;
}
.....
}
相关文章:
- visual在c++中将十进制数转换为二进制数
- 二进制数之和(使用C样式字符串)
- 以 10 c++ 为基数显示 1024 位二进制数
- 二进制数和位标志'x'
- C++ 二进制数到整数值的数组
- 通过C++模板元编程从数组名称推断数组维度
- 二进制数组中最大连续 1 的起始和结束索引,以 C++ 为单位
- dev如何在C++中比较二进制数
- 递归回溯打印长度为N的二进制数的所有组合,而不使用循环
- 向后打印二进制数
- 使用位黑客反转二进制数
- 添加两个二进制数(整数数组)
- 从 0 到 n 的二进制数,具有相同数量的字符/
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- 所有测试用例都将小数点转换为二进制数
- 尝试将二进制数组转换为十六进制C++然后打印结果
- 如何缩放二进制数组以保留值
- 计数二进制数中连续1的数量
- 使用堆栈将十进制数转换为二进制数
- C++ 添加二进制数 - 分段错误