Monday, May 16, 2016

the program to store the data in a data file. Give appropriate options for data manipulations.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<fstream.h>

class node
{
int info;
node *next,*TEMP,*TEMP1,*FIRST;
public:
node()
{
info=0;
FIRST=NULL;
}
void operator +(int x)
{
TEMP=new node();
TEMP->info=x;
TEMP->next=NULL;
if(FIRST==NULL)
FIRST=TEMP;
else
{
TEMP1=FIRST;
if(TEMP1->info >=x)
{
TEMP->next=FIRST;
FIRST=TEMP;
}
else
{
TEMP1=FIRST;
while(TEMP1->next!=NULL && TEMP1->next->info<=x)
TEMP1=TEMP1->next;
TEMP->next=TEMP1->next;
TEMP1->next=TEMP;
}
}
}
void write()
{
TEMP=FIRST;
ofstream wr("data.txt");
do
{
wr<<TEMP<<"   :   "<<TEMP->info<<endl;
TEMP=TEMP->next;
}while(TEMP!=NULL);
}
void read()
{
char w;
ifstream read("data.txt");
while(read)
{
read.get(w);
cout<<w;
}
cout<<endl;
}
};
void main()
{
clrscr();

int ch;
node n;
do
{
cout<<"1.write in file\n2.Read from file\n0.Exit"<<endl;
cout<<"Enter your choice = ";
cin>>ch;
switch(ch)
{
case 1:
char ch1;
int x;
while(ch1!='n')
{
cout<<"Enter data = ";
cin>>x;
n+x;
cout<<"Do u want to insert again  (y/n) ";
cin>>ch1;
}
n.write();
cout<<"Write successfully..."<<endl;
break;
case 2:
cout<<"\nAddress\t\t\Data\n--------------------\n";
n.read();
break;
case 0:
cout<<"\nPress Any Key to Exit...";
break;
default:
cout<<"invalid input"<<endl;
break;
}
}while(ch!=0);

getch();

}

No comments:

Post a Comment