#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.H>
//*********************************************
struct student
{
unsigned long int stdno;
char name[16];
char family[31];
unsigned long int sn;
char dob [9];
int startyear;
char major[40];
};
//*********************************************
struct classroom
{
struct student students[100];
int num;
}myclass;
//*********************************************
char menu(int c);
void enter(struct classroom &std);
void printlist(struct classroom &std);
void sorta(struct classroom &std);
void sortd(struct classroom &std);
void display(struct classroom std,char ch);
int linearsearch(struct classroom std,char esm[]);
int binarysearch(struct classroom std,char esm[]);
void edit(struct classroom &std);
void del(struct classroom &std);
//*********************************************
void main()
{
myclass.num=0;
char ch,esm;
while(1)
{
ch=menu(myclass.num) ;
switch(ch)
{
case 'E' :
case 'e' :
enter(myclass);
break ;
case 'b' :
case 'B' :
display(myclass,'b');
break ;
case 'L' :
case 'l' :
display(myclass,'l');
break ;
case 'A' :
case 'a' :
sorta(myclass);
break ;
case 'D' :
case 'd' :
sortd(myclass);
break ;
case 'P' :
case 'p' :
printlist(myclass);
break ;
case 'I':
case 'i':
edit(myclass);
break;
case'T':
case't':
del(myclass);
break;
case 'x' :
case 'X' :
case 27 :
exit(0) ;
break ;
default :
putch(7);
}
}
}
//*********************************************
char menu(int c)
{
char ch ;
clrscr();
cout << " Number of Entered Names : " << c ;
cout << "\n ==================================" ;
cout << "\n Enter information of student " ;
cout << "\n sort Ascending" ;
cout << "\n sort Descending " ;
cout << "\n Print list" ;
cout << "\n eXit" ;
cout << "\n Linearsearch";
cout << "\n Binarysearch";
cout << "\n edIt ";
cout << "\n deleTe ";
gotoxy(2,3) ;
ch=getch();
clrscr();
return ch ;
}
//*********************************************
void enter(struct classroom &std)
{
if(std.num>=100)
{
char temp[100];
cout<<"\n your number is high.";
return ;
}
cout<<"\n name: nasim";
cout<<"\n family: momenzade";
cout<<"\n std no: 12";
cout<<"\n zip code: 45";
gotoxy(13,2);
gets(std.students[std.num].name);
gotoxy(13,3);
gets(std.students[std.num].family);
gotoxy(13,4);
cin>>std.students[std.num].stdno;
gotoxy(13,5);
cin>>std.students[std.num].sn;
std.num++;
}
//*********************************************
void printlist(struct classroom &std)
{
cout << " Row student " ;
cout << "\n === ================================== " ;
for(int i=0;i<std.num;i++)
{
printf("\n %3d %s %s ",i+1 ,std.students[i].name,std.students[i].family);
printf("\t student number: %ld ",std.students[i].stdno);
printf("\t zip code: %ld ",std.students[i].sn);
}
cout << "\n\n Press any key to go back ." ;
getch() ;
}
//*********************************************
void display(struct classroom std,char ch)
{
int i;
char esm[30];
cout << "\n Enter a Name for search: " ;
gets(esm);
if(ch=='l')
i=linearsearch(std,esm);
if(ch=='b')
i=binarysearch(std,esm);
if(i==-1)
cout<<"not found" ;
else
cout<<"\n found in {"<<i<<"}block.";
getch();
}
//*********************************************
int linearsearch(struct classroom std,char esm[])
{
for(int i=0;i<std.num;i++)
if(!strcmp(std.students[i].name,esm))
return i;
return -1;
}
//*********************************************
int binarysearch(struct classroom std,char esm[])
{
sorta(std);
//sortd(std);
int mid,low=0,high=std.num-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(esm,std.students[mid].name)<0)
high=mid-1;
else if(strcmp(esm,std.students[mid].name)>0)
low=mid+1;
else
return mid;
}
return -1;
}
//*********************************************
void sorta(struct classroom &std)
{
struct student temp;
for(int i=0 ;i<(std.num-1);i++)
for(int j=i+1 ; j<(std.num);j++)
if(strcmpi(std.students[i].family,std.students[j].family)>0)
{
temp=std.students[i];
std.students[i]=std.students[j];
std.students[j]=temp;
}
}
//*********************************************
void sortd(struct classroom &std)
{
struct student temp;
for(int i=0 ;i<(std.num-1);i++)
for(int j=i+1; j<(std.num);j++)
if(strcmpi(std.students[i].family,std.students[j].family)<0)
{
temp=std.students[i];
std.students[i]=std.students[j];
std.students[j]=temp;
}
}
//*********************************************
void edit(struct classroom &std)
{
int i;
long int x,y;
char esm[100],esm2[100];
cout<<"\n enter a name for edit:";
gets(esm);
i=linearsearch(std,esm);
if(i>=0)
{
cout<<"\n row="<<i+1;
cout<<"\n enter a new name:";
gets(esm2);
strcpy(std.students[i].name,esm2);
cout<<"\n enter a new family:";
gets(esm2);
strcpy(std.students[i].family,esm2);
cout<<"\n enter a student number:";
cin>>x;
std.students[i],std.num=x;
cout<<"\n enter a zip code:";
cin>>y;
std.students[i].sn=y;
}
else
cout<<"\n not found." ;
getch();
}
//*********************************************
void del(struct classroom &std)
{
int i;
char esm[100];
cout<<"\n enter a name for delete:";
gets(esm);
i=linearsearch(std,esm);
if(i>=0)
{
for(int j=i;j<std.num;j++)
std.students[j]=std.students[j+1];
std.num--;
cout<<"\b";
}
else
cout<<"\n not found.";
getch();
}