#include <iostream.h>
#include <ctype.h>
#include <string.h>
#define MAX_NAME_LEN 30
typedef struct Sheet
{
char Name[MAX_NAME_LEN + 1];
struct Sheet *Next, *Prev;
}Sheet;
//prototypes
void initialise(Sheet *&, char arr[], Sheet *&);
void right(Sheet *);
void left(Sheet *);
void first(Sheet *&);
void last(Sheet *&);
void erase(Sheet *);
void insert(char word[], Sheet *&, Sheet *&);
void rename();
void display(Sheet *&);
void strToLower(char s[]);
void main()
{
Sheet *Head = NULL, *Tail = NULL;
Sheet *mvlr=NULL;
char init1[MAX_NAME_LEN+1]="sheet1";
char init2[MAX_NAME_LEN+1]="sheet2";
char init3[MAX_NAME_LEN+1]="sheet3";
initialise(Head, init1, Tail);
initialise(Head, init2, Tail);
initialise(Head, init3, Tail);
char Option;
char OldName[MAX_NAME_LEN+1], NewName[MAX_NAME_LEN+1];
cout<<"************ Selection Options *************"<<endl;
cout<<endl;
while(Option != 'E')
{
cout<<"Press 'R' to move to the next sheet\n";
cout<<"Press 'L' to move to the previous sheet\n";
cout<<"Press 'H' to jump to the first sheet\n";
cout<<"Press 'T' to jump to the last sheet\n";
cout<<"Press 'S' to add a new sheet to the list\n";
cout<<"Press 'D' to delete a sheet from the list\n";
cout<<"Press 'V' to view the sheets currently in the list\n";
cout<<"Press 'N' to rename a sheet\n";
cout<<"Press 'E' to exit\n";
cout<<endl;
cout<<"Enter your option: ";
cin>>Option;
Option = toupper(Option);
switch(Option)
{
case 'R':
right(Head);
break;
case 'L':
left(Head);
break;
case 'H':
first(Head);
break;
case 'T':
last(Tail);
break;
case 'S':
cout<<"Enter a name for this new sheet: ";
cin>>NewName;
strToLower(NewName);
insert(NewName, Head, Tail);
cout<<endl;
break;
case 'D':
cout<<"Enter the name of the sheet you wish to delete: ";
cin>>OldName;
strToLower(OldName);
erase(Head);
break;
case 'V':
cout<<"Displaying list: "<<endl;
display(Head);
cout<<endl;
break;
case 'N':
cout<<"Enter name to change: ";
cin>>OldName[MAX_NAME_LEN + 1];
strToLower(OldName);
cout<<"Enter new name for sheet: ";
cin>>NewName[MAX_NAME_LEN + 1];
strToLower(NewName);
rename();
break;
case 'E':
break;
default:
cout<<endl;
cout<<"Please select an option from the list"<<endl;
cout<<endl;
break;
}//end switch
}//end while
}//end main
void initialise(Sheet *&L, char arr[], Sheet *&T)
{
Sheet *e, *curr, *prev;
e = new Sheet;
strcpy(e->Name, arr);
e->Next = NULL;
e->Prev = NULL;
if(L == NULL)
{
L = e;
}
else
{
prev = curr = L;
while(curr != NULL)
{
prev = curr;
curr = curr->Next;
}
e->Next = NULL;
prev->Next = e;
e->Prev = prev;
T = e;
}
}
void right(Sheet *Head)
{
Sheet *Current, *Previous;
Current = Head;
Current = Current->Next;
Previous = Current->Prev;
cout<<"Current Sheet: "<<Current->Name;
cout<<endl;
}
void left(Sheet *mvlr)
{
Sheet *Current = new Sheet, *Previous = new Sheet;
cout<<"Previous Sheet: "<<Current->Name<<endl;
Current = Current->Prev;
Previous = Current->Next;
cout<<"Current Sheet: "<<Current->Name<<endl;
if(Current->Prev == NULL)
{
cout<<"No more sheets!!!11!one"<<endl;
}
cout<<endl;
}
void first(Sheet *&L)
{
Sheet *e;
e = L;
cout<<"The first sheet in the list is:"<<'\t';
cout<<e->Name<<endl;
cout<<endl;
}
void last(Sheet *&L)
{
Sheet *e;
e = L;
cout<<"The last sheet in the list is: "<<'\t';
cout<<e->Name<<endl;
cout<<endl;
}
void erase(Sheet *L)
{
Sheet *Current = L;
while(Current != NULL || (!strcmp("OldName", Current->Name)))
{
Current = Current->Next;
delete Current;
L = Current;
}
}
//function to insert a new sheet
//inserts at end of list only.
void insert(char word[], Sheet *&L, Sheet *&T)
{
Sheet *e, *curr, *prev;
e = new Sheet;
strcpy(e->Name, word);
e->Next = NULL;
e->Prev = NULL;
if(L == NULL)
{
L = e;
}
else
{
prev = curr = L;
while(curr != NULL)
{
prev=curr;
curr=curr->Next;
}
e->Next = NULL;
prev->Next = e;
e->Prev = prev;
T = e;
}
}
//function to display the current sheets in list
void display(Sheet *&L)
{
Sheet *fuck;
fuck = L;
while(fuck != NULL)
{
cout<<fuck->Name<<endl;
fuck = fuck->Next;
}
}
//function to rename a sheet
void rename()
{
Sheet *Current;
char OldName[MAX_NAME_LEN + 1], NewName[MAX_NAME_LEN + 1];
if(!strcmp(Current->Name, OldName))
{
strcpy(NewName, Current->Name);
cout<<Current->Name;
}
}
void strToLower(char s[])
{ int i ;
for(i=0 ; s[i] != '\0' ; i++)
{
s[i] = tolower(s[i]) ;
}
}
right, the code above partially works and I'm hoping somebody out there can help me understand why some of the functions and code wont work.
It's a double linked list.
user has to be able to
- move right throught the list and back left.
- jump to the head and jump to the tail.
- insert a new sheet anywhere and be in alphabetical order
- delete any sheet from anywhere
- rename a sheet
- display the current sheets in the list
- name are case insensitive
There are a few of us working on this together and so far, the display, jump to head/tails and insert [but only at tail] works.
MS Visual C++ 6.0 just crashes when I press 'R' or 'L' to move left or right.
Delete doesn't seem to like searching for the sheet we want to delete.
Rename I think is the comparing strings.
Any help or ideas is greatly appreciated.
thanks
/InvalidDLL