有点难过接下来要做什么。我必须创建一个名为IntSet的类,它使用以下数据成员表示一组数学整数:
指向一个指向动态分配的数组的指针,该数组保存当前在IntSet中的值
包含数组当前大小的int(只要add()方法创建更大的数组,就需要更新 **
一个int,它保存IntSet中当前值的数量(需要在add()和remove()方法中更新。
除了最后一部分之外,大部分程序都是完整的,这要求我将两组整数组合在一起并使它们相互作用。我必须使用以下内容:
removeDifferent():setA.removeDifferent(setB)应该从setA中删除任何不在setB中的值。
removeSame():setA.removeSame(setB)应该从setA中删除同样在setB中的任何值。
部首:
#include <iostream>
class IntSet{
public:
void print(); //prints IntSet
IntSet(); //Constructor
~IntSet(); //Destructor
int size(); //returns number of values currently in IntSet
bool isEmpty(); //returns true if IntSet contains no integers. False otherwise
bool contains(int i) const; //returns true if a value is in IntSet. False otherwise.
void add(int i); //adds a value to IntSet. If current array is full, allocate a new array that is twice as big.
void remove(int i); //removes value from IntSet by shifting over all of the subsequent values in the array.
void addAll()
void removeDifferent()
void removeSame()
//----helper methods----
int getIndex(int integer); //returns the integer on an index
private:
int* ptr; //points to a dynamically allocated array that holds the values currently in IntSet
int sizeOfArray; //holds current size of Array
int numberOfValues; //holds number of values currently in IntSet
};
主要:
#include "IntSet.hpp"
#include <iostream>
//prints IntSet
void IntSet::print(){
for (int i = 0; i < numberOfValues; i++){
std::cout << ptr[i] << " ";
}
std::cout << std::endl;
}
//delcaring variables
IntSet::IntSet(){
numberOfValues = 0;
sizeOfArray = 10;
ptr = new int[10];
}
IntSet::~IntSet(){
delete[] ptr;
}
//Returning the number of values in the IntSet
int IntSet::size(){
return numberOfValues;
}
//Determining whether the stack is empty
bool IntSet::isEmpty(){
if(numberOfValues == 0){
return true;
}
return false;
}
//defining contains() function
bool IntSet::contains(int i) const
{
for (int k = 0; k < numberOfValues; k++){
if (ptr[k] == i){
return true;
}
}
return false;
}
//defining add() function
void IntSet::add(int i){
if (contains(i)){
return;
}
if (numberOfValues == sizeOfArray)
{
sizeOfArray = sizeOfArray * 2; //doubling size of arrayCapacity
int* temp = new int[sizeOfArray]; //allocating new one that's twice as large
for (int i = 0; i < numberOfValues; i++)
{
temp[i] = ptr[i]; //copying old stuff into new one
}
delete[] ptr; //deallocate old array
ptr = temp; //set ptr to new array
}
ptr[numberOfValues] = i;
numberOfValues++;
}
void IntSet::remove(int i){
if(!contains(i)){
return;
}
bool bIntRemoved = false;
for(int k=0; k < numberOfValues; k++){
// check if we are currently searching or shifting
if(!bIntRemoved){
if(ptr[k] == i){
// found the int to remove
bIntRemoved = true;
}
}else if(k < numberOfValues-1){
ptr[k] = ptr[k+1];
} // else, we are at the last index and we have nothing to shift
}
numberOfValues--;
}
int IntSet::getIndex(int index){
return ptr[index];
}
void IntSet::addAll(IntSet b){
for(int i = 0; i < b.size(); i++){
add(b.getIndex(i));
}
}
如何合并我的add()和remove()函数来组合两组整数并让它们遵循这些规则?我甚至不知道如何开始写它们:(
谢谢大家!
编辑:addAll()已完成,但其他两个仍未点击
答案 0 :(得分:0)
嗯,您已经实现了您的包含方法并假设您已经正确完成,您应该在实现其他功能时使用该方法。
removeDifferent(IntSet setB){
for(int i = 0; i < numberOfValues; i++){
if(!setB.contains(ptr[i])){
remove(ptr[i]);
i--; //make sure not to skip any elements by moving back one
}
}
} //end of removeDifferent
removeSame(IntSet setB){
for(int i = 0; i < numberOfValues; i++){
if(setB.contains(ptr[i])){
remove(ptr[i]);
i--; //make sure not to skip any elements by moving back one
}
}
} //end of removeSame
至于你的add all函数,我不认为有一个很好的方法来添加另一个列表的元素,因为int * ptr是私有的,并且没有返回任何元素的方法。从理论上讲,您可以扫描每个可能的整数值,但这绝对不是理想的,可能不是预期的解决方案。你确定这里的所有方法都是正确的吗?
#include <climits> //need to include that for this one
void addAll(IntSet setB){
for(int i = INT_MIN; i < INT_MAX && !setB.isEmpty(); i++){
int removals = 0;
while(setB.contains(i)){ //temporarily remove all i's in setB
removals++;
add(i);
setB.remove(i);
}
while(removals > 0){ //add back all i's that we previously removed
setB.add(i);
removals--;
}
}
}
我还没有检查过这些编译,但这绝对是这些功能的基本思路。
祝你好运