You can implement many more menus. Just Define a class of your need declare a function getFunctionOfChoice() ( or as you want you can give name). Also to call menu inside CMenu class you need to define your own menu object.
To build a menu use std::vector<std::string> mineMenu. use function push_back(/*arguments as string only*/).
pass this menu to CMenu::getChoice( your-menu-pointer->menu-Vector, "Header Of Your Choice to display", lastLine);
this will return index number, you entered to select from menu item. Get it in a int variable choice. Then pass it to
switch statements. Which will handle so-on. As given here.
You can also build its shared library as "libmenus.so", as I explored in old blogs. And then add its header file and library file in your project and use it as defined here.
I've added two shared libraries named : "libvalidator.so" & "libclrscr-getch.so". I already posted those, how to build libraires
and how to attach these in our project. Please refer previous posts.
you can also build this project as shared library and call this lib to another project without including source code. As we did with "libvalidator.so" and "libclrscr-getch.so". its may be "libmenus.so" or as you wish.
To run this program do all these :
1. Set "so library files" i.e. shared libraries to path "/opt/so-files/sos/.". Then set library path to LD_LIBRARY_PATH for this dir. Then run MENUs.
as follows :-
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/so-files/sos/clrscr-getch-so:/opt/so-files/sos/validators
or
you can put header files in "/usr/include/clrscr-getch" "/usr/include/menus/" "/usr/include/validator"
this will call headers without adding header file path in our project
and lib files in "/usr/lib64/clrscr-getch" "/usr/lib64/menus" "/usr/lib64/validtor"
here you must give full path for each library file containing directory.
then run this executable from any where on your system. Mine executable file name is : Menus , run this as follows :-
$ ./Menus <-| ( i.e. press Enter )
main.cpp :-
-----------
#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include "CMenu.h"
#include "CLinkedList.h"
#include "CQueue.h"
#include "CSorting.h"
#include "CVecMenu.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
// CLinkedlist *llist = new CLinkedlist;
CValidator *validator = new CValidator;
CClrscrGetch *get = new CClrscrGetch;
CMenu *menu = new CMenu;
CVecMenu *vecmenu = new CVecMenu;
CLinkedList *llist = new CLinkedList;
CQueue *qu = new CQueue;
CSorting *sor = new CSorting;
int choice = 0;
do
{
get->clrscr();
int lastLine = 0;
choice = menu->getChoice(vecmenu->mainMenu, "MAIN MENU", lastLine);
switch(choice)
{
case 1:
get->clrscr();
llist->getFunctionOfChoice();
break;
case 2:
get->clrscr();
qu->getFunctionOfChoice();
// menu->get->getch();
break;
case 3:
cout << " Stack ";
menu->get->getch();
break;
case 4:
// cout << " Sorting ";
sor->getFunctionOfChoice();
// menu->get->getch();
break;
case 5:
cout << " Searching ";
menu->get->getch();
break;
case 6:
cout << " Trees ";
menu->get->getch();
break;
case 7:
cout << " Graphs ";
menu->get->getch();
break;
case 8:
cout << " Sparse Metrices ";
menu->get->getch();
break;
case 9:
get->clrscr();
exit(0);
}
}while(choice != 9 );
return 0;
}
/*
You can implement many more menus. Just Define a class of your need declare a function getFunctionOfChoice()
( or as you want you can give name). Also to call menu inside CMenu class you need to define your own menu object.
To build a menu use std::vector<std::string> mineMenu. use function push_cack(/*arguments as string only*//*).
pass this menu to CMenu::getChoice( your-menu-pointer->menu-Vector, "Header Of Your Choice toi display", lastLine);
this will return index number, you entered to select from menu item. Get it in a int variable choice. Then pass it to
switch statements. Which will handle so-on. As given above.
*/
--------------------------------------------------------------------------
File : CVecMenu.h :-
--------------------
#ifndef CVECMENU_H
#define CVECMENU_H
#include <vector>
#include <string>
class CVecMenu
{
public:
CVecMenu();
virtual ~CVecMenu();
CVecMenu(const CVecMenu& other);
CVecMenu& operator=(const CVecMenu& other);
std::vector<std::string> menu;
std::vector<std::string>::const_iterator iter;
std::vector<std::string> mainMenu;
std::vector<std::string>::const_iterator iterMainMenu;
std::vector<std::string> stackMenu;
std::vector<std::string>::const_iterator iterStackMenu;
std::vector<std::string> queueMenu;
std::vector<std::string>::const_iterator iterQueueMenu;
std::vector<std::string> listMenu;
std::vector<std::string>::const_iterator iterListMenu;
std::vector<std::string> listSubMenu1;
std::vector<std::string>::const_iterator iterListSubMenu1;
std::vector<std::string> listOperationalMenu;
std::vector<std::string>::const_iterator iterListOperationalMenu;
std::vector<std::string> treeMenu;
std::vector<std::string>::const_iterator iterTreeMenu;
std::vector<std::string> sortingMenu;
std::vector<std::string>::const_iterator iterSortingMenu;
std::vector<std::string> searchMenu;
std::vector<std::string>::const_iterator iterSearchMenu;
std::vector<std::string> graphMenu;
std::vector<std::string>::const_iterator iterGraphMenu;
std::vector<std::string> sparseMetricesMenu;
std::vector<std::string>::const_iterator iterSparseMtrices;
};
#endif // CVECMENU_H
--------------------------------------------------------------------------
File : CVecMenu.cpp :-
----------------------
#include "CVecMenu.h"
CVecMenu::CVecMenu()
{
//ctor
mainMenu.push_back("1. Linked List ");
mainMenu.push_back("2. Queue ");
mainMenu.push_back("3. Stack ");
mainMenu.push_back("4. Sorting ");
mainMenu.push_back("5. Searching ");
mainMenu.push_back("6. Trees ");
mainMenu.push_back("7. Graphs ");
mainMenu.push_back("8. Sparse Metrices ");
mainMenu.push_back("9. Exit ");
listMenu.push_back("1. Singlly Linked List");
listMenu.push_back("2. Doubly Linked List ");
listMenu.push_back("3. Circular linked List ");
listMenu.push_back("4. Doubly Circular Linked List ");
listMenu.push_back("5. Back ");
listMenu.push_back("6. Exit ");
listOperationalMenu.push_back("1. Create ");
listOperationalMenu.push_back("2. Add at beginning ");
listOperationalMenu.push_back("3. Add after node ");
listOperationalMenu.push_back("4. Add Before node ");
listOperationalMenu.push_back("5. Add last ");
listOperationalMenu.push_back("6. Delete first node ");
listOperationalMenu.push_back("7. Delete node before a node ");
listOperationalMenu.push_back("8. Delete node after a node ");
listOperationalMenu.push_back("9. Delete a last node ");
listOperationalMenu.push_back("10. Delete Specified node ");
queueMenu.push_back("1. Sequential Queue as Linked List ");
queueMenu.push_back("2. Priority Queue ");
queueMenu.push_back("3. Circular Queue ");
queueMenu.push_back("4. Dequeue ");
queueMenu.push_back("5. Back");
queueMenu.push_back("6. End");
treeMenu.push_back("1. Binary Tree ");
treeMenu.push_back("2. Binary Search Tree ");
treeMenu.push_back("3. Expressionsin Binary Search Tree ");
treeMenu.push_back("4. Extened Binary Tree ");
treeMenu.push_back("5. Threaded Binary Tree ");
treeMenu.push_back("6. General Trees ");
treeMenu.push_back("7. AVL Trees ");
treeMenu.push_back("8. 2-3 Trees ");
treeMenu.push_back("9. B Tree ");
treeMenu.push_back("10. Heap ");
searchMenu.push_back("1. Linear Search ");
searchMenu.push_back("2. Binary Search ");
sortingMenu.push_back("1. Bubble Sort ");
sortingMenu.push_back("2. Selection Sort ");
sortingMenu.push_back("3. Quick Sort ");
sortingMenu.push_back("4. Insertion Sort ");
sortingMenu.push_back("5. Binary Tree Sort ");
sortingMenu.push_back("6. Heap Sort ");
sortingMenu.push_back("7. Merge Sort ");
sortingMenu.push_back("8. External Sort ");
sortingMenu.push_back("9. Back");
sortingMenu.push_back("10. Exit");
graphMenu.push_back("1. Depth First Search ");
graphMenu.push_back("2. Breadth First Search ");
graphMenu.push_back("3. Spanning Tree ");
graphMenu.push_back("4. Kruskal Algorithms ");
sparseMetricesMenu.push_back("1. Transpose of Sparse Metrices ");
sparseMetricesMenu.push_back("2. Addition of Two Sparse Metrices ");
sparseMetricesMenu.push_back("3. Multiplication of Two Sparse Metrices ");
sparseMetricesMenu.push_back("4. Linked Representation of Sparse Metrices ");
}
CVecMenu::~CVecMenu()
{
//dtor
}
CVecMenu::CVecMenu(const CVecMenu& other)
{
//copy ctor
}
CVecMenu& CVecMenu::operator=(const CVecMenu& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
--------------------------------------------------------------------------
file : CLinkedList.h :-
-----------------------
#ifndef CLINKEDLIST_H
#define CLINKEDLIST_H
#include "CMenu.h"
#include "CVecMenu.h"
#include <validator/validator.h>
#include <clrscr-getch/clrscr-getch.h>
class CLinkedList
{
public:
CLinkedList();
virtual ~CLinkedList();
CLinkedList(const CLinkedList& other);
CLinkedList& operator=(const CLinkedList& other);
// bool getFuncitonOfChoice(int choice);
int getFunctionOfChoice();
CMenu *menu;
CVecMenu *vecmenu;
CClrscrGetch *get;
CValidator *validator;
protected:
private:
int lastLine;
};
#endif // CLINKEDLIST_H
--------------------------------------------------------------------------
File : ClinkedList.cpp :-
-------------------------
#include "CLinkedList.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
CLinkedList::CLinkedList()
{
//ctor
menu = new CMenu;
vecmenu = new CVecMenu;
get = new CClrscrGetch;
validator = new CValidator;
}
CLinkedList::~CLinkedList()
{
//dtor
delete menu;
delete get;
delete validator;
delete vecmenu;
}
CLinkedList::CLinkedList(const CLinkedList& other)
{
//copy ctor
}
CLinkedList& CLinkedList::operator=(const CLinkedList& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
int CLinkedList::getFunctionOfChoice()
{
int choice;
do
{
get->clrscr();
choice = menu->getChoice(vecmenu->listMenu, "Linked List Menu", lastLine);
int downline;
switch(choice)
{
case 1:
cout << " Singlly Linked list ";
get->getch();
break;
case 2:
cout << " doubly linked list ";
get->getch();
break;
case 3:
cout << " Circular linked list ";
get->getch();
break;
case 4:
cout << " doubly circular linked list ";
get->getch();
break;
case 5:
break;
case 6:
get->clrscr();
exit(0);
}
}while(choice != 6 && choice != 5);
return 0;
}
--------------------------------------------------------------------------
File : CMenu.h :-
-----------------
#ifndef CMENU_H
#define CMENU_H
#include <vector>
#include <string>
#include <clrscr-getch/clrscr-getch.h>
#include <validator/validator.h>
#include "CString.h"
class CMenu
{
protected:
private:
int i;
int spacer;
int l;
std::string strchoice;
std::string header;
struct winsize w;
bool tf;
// CString *strtrim;
public:
CMenu();
virtual ~CMenu();
CValidator *validator;
CClrscrGetch * get;
int printMenu(std::vector<std::string> menu, std::string header);
int getChoice(std::vector<std::string> choices, std::string header, int &lastLine);
};
#endif // CMENU_H
--------------------------------------------------------------------------
File : CMenu.cpp :-
-------------------
#include "CMenu.h"
#include <ctype.h>
//#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::boolalpha;
CMenu::CMenu()
{
//ctor
validator = new CValidator;
get = new CClrscrGetch;
// strtrim = new CString;
}
CMenu::~CMenu()
{
//dtor
delete validator;
delete get;
}
int CMenu::printMenu(vector<string> menuDisplay, string header)
{
vector<string>::const_iterator iterMenu;
menu->get->gotoxy(15, 2);
cout << header;
menu->get->gotoxy(13, 3);
int len = header.length();
spacer = 13 + 3 + len;
for( i = 13; i <= spacer; i++)
{
cout << "-";
}
l = 5;
iterMenu = menuDisplay.begin();
while(iterMenu != menuDisplay.end())
{
menu->get->gotoxy(2, l);
cout << *iterMenu++;
l++;
}
return l;
}
int CMenu::getChoice(vector<string> choice, string header, int &lastLine)
{
int l = 0;
int optionChoice = 0;
int i = 0;
int j = 0;
int downline;
int display = 30;
int limit = 0;
int d = 0;
char c = ' ';
char ch;
bool tf = false;
string strtrimmed;
string strcheck;
strcheck.clear();
string strch;
l = printMenu(choice, header);
get->gotoxy(15, ++l);
int lastOptIndex = choice.size();
string sloi = std::to_string(lastOptIndex);
int lensloi = sloi.length();
strch.clear();
get->gotoxy(15, l);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int i = 0; i < w.ws_col; i++)
{
cout << " ";
}
get->gotoxy(15, l);
cout << "Enter Choice : ";
downline = l;
do
{
get->gotoxy(display + i, l);
ch = get->getche();
strcheck += ch;
i++;
tf = CString::trimfunc(&strcheck);
if(tf == false)
{
--i;
get->gotoxy(display + i, l);
continue;
}
if(tf == true && ch == ' ')
{
int len = strcheck.length();
get->gotoxy(display + len, l);
--i;
continue;
}
if( (int(ch) == 127) || (strcheck.length() > lensloi) || (int(ch) == 10) )
{
j = strcheck.length();
int m = j;
if(int(strcheck.at(--m)) == 10 )
{
strcheck.pop_back();
}
if( int(ch) == 127 )
{
strcheck.pop_back();
if(strcheck.length() > 0)
{
strcheck.pop_back();
i -= 2;
get->gotoxy(display + i, l);
cout << " ";
get->gotoxy(display + i, l);
}
else
{
i = 0;
}
continue;
}
if( strcheck.length() > lensloi )
{
strcheck.pop_back();
int sz = strcheck.length();
get->gotoxy(display + sz, l);
cout << " ";
get->gotoxy(display + sz, l);
--i;
continue;
}
tf = CValidator::intvalidator(strcheck);
if(tf == false)
{
strcheck.clear();
strch.clear();
i = 0;
get->gotoxy(display + 2, l);
cout << " -> invalid input, please correct it";
get->getch();
get->gotoxy(display, l);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
for(int k = display; k < w.ws_col; k++)
{
cout << " ";
}
get->gotoxy(display, l);
continue;
}
else if (tf == true)
{
limit = CString::intChange(strcheck);
if( limit < 1 || limit > lastOptIndex )
{
strcheck.clear();
get->gotoxy(display + lensloi + 1, l);
cout << " -> outside of range, only between 1 to " << lastOptIndex;
get->getch();
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
get->gotoxy(display, l);
for(d = display; d < w.ws_col; d++)
{
get->gotoxy(d, l);
cout << " ";
}
get->gotoxy(display, l);
i = 0;
continue;
}
else if( int(ch) == 10)
{
optionChoice = std::atoi(strcheck.c_str());
}
}
}
}while(optionChoice < 1 || optionChoice > lastOptIndex );
lastLine = l + 1;
return optionChoice;
}
--------------------------------------------------------------------------
File : CQueue.h :-
------------------
#ifndef CQUEUE_H
#define CQUEUE_H
#include <clrscr-getch/clrscr-getch.h>
#include <validator/validator.h>
#include "CMenu.h"
#include "CVecMenu.h"
class CQueue
{
public:
CQueue();
virtual ~CQueue();
CQueue(const CQueue& other);
CQueue& operator=(const CQueue& other);
int getFunctionOfChoice();
protected:
private:
CMenu * menu;
CClrscrGetch *get;
CValidator *validator;
CVecMenu *vecmenu;
};
#endif // CQUEUE_H
--------------------------------------------------------------------------
File : CQueue.cpp :-
--------------------
#include "CQueue.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
CQueue::CQueue()
{
//ctor
menu = new CMenu;
get = new CClrscrGetch;
validator = new CValidator;
vecmenu = new CVecMenu;
}
CQueue::~CQueue()
{
//dtor
delete menu;
delete get;
delete validator;
delete vecmenu;
}
CQueue::CQueue(const CQueue& other)
{
//copy ctor
}
CQueue& CQueue::operator=(const CQueue& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
int CQueue::getFunctionOfChoice()
{
int choice;
do
{
get->clrscr();
int lastLine = 0;
int downline;
choice = menu->getChoice(vecmenu->queueMenu, "Queue Menu", lastLine);
downline = lastLine;
switch(choice)
{
case 1:
cout << " Sequential Queue as Linked List ";
get->getch();
break;
case 2:
cout << " Priority Queue ";
get->getch();
break;
case 3:
cout << " Circular Queue ";
get->getch();
break;
case 4:
cout << " Dequeue ";
get->getch();
break;
case 5:
break;
case 6:
get->clrscr();
exit(0);
}
}while(choice != 6 && choice != 5);
return 0;
}
--------------------------------------------------------------------------
File : CSorting.h :-
--------------------
#ifndef CSORTING_H
#define CSORTING_H
#include "CMenu.h"
#include "CVecMenu.h"
#include <iostream>
#include <clrscr-getch/clrscr-getch.h>
#include <validator/validator.h>
class CSorting
{
public:
CSorting();
virtual ~CSorting();
CSorting(const CSorting& other);
CSorting& operator=(const CSorting& other);
int getFunctionOfChoice();
protected:
private:
CMenu * menu;
CValidator * validator;
CClrscrGetch *get;
CVecMenu *vecmenu;
};
#endif // CSORTING_H
--------------------------------------------------------------------------
File : CSorting.cpp :-
----------------------
#include "CSorting.h"
#include <iostream>
using std::cout;
CSorting::CSorting()
{
//ctor
menu = new CMenu;
get = new CClrscrGetch;
validator = new CValidator;
vecmenu = new CVecMenu;
}
CSorting::~CSorting()
{
//dtor
delete menu;
delete get;
delete validator;
delete vecmenu;
}
CSorting::CSorting(const CSorting& other)
{
//copy ctor
}
CSorting& CSorting::operator=(const CSorting& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
int CSorting::getFunctionOfChoice()
{
int choice;
int lastLine;
do
{
get->clrscr();
choice = menu->getChoice(vecmenu->sortingMenu, "Sorting Menu", lastLine);
switch(choice)
{
case 1:
cout << "Bubble Sort";
get->getch();
break;
case 2:
cout << "Selection Sort";
get->getch();
break;
case 3:
cout << "Quick Sort";
get->getch();
break;
case 4:
cout << "Insertion Sort";
get->getch();
break;
case 5:
cout << "Binary Tree Sort";
get->getch();
break;
case 6:
cout << "Heap Sort";
get->getch();
break;
case 7:
cout << "Merge Sort";
get->getch();
break;
case 8 :
cout << "External Sort";
get->getch();
break;
case 9:
break;
case 10:
get->clrscr();
exit(0);
}
}while (choice != 9 && choice != 10);
return 0;
}
--------------------------------------------------------------------------
File : CStack.h :-
------------------
#ifndef CSTACK_H
#define CSTACK_H
#include "CMenu.h"
#include <iostream>
#include <clrscr-getch/clrscr-getch.h>
#include <validator/validator.h>
class CStack
{
public:
CStack();
virtual ~CStack();
CStack(const CStack& other);
CStack& operator=(const CStack& other);
int getFucitonOfChoice();
protected:
private:
CMenu *menu;
CValidator * validator;
CClrscrGetch *get;
};
#endif // CSTACK_H
--------------------------------------------------------------------------
File : CStack.cpp :-
--------------------
#include "CStack.h"
CStack::CStack()
{
//ctor
menu = new CMenu;
get = new CClrscrGetch;
validator = new CValidator;
}
CStack::~CStack()
{
//dtor
delete menu;
delete get;
delete validator;
}
CStack::CStack(const CStack& other)
{
//copy ctor
}
CStack& CStack::operator=(const CStack& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
int CStack::getFucitonOfChoice()
{
int choice;
// do
// {
// chioce = menu->getChoice(menu->)
// switch(choice)
// {
// }
//
// }while()
return 0;
}
--------------------------------------------------------------------------
File : CString.h :-
-------------------
#ifndef CSTRING_H
#define CSTRING_H
#include <string>
#include <iostream>
class CString
{
public:
CString();
virtual ~CString();
CString(const CString& other);
CString& operator=(const CString& other);
static bool trimfunc(std::string *str);
static int intChange(std::string str);
protected:
private:
};
#endif // CSTRING_H
--------------------------------------------------------------------------
File : CString.cpp :-
---------------------
#include "CString.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
CString::CString()
{
//ctor
}
CString::~CString()
{
//dtor
}
CString::CString(const CString& other)
{
//copy ctor
}
CString& CString::operator=(const CString& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
bool CString::trimfunc(string *str2)
{
int atstart = 0, atend = -1;
string str;
if(str2->length() != 0)
{
str = *str2;
}
else
{
return false;
}
for(int i = 0; i < str.size(); i++)
{
if(str[i] != ' ')
{
atstart = i;
break;
}
}
for(int i = str.size()-1; i >= 0; i--)
{
if(str[i] != ' ')
{
atend = i;
break;
}
}
str2->clear();
for(int i = atstart; i <= atend; i++)
{
*str2 += str[i];
}
if(str2->length() == 0)
{
return false;
}
return true;
}
int CString::intChange(string str)
{
int i = std::atoi(str.c_str());
return i;
}
--------------------------------------------------------------------------
--------------------------------------------------------------------------











