Hello everybody. I got this assignment. I've been trying to solve it for a week but to no avail. Please help me.
Quote:
Write a program using the List class (given to me) to implement a To-Do list. The items in the list should be string. The users should be prompted to enter a command (add an item, mark an item as done or partially done, delete an item, and print the list ) and data as neccessary. Simply storing items in the list is easy, but the List class doesn't directly support the recording of the status of each task. You can implement a struct or a class thta represents an item and its status, and modify the List class to work with this struct or class as its item type.
|
This is the header file of the List class
//******************************************************************
// SPECIFICATION FILE (list.h)
// This file gives the specification of a list abstract data type.
// The list components are not assumed to be in order by value.
// To save space, we omit from each function the precondition
// comments that document the assumptions made about valid input
// parameter data. These would be included in a program intended
// for actual use.
//******************************************************************
#include <string>
#include "status.h"
const int MAX_LENGTH =100; // Maximum possible number of
// components needed
using namespace std;
typedef string ItemType; // Type of each component
// (a simple type or string class)
class List
{
public:
bool IsEmpty() const;
// Postcondition:
// Return value == true, if list is empty
// == false, otherwise
bool IsFull() const;
// Postcondition:
// Return value == true, if list is full
// == false, otherwise
int Length() const;
// Postcondition:
// Return value == length of list
void Insert( /* in */ ItemType item);
// Precondition:
// NOT IsFull()
// Postcondition:
// item is in list
// && Length() == Length()@entry + 1
void Delete( /* in */ ItemType item );
// Precondition:
// NOT IsEmpty()
// Postcondition:
// IF item is in list at entry
// First occurrence of item is no longer in list
// && Length() == Length()@entry - 1
// ELSE
// List is unchanged
bool IsPresent( /* in */ ItemType item ) const;
// Postcondition:
// Return value == true, if item is in list
// == false, otherwise
void Reset();
// Postcondition:
// Iteration is initialized
ItemType GetNextItem();
// Precondition:
// Iteration has been initialized by call to Reset;
// No transformers have been invoked since last call
// Postcondition:
// Return value is the item at the current position
// in the list on entry;
// If last item has been returned, the next call will
// return the first item.
void SelSort();
// Postcondition:
// List components are in ascending order of value
List();
// Constructor
// Postcondition:
// Empty list is created
private:
int length;
int currentPos;
Status sts;
ItemType data[MAX_LENGTH];
};
This is the given implementation file of the List class
//******************************************************************
// IMPLEMENTATION FILE (list.cpp)
// This file implements the List class member functions
// List representation: a one-dimensional array and a length
// variable
//******************************************************************
#include "list.h"
#include <iostream>
#include <string>
using namespace std;
// Private members of class:
// int length; Length of the list
// ItemType data[MAX_LENGTH]; Array holding the list items
//******************************************************************
List::List()
// Constructor
// Postcondition:
// length == 0
{
length = 0;
}
//******************************************************************
bool List::IsEmpty() const
// Reports whether list is empty
// Postcondition:
// Function value == true, if length == 0
// == false, otherwise
{
return (length == 0);
}
//******************************************************************
bool List::IsFull() const
// Reports whether list is full
// Postcondition:
// Function value == true, if length == MAX_LENGTH
// == false, otherwise
{
return (length == MAX_LENGTH);
}
//******************************************************************
int List::Length() const
// Returns current length of list
// Postcondition:
// Function value == length
{
return length;
}
//******************************************************************
void List::Insert( /* in */ ItemType item)
// Inserts item into the list
// Precondition:
// length < MAX_LENGTH
// && item is assigned
// Postcondition:
// data[length@entry] == item
// && length == length@entry + 1
{
data[length] = item;
length++;
}
//******************************************************************
void List::Delete( /* in */ ItemType item )
// Deletes item from the list, if it is there
// Precondition:
// length > 0
// && item is assigned
// Postcondition:
// IF item is in data array at entry
// First occurrence of item is no longer in array
// && length == length@entry - 1
// ELSE
// length and data array are unchanged
{
int index = 0; // Index variable
while (index < length && item != data[index])
index++;
if (index < length)
{ // Remove item
data[index] = data[length-1];
length--;
}
}
//******************************************************************
bool List::IsPresent( /* in */ ItemType item ) const
// Searches the list for item, reporting whether it was found
// Precondition:
// item is assigned
// Postcondition:
// Function value == true, if item is in data[0..length-1]
// == false, otherwise
{
int index = 0; // Index variable
while (index < length && item != data[index])
index++;
return (index < length);
}
//******************************************************************
void List::Reset()
// Postcondition:
// Iteration is initialized
{
currentPos = 0;
}
//******************************************************************
ItemType List::GetNextItem()
// Precondition:
// Iteration has been initialized by call to Reset;
// No transformers have been invoked since last call
// Postcondition:
// Returns item at the currentPos@entry in the list and
// resets current to next position or first position if
// last item is returned
{
ItemType item;
item = data[currentPos];
if (currentPos == length - 1)
currentPos = 0;
else
currentPos++;
return item;
}
//******************************************************************
void List::SelSort()
// Sorts list into ascending order
// Postcondition:
// data array contains the same values as data@entry, rearranged
// into ascending order
{
ItemType temp; // Temporary variable
int passCount; // Loop control variable
int searchIndx; // Loop control variable
int minIndx; // Index of minimum so far
for (passCount = 0; passCount < length - 1; passCount++)
{
minIndx = passCount;
// Find the index of the smallest component
// in data[passCount..length-1]
for (searchIndx = passCount + 1; searchIndx < length;
searchIndx++)
if (data[searchIndx] < data[minIndx])
minIndx = searchIndx;
// Swap data[minIndx] and data[passCount]
temp = data[minIndx];
data[minIndx] = data[passCount];
data[passCount] = temp;
}
}
//******************************************************************
This is the new class that I've been trying to write, but I just don't know how to use them in the List class
Header file:
//*****************************************************
// THE SPECIFICATION FILE status.h
// This class is created to complement the list ADT
//******************************************************
#include <string>
using namespace std;
class Status
{
public:
Status();
// Postcondition:
// Class constance is constructed and its status is set to UNDONE
Status(char);
// Postcondition:
// The status of the item is constructed and set accordingly
string ShowStatus() const;
// Postcondition:
// The status of the item is shown
void ChangeStatus(char);
// Postcondition:
// The status of the item is set to DONE, PARTIAL, or UNDONE
private:
string statType; // could be DONE, PARTIAL, UNDONE
};
[/php]
Implementation file:
[php]
//*****************************************************
// THE IMPLEMENTATION FILE status.h
// This class is created to complement the list ADT
//*****************************************************
#include "status.h"
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
// Private members of the class
// string statType;
//******************************************************
Status::Status()
{
statType = "UNDONE";
}
//******************************************************
Status::Status(char c)
{
switch(toupper(c))
{
case 'D':
statType = "DONE";
break;
case 'U':
statType = "UNDONE";
break;
case 'P':
statType = "PARTIAL";
break;
}
}
//*******************************************************
string Status::ShowStatus() const
{
return statType;
}
//*******************************************************
void Status::ChangeStatus(char c)
{
switch(toupper(c))
{
case 'D':
statType = "DONE";
break;
case 'U':
statType = "UNDONE";
break;
case 'P':
statType = "PARTIAL";
break;
}
}
//*******************************************************
Please help. I've been working with this like crazy. Thanks.