- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为家庭作业创建一个二进制链表,它只存储 1 位的度数。我可以获得最高阶数,在二进制列表中的任何位置设置位,并返回在特定阶数出现的位,但出于某种原因,我在创建复制构造函数和赋值 (=) 运算符时遇到了最大的麻烦。这是我的代码:
// copy constructor
// creates a new linked list where the contents are a deep copy of the provided list
Binary::Binary(const Binary &b)
{
Binary clone;
for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next)
{
clone.set_bit(1, current_other->degree);
}
}
// assignment operator
// sets the current link list to be a deep copy of the provided list.
// make sure to check if assigning to itself, and make sure to free old memory
// before making the copy.
Binary& Binary::operator=(const Binary &other)
{
Binary clone;
for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next)
{
clone.set_bit(1, current_other->degree);
}
return clone;
}
我的逻辑有问题吗?有人请帮忙!
附言我已经多次测试了我的 set_bit(b,d) 和其他方法,我知道只有这些方法搞砸了,因为当我尝试“Binary b3(b2)”或“Binary b3 = b2”时,程序会停止点并说“分配 1.exe 中 0x00DC4B18 处的未处理异常:0xC0000005:访问冲突读取位置 0xCCCCCCD0”。
编辑:我也有一个默认构造函数:Binary() {firstTerm = nullptr;}
编辑编辑:
输出:
TESTING DEFAULT CONSTRUCTOR
The binary number b1 is empty.
TESTING GET AND SET METHODS
The highest bit of binary number b1 is 5.
The bit of binary number b1 at degree 5 is 1.
The bit of binary number b1 at degree 2 is 0.
The bit of binary number b1 at degree 1 is 0.
TESTING PARAMETER CONSTRUCTOR
The bit of binary number b1 at degree 2 is 1.
The bit of binary number b1 at degree 0 is 1.
The bit of binary number b1 at degree 1 is 0.
TESTING COPY CONSTRUCTOR
B2 = 101
B3 = _
Unhandled exception at 0x00C04B18 in Assignment 1.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.
测试代码:
#include <iostream>
#include "binary.h"
using namespace std;
int main (void)
{
// test default constructor
Binary b1;
cout << "TESTING DEFAULT CONSTRUCTOR" << endl;
if (b1.get_degree() == -1)
cout << "\tThe binary number b1 is empty." << endl;
else
cout << "\tThe binary number b1 is NOT empty. (INCORRECT)" << endl;
// test get_bit, set_bit, and get_degree
cout << "\nTESTING GET AND SET METHODS" << endl;
b1.set_bit(1, 2);
b1.set_bit(1, 5);
b1.set_bit(1, 0);
b1.set_bit(0, 2);
if (b1.get_degree() == 5)
cout << "\tThe highest bit of binary number b1 is 5." << endl;
else
cout << "\tThe highest bit of binary number b1 is NOT 5. (INCORRECT)" << endl;
if (b1.get_bit(5) == 1)
cout << "\tThe bit of binary number b1 at degree 5 is 1." << endl;
else
cout << "\tThe bit of binary number b1 at degree 5 is 0. (INCORRECT)" << endl;
if (b1.get_bit(2) == 0)
cout << "\tThe bit of binary number b1 at degree 2 is 0." << endl;
else
cout << "\tThe bit of binary number b1 at degree 2 is 1. (INCORRECT)" << endl;
if (b1.get_bit(1) == 0)
cout << "\tThe bit of binary number b1 at degree 1 is 0." << endl;
else
cout << "\tThe bit of binary number b1 at degree 1 is 1. (INCORRECT)" << endl;
// test parameter constructor
cout << "\nTESTING PARAMETER CONSTRUCTOR" << endl;
Binary b2(5);
if (b2.get_bit(2) == 1)
cout << "\tThe bit of binary number b2 at degree 2 is 1." << endl;
else
cout << "\tThe bit of binary number b2 at degree 2 is 0. (INCORRECT)" << endl;
if (b2.get_bit(0) == 1)
cout << "\tThe bit of binary number b2 at degree 0 is 1." << endl;
else
cout << "\tThe bit of binary number b2 at degree 0 is 0. (INCORRECT)" << endl;
if (b2.get_bit(1) == 0)
cout << "\tThe bit of binary number b2 at degree 1 is 0." << endl;
else
cout << "\tThe bit of binary number b2 at degree 1 is 1. (INCORRECT)" << endl;
// test copy constructor
cout << "\nTESTING COPY CONSTRUCTOR" << endl;
cout << "B2= " << b2 << endl;
b2.set_bit(1,1);
Binary b3(b2);
cout << "B3= " << b3 << endl;
b2.set_bit(1, 1);
cout << "B2= " << b2 << endl;
cout << "B3= " << b3 << endl;
if (b3.get_bit(2) == 1)
cout << "\tThe bit of binary number b3 at degree 2 is 1." << endl;
else
cout << "\tThe bit of binary number b3 at degree 2 is 0. (INCORRECT)" << endl;
if (b3.get_bit(0) == 1)
cout << "\tThe bit of binary number b3 at degree 0 is 1." << endl;
else
cout << "\tThe bit of binary number b3 at degree 0 is 0. (INCORRECT)" << endl;
if (b3.get_bit(1) == 0)
cout << "\tThe bit of binary number b3 at degree 1 is 0." << endl;
else
cout << "\tThe bit of binary number b3 at degree 1 is 1. (INCORRECT)" << endl;
// test assignment operator
cout << "\nTESTING ASSIGNMENT OPERATOR" << endl;
b2 = b3;
b3.set_bit(1, 1);
if (b2.get_bit(2) == 1)
cout << "\tThe bit of binary number b2 at degree 2 is 1." << endl;
else
cout << "\tThe bit of binary number b2 at degree 2 is 0. (INCORRECT)" << endl;
if (b2.get_bit(0) == 1)
cout << "\tThe bit of binary number b2 at degree 0 is 1." << endl;
else
cout << "\tThe bit of binary number b2 at degree 0 is 0. (INCORRECT)" << endl;
if (b2.get_bit(1) == 0)
cout << "\tThe bit of binary number b2 at degree 1 is 0." << endl;
else
cout << "\tThe bit of binary number b2 at degree 1 is 1. (INCORRECT)" << endl;
// test convert
cout << "\nTESTING CONVERT METHOD" << endl;
if (b1.convert() == 33)
cout << "\tThe decimal value of binary number b1 is 33." << endl;
else
cout << "\tThe decimal value of binary number b1 is NOT 33. (INCORRECT)" << endl;
// test output operator
cout << "\nTESTING OUTPUT OPERATOR" << endl;
cout << "\tThe binary number b1 is " << b1 << endl;
cout << "\tThe number b1 should be 100001" << endl;
// test addition
cout << "\nTESTING ADDITION OPERATOR" << endl;
Binary b4 = b2 + b3;
if (b4.convert() == 12)
cout << "\t101 + 111 = 1100." << endl;
else
cout << "\t101 + 111 != 1100. (INCORRECT)" << endl;
// test subtraction
cout << "\nTESTING SUBTRACTION OPERATOR" << endl;
Binary b5(b1 - b2);
if (b5.convert() == 28)
cout << "\t100001 - 101 = 11100." << endl;
else
cout << "\t100001 - 101 != 11100. (INCORRECT)" << endl;
// test multiplication
cout << "\nTESTING MULTIPLICATION OPERATOR" << endl;
Binary b6 = b3 * b2;
if (b6.convert() == 35)
cout << "\t111 * 101 = 100011." << endl;
else
cout << "\t111 * 101 != 100011. (INCORRECT)" << endl;
system("pause");
}
二进制.h:
#ifndef _BINARY_H_
#define _BINARY_H_
#include <iostream>
class Binary {
private:
struct BinaryNode {
int degree;
BinaryNode* next;
BinaryNode(int d, BinaryNode* n): degree(d),next(n) {}
};
BinaryNode *firstTerm;
public:
// default constructor
Binary() {firstTerm = nullptr;}
// constructor
// takes a value representing a decimal number and creates
// the binary linked list representation of it.
Binary(int x);
// sets the term with degree d and bit b
// notice a node is created if bit is 1 AND a node
// for that degree doesn't exist, or the node is removed
// if the bit is 0 AND the node with that degree already exists
void set_bit(int b, int d);
// returns one if a term with degree d exists, zero otherwise
int get_bit(int d) const;
// returns the decimal integer representation of the binary number.
int convert() const ;
// returns the highest degree of any term in the binary number
// returns -1 if the the list is empty.
int get_degree() const;
// destructor
// make sure that all memory is returned (freed up) correctly
~Binary();
// copy constructor
// creates a new linked list where the contents are a deep copy of the provided list
Binary(const Binary &b);
// assignment operator
// sets the current link list to be a deep copy of the provided list.
// make sure to check if assigning to itself, and make sure to free old memory
// before making the copy.
Binary& operator=(const Binary &other);
// prints the binary number to the output stream o
// please use like: 10001101
// terms must be printed in descending order of degree
friend std::ostream& operator<<(std::ostream &o, const Binary &b);
// returns a new binary number representing the addition of 2 provided binary numbers.
// do NOT simply convert the numbers to decimal using convert(),add them,
// then convert back to binary.
friend Binary operator+(const Binary &b1, const Binary &b2);
// returns a new binary number representing the subtraction
// of 2 provided binary numbers. can assume b1 will always be
// larger than b2.
// do NOT simply convert the numbers to decimal using convert(),subtract them,
// then convert back to binary.
friend Binary operator-(const Binary &b1, const Binary &b2);
// returns a new binary number representing the multiplication
// of 2 provided binary numbers.
// do NOT simply convert the numbers to decimal using convert(),multiply them,
// then convert back to binary.
friend Binary operator*(const Binary &b1, const Binary &b2);
};
std::ostream& operator<<(std::ostream &o, const Binary &b);
Binary operator+(const Binary &b1, const Binary &b2);
Binary operator-(const Binary &b1, const Binary &b2);
Binary operator*(const Binary &b1, const Binary &b2);
#endif
二进制.cpp:
#include "binary.h"
using namespace std;
// constructor
// takes a value representing a decimal number and creates
// the binary linked list representation of it.
Binary::Binary(int x)
{
firstTerm = nullptr;
int deg = 0;
int n = x;
while (n != 0)
{
set_bit(n%2, deg);
n = n/2;
++deg;
}
}
// sets the term with degree d and bit b
// notice a node is created if bit is 1 AND a node
// for that degree doesn't exist, or the node is removed
// if the bit is 0 AND the node with that degree already exists
void Binary::set_bit(int b, int d)
{
if (b == 1)
{
if (firstTerm == nullptr || d == 0)
{
firstTerm = new BinaryNode(d, firstTerm);
}
else
{
BinaryNode *current, *prev = firstTerm;
for(current = firstTerm; current != nullptr; current = current->next)
{
if (current->next == nullptr)
{
current->next = new BinaryNode(d, nullptr);
break;
}
else if (current->degree == d)
{
prev->next = new BinaryNode (d, current->next);
delete current;
break;
}
else if(current->degree > d)
{
prev->next = new BinaryNode (d, current);
break;
}
prev = current;
}
}
}
else
{
BinaryNode *current, *prev = firstTerm;
for(current = firstTerm; current != nullptr; current = current->next)
{
if (current->degree == d)
{
prev->next = current->next;
delete current;
break;
}
prev = current;
}
}
}
// returns one if a term with degree d exists, zero otherwise
int Binary::get_bit(int d) const
{
for (BinaryNode *current = firstTerm; current != nullptr; current = current->next)
{
if (current == nullptr)
break;
if (current->degree == d)
return 1;
}
return 0;
}
// returns the decimal integer representation of the binary number.
int Binary::convert() const
{
int sum = 0;
for (BinaryNode* current = firstTerm; current != nullptr; current = current->next)
{
sum = sum + (int)pow(2,current->degree);
}
return sum;
}
// returns the highest degree of any term in the binary number
// returns -1 if the the list is empty.
int Binary::get_degree() const
{
if (firstTerm == nullptr)
{return -1;}
else
{
BinaryNode *current;
for (current = firstTerm; current->next != nullptr; current = current->next);
return current->degree;
}
}
// destructor
// make sure that all memory is returned (freed up) correctly
Binary::~Binary()
{
BinaryNode* tmp;
for(BinaryNode* current = firstTerm; current != nullptr; current = tmp)
{
tmp = current->next;
delete current;
}
}
// copy constructor
// creates a new linked list where the contents are a deep copy of the provided list
Binary::Binary(const Binary &b)
{
for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next)
{
set_bit(1, current_other->degree);
}
}
// assignment operator
// sets the current link list to be a deep copy of the provided list.
// make sure to check if assigning to itself, and make sure to free old memory
// before making the copy.
Binary& Binary::operator=(const Binary &other)
{
Binary clone;
for(BinaryNode* current_other = other.firstTerm; current_other != nullptr; current_other = current_other->next)
{
clone.set_bit(1, current_other->degree);
}
return clone;
}
// prints the binary number to the output stream o
// please use like: 10001101
// terms must be printed in descending order of degree
std::ostream& operator<<(std::ostream &o, const Binary &b)
{
for(int i = b.get_degree(); i >= 0; --i)
{
o << b.get_bit(i);
}
return o;
}
// returns a new binary number representing the addition of 2 provided binary numbers.
// do NOT simply convert the numbers to decimal using convert(),add them,
// then convert back to binary.
Binary operator+(const Binary &b1, const Binary &b2)
{
int l = b1.get_degree();
if (b1.get_degree() < b2.get_degree())
{
l = b2.get_degree();
}
int i, c = 0;
Binary sum;
for (i = 0; i <= l; ++i)
{
sum.set_bit(((b1.get_bit(i) ^ b2.get_bit(i)) ^ c), i); //get sum (A XOR B XOR C)
c = ((b1.get_bit(i) & b2.get_bit(i)) | (b1.get_bit(i) &c)) | (b2.get_bit(i) & c); //get carry bit (AB + BC + CA)
}
sum.set_bit(c, i);
return sum;
}
// returns a new binary number representing the subtraction
// of 2 provided binary numbers. can assume b1 will always be
// larger than b2.
// do NOT simply convert the numbers to decimal using convert(),subtract them,
// then convert back to binary.
Binary operator-(const Binary &b1, const Binary &b2)
{
Binary one = Binary(1);
Binary inv, two, result, fresult;
int i, l = b2.get_degree() + 1;
for(i = 0; i <= l; ++i)
{
if (b2.get_bit(i) == 1)
inv.set_bit(0,i);
else
inv.set_bit(1,i);
}
two = inv + one;
result = two + b1;
if (b1.get_degree() > l)
{
l = b1.get_degree();
}
for (l; l >= 0; l--)
{
fresult.set_bit(result.get_bit(l), l);
}
return (fresult);
}
// returns a new binary number representing the multiplication
// of 2 provided binary numbers.
// do NOT simply convert the numbers to decimal using convert(),multiply them,
// then convert back to binary.
Binary operator*(const Binary &b1, const Binary &b2)
{
Binary prod = b1;
for (int i = 1; i < b2.convert(); ++i)
{
prod = prod + b1;
}
return prod;
}
最佳答案
Binary::Binary(const Binary &b)
{
Binary clone;
for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next)
{
clone.set_bit(1, current_other->degree);
}
}
您创建一个名为clone
的对象,设置它的位,然后将其丢弃。这似乎不对。也许你的意思是:
Binary::Binary(const Binary &b)
{
for(BinaryNode* current_other = b.firstTerm; current_other != nullptr; current_other = current_other->next)
{
set_bit(1, current_other->degree);
}
}
关于c++ - 复制构造函数和赋值运算符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15301329/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!