C 二进制加法器在大多数情况下只能工作
我目前正在研究一个二进制加法器 寄存器A和B是输入寄存器。它们存储为双链接列表。寄存器S用于输出。(适用于真值表) 以下是提供的真值表: A | B | CarryIn | S | CarryOut 0 | 0 | 0 | 0 | 0 0 | 0 | 1 | 1 | 0 0 | 1 | 0 | 1 | 0 0 | 1 | 1 | 0 | 1 1 | 0 | 0 | 1 | 0 1 | 0 | 1 | 0 | 1 1 | 1 | 0 | 0 | 1 1 | 1 | 1 | 1 | 1 以下是包含指向它们的指针以及其他数据的结构(CPU):C 二进制加法器在大多数情况下只能工作,c,binary,add,C,Binary,Add,我目前正在研究一个二进制加法器 寄存器A和B是输入寄存器。它们存储为双链接列表。寄存器S用于输出。(适用于真值表) 以下是提供的真值表: A | B | CarryIn | S | CarryOut 0 | 0 | 0 | 0 | 0 0 | 0 | 1 | 1 | 0 0 | 1 | 0 | 1 | 0 0 | 1 | 1 | 0 | 1 1 | 0 | 0 | 1 | 0 1 | 0 | 1 | 0 | 1 1 | 1 | 0 | 0 | 1 1 | 1 | 1 | 1 | 1 以下是包含指
struct cpu_t
{
int word_size;
int unsign;
int overflow;
int carry;
int sign;
int parity;
int zero;
struct bit_t *r1_head;
struct bit_t *r1_tail;
struct bit_t *r2_head;
struct bit_t *r2_tail;
struct bit_t *r3_head;
struct bit_t *r3_tail;
};
这是我的add函数:
void add_function(struct cpu_t *cpu)
{
int i = 0;
struct bit_t *temp1 = cpu->r1_tail;
struct bit_t *temp2 = cpu->r2_tail;
struct bit_t *temp3 = cpu->r3_tail;
while(i < (cpu->word_size))
{
if(temp1->n == 0 && temp2->n == 0 && cpu->carry == 0)
{
temp3->n = 0;
cpu->carry = 0;
}
else if(temp1->n == 0 && temp2->n == 0 && cpu->carry == 1)
{
temp3->n = 1;
cpu->carry = 0;
}
else if(temp1->n == 0 && temp2->n == 1 && cpu->carry == 0)
{
temp3->n = 1;
cpu->carry = 0;
}
else if(temp1->n == 0 && temp2->n == 1 && cpu->carry == 1)
{
temp3->n = 0;
cpu->carry = 1;
}
else if(temp1->n == 1 && temp2->n == 0 && cpu->carry == 0)
{
temp3->n = 1;
cpu->carry = 0;
}
else if(temp1->n == 1 && temp2->n == 0 && cpu->carry == 1)
{
temp3->n = 0;
cpu->carry = 1;
}
else if(temp1->n == 1 && temp2->n == 1 && cpu->carry == 0)
{
temp3->n = 0;
cpu->carry = 1;
}
else if(temp1->n == 1 && temp2->n == 1 && cpu->carry == 1)
{
temp3->n = 1;
cpu->carry = 1;
}
temp1 = temp1->prev;
temp2 = temp2->prev;
temp3 = temp3->prev;
i++;
}
}
void add_函数(结构cpu_t*cpu)
{
int i=0;
结构位\u t*temp1=cpu->r1\u tail;
结构位\u t*temp2=cpu->r2\u尾;
结构位*temp3=cpu->r3\u尾;
而(i<(cpu->字大小))
{
如果(temp1->n==0&&temp2->n==0&&cpu->carry==0)
{
temp3->n=0;
cpu->进位=0;
}
else if(temp1->n==0&&temp2->n==0&&cpu->carry==1)
{
temp3->n=1;
cpu->进位=0;
}
else if(temp1->n==0&&temp2->n==1&&cpu->carry==0)
{
temp3->n=1;
cpu->进位=0;
}
else if(temp1->n==0&&temp2->n==1&&cpu->carry==1)
{
temp3->n=0;
cpu->进位=1;
}
else if(temp1->n==1&&temp2->n==0&&cpu->carry==0)
{
temp3->n=1;
cpu->进位=0;
}
else if(temp1->n==1&&temp2->n==0&&cpu->carry==1)
{
temp3->n=0;
cpu->进位=1;
}
else if(temp1->n==1&&temp2->n==1&&cpu->carry==0)
{
temp3->n=0;
cpu->进位=1;
}
else if(temp1->n==1&&temp2->n==1&&cpu->carry==1)
{
temp3->n=1;
cpu->进位=1;
}
temp1=temp1->prev;
temp2=temp2->prev;
temp3=temp3->prev;
i++;
}
}
以下是一些示例输出,以显示我遇到的问题类型:
字号大小为2:
01+01=10(正确)
字号大小为4:
0111+0001=1001(错误)
字号大小为8:
10101010+01010101=11111111(正确)
11101101+01101000=01010110(不正确,实际为01010101)
字号大小为4:
1001+0110=1111(正确)
1111+1111=1110(正确)
那么,根据这个输入,有人知道我的代码为什么不能工作吗?有什么想法吗
如果您需要我编辑更多的代码,我想我可以很容易地做到这一点。如果没有,我会通过评论告诉你你需要什么
谢谢你能给我的帮助 OP的测试用例期望
cpu->carry
在执行void add\u函数(struct cpu\u t*cpu)
时有一个0
看起来OP应该有2个add函数并相应地使用
void addc_function(struct cpu_t *cpu) {
// existing code
}
void add_function(struct cpu_t *cpu) {
cpu->carry = 0;
addc_function(cpu);
}
OP的测试用例期望
cpu->carry
在执行void add\u函数(struct cpu\u t*cpu)
时有一个0
看起来OP应该有2个add函数并相应地使用
void addc_function(struct cpu_t *cpu) {
// existing code
}
void add_function(struct cpu_t *cpu) {
cpu->carry = 0;
addc_function(cpu);
}
我想说所有的例子都是正确的。0111+0001应等于1000。我的计算器说是1001。这是怎么回事?但在问题中它的读数是
0111+0001=1000(错误)
。当code确实0111+0001
时,cpu->carry
是否先设置为0?代码可能与上一次计算的值相差1。很抱歉,Eugene。我很难在我得到的答案和我在网上得到的答案之间取得平衡。我搞混了一对。我会解决这个问题的。我会说所有的例子都是正确的。编辑:不。0111+0001应等于1000。我的计算器说是1001。这是怎么回事?但在问题中它的读数是0111+0001=1000(错误)
。当code确实0111+0001
时,cpu->carry
是否先设置为0?代码可能与上一次计算的值相差1。很抱歉,Eugene。我很难在我得到的答案和我在网上得到的答案之间取得平衡。我搞混了一对。我会解决的。