Popular Posts

6/24/2012

Product Management in C++




Let see the following code :
===================================================
#include<iostream.h>                                                            
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<stdio.h>
#include<process.h>
#include<iomanip.h>
class Product
{ public: int id;
char pro_name[20];
float im_price;
float sell_price;
char cate[20];
int i_day,i_mon,i_year;
char sup[20];
int qty;
char staffname[20];
};
typedef class nodetype *pointertype;
class nodetype
{ private: Product info;
nodetype *left;
nodetype *right;
public:  nodetype(){};
void setleft(pointertype ptr);
void setright(pointertype ptr);
friend nodetype* getnode();
friend void initialize(pointertype  *plist);
friend void update(pointertype *,int);
friend void insertend(pointertype *plist,int i);
friend void insertfirst(pointertype  *plist,int i);
friend void insertafter(pointertype *plist,int i);
friend void add(pointertype &ptr,int j,pointertype *plist);
friend void Traverse(pointertype  *plist);
friend void Delete_select(pointertype  *plist,int n);
friend void Delete_menu(pointertype *plist);
friend void Delete_first(pointertype  *plist);
friend void Delete_after(pointertype  *plist, int i);
friend void Delete_after(pointertype  *plist,char *na);
friend void Delete_end(pointertype  *plist);
friend void sell(pointertype  *plist);
friend void sort_inc(pointertype  *plist);
friend void sort_dec(pointertype  *plist);
friend void search(pointertype *plist,int sid);
friend pointertype * initialize();
};
//-------------------------------------Detect_Error_Float------------------
float detect_error_float()
{ char str[30];
float a=9.9;
cin.clear();
cin.seekg(0,ios::end);
cin.get(str,20);
for(int i=0;i<strlen(str);i++)
if(!isdigit(str[i]))
if(str[i]=='.')
{  i++;
for(;i<strlen(str);i++)
if(!isdigit(str[i]))
throw a;
}
else
throw a;
return atof(str);
}
//-------------------------------------Detect_Error_Int------------------
int detect_error_int()
{ char str[30];
cin.clear();
cin.seekg(0,ios::end);
cin.get(str,20);
for(int i=0;i<strlen(str);i++)
if(!isdigit(str[i]))
throw 0;
return atoi(str);
}
//----------------------------------Function initialize--------------
pointertype * initialize()
{ pointertype *plist=new pointertype;
*plist=NULL;
return plist;
}
//----------------------------------Function setleft-----------------------
void nodetype::setleft(pointertype ptr)
{ left=ptr;
}
//----------------------------------Function setright----------------------
void nodetype::setright(pointertype ptr)
{ right=ptr;
}
//----------------------------------Function sell-------------------------
void sell(pointertype *plist)
{ clrscr();
if(*plist==NULL)
{ cout<<"Nothing to order!";
getch();
}
else
{
again:
clrscr();
cout<<"Enter id of product you want to buy:";
int id,i=0;
try
{ id=detect_error_int();
}
catch(int a)
{ goto again;
}
pointertype ptr;
ptr=*plist;
while(ptr!=NULL)
{ if(ptr->info.id==id)
{ i=1;
break;
}
ptr=ptr->right;
}
if(i==1)
{  again1:
clrscr();
cout<<"Enter quentity to order:";
int qty;
try
{ qty=detect_error_int();
}
catch(int a)
{ goto again1;
      }
if(ptr->info.qty<qty)
{ cout<<"Out of order!";
getch();
}
else
ptr->info.qty=ptr->info.qty-qty;
}
else
{ cout<<"Not found!";
getch();
}
}

}
//----------------------------------Delete First----------------
void Delete_first(pointertype *plist)
{ pointertype ptr;
ptr=*plist;
if(*plist==NULL)
cout<<"No Item In Stock!";
else if(ptr->left==NULL && ptr->right==NULL)
{  *plist=NULL;
cout<<"Delete First Success!";
}
else if(ptr->left==NULL && ptr->right!=NULL)
{ ptr=ptr->right;
ptr->left=NULL;
*plist=ptr;
cout<<"Delete First Success!";
}
}
//-------------------------Delete_after (ID) -------------------------
void Delete_after(pointertype *plist,int i){
pointertype p,ptr;
int f=0;
p=*plist;
while(p!=NULL){
if(p->info.id==i)
{  if(p->right==NULL)
{ cout<<"This is the last item, so no data can be delete.";
return;
}
ptr=p->right;
p->right=ptr->right;
delete(ptr);
f++;
break;
}
p=p->right;
}
if(f==0) cout<<"\nInput Wrong ID!";
else
cout<<"Delete After Successed!!";
}
//--------------------------Delete After (name)----------------------
void Delete_after(pointertype *plist,char *na){
pointertype p,ptr;
int f=0;
p=*plist;
while(p!=NULL){
if(strcmp(p->info.pro_name,na)==0)
{  if(p->right==NULL)
{ cout<<"This is the last item, so no data can be delete.";
return;
}
ptr=p->right;
p->right=ptr->right;
delete(ptr);
f++;
break;
}
p=p->right;
}
if(f==0) cout<<"\nInput Wrong Name!";
else
cout<<"Delete After successed!!";
}
//-------------------------------Delete_end-------------------------
void Delete_end(pointertype *plist){
pointertype ptr,p;
ptr=*plist;
p=*plist;
if(*plist==NULL)
cout<<"No Item In Stock!";
else if(ptr->left==NULL && ptr->right==NULL){
*plist=NULL;
cout<<"Delete End Success!";
}
else
{ while(ptr->right!=NULL)
ptr=ptr->right;
while(p->right!=ptr)
p=p->right;
p->right=NULL;
delete(ptr);
cout<<"Delete End Success!";
}
}
//------------------------------Delete_select------------------------------
void Delete_select(pointertype *plist,int n)
{  if(*plist==NULL)
{ cout<<"\n\nNothing to delete!";
getch();
}
else
{ pointertype ptr,p;
ptr=*plist;
for(int i=1;i<=n;i++) // n is the node we want to delete.
ptr=ptr->right; // ptr will point to node we want to delete
if(ptr->left==NULL&&ptr->right==NULL) // delete last node when there is only 1 node
{  *plist=NULL;
}
else if(ptr->right==NULL && ptr->left!=NULL) // delete last node when there are only 2 node
{ ptr=ptr->left;
ptr->right=NULL;
}
else if(ptr->left==NULL && ptr->right!=NULL) // delete first node when there are only 2 node
{ ptr=ptr->right;
ptr->left=NULL;
*plist=ptr;
}
else // delete node when nodes are more than 2
{
p=ptr;
ptr=ptr->left;
ptr->right=p->right;
ptr=ptr->right;
ptr->left=p->left;
}
}
}
//-----------------------------------Update----------------------------
void update(pointertype *plist,int id)
{ pointertype ptr;
int a=0;
ptr=*plist;
while(ptr!=NULL)
{ if(ptr->info.id==id)
{  a=1;
break;
}
ptr=ptr->right;
}
if(a==0)
{ cout<<"Not found!";
getch();
}
else                                            
{  cout<<"[1]. Update product name\n [2]. Update import price\n [3]. Sell price\n [4]. Update Category\n [5].Update import day\n [6]. Update import month\n [7]. Update import year\n [8].Update supplier\n [9].Update Quentity\n [10].Update staff's name";
int ch;
again:
cout<<"\nEnter number:";
try
{ ch=detect_error_int();
}
catch(int a)
{ clreol();
goto again;
}
clrscr();
switch(ch)
{ case 1: cout<<"Enter new product name:";
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.pro_name,20);
break;
case 2:     again2:
clrscr();
cout<<"Enter new import price:";
try
{ ptr->info.im_price=detect_error_float();
}
catch(float a)
{ goto again2;
}
break;
case 3:     again3:
clrscr();
cout<<"Enter new sell price:";
try
{ ptr->info.sell_price=detect_error_float();
}
catch(float a)
{ goto again3;
}
break;
case 4: cout<<"Enter new category:";
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.cate,20);
break;
case 5:     again4:
clrscr();
cout<<"Enter new import day:";
try
{ ptr->info.i_day=detect_error_int();
}
catch(int a)
{ goto again4;
}
break;
case 6:     again5:
clrscr();
cout<<"Enter new import month:";
try
{ ptr->info.i_mon=detect_error_int();
}
catch(int a)
{ goto again5;
}
break;
case 7:     again6:
clrscr();
cout<<"Enter new import year:";
try
{ ptr->info.i_year=detect_error_int();
}
catch(int a)
{ goto again6;
}
break;
case 8: cout<<"Enter new supplier:";
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.sup,20);
break;
case 9:    again7:
clrscr();
cout<<"Enter new quentity:";
try
{ ptr->info.qty=detect_error_int();
}
catch(int a)
{ goto again7;
}
break;
case 10: cout<<"Enter new staff's name:";
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.staffname,20);
break;
}
}
}
//----------------------------------------Traverse-------------------------
void Traverse(pointertype *plist)
{  clrscr();
if(*plist!=NULL)
{
char ch;
int i=2,y=0;
do
{  gotoxy(5,1);
cout<<"ID"<<"     Pro_Name"<<"      Im_price"<<"      Category"<<"       Date"<<"        Quentity";
pointertype ptr;
ptr=*plist;
while(ptr!=NULL)
{  gotoxy(1,2*(y+2)-2); // Frame
cout<<" ----------------------------------------------------------------------------";
gotoxy(1,2*(y+2)-1);
cout<<"|*=>";
gotoxy(78,2*(y+2)-1);
cout<<"|";
gotoxy(1,2*(y+2));
cout<<" ----------------------------------------------------------------------------";

gotoxy(5,2*i-1);
cout.fill('0');
cout<<setw(5)<<setiosflags(ios::right)<<ptr->info.id<<"  ";
cout.fill(' ');
cout<<setw(14)<<setiosflags(ios::left)<<ptr->info.pro_name;
cout.fill('0');
cout<<" "<<setw(5)<<setiosflags(ios::right)<<ptr->info.im_price<<'$';
cout<<"       ";
cout<<setw(14);
cout.fill(' ');
cout<<setiosflags(ios::left)<<ptr->info.cate;
cout<<' ';
cout<<ptr->info.i_day<<'/'<<ptr->info.i_mon<<'/';
cout<<setw(6);
cout<<setiosflags(ios::left)<<ptr->info.i_year;
cout<<"  ";
cout<<setw(8);
cout.fill('0');
cout<<setiosflags(ios::right)<<ptr->info.qty;
ptr=ptr->right;
i++;
}
ch=getch();
switch(ch)
{ case 's': y++;break;
case 'w': y--;break;
case  13:   Delete_select(plist,y); // y can indicate which node we want to delete
if(y==i-3)// protect when delete last node. then the select item will move previous
y--;
                  break;
}
if(y>=i-2)
y=0;
if(y<0)
y=i-3;
i=2;
clrscr();
}while(ch!=27);
}
else
cout<<"Empty list!!";
}
//-------------------------------------Getnode-----------------------
nodetype* getnode()
{ pointertype plist=NULL;
plist=new nodetype;
plist->left=NULL;
plist->right=NULL;
return plist;
}
//-------------------------------------Add-----------------------------
void add(pointertype &ptr,int j,pointertype *plist)
{     clrscr();
int id;
again:
pointertype p;
gotoxy(3,3);
cout<<(j+1);
again7:
gotoxy(5,3);
cout<<"\tEnter id:";
gotoxy(29,3);
cout<<"--------------";
gotoxy(29,3);

try
{ id=detect_error_int();
}
catch (int a)
{ clreol;
goto again7;
}
if(*plist==NULL)
{ *plist=getnode();
(*plist)->right=NULL;
(*plist)->left=NULL;
(*plist)->info.id=id;
}
else
{
for(p=*plist;p!=NULL;p=p->right)
if(id==p->info.id)
goto again;
ptr->info.id=id;
}

gotoxy(5,5);
cout<<"\tEnter Product Name:";
gotoxy(29,5);
cout<<"--------------";
gotoxy(29,5);
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.pro_name,20);

again1:
gotoxy(5,7);
cout<<"\tEnter Import price:";
gotoxy(29,7);
cout<<"--------------";
gotoxy(29,7);
try{
ptr->info.im_price=detect_error_float();
}
catch (float a)
{ clreol();
goto again1;
}

again2:
gotoxy(5,9);
cout<<"\tEnter Sell price:";
gotoxy(29,9);
cout<<"--------------";
gotoxy(29,9);
 try
 { ptr->info.sell_price=detect_error_float();
 }
 catch (float a)
 {   clreol;
goto again2;
 }
gotoxy(5,11);
cout<<"\tEnter category:";
gotoxy(29,11);
cout<<"--------------";
gotoxy(29,11);
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.cate,20);

again3:
gotoxy(5,13);
cout<<"\tEnter day:";
gotoxy(29,13);
cout<<"--";
gotoxy(29,13);
try
{ ptr->info.i_day=detect_error_int();
}
catch (int a)
{ clreol();
goto again3;
}

again4:
gotoxy(35,13);
cout<<"Month:";
gotoxy(42,13);
cout<<"--";
gotoxy(42,13);
try
{ ptr->info.i_mon=detect_error_int();
}
catch (int a)
{ clreol();
goto again4;
}

again5:
gotoxy(47,13);
cout<<"Year:";
gotoxy(53,13);
cout<<"----";
gotoxy(53,13);
try
{ ptr->info.i_year=detect_error_int();
}
catch (int a)
{ clreol();
goto again5;
}
gotoxy(5,15);
cout<<"\tEnter supplier:";
gotoxy(29,15);
cout<<"--------------";
gotoxy(29,15);
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.sup,20);

again6:
gotoxy(5,17);
cout<<"\tEnter quentity:";
gotoxy(29,17);
cout<<"--------------";
gotoxy(29,17);
try
{ ptr->info.qty=detect_error_int();
}
catch (int a)
{ clreol();
goto again6;
}

gotoxy(5,19);
cout<<"\tEnter staff's name:";
gotoxy(29,19);
cout<<"--------------";
gotoxy(29,19);
cin.clear();
cin.seekg(0,ios::end);
cin.get(ptr->info.staffname,20);
}
//----------------------------------------Insertend----------------------------
void insertend(pointertype *plist,int i)
{ pointertype ptr,p;
p=getnode();
add(p,i,plist);
ptr=*plist;
while(ptr->right!=NULL)
ptr=ptr->right;
ptr->right=p;
p->left=ptr;
p->right=NULL;
}
//-------------------------------------------InsertFirst----------------------
void insertfirst(pointertype *plist,int i)
{  pointertype p;
p=getnode();
add(p,i,plist);
p->right=*plist;
p->left=NULL;
p->right->left=p;
*plist=p;

}
//-------------------------------------------Insertafter----------------
void insertafter(pointertype *plist,int i)
{ pointertype p=*plist,ptr;
again:
clrscr();
cout<<"Enter id of data which you want to insert after it:";
int id,a=0;
try
{ id=detect_error_int();
}
catch(int a)
{ clreol();
goto again;
}
while(p!=NULL)
{ if(id==p->info.id)
{ a=1;
ptr=p;
}
p=p->right;
}
if(a==0)
{ cout<<"Not found!!";
getch();
}
else
{ p=getnode();
add(p,i,plist);
p->right=ptr->right;
p->left=ptr;
ptr->right=p;
}
}
//-------------------------------------Delete menu---------------------------
void Delete_menu(pointertype *plist)
{
char menu[3][13]={"Delete First", "Delete After", "Delete End" };
char ch='s';
int y=0;
do
{
for(int j=15;j<20;j++) // Output border of menu
{  gotoxy(43,j);
cout<<"|               |";
}
gotoxy(44,17);
cout<<"---------------";
gotoxy(44,20);
cout<<"---------------";

for(int i=0;i<3;i++)  //Output menu select
{  gotoxy(44,(2*(i+8))-1);
if(i==y)
{  cout<<"*=>"<<menu[i];
gotoxy(44,2*i+14);
cout<<"---------------";
gotoxy(44,2*i+16);
cout<<"---------------";
}
else
cout<<"   "<<menu[i];
}
ch=getch();
switch(ch)
{ case 's': y++; break;
case 'w': y--; break;
case 'a': return;
case 13: clrscr();
if (*plist==NULL)
cout<<"Empty list!!";
else
{
if(y==0)//-----------------------Delete First---------------
{
Delete_first(plist);
}
else if(y==1)//-------------------Delete After--------------
{  if((*plist)->right==NULL&&(*plist)->left==NULL)
cout<<"You can not choose delete after becuase there is only one item left.";
else
{  again:
clrscr();
cout<<"[1].Delete by id\n";
cout<<"[2].Delete by Name\n";
cout<<"Choose one of them:";
int choice;
try
{ choice=detect_error_int();
}
catch(int a)
{ goto again;
}
clrscr();
switch(choice)
{
case 1:     again1:
clrscr();
cout<<"\n\nEnter Id You Want to Delete After:";
int id;
try
{ id=detect_error_int();
}
catch(int a)
{ goto again1;
}
Delete_after(plist,id);
break;
case 2:   cout<<"\n\nEnter Name You Want to Delete After:";
char na[20];
cin.clear();
cin.seekg(0,ios::end);
cin.get(na,20);
Delete_after(plist,na);
break;
}
}
}
else if(y==2)//--------------------------Delete End-------------
Delete_end(plist);
}
getch();
return ;
}
if(y<0)
y=2;
if(y>=3)
y=0;
for(i=0;i<3;i++)  //Output menu select
{  gotoxy(44,(2*(i+7))-1);
cout<<"               ";
}
}while(ch!=27);
}
//------------------------------------Insert Menu-------------------------------
void insert_menu(pointertype *plist)
{ char menu[3][13]={"Insert First", "Insert After", "Insert End" };
char ch='s';
int y=0;
do
{
for(int j=13;j<18;j++) // Output border of menu
{  gotoxy(43,j);
cout<<"|               |";
}
gotoxy(44,15);
cout<<"---------------";
gotoxy(44,18);
cout<<"---------------";

for(int i=0;i<3;i++)  //Output menu select
{  gotoxy(44,(2*(i+7))-1);
if(i==y)
{  cout<<"*=>"<<menu[i];
gotoxy(44,2*i+12);
cout<<"---------------";
gotoxy(44,2*i+14);
cout<<"---------------";
}
else
cout<<"   "<<menu[i];
}
ch=getch();
switch(ch)
{ case 's': y++; break;
case 'w': y--; break;
case 'a':   return;
case 13:    clrscr();
if (*plist==NULL)
add(*plist,0,plist);
else
{ if (y==2)//------------------------Insert End-----------
insertend(plist,0);
else if (y==0)//----------------------Insert First---------
insertfirst(plist,0);
else if (y==1)//----------------------Insert After------------
insertafter(plist,0);
}
return ;
}
if(y<0)
y=2;
if(y>=3)
y=0;
for(i=0;i<3;i++)  //Output menu select
{  gotoxy(44,(2*(i+7))-1);
cout<<"               ";
}
}while(ch!=27);
}
//----------------------------------------Sort Menu------------------------
void sort_menu(pointertype *plist)
{ char menu[2][10]={"Decrease", "Increase", };
char ch='s';
int y=0;
do
{
for(int j=11;j<14;j++) // Output border of menu
{  gotoxy(43,j);
cout<<"|            |";
}
gotoxy(44,10);
cout<<"------------";
gotoxy(44,14);
cout<<"------------";

for(int i=0;i<2;i++)  //Output menu select
{  gotoxy(44,(2*(i+6))-1);
if(i==y)
{  cout<<"*=>"<<menu[i];
gotoxy(44,2*i+10);
cout<<"------------";
gotoxy(44,2*i+12);
cout<<"------------";
}
else
cout<<"   "<<menu[i];
}
ch=getch();
switch(ch)
{ case 's': y++; break;
case 'w': y--; break;
case 'a': return;
case 13:    clrscr();
if(*plist==NULL)
{ cout<<"Empty list!!";
getch();
}
else
{
if(y==0)//---------------------Sort Decrease----------
{ cout<<"dec";
sort_dec(plist);
}//decrease function
else if(y==1)//-----------------Sort Increase-----------
{ cout<<"Inc";
sort_inc(plist);
}
cout<<"Sort Completed!";
getch();
}
return;
}
if(y<0)
y=1;
if(y>=2)
y=0;
for(i=0;i<2;i++)
{  gotoxy(44,(2*(i+6))-1);
cout<<"            ";
}
}while(ch!=27);
}
//-------------------------------------------Search-----------------------
void search(pointertype *plist,int sid)
{  clrscr();
pointertype ptr,*temp=NULL; int f=0;
temp=new pointertype;
ptr=*plist;
while(ptr!=NULL){
if(ptr->info.id==sid)
{ f=1;
*temp=getnode();
(*temp)->info=ptr->info;
(*temp)->right=NULL;
(*temp)->left=NULL;
Traverse(temp);
}  ptr=ptr->right;
}
if(f==0)
cout<<"Information not found!";
}
//--------------------------------------------Sort Increase-----------------
void sort_inc(pointertype *plist)
{
pointertype p,q;
Product temp;
for(p=*plist;p!=NULL;p=p->right)
for(q=p->right;q!=NULL;q=q->right)
if(p->info.id>q->info.id)
{ temp=q->info;
 q->info=p->info;
 p->info=temp;
}

}
//-------------------------------------------------Sort decrease-------------
void sort_dec(pointertype *plist)
{     pointertype p,q;Product temp;
p=*plist;
while(p!=NULL)
{ q=p->right;
while(q!=NULL)
{ if(p->info.id<q->info.id)
{ temp=q->info;
q->info=p->info;
p->info=temp;
}
q=q->right;
}
p=p->right;
}

}
void main()
{ char menu[9][10]={"Add      ", "Traverse ", "Search   ", "Update   ", "Sort    >", "Insert  >", "Delete >", "Sell     ", "Exit     "};
char ch='s';
int y=0;
pointertype *plist;
plist=initialize();
do
{  gotoxy(5,23);
cout<<"Lecturer: Ung Rithy";
gotoxy(60,24);
printf("s=Down w=%c d=%c a=%c",24,26,27);
gotoxy(5,24);
cout<<"Program by M1 students";
gotoxy(5,1);
cout<<"********************";
gotoxy(5,2);
cout<<"*==================*";
gotoxy(5,3);
cout<<"*Product Management*";
gotoxy(5,4);
cout<<"*==================*";
gotoxy(5,5);
cout<<"********************";
for(int j=3;j<20;j++) // Output border of menu
{  gotoxy(30,j);
cout<<"|            |";
}
gotoxy(31,2);
cout<<"------------";
gotoxy(31,20);
cout<<"------------";
for(int i=0;i<9;i++)  //Output menu select
{  gotoxy(31,(2*(i+2))-1);
if(i==y)
{  cout<<"*=>"<<menu[i];
gotoxy(31,2*i+2);
cout<<"------------";
gotoxy(31,2*i+4);
cout<<"------------";
}
else
cout<<"   "<<menu[i];
}
ch=getch();
switch(ch)
{  case 's': y++; break;
case 'w': y--; break;
case 'd': if(y==4)//------------Sort---------
sort_menu(plist);
else if(y==5)//--------------Insert------------
insert_menu(plist);
else if(y==6)//-----------------Delete--------------
Delete_menu(plist);
break;
case 13:    clrscr();
if(y==0)//--------------------------Add--------------
{  again1:
clrscr();
cout<<"Enter n:";
int n;
try
{ n=detect_error_int();
}
catch (int a)
{
goto again1;
}
for(int i=0;i<n;i++)
if(*plist==NULL)
add(*plist,0,plist);
else
insertend(plist,i);
}
else if(y==1)//------------------------------Traverse-------------
{  if(*plist==NULL)
{ cout<<"Empty List!!";
getch();
}
else
Traverse(plist);
}
else if(y==2)//------------------------Search-----------------------
{ if(*plist!=NULL)
{  again:
clrscr();
cout<<"Enter id that u want to search:";
int id;
try
{ id=detect_error_int();
}
catch(int a)
{ goto again;
}
search(plist,id);getch();
}
else
{ clrscr();
cout<<"Empty list!!";
getch();
}
}
else if(y==3)//----------------------------Update------------------
{  if(*plist!=NULL)
{  again2:
clrscr();
cout<<"Enter id that u want to update:";
int id;
try
{ id=detect_error_int();
}
catch(int a)
{ goto again2;
}
update(plist,id);
}
else
{ clrscr();
cout<<"Empty list!!";
getch();
}
}
else if(y==7)//---------------------Sell----------
sell(plist);
else if(y==8)//---------------------------Exit----------
exit(1);
break;
}
if(y<0)
y=8;
if(y>8)
y=0;
clrscr();
}while(1);
}

//Click like and share it to your friends .!

Staff Form in C++



The following code :
//RUPP
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>

template<class T, class T1>    // Template Sort
void sort(T a[], int n, T1 a1[])
{  T1 temp;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
if(a[i]>a[j])
{  temp=a1[i];
a1[i]=a1[j];
a1[j]=temp;
}
}

template<class T,class T1> //Template Search ID
int search(T a[], int n, T1 key)
 {
int k=0;
for(int i=0; i<n; i++)
if(a[i]==key)
{
k++; break;
}
return k==0?-1:i;
 }

class Staff
{
private:
int id, hour, salary;
float rate, am;
char name[30], sex;
public:

Staff(int ids=0, int h=0, int sa=0, float ra=0, float a=0, char *na="AAA", char s='M') {
id=ids;
hour=h;
salary=sa;
rate=ra;
am=a;
strcpy(name,na);
sex=s; }

Staff(Staff &other) {
id=other.id;
hour=other.hour;
salary=other.salary;
rate=other.rate;
am=other.am;
strcpy(name,other.name);
sex=other.sex; }

//Accessor Function
int getID() { return id; }
int getHour()   { return hour; }
int getSalary() { return salary; }
float getRate() { return rate; }
float getAmount() { return am; }
char *getName()   { return name; }
char getSex()     { return sex; }

//Implementor Function
void setID(int ids=0) { id=ids; }
void setHour(int h=0) { hour=h;  }
void setSalary(int sa=0) { salary=sa; }
void setRate(int ra=0) { rate=ra;  }
void setName(char *na="AAA") { strcpy(name,na); }
void setSex(char s='M') { sex=s; }
void setAmount(float a=0) { am=a; }

/*
void input() {
cout<<"Enter ID:";     cin>>id;
cout<<"Enter Name:";   cin.seekg(0); cin.get(name,30);
cout<<"Enter Sex:";  cin.seekg(0); cin>>sex;
cout<<"Enter Salary:"; cin>>salary;
cout<<"Enter Hour:";   cin>>hour;
cout<<"Enter Rate:";   cin>>rate; }
*/
void input()
{
cout<<"Enter ID:";     cin>>id;
cout<<"Enter Name:";   cin.seekg(0); cin.get(name,30);
cout<<"Enter Sex:";  cin.seekg(0); cin>>sex;
cout<<"Enter Salary:"; cin>>salary;
cout<<"Enter Hour:";   cin>>hour;
cout<<"Enter Rate:";   cin>>rate;
}
//Insertion operator
friend istream& operator>>(istream& in, Staff& st) {
cout<<"\nEnter ID:";   in>>st.id;
cout<<"Enter Name:";   cin.seekg(0); cin.get(st.name,30);
cout<<"Enter Sex:";  cin.seekg(0); in>>st.sex;
cout<<"Enter Salary:"; in>>st.salary;
cout<<"Enter Hour:";   in>>st.hour;
cout<<"Enter Rate:";   in>>st.rate;
return in; }

float amount() { return am=salary+(hour*rate); }

void output() {
cout<<id<<"\t"<<name<<"\t"<<sex<<"\t"<<salary<<"\t"<<hour<<"\t"<<rate<<"\t"<<amount()<<endl;}

//Extraction operator
friend ostream& operator<<(ostream& out, Staff& st) {
out<<st.id<<"\t"<<st.name<<"\t"<<st.sex<<"\t"<<st.salary<<"\t"<<st.hour<<"\t"<<st.rate<<"\t"<<st.amount()<<endl;
return out;}

static void printHeading() { cout<<"\nID\tName\tSex\tSalary\tHour\tRate\tAmount\n"; }

//Assignment operator ==
int operator ==(char  *v)  { return (strcmp(this->getName(),v)==0); }

//Arithmetic operator +
Staff operator+(float b) { am=am+b; return *this; }

//Comparison operator >
int operator >(Staff & m)  { return (am>m.am); }

//~Staff() {
// cout<<"\nAll of data has been destoyed"; }
};


Staff *max(Staff c[], int n)
{
Staff *p;
p=&c[0];
for(int i=0; i<n; i++)
if(p->amount()<c[i].amount())
p=&c[i];
return p;
}

// Unfuntion member
int total(Staff c[], int n)
{
int s=0;
for(int i=0; i<n; i++)
s=s+c[i].getAmount();
return s;
}

void display(Staff c[], int n)
{
Staff obj;
obj.printHeading();
for(int i=0; i<n; i++)
c[i].output();
}

void update(Staff c[], int n)
{
int find, pos, i;
Staff obj;
clrscr();
cout<<"Update Staff List:\n";
display(c, n);

back:
int index=-1;
cout<<"\nEnter ID you want to search for update = "; cin>>find;
obj.printHeading();
for(i=0; i<n; i++)
 if(c[i].getID()==find)
{
index=i;
c[i].output();
}

if(index==-1)
{
cout<<"ID which you just search is not found!\n";
goto back;

}

cout<<"\nPlease Press:\n";
cout<<"\t1. Go to update staff name.\n";
cout<<"\t2. Go to update salary.\n";
cout<<"\t3. Go to update hour.\n";
cout<<"\t4. Go to update rate.\n";
cout<<"\nEnter position you want to update= "; cin>>pos;
switch(pos)
{
case 1:
char na[30];
cout<<"\nPlease input new staff name for update= "; cin.seekg(0); cin.get(na,30);
c[index].setName(na); break;

case 2:
int sa;
cout<<"\nPlease input new salary for update= "; cin>>sa;
c[index].setSalary(sa); break;

case 3:
int ho;
cout<<"\nPlease input new hour for update= "; cin>>ho;
c[index].setHour(ho); break;

case 4:
int ra;
cout<<"\nPlease input new salary for update= "; cin>>ra;
c[index].setRate(ra); break;
}
}


void main()
{
char ch;
int i, *a, *sort_id, s;
int select, result;
Staff obj, *ptr, *p;
do
{
clrscr();
cout<<"Press 1.Go to insert N staff.\n";
cout<<"Press 2.Go to display all staff information.\n";
cout<<"Press 3.Go to search any staff information as ID or Name.\n";
cout<<"Press 4.Go to sort staff information as ID or Amount.\n";
cout<<"Press 5.Go to update any staff information.\n";
cout<<"Press 6.Go to diplay max of amount of each staff.\n";
cout<<"Press 7.Go to diplay total of amount.\n";
cout<<"Press 8.Exit the program.\n";
cin>>select;
switch(select)
{
case 1:
clrscr();
int n;
cout<<"Enter n:"; cin>>n;
p=new Staff[n];
for(i=0; i<n; i++)
cin>>p[i]; //p[i].input();
break;

case 2:
clrscr();
cout<<"\nDisplay Staff Information:";
display(p, n);
break;

case 3:
clrscr();
cout<<"\nPress 1.Go to search by ID.\n";
cout<<"Press 2.Go to search by Name.";
cin>>select;
switch(select)
{

case 1:
//Template Search ID====
cout<<"\nSearch Staff ID:";
display(p, n);
cout<<endl;

bingo:
int key;
a=new int[n];
for(i=0; i<n; i++)
a[i]=p[i].getID();

cout<<"\nEnter staffID you want to search="; cin>>key;
result=search(a,n,key);
if(result==-1)
cout<<"Search not found.\n";
else
{
obj.printHeading();
cout<<p[result];
}

if(result==-1)
{
goto bingo;
}
break;

case 2:
clrscr();
 //Template Search Name===
cout<<"Search Staff Name:";
display(p, n);
cout<<endl;

gogo:
char s_key[30];
cout<<"\nEnter name you want to search=";
cin.clear(); cin.seekg(0,ios::end); cin.get(s_key,30);
result=search(p,n,s_key);
if(result==-1)
cout<<"Search not found.\n";
else
{
obj.printHeading();
cout<<p[result];
}
if(result==-1)
{
goto gogo;
}
break;
}break;

case 4:
clrscr();
cout<<"Press 1.Go to sort by ID.\n";
cout<<"Press 2.Go to sort by Amount.";
cin>>select;
switch(select)
{
case 1:
cout<<"Before Sort Staff ID:";
display(p, n);
cout<<endl;

cout<<"\nAfter soft data ID";
sort_id=new int[n];
for(i=0;i<n;i++)
sort_id[i]=p[i].getID();
sort(sort_id,n,p);
display(p, n);
break;

case 2:
cout<<"Before Sort Staff Amount:";
display(p, n);
cout<<endl;

cout<<"\nAfter Sort Staff Amount:";
sort(p,n,p);
display(p, n);
break;
}break;

case 5:
update(p,n);
cout<<"\nShow all data after update:";
display(p, n);
break;
case 6:
clrscr();
cout<<"Show all staff information:";
display(p, n);
cout<<endl;
cout<<"\nMax of staff amount is:";
obj.printHeading();
ptr=max(p,n);
ptr->output();
break;

case 7:
 cout<<"Show all staff information:";
 display(p, n);
 cout<<endl;
 s=total(p,n);
 cout<<"\nTotal of all staff amount= "<<s<<endl;
 break;

case 8:
delete[] p;
exit(0);
break;
}
cout<<"\nPress Y, if you want to continue or press any key to exit!";
ch=getch();
}while(ch=='y');
}


/*
class ErrorGender
{
public:
Error() { cout<<"Error:";}
char *message() { return "Incorrect type Gender F or M !";}
};

class ErrorID
{
public:
Error() { cout<<"Error:";}
char *message() { return "Incorrect type ID, must be a number !";}
};

char readGender(char *str)
{
char Gen;
cin>>Gen;
if(!strchr(str,Gen)) throw ErrorGender();
return Gen;
}
char readID(int IdS)
{
int IDS
cin>>IDS;
if(!strchr(IdS,IDS)) throw ErrorID();
return IDS;
}


void main()
{
char ch;
int i, *a, *sort_id, s;
int select, result;
Staff obj, *ptr, *p;
do

{
clrscr();
cout<<"Press 1.Go to insert N staff.\n";
cout<<"Press 2.Go to display all staff information.\n";
cout<<"Press 3.Go to search any staff information as ID or Name.\n";
cout<<"Press 4.Go to sort staff information as ID or Amount.\n";
cout<<"Press 5.Go to update any staff information.\n";
cout<<"Press 6.Go to diplay max of amount of each staff.\n";
cout<<"Press 7.Go to diplay total of amount.\n";
cout<<"Press 8.Exit the program.\n";
cin>>select;
switch(select)
{
case 1:
clrscr();
int n;
cout<<"Enter n:"; cin>>n;
p=new Staff[n];
//for(i=0; i<n; i++)
// cin>>p[i]; //p[i].input();
do
{
try
{
//for(i=0; i<n; i++)
//cin>>p[i];
obj.
cout<<"Your Sex: ";
obj.getSex()==read("fFmM");
}
catch (Error d)
 {
cout<<d.message()<<endl;
 }
cout<<"Press Y to continues !";
cin.clear(); cin.seekg(0,ios::end);
char ch;
ch=getch();
}while(ch!='y'); break;

case 2:
clrscr();
cout<<"Display Staff Information:";
display(p, n);
break;

case 3:
clrscr();
cout<<"Press 1.Go to search by ID.\n";
cout<<"Press 2.Go to search by Name.";
cin>>select;
switch(select)
{

case 1:
//Template Search ID====
bingo:
cout<<"Search Staff ID:";
display(p, n);
cout<<endl;

int key;
a=new int[n];
for(i=0; i<n; i++)
a[i]=p[i].getID();

cout<<"Enter staffID you want to search="; cin>>key;
result=search(a,n,key);
if(result==-1)
cout<<"Search not found.\n";
else
cout<<p[result];

if(result==-1)
{
goto bingo;
}
break;

case 2:
gogo:
clrscr();
 //Template Search Name===
cout<<"Search Staff Name:";
display(p, n);
cout<<endl;

char s_key[30];
cout<<"Enter name you want to search=";
cin.clear(); cin.seekg(0,ios::end); cin.get(s_key,30);
result=search(p,n,s_key);
if(result==-1)
cout<<"Search not found";
else
cout<<p[result];

if(result==-1)
{
goto gogo;
}
break;
}break;

case 4:
clrscr();
cout<<"Press 1.Go to sort by ID.\n";
cout<<"Press 2.Go to sort by Amount.";
cin>>select;
switch(select)
{
case 1:
cout<<"Before Sort Staff ID:";
display(p, n);
cout<<endl;

cout<<"\nAfter soft data ID";
sort_id=new int[n];
for(i=0;i<n;i++)
sort_id[i]=p[i].getID();
sort(sort_id,n,p);
display(p, n);
break;

case 2:
cout<<"Before Sort Staff Amount:";
display(p, n);
cout<<endl;

cout<<"\nAfter Sort Staff Amount:";
sort(p,n,p);
display(p, n);
break;
}break;

case 5:
update(p,n);
cout<<"\nShow all data after update:";
display(p, n);
break;
case 6:
cout<<"Show all staff information:";
display(p, n);
cout<<endl;
cout<<"\nMax of staff amount is:";
obj.printHeading();
ptr=max(p,n);
ptr->output();
break;

case 7:
 cout<<"Show all staff information:";
 display(p, n);
 cout<<endl;
 cout<<"Show all Staff Amount:";
 s=total(p,n);
 cout<<"\t\t\t\t\t  Total="<<s<<endl;
 break;

case 8:
delete[] p;
exit(0);
break;
}
cout<<"\nPress Y, if you want to continue or press any key to exit!";
ch=getch();
}while(ch=='y');
}*/

//Click like and share it to your friends if u like this post ..!

Sample Operator_Overloading in C++

Let see the following code :
====================

#include<iostream.h>
#include<math.h>
class Rectangle{
private:
float l,w;
public:
Rectangle(float l1,float w1):l(l1),w(w1){}
void output();
float area(){return (l*w);}
Rectangle operator=(float s);
Rectangle operator+(Rectangle p);
float operator-(Rectangle p);
int operator>(Rectangle p);
int operator==(Rectangle p);
float operator*();
float operator!();
};
void Rectangle::output(){
cout<<l<<"\t"<<w<<"\t"<<area()<<endl;
}

Rectangle Rectangle::operator=(float s){
w=sqrt(s/2);
l=2*w;
return *this;

//Or other ways that use return Constructor
/*float w1,l1;
w1=sqrt(s/2);
l1=2*w1;
return Rectangle(l1,w1);*/
}

Rectangle Rectangle::operator+(Rectangle p){
//you want to get new object
  float w1,l1;
l1=l+p.l; //l & w refer to own object and "p." refer to other object
w1=w+p.w;
return Rectangle(l1,w1);

//You want ot Update the same object
/* l=l+p.l;
w=w+p.w;
return *this;*/
}

float Rectangle::operator-(Rectangle p){
return fabs(area()-p.area());
}
int Rectangle::operator>(Rectangle p){
return (area()>p.area());
}
int Rectangle::operator==(Rectangle p){
return (area()==p.area());
}
float Rectangle::operator*(){ return area();}
float Rectangle::operator!(){
float d;
d=sqrt(l*l+w*w);
return d;
}
void main(){
Rectangle P1(5.6,3.2),P2(5.2,3.0),P3(9.2,7.0);
cout<<"Length\tWidth\tArea\n";
P1.output();
P2.output();
P3.output();

cout<<"\n\nUse Operator Funtion\n";
P1=72.0; P1.output();
P3=P1+P2; P3.output(); //or (P1+P2).output();
//show the sam object P1.output();

float s1,s2,s3;
s1=P1-P2; cout<<"S1:"<<s1<<endl;
s2=*P1; cout<<"\nArea of P1:\t"<<s2<<endl;
s3=!P1; cout<<"\nDiagonal of P1:\t"<<s3<<endl;
if(P1>P2) cout<<"\nArea of P1>P2"<<endl;
else if (P1==P2) cout<<"\nArea of P1=P2"<<endl;
else cout<<"\nArea of p1<p2"<<endl;
}
//Leave us a command and share it to ur friends ..!