#ifndef AARRAY_H
#define AARRAY_H
#include
#include
#include
template
class Lst /*this list contains the string that each value is matched with,
the value,and the previous and next instances of Lst in the linked list*/
{
public:
char *match;
T *val;
Lst *next;
Lst *prev;
};
template
class AArray
{
public:
T &operator[](char * x);/*this is used so the programmer can access
each string-value pair through array notation*/
AArray();//constructor
int GetError();//this returns 1 if a value trying to be accessed doesnt exist yet
private:
int Error;//error value
Lst
Lst
Lst
};
template
AArray
{
return Error;//returns the latest error to the user
}
template
AArray
{
list=new Lst
begin=end=list;//all are the same right now
list->next=list->prev=NULL;/*initialize previous and next to NULL since
nothing else exists */
}
template
T & AArray
{
Error=0; //reset the error value
list=begin;//start at the beginning of the list
do//loop through the entire list
{
if(!strcmpi(x,list->match))/*if the users text matches the current
string value, return the associated value(not case sensitive)*/
return *(list->val);
list=list->next;//advance the list if it didnt find a match
} while(list);//loop until list is a NULL
list=end;//just to make sure its in the proper position
Error=1;//the thing didnt exist, so an error occured
list->next=new Lst
list->next->prev=list;/*do the standard linked list
operations for insertion at the end of a list */
list->next->next=NULL;
list=list->next;
end=list;//set the end to the real end of the list
strcpy(list->match,x);//fill the matching text with the users text
return *list->val;/*return the value(this is needed for assignments)
note that this will be a garbage value, so yo must check GetError()
if you are suspicious */
}
//this is just a sample
int main(void)
{
clrscr();
AArray
x["duke"]=5;//set one value
cout << x["duke"] << "" << x["dog"];//print out the value you just set, + just a made up string
//GetError() now returns 1("dog") isnt a
// string contained in the list
x["duke"]=16; x["dog"]=13;//change an existing value, and set another value
cout <<"" << x["duke"] << "" << x["dog"];//print em out now
getch();
}
#endif