Hi, i'm currently doing a college assignment about stacks in C++... and i'm failing miserably!! i've managed to do most of the functions, but i'm completely stumped on the search and sort functions! if anyone could help me, i'd be eternally grateful
here's the code i have so far (i'd ignore my 'attempts' at the functions so far, they're basically copied and pasted and very slightly modified... no where near working!):
Code:
/* U18.01/U19.01 - Programming Concepts and Practise
12th January, 2002
By Nick Drew
A modular based C++ Solution which simulates the operation of a stack data
structure. */
#include <iostream.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
int mynumbers [10];
int point;
void push (void);
void pull (void);
void display (void);
void status (void);
void displaya (void);
void search (void);
void sort (void);
void pause (void);
int main (void)
{
char option;
point =-1;
// - - - - - Display Main Menu - - - - - /
do
{
clrscr();
cout << ".: Main Menu :.";
cout << "\n\nA :: Push Item";
cout << "\nB :: Pull Item";
cout << "\nC :: Display Stack";
cout << "\nD :: Check Stack Status";
cout << "\nE :: Display Array";
cout << "\nF :: Linear Search";
cout << "\nG :: Sort";
cout << "\nX :: Exit";
cout << "\n\nChoose Option Now : ";
cin >> option;
option = toupper(option);
switch (option)
{
case 'A' : push();
break;
case 'B' : pull();
break;
case 'C' : display();
break;
case 'D' : status();
break;
case 'E' : displaya();
break;
case 'F' : search();
break;
case 'G' : sort();
break;
case 'X' : exit(0);
}
}while (option!= 'X');
// - - - - - - - - - - - - - - - - - - /
return 0;
}
// - - - - - Push Function - - - - - //
void push (void)
{
clrscr();
if (point < 9)
{
point++;
cout << ".: Push Item:.\n\n\n";
cout << "Please Enter Number : ";
cin >> mynumbers[point];
}
else
{
cout << "Stack is already full!";
}
pause();
}
// - - - - - - - - - - - - - - - - - //
// - - - - - Pull Function - - - - - //
void pull (void)
{
clrscr();
if (point < 0)
{
cout << "Stack is empty!";
}
else
{
point--;
cout << "Item Pulled!\n";
cout << "Data" << mynumbers [point+1] << "removed";
}
pause();
}
// - - - - - - - - - - - - - - - - - //
// - - -Display Stack Function- - - //
void display (void)
{
int counter;
clrscr();
if (point > -1)
{
for (counter = point; counter >=0; counter --)
{
cout << "\nPosition " << counter << "contains" << mynumbers[counter];
}
}
else
{
cout << "There are no items in the stack! Nothing to display!";
}
pause();
}
// - - - - - - - - - - - - - - - - //
// - Check Stack Status Function - //
void status (void)
{
int howmany;
clrscr();
if (point ==-1)
{
cout << "\n\nStack Empty";
}
else
{
howmany = point + 1;
cout << "\n\nStack contains " << howmany << " elements";
}
pause();
}
// - - - - - - - - - - - - - - - - //
// - - Display Array Function - - //
void displaya (void)
{
int counter;
clrscr();
for (counter = -1; counter <10; counter ++)
{
cout << "\nPosition " << counter << "contains" << mynumbers[counter];
cout << endl;
}
pause();
}
// - - - - - - - - - - - - - - - - //
// - - - - Search Function - - - - //
void search (void)
{
int iIndex; // flag to indicate when an item has been found
int iNot_Found;
// Start from element zero
iIndex = 0;
// assume not found initially
iNot_Found = 1;
// now loop until either the item is found or
// reached the end of the array
while ( ( iNot_Found == 1 )
&& ( iIndex < iSize ) )
{
// if a match
if ( cArray[iIndex] == cTarget )
{
iNot_Found = 0;
}
else // otherwise go onto next element
{
iIndex = iIndex + 1;
} // endif
} // endwhile
// If not found
if ( iNot_Found == 1 )
{
return -1;
}
else
{
return iIndex;
} // endif
} // endfunction SearchCharArray
// - - - - - - - - - - - - - - - - //
// - - - - Sort Function - - - - - //
void sort (void)
{
int InsertInList (string ThisList[], string sItem, int iCurSize)
{
int i = iCurSize;
iCurSize = iCurSize + 1;
while ( ThisList[i-1] > sItem && i > 0)
{
ThisList[i] =ThisList[i -1]; // move each item down 1
i = i - 1;
}
ThisList[i] = sItem; // insert new item
return iCurSize;
}
void pause (void)
{
char keypress;
cout << "\n\n\n Please press any key to continue";
keypress = getch();
}