GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jun-2006, 12:28
vnca_1 vnca_1 is offline
New Member
 
Join Date: Jun 2006
Posts: 15
vnca_1 is on a distinguished road

C++ class -- Please help


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
CPP / C++ / C Code:
//******************************************************************
// 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
CPP / C++ / C Code:
//******************************************************************
// 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:
CPP / C++ / C Code:
//*****************************************************
//     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.
Last edited by cable_guy_67 : 13-Jun-2006 at 13:29. Reason: Changed PHP code tags to C++ code tags
  #2  
Old 13-Jun-2006, 13:33
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: C++ class -- Please help


First of all, to get correct syntax highlighting for C/C++, use the [ c++ ] tags instead of the [ php ] tags.

On to your question. I think you need to change the typedef string ItemType; line in the List header file to typedef your Status class as the List's type. (NOTE: because of the List class's internal processing, you will need to add an operator!= (not equal to) overload in your Status class, plus an operator= (assignment) and an operator< (less than). The standard STL string class already does this, which is why it compiles in the List class without errors.)
After you make the List use the Status class, you'll need to build a program interface around the List to change the status of the item etc as per the project description.
NOTE #2: To prevent perplexing errors later, I would change the List's GetNextItem() function to return an ItemType& (by reference, that is) instead of an ItemType by value, because if you GetNextItem by value (in other words, copy the item from the List's data array) and then change the status, the item that the List knows about will not change. So you need to keep a reference directly to the List's data array in order to modify it.
  #3  
Old 14-Jun-2006, 03:29
vnca_1 vnca_1 is offline
New Member
 
Join Date: Jun 2006
Posts: 15
vnca_1 is on a distinguished road

Re: C++ class -- Please help


Thank you very much. A friend helped me to modify the program that way, and it works now.

I'm just an amateur in C++, and I really want to be really good at it. Do you suggest me anything?

Thanks again.

BB.
  #4  
Old 14-Jun-2006, 12:31
Gamer_2k4's Avatar
Gamer_2k4 Gamer_2k4 is offline
Member
 
Join Date: Apr 2005
Location: Wisconsin
Posts: 117
Gamer_2k4 will become famous soon enough

Re: C++ class -- Please help


Read lots of programming books. Use online tutorials. Take programming classes. Write a lot of code. The last one was the most useful for me. I'm trying to write a game right now, and every time I need to figure out how to do something new, I online, take a tutorial, and learn it. The only book I use is "Learn C++ in 21 Days" by Jesse Liberty. I think it's an excellent guide and reference.

Gamer_2k4
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
Opinion on my code and a c++ class question FlipNode C++ Forum 7 07-Feb-2006 08:15
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.