Здравствуйте,подскажите, пожалуйста.
Я не могу найти ошибку в функции DataType getCursor()
, она возвращает неправильное значение. Подскажите, пожалуйста,что я не так делаю?
После дебаггинга понял , что переменная cursor
имеет правильное значение, а сам dataItems[cursor];
возвращает мусор.
И сама сумма также неверна.
listADT.h
const int defMaxListSize = 10;
typedef int DataType;
class ListADT
{
private:
//Data members
int maxSize=11;
int size=10;
int cursor=0;
DataType *dataItems=NULL;
public:
//Constructor
ListADT(int maxNumber = defMaxListSize);
//Destructor
~ListADT();
//List manipulation operations
void insert(const DataType &newDataItem); //Insert after cursor
void remove(); //Remove Data item
//void replace(const DataType &newDataItem); //Replace data item
//List status operations
bool isEmpty()const; //List is empty
bool isFull() const; //List is full
//List iteration operations
void gotoBeginning() ; //Go to beginning
void gotoEnd(); //Go to end
bool gotoNext(); //Go to next data item
bool gotoPrior(); //Go to prior data item
DataType getCursor(); //Return data item
//Output the list structure -used in testinf/debugging
//void showStructure()const;
};
listADT.cpp
#include "ListADT.h"
ListADT::ListADT(int maxNumber)
{
cursor = 0;
dataItems = new DataType[maxNumber];
//dataItems = { 0 };
}
ListADT::~ListADT()
{
delete [] dataItems;
}
void ListADT::insert(const DataType & newDataItem)
{
if (size + 1 <= maxSize)
{
dataItems[cursor] = newDataItem;
cursor++;
}
}
void ListADT::remove()
{
if (size - 1 >= 0)
cursor--;
}
bool ListADT::isEmpty() const
{
if (size <= 0)
return true;
else
return false;
}
bool ListADT::isFull() const
{
if (size == defMaxListSize)
return true;
else
return false;
}
void ListADT::gotoBeginning()
{
cursor = 0;
}
void ListADT::gotoEnd()
{
cursor = size;
}
bool ListADT::gotoNext()
{
if (cursor < size)
{
cursor++;
return true;
}
return false;
}
bool ListADT::gotoPrior()
{
if (!isEmpty() && cursor != 1)
{
cursor--;
return true;
}
else return false;
}
DataType ListADT::getCursor()
{
if (!isEmpty())
{
std::cout << "C: " << cursor << std::endl; //правильный результат
return dataItems[cursor]; // возвращает мусор
}
else
{
std::cout << "Empty list" << std::endl;
}
}
source.cpp
#include <iostream>
#include "ListADT.h"
void main()
{
ListADT samples;
int newSample=0;
int total = 0;
//Read in a set of samples from the keyboard.
std::cout << "Enter list of samples (end with eof): ";
for (int i = 0; i < 10; i++)
{
std::cin >> newSample;
samples.insert(newSample);
}
//Sum the samples and output the result.
if (!samples.isEmpty())
{
samples.gotoBeginning();
do {
std::cout << "CUR " << samples.getCursor() << std::endl;
total += samples.getCursor();
}
while (samples.gotoNext());
}
std::cout << "Sum is " << total << std::endl;
system("pause");
}