Code:
ItemType.h
#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
const int MAX_ITEMS = 10;
enum RelationType {LESS, EQUAL, GREATER};
class ItemType
{
public :
ItemType();
// PURPOSE: To set each variable with a default value
// INPUT: none
// PRE: all variables exist
// OUTPUT: none
// POST: default data is assigned
// NOTE: none
ItemType(int id, float exam1, float exam2, float final, float prog,
float quizzes, char addOrDelete);
// PURPOSE: This is used to initialize values to variables
// INPUT: id, exam1, exam2, final, prog, quizzes, addOrDelete
// PRE: all variables are assinged and ok
// OUTPUT: none
// POST: id, exam1, exam2, final, prog, quizzes, and addOrDelete are
// assinged
// NOTE: none
RelationType ComparedTo(ItemType) const;
// PURPOSE: This comapres one item of ItemType to another item to return
// either greater, less, or equal
// INPUT: ItemType
// PRE: ItemType is valid and assinged
// OUTPUT: LESS, GREATER, or EQUAL
// POST: That less, greater, or equal is returned
// NOTE: none
char GetItemFromFile(ifstream& inFile);
// PURPOSE: This will read the items from an inFile, and will also
// determine what variables to read by it's first character
// INPUT: inFile
// PRE: inFile is opened and OK
// OUTPUT: addOrDelete
// POST: all variables are assinged and addOrDelete is returned
// NOTE: none
void WriteItemToFile(ofstream& outFile) const;
// PURPOSE: This will write all variables (id, exam1, exam2, final, prog,
// and quizzes, to an outFile
// INPUT: none
// PRE: outFile is opened and OK
// OUTPUT: outFile
// POST: all variables are assigned to the outFile
// NOTE: none
void PrintLstFullErr(ofstream& outFile) const;
// PURPOSE: This will print the error message when the list is full and
// is trying to be added too
// INPUT: none
// PRE: list is full and trying to be written to
// OUTPUT: outFile
// POST: error message is written to outFile
// NOTE: none
void PrintEmptyLstErr(ofstream& outFile);
// PURPOSE: This will print an error message when the list is empty but
// a line is trying to be deleted from it
// INPUT: none
// PRE: list is empty and is trying to be deleted from
// OUTPUT: outFile
// POST: error message is written to the outFile
// NOTE: none
private :
int id;
float exam1;
float exam2;
float final;
float prog;
float quizzes;
char addOrDelete;
} ;
SortedLinkedList.h
#include "ItemType.h"
typedef int ItemType; // Node definition
struct NodeType
{
ItemType info;
NodeType* next;
};
class SortedType
{
public:
SortedType();
// Constructor
// Postcondition: Empty list is created
~SortedType();
// Destructor
// Postcondition: List is destroyed
SortedType(const SortedType& otherList);
// Copy-constructor
// Postcondition: List is created as a duplicate of otherList
bool IsFull() const;
// Determines whether list is full.
// Post: Function value = (list is full)
int LengthIs() const;
// Determines the number of elements in list.
// Post: Function value = number of elements in list.
void MakeEmpty();
// Initializes list to empty state.
// Post: List is empty.
void RetrieveItem(ItemType& item, bool& found);
// Retrieves list element whose key matches item's key
// (if present).
// Pre: Key member of item is initialized.
// Post: If there is an element someItem whose key matches
// item's key, then found = true and item is a copy
// of someItem; otherwise found = false and item is
// unchanged.
// List is unchanged.
void InsertItem(ItemType item);
// Adds item to list.
// Pre: List is not full.
// item is not in list.
// Post: item is in list
// (added) // list order is preserved
void DeleteItem(ItemType item);
// Deletes the element whose key matches item's key.
// Pre: Key member of item is initialized.
// One and only one element in list has a key matching
// item's key.
// Post: No element in list has a key matching item's key.
void ResetList();
// Initializes current position to end of list for iteration
// through list.
// Post: Current position is at end of list.
void GetNextItem(ItemType& item);
// Gets the next element in list.
// Pre:
// Post: If Current position is at end, currentPos points to head of list,
// else item is copy of element at current position.
// Advance current position.
void Print();
private:
NodeType* listData;
int length;
NodeType* currentPos;
};
runSortedLinkedList.cxx
#include "SortedLinkedList.h"
#include <fstream>
using namespace std;
void FileToList(SortedType& intList, ifstream& inFile, ofstream&
outFile);
// PURPOSE: To take an item from the GetItemFromFile function, and to add
// it if addOrDelete was 'A' or delete it if addOrDelete was 'D'. Also to
// print error messages accordingly.
// INPUT: intList, inFile
// PRE: intList is declared, inFile is OK, outFile is OK
// OUTPUT: outFile
// POST: An item(s) were written to intList with an error message if
// according
// NOTE: none
void ListToFile(SortedType& intList, ofstream& outFile);
// PURPOSE: To print the list to a file as long as data exists in the list
// INPUT: intList
// PRE: intList is declared and OK, outFile is OK
// OUTPUT: outFile
// POST: All items are written to the outFile
// NOTE: none
int main()
{
ifstream inFile;
ofstream outFile;
SortedType intList;
FileToList(intList, inFile, outFile);
ListToFile(intList, outFile);
return 0;
}
void FileToList(SortedType& intList, ifstream& inFile, ofstream&
outFile)
{
ItemType item;
char addOrDelete;
inFile.open("in.data");
intList.MakeEmpty();
addOrDelete = item.GetItemFromFile(inFile);
while (inFile)
{
if (addOrDelete == 'A')
{
if (!intList.IsFull())
intList.InsertItem(item);
else{
ListToFile(intList, outFile);
item.PrintLstFullErr(outFile);
}
addOrDelete = item.GetItemFromFile(inFile);
}
else if (addOrDelete == 'D')
{
if(intList.LengthIs() == 0){
ListToFile(intList, outFile);
item.PrintEmptyLstErr(outFile);
}
else
{
intList.DeleteItem(item);
}
addOrDelete = item.GetItemFromFile(inFile);
}
}
}
void ListToFile(SortedType& intList, ofstream& outFile)
{
ItemType item;
int length;
outFile.open("out.data");
intList.ResetList();
length = intList.LengthIs();
outFile << "STUDENT ID" << setw(6) << "EXAM1" << setw(10) << "EXAM2"
<< setw(9) << "FINAL" << setw(9) << "PROG" << setw(14)
<< "QUIZZES" << endl;
for (int count = 1; count <= length; count++)
{
intList.GetNextItem(item);
item.WriteItemToFile(outFile);
}
}
If you need any of the .cxx for the ItemType or the SortedLinkedList let me know. Any help on this situation and how i can fix it would be greatly appreciated. Thank you!!