You are on page 1of 2

#include<iostream.

h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
fstream f;
char text[50],ch;
int n,r,i,k=0,j;
f.open("comp.txt",ios::out|ios::in|ios::trunc);
l:
cout<<"\nEnter the choice:\n1.Compression\n2.Decompression\n";
cin>>n;
switch(n)
{
case 1:
{
cout<<"Enter data (Press $ to end input):\n";
cin>>text;
i=0;
while((ch=text[i])!='$')
{
f<<ch;
j=i;
while(text[++i]==ch);
f<<(i-j);
}
cout<<"\nThe compressed data:\n";
k=f.tellp();
i=0;
f.seekg(0);
while(i<k)
{
f>>ch;
f>>j;
cout<<ch<<j;
i+=2;
}
break;
}
case 2:
{
cout<<"\nThe Decompressed data:\n";
f.seekg(0);
i=0;
while(i<k)
{
f>>ch;
f>>j;
while(j--)
cout<<ch;
i+=2;
}
break;
}
}
cout<<"\nHit 1 to continue:";
cin>>r;
if(r==1)

goto l;
f.close();
getch();
}

You might also like