Stucture - Array + One Pointer
Stucture - Array + One Pointer
Student:
#include<iostream>
using namespace std;
#include<string.h>
struct Student
{
int roll;
char name[20];
float marks;
int getRoll()
{
return this -> roll;
}
char* getName()
{
return this -> name;
}
float getMarks()
{
return this -> marks;
}
// display function
Student* stud;
stud=new Student();
stud->display();
stud=new Student(17,"DANNY",95);
stud->display();
int roll,mks;
int i,n;
char name[30];
//store
stud=new Student[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter roll no.of a student :";
cin>>roll;
stud[i].setRoll(roll);
//display
for(i=0;i<n;i++)
{
stud[i].display();
cout<<"\n";
}
//search
int result;
cout<<"\nEnter the search student name:";
cin>>name;
result=search(stud,n,name);
if(result==1)
{
cout<<"Student Name Found.\n";
}
else
{
cout<<"Student Name Not Found.\n";
}
}
int search(Student*stud,int n,char*name) //non member function
{
int flag=0,i,result;
for(i=0;i<n;i++)
{
result=strcmp(stud[i].getName(),name);
if(result==0)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//2.Employee:
#include<iostream>
using namespace std;
#include<string.h>
struct Employee
{
int id;
char name[20];
double salary;
//setter function
//getter function
int getId()
{
return this -> id;
}
char * getName()
{
return this -> name;
}
double getSalary()
{
return this -> salary;
}
//display function
int id;
char name[30];
double salary;
int i,n;
//store
emp=new Employee[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter employee id:";
cin>>id;
emp[i].setId(id);
//display
for(i=0;i<n;i++)
{
emp[i].display();
cout<<"\n";
}
//search
int result;
cout<<"Enter ID to be search:";
cin>>n;
result=search(emp,n,id);
if(result==1)
{
cout<<"ID found";
}
else
{
cout<<"ID not found";
}
}
int search(Employee*emp,int n,int id) //Non Member Function
{
int flag=0,i;
for(i=0;i<n;i++)
{
if(emp[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//3 Admin
#include<iostream>
using namespace std;
#include<string.h>
struct Admin
{
int id;
char name[20];
double salary,allounce;
// setter function
// getter function
int getId()
{
return this->id;
}
char* getName()
{
return this -> name;
}
double getSalary()
{
return this->salary;
}
double getAllounce()
{
return this->allounce;
}
// display function
int id;
char name[30];
double salary,allounce;
int i,n;
//store
ad=new Admin[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter admin id :";
cin>>id;
ad[i].setId(id);
//display
for(i=0;i<n;i++)
{
ad[i].display();
cout<<"\n";
}
//search
int result;
cout<<"\nEnter ID to be search :";
cin>>n;
result=search(ad,n,id);
if(result==1)
{
cout<<"ID found";
}
else
{
cout<<"ID not found";
}
}
for(i=0;i<n;i++)
{
if(ad[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//4. HR Manager:
#include<iostream>
using namespace std;
#include<string.h>
struct Hr_manager
{
int id;
char name[20];
double salary,commision;
//setter function
//getter function
int getId()
{
return this -> id;
}
char * getName()
{
return this -> name;
}
double getSalary()
{
return this -> salary;
}
double getCommision()
{
return this -> commision;
}
//display function
int id,target;
char name[30];
double salary,commision;
int i,n;
//store
hr=new Hr_manager[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter id of hr manager:";
cin>>id;
hr[i].setId(id);
//display
for(i=0;i<n;i++)
{
hr[i].display();
cout<<"\n";
}
//search
int result;
cout<<"\nEnter ID to be search :";
cin>>n;
result=search(hr,n,id);
if(result!=-1)
{
cout<<"ID found";
}
else
{
cout<<"ID not found";
}
}
for(i=0;i<n;i++)
{
if(hr[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//5.Sales Manager:
#include<iostream>
using namespace std;
#include<string.h>
struct Salesmanager
{
int id;
char name[20];
double target,salary,incentive;
//setter function
//getter function
int getId()
{
return this ->id;
}
char * getName()
{
return this -> name;
}
double getTarget()
{
return this -> target;
}
double getSalary()
{
return this -> salary;
}
double getIncentive()
{
return this -> incentive;
}
//display function
int id,target;
char name[30];
double salary,incentive;
int i,n;
sm=new Salesmanager[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter id of sales manager:";
cin>>id;
sm[i].setId(id);
//display
for(i=0;i<n;i++)
{
sm[i].display();
cout<<"\n";
}
//search
int result;
cout<<"\nEnter ID to be search :";
cin>>n;
result=search(sm,n,id);
if(result!=-1)
{
cout<<"ID found";
}
else
{
cout<<"ID Not found";
}
}
for(i=0;i<n;i++)
{
if(sm[i].getId()==id)
{
flag=1;
return 1;
}
}
if(flag==0)
return 0;
}
//6.Date:
#include<iostream>
using namespace std;
#include<string.h>
struct Date
{
int day,month,year;
//setter function
//getter function
int getDay()
{
return this -> day;
}
int getMonth()
{
return this -> month;
}
int getYear()
{
return this -> year;
}
//display function
int da,mo,yr;
int i,n;
dt = new Date[n];
//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Day :";
cin >> da;
dt[i].setDay(da);
//display
for(i=0;i<n;i++)
{
cout << dt[i].getDay() << "/" << dt[i].getMonth() << "/" <<dt[i].getYear() << "\n";
//search
Date *d1;
d1 = new Date;
cout << "Enter Date You Want to search :\n";
cout << "Day :";
cin >> da;
d1->setDay(da);
cout << "Month :";
cin >> mo;
d1->setMonth(mo);
cout << "Year :";
cin >> yr;
d1->setYear(yr);
int result;
result = search(dt,n,d1);
if(result==-1)
{
cout << "\nDate Not Found\n";
}
else
{
cout << "\nDate Found\n";
d1[result].display();
}
return 0;
}
for(i=0;i<n;i++)
{
if((dt[i].day == d1->getDay()) && (dt[i].month == d1->getMonth()) && (dt[i].year ==
d1->getYear()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}
//7.Time:
#include<iostream>
using namespace std;
struct Time
{
int hr,min,sec;
Time()//Default Constructor
{
cout << "**Default constructor**\n\n";
this -> hr=0;
this -> min=0;
this -> sec=0;
}
Time(int h,int m, int s)//parameterised Constructor
{
cout << "\n\n**Parameterised constructor**\n\n";
this -> hr=h;
this -> min=m;
this -> sec=s;
}
//setter function
void setHr(int h)
{
this->hr = h;
}
void setMin(int m)
{
this->min = m;
}
void setSec(int s)
{
this->sec = s;
}
//getter function
int getHr()
{
return this -> hr;
}
int getMin()
{
return this -> min;
}
int getSec()
{
return this -> sec;
}
//display function
int hr,min,sec;
int i,n;
ti = new Time[n];
//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Hours :";
cin >> hr ;
ti[i].setHr(hr);
//display
for(i=0;i<n;i++)
{
cout << ti[i].getHr() << ":" << ti[i].getMin() << ":" << ti[i].getSec() << "\n";
//search
Time * t1;
t1 = new Time;
cout << "Enter Time You Want to search :\n";
cout << "Hours :";
cin >> hr;
t1->setHr(hr);
cout << "Minutes :";
cin >> min;
t1->setMin(min);
cout << "Seconds :";
cin >> sec;
t1->setSec(sec);
int result;
result = search(ti,n,t1);
if(result==-1)
{
cout << "\nTime Not Found\n";
}
else
{
cout << "\nTime Found\n";
t1[result].display();
}
return 0;
}
for(i=0;i<n;i++)
{
if((ti[i].hr == t1->getHr()) && (ti[i].min == t1->getMin()) && (ti[i].sec == t1->getSec()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}
//8.Distance:
#include<iostream>
using namespace std;
#include<string.h>
struct Distance
{
int feet,inch;
//setter function
//getter function
int getFeet()
{
return this -> feet;
}
int getInch()
{
return this -> inch;
}
//display function
int feet,inch;
int i,n;
ds = new Distance[n];
//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Feet :";
cin >> feet;
ds[i].setFeet(feet);
//display
for(i=0;i<n;i++)
{
cout << "Feet :" << ds[i].getFeet() << "\n" ;
cout << "Inches :" << ds[i].getInch() << "\n\n";
}
//search
Distance *ds1;
ds1 = new Distance;
cout << "Enter Distance You Want to search :\n";
cout << "Feet :";
cin >> feet;
ds1->setFeet(feet);
cout << "Inchs :";
cin >> inch;
ds1->setInch(inch);
int result;
result = search(ds, n, ds1);
if(result==-1)
{
cout << "\n\nDistance Not Found\n";
}
else
{
cout << "\n\nDistance Found :\n";
ds1[result].display();
}
return 0;
}
for(i=0;i<n;i++)
{
if((ds[i].feet == ds1->getFeet()) && (ds[i].inch == ds1->getInch()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}
//9.Complex:
#include<iostream>
using namespace std;
#include<string.h>
struct Complex
{
int real,imag;
int getReal()
{
return this -> real;
}
int getImag()
{
return this -> imag;
}
int real,imag;
int i,n;
cp = new Complex[n];
//Store
for(i=0;i<n;i++)
{
cout << "\nEnter Real no :";
cin >> real;
cp[i].setReal(real);
//display
for(i=0;i<n;i++)
{
cout << cp[i].getReal() << "+" << cp[i].getImag() << "i" << "\n";
//search
Complex *c1;
c1 = new Complex;
cout << "Enter Complex number You Want to search :\n";
cout << "Real no :";
cin >> real;
c1->setReal(real);
cout << "Imaginery no :";
cin >> imag;
c1->setImag(imag);
int result;
result = search(cp, n, c1);
if(result==-1)
{
cout << "\nComplex number Not Found\n";
}
else
{
cout << "\nComplex number Found :\n";
c1[result].display();
}
return 0;
}
for(i=0;i<n;i++)
{
if((cp[i].real == c1->getReal()) && (cp[i].imag == c1->getImag()))
{
flag = 0;
break;
}
else
{
flag = -1;
}
}
return flag;
}