以下是我需要关注的工作表https://imgur.com/a/JuLpQZt
这是我现在的代码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
void arithmetic();
int ALU(unsigned char operand1, unsigned char operand2, unsigned char control_signals);
int askOperand1();
int askOperand2();
unsigned char operand1;
unsigned char operand2;
unsigned char control_signals;
const int ACC = 16; //ACC = ACCUMULATOR
int main()
{
for(;;)
{
system("cls");
ALU(operand2,operand2,control_signals);
}
getch();
return 0;
}
int ALU(unsigned char operand1, unsigned char operand2, unsigned char control_signals)
{
operand1=askOperand1();
operand2=askOperand2();
int pos1,pos2;
unsigned char bin8_1[] = "00000000";
unsigned char bin8_2[] = "00000000";
/*OPERAND 1*/
for (pos1 = 8; pos1 >= 0; --pos1)
{
if (operand1 % 2)
bin8_1[pos1] = '1';
operand1 /= 2;
}
printf("\n\nBinary Equivalence of Operand 1: %s",bin8_1);
/*OPERAND 2*/
for (pos2 = 8; pos2 >= 0; --pos2)
{
if (operand2 % 2)
bin8_2[pos2] = '1';
operand2 /= 2;
}
printf("\n\nBinary Equivalence of Operand 2: %s",bin8_2);
/*ARITHMETIC FUNCTIONS*/
int option, remainder = 0, sum[ACC], k;
arithmetic();
scanf("%d",&option);
switch(option)
{
case 1: //ADDITION
while (bin8_1 != 0 || bin8_2 != 0)
{
sum[k++] =(bin8_1 % 10 + bin8_2 % 10 + remainder) % 2;
remainder =(bin8_1 % 10 + bin8_2 % 10 + remainder) / 2;
bin8_1 = bin8_1 / 10;
bin8_2 = bin8_2 / 10;
}
if (remainder != 0)
sum[k++] = remainder;
--k;
printf("Sum of two binary numbers: ");
while (k >= 0)
printf("%d", sum[k--]);
break;
case 2: //SUBTRACTION VIA 2'S COMPLEMENT
break;
case 3: //MULTIPLICATION
break;
case 4: //DIVISION
break;
}
}
int askOperand1()
{
int ask1;
printf("\n\nEnter Operand 1(in decimal): ");
scanf("%d",&ask1);
if(ask1>255)
{
printf("\n\nINVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
getch();
exit(1);
}
return ask1;
}
int askOperand2()
{
int ask2;
printf("\nEnter Operand 2(in decimal): ");
scanf("%d",&ask2);
if(ask2>255)
{
printf("\n\nINVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
getch();
exit(1);
}
return ask2;
}
void arithmetic()
{
printf("\n\n");
printf("[1] ADDITION\n");
printf("[2] SUBTRACTION\n");
printf("[3] MULTIPLICATION\n");
printf("[4] DIVISION\n");
printf("\nOption: ");
}
输入必须仅为0-255的十进制数。然后它将转换为二进制。将添加两个二进制文件,然后打印出16位输出。我也不知道有关control_signals变量的任何信息,我不能问我的老师,因为他离开了1周。
答案 0 :(得分:1)
您将bin8_1
和bin8_2
声明为unsigned char bin8_#[]
这意味着它是unsigned char
。
然后您将此数组与int
进行比较。尝试使用简单的unsigned char
。