int LinkedList::DoStuff()
{
Node *Current = next_;
while ( Current != NULL )
{
Current = Current->next_;
length_++;
}
// At the last iteration we have reached the end/tail/last node
return length_;
}
Run Code Online (Sandbox Code Playgroud)
除了最后一个节点之外没有其他节点.我怎样才能穿过前端的尾端?
看一下main函数,我们可以看到我已经将"星期一"硬编码到我的setDay公共函数中.使用c-string(就像我在setDay中所做的那样)很容易从用户那里获取一周中的某一天,但是我如何要求用户将n添加到设置的日期,"星期一"并提出"星期四"?这很难,因为typdef枚举{无效,星期一,星期二,星期三,星期四,星期五,星期六,星期日}不解释9是0和/或10为1.
#include <iostream>
using std::cout;
using std::endl;
class DayOfTheWeek //class is encapsulation of functions and members that manipulate the data.
{
public:
DayOfTheWeek(); // Constructor
virtual ~DayOfTheWeek(); // Destructor
void setDay(const char * day); // Function to set the day
void printDay() const; // Function to Print the day.
const char * getDay() const; // Function to get the day.
const char * plusOneDay(); // Next day function
const char * minusOneDay(); // Previous day function
const char * addDays(int …
Run Code Online (Sandbox Code Playgroud) 我在main.cpp中做的是读取文本文件两次:一次从每个路径名中删除'\',然后再次将文件的原始名称提供给我的实现文件中的SetPath().
// This a read file sub-routine called in the main function.
// Its purpose was intentionally set out to read the file, and pass information
// to functions to MyClassFile Implementational file.
// global varaible to have the file spec.
char MyClass::List_[ ARRY_SZ ] = {};
int main()
{
..
inFile.open( MyClass::List_, ios::in );
while ( inFile.peek() != '\n' )
{
inFile.get( ch );
if ( ch == 92 )
count++;
}
inFile.clear();
inFile.close();
inFile2.open( MyClass::List_, ios::in );
while …
Run Code Online (Sandbox Code Playgroud) 我有一份我差不多完成的家庭作业.
由于效率低下,我只是想知道如何在程序结束时防止崩溃.
quack::quack(int capacity) : backPtr( NULL ), frontPtr( NULL )
{
items = new item[capacity];
backPtr = new item;
frontPtr = new item;
midPtr = new item;
current = new item;
maxSize = capacity;
back = maxSize-1;
count = 0;
top = -1;
}
quack::~quack(void)
{
delete frontPtr;
delete backPtr;
delete current;
delete midPtr;
delete [] items; //Heap Corruption Debug Error at the end of program.
items = NULL;
maxSize = 0;
back = 0;
}
bool quack::pushFront(const int …
Run Code Online (Sandbox Code Playgroud) int hazmat::hashStr(char const * const str)
{
int count = 0;
for ( unsigned i = 0; i < strlen( str ); i++ )
{
count += str[i]; // get the ascii sum.
}
return count % maxSize;
}
Run Code Online (Sandbox Code Playgroud) 我已经尝试了一切.即使是java的论坛:
java.lang.String.hashCode():
s[0]*(31^(n-1)) + s[1]*(31^(n-2)) + ... + s[n-1]
Run Code Online (Sandbox Code Playgroud)
我把它解释为一个总和:虽然我不太清楚如何处理s [n-1];
int hashmap::hashstr( const char*const str )
{
int result = 0;
int i = 0;
int j = 0;
int k = 0;
unsigned n = 0;
for ( ; str[n] != NULL; ++n ); // how many characters?
while ( j != n ) // no more characters
{
result += str[i] * ( 31 ^ ( n - k ) );
j++;
i++;
k++;
}
return result …
Run Code Online (Sandbox Code Playgroud) 以下是股票来源的实施文件的片段:
stock::stock(char const * const symbol, char const * const name, int sharePrice, date priceDate )
: m_sharePrice( sharePrice )
{
if ( symbol )
{
size_t length = strlen( symbol ) +1;
m_symbol = new char[length];
strcpy_s( m_symbol, length, symbol );
}
else m_symbol = NULL;
if ( name )
{
size_t length = strlen( name ) +1;
m_name = new char[length];
strcpy_s( m_name, length, name );
}
else m_name = NULL;
dateObj = &priceDate;
}
Run Code Online (Sandbox Code Playgroud)
这就是我用cstrings分配内存的方法.在主要的情况下,参数传递的方式类似于"符号",名称",10,date :: JANUARY,19,1677.日期是一个对象,其中的月份是enumaterted.这是最后一个参数"日期"类型是什么iim当我从一个源文件移动到下一个源文件时,我无法保留属性.我看到在上面的函数中,"dateObj"具有我需要的属性.但是当我将它转移到另一个源文件时价值观已经消失...... …
我只是得到垃圾值.并且调试器显示正确的值是很奇怪的.但它印刷的怪异东西......
这个第一部分很好.从本质上讲,它只是把我带到我的问题.我有我需要在h.hashtable [hashIndex]数组中打印的内容.
ostream& operator<<(ostream& out, const hashmap& h)
{
const char *getSymbol = NULL;
for ( int hashIndex = 0; hashIndex < maxSize; hashIndex++ )
{
getSymbol = h.hashTable[hashIndex].getSymbol();
if ( getSymbol ) // Find the one I added.
{
h.hashTable->display(out);
return out << h.hashTable[hashIndex];
}
}
return out;
}
Run Code Online (Sandbox Code Playgroud) 你如何从基于数组的哈希表中删除?我需要准备从表中删除几个符号.如果我在固定大小的字符数组中转储我要删除的内容,那么如何找到"可能"删除的内容?
bool hashmap::get(char const * const symbol, stock& s) const
{
int hashVal = this->hashStr( symbol );
int initialHash = -1;
while ( hashTable[hashVal].m_symbol != NULL )
{ // try to find a match for the stock associated with the symbol.
if ( initialHash == -1 )
{
initialHash = hashVal;
}
if ( strcmp( hashTable[hashVal].m_symbol, symbol ) == 0 )
{
s = &hashTable[hashVal];
return true;
}
++hashVal %= maxSize;
}
if ( hashTable[hashVal].m_symbol == NULL || hashVal == …
Run Code Online (Sandbox Code Playgroud) 假设你有一个容量为5的数组,并且假设你有一个计数变量,它计算添加到数组中的每个条目.你怎么会真正的数组呢?使用C++语法?
void BST::reallocate()
{
item *new_array = new item[size*2];
for ( int array_index = 0; array_index < size * 2; array_index++ )
{
if ( ! items[array_index].empty )
{
new_array[array_index].theData = items[array_index].theData;
new_array[array_index].empty = false;
}
}
maxSize += size;
delete [] items;
items = NULL;
items = new_array;
}
Run Code Online (Sandbox Code Playgroud)
你如何重新分配数组?BST ctor下面是私有项结构,只是为了消除任何混淆.
BST::BST(int capacity) : items(new item[capacity]), Position(0),
leftChild(0), rightChild(0), maxSize(capacity)
{
}
Run Code Online (Sandbox Code Playgroud)
这是在BST标题中:
private:
int size;
int maxSize;
int Position;
int leftChild;
int rightChild;
struct item
{
bool empty; …
Run Code Online (Sandbox Code Playgroud)