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 ..!
0 comments:
Post a Comment