#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
int n;
void creat();
void display();
void sort();
void search();
class second
{
public:
char bname[10];
int relno;
void putdata1()
{
cout<<bname<<"\t"<<relno<<endl;
}
};
class student
{
public:
char name[10],author[10];
char accno[10],price[10];
void getdata()
{
cout<<"enter book name:"<<endl;
gets(name);
cout<<"enter book author:"<<endl;
gets(author);
cout<<"enter book account number:"<<endl;
cin>>accno;
cout<<"enter student book price:"<<endl;
cin>>price;
}
void putdata()
{
cout<<name<<"\t"<<author<<"\t"<<accno<<"\t"<<price<<"\n";
}
};
void main()
{
int ch;
clrscr();
while(1)
{
cout<<"MENU"<<endl;
cout<<"1.CREAT"<<endl;
cout<<"2.sort"<<endl;
cout<<"3.SEARCH"<<endl;
cout<<"4.DISPLAY"<<endl;
cout<<"5.EXIT"<<endl;
cout<<"enter ur choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:creat();break;
case 2:sort();break;
case 3:search();break;
case 4:display();break;
case 5:exit(1);
default:cout<<"INVALId CHOICE"<<endl;
}
}
}
void creat()
{
student s;
second r;
int rrn=0;
fstream f;
f.open("secondry.indx",ios::out);
fstream f1;
f1.open("relat.txt",ios::out);
cout<<"enter how many records do u want:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
s.getdata();
strcpy(r.bname,s.name);
r.relno=rrn;
rrn++;
f.write((char *)&s,sizeof(s));
f1.write((char *)&r,sizeof(r));
}
f.close();
f1.close();
}
void display()
{
student s;
second r;
fstream f;
f.open("secondry.indx",ios::in);
cout<<"the data file contents are:"<<endl;
while(f.read((char *)&s,sizeof(s)))
{
s.putdata();
}
f.close();
fstream f1;
f1.open("relat.txt",ios::in);
cout<<"the secondary index file deails are:"<<endl;
while(f1.read((char *)&r,sizeof(r)))
{
r.putdata1();
}
f1.close();
}
void sort()
{
second x,y;
fstream f1;
f1.open("relat.txt",ios::in|ios::out);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
f1.seekg(i*sizeof(x),ios::beg);
f1.read((char *)&x,sizeof(x));
f1.seekg(j*sizeof(y),ios::beg);
f1.read((char *)&y,sizeof(y));
if(strcmp(x.bname,y.bname)>0)
{
f1.seekp(i*sizeof(y));
f1.write((char *)&y,sizeof(y));
f1.seekp(j*sizeof(x));
f1.write((char *)&x,sizeof(x));
}
}
}
f1.close();
}
void search()
{
int cnt=0;
char key[10];
fstream f,f1;
second x;
student s;
f.open("secondry.indx",ios::out|ios::in);
f1.open("relat.txt",ios::in|ios::out);
cout<<"enter the key:"<<endl;
cin>>key;
while(f1.read((char *)&x,sizeof(x)))
{
if(strcmp(key,x.bname)==0)
{
f.seekg((x.relno)*sizeof(s),ios::beg);
f.read((char *)&s,sizeof(s));
s.putdata();
cnt++;
}
}
if(cnt<=0)
cout<<"search is not valid:"<<endl;
f.close();
f1.close();
}
#include<stdio.h>
#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
int n;
void creat();
void display();
void sort();
void search();
class second
{
public:
char bname[10];
int relno;
void putdata1()
{
cout<<bname<<"\t"<<relno<<endl;
}
};
class student
{
public:
char name[10],author[10];
char accno[10],price[10];
void getdata()
{
cout<<"enter book name:"<<endl;
gets(name);
cout<<"enter book author:"<<endl;
gets(author);
cout<<"enter book account number:"<<endl;
cin>>accno;
cout<<"enter student book price:"<<endl;
cin>>price;
}
void putdata()
{
cout<<name<<"\t"<<author<<"\t"<<accno<<"\t"<<price<<"\n";
}
};
void main()
{
int ch;
clrscr();
while(1)
{
cout<<"MENU"<<endl;
cout<<"1.CREAT"<<endl;
cout<<"2.sort"<<endl;
cout<<"3.SEARCH"<<endl;
cout<<"4.DISPLAY"<<endl;
cout<<"5.EXIT"<<endl;
cout<<"enter ur choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:creat();break;
case 2:sort();break;
case 3:search();break;
case 4:display();break;
case 5:exit(1);
default:cout<<"INVALId CHOICE"<<endl;
}
}
}
void creat()
{
student s;
second r;
int rrn=0;
fstream f;
f.open("secondry.indx",ios::out);
fstream f1;
f1.open("relat.txt",ios::out);
cout<<"enter how many records do u want:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
s.getdata();
strcpy(r.bname,s.name);
r.relno=rrn;
rrn++;
f.write((char *)&s,sizeof(s));
f1.write((char *)&r,sizeof(r));
}
f.close();
f1.close();
}
void display()
{
student s;
second r;
fstream f;
f.open("secondry.indx",ios::in);
cout<<"the data file contents are:"<<endl;
while(f.read((char *)&s,sizeof(s)))
{
s.putdata();
}
f.close();
fstream f1;
f1.open("relat.txt",ios::in);
cout<<"the secondary index file deails are:"<<endl;
while(f1.read((char *)&r,sizeof(r)))
{
r.putdata1();
}
f1.close();
}
void sort()
{
second x,y;
fstream f1;
f1.open("relat.txt",ios::in|ios::out);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
f1.seekg(i*sizeof(x),ios::beg);
f1.read((char *)&x,sizeof(x));
f1.seekg(j*sizeof(y),ios::beg);
f1.read((char *)&y,sizeof(y));
if(strcmp(x.bname,y.bname)>0)
{
f1.seekp(i*sizeof(y));
f1.write((char *)&y,sizeof(y));
f1.seekp(j*sizeof(x));
f1.write((char *)&x,sizeof(x));
}
}
}
f1.close();
}
void search()
{
int cnt=0;
char key[10];
fstream f,f1;
second x;
student s;
f.open("secondry.indx",ios::out|ios::in);
f1.open("relat.txt",ios::in|ios::out);
cout<<"enter the key:"<<endl;
cin>>key;
while(f1.read((char *)&x,sizeof(x)))
{
if(strcmp(key,x.bname)==0)
{
f.seekg((x.relno)*sizeof(s),ios::beg);
f.read((char *)&s,sizeof(s));
s.putdata();
cnt++;
}
}
if(cnt<=0)
cout<<"search is not valid:"<<endl;
f.close();
f1.close();
}
No comments:
Post a Comment