#include<stdio.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include <iostream.h>
#define MAX 128
char dictionary[MAX][10];
int dictionarylen=-1;
bool finddict(char str[])
{
if(str[1]=='\0' && str[0]<128)
return 1;
for(int i=0;i<=dictionarylen;i++)
{
if(!strcmp(str,dictionary[i]))
return 1;
}
return 0;
}
void adddict(char str[],char ch)
{
int i;
for(int i=0;i<strlen(str);i++)
cout<<"\n'"<<str[i]<<"'\t'"<<str[i]<<"'("<<(int)str[i]<<")";
if(ch == ' ')
cout<<"\n' '";
else
cout<<"\nEOF";
dictionarylen++;
strcpy(dictionary[dictionarylen],str);
cout<<"\t\t\t\t'"<<dictionary[dictionarylen]<<"'("<<dictionarylen+128<<")";
}
int findindex(char str[])
{
if(strlen(str)==1 && str[0]<128)
return int(str[0]);
for(int i=0;i<=dictionarylen;i++)
{
if(!strcmp(str,dictionary[i]))
return i+128;
}
}
int main()
{
char ch;
char str[10];
int i,indexloc=-1;
cout<<"The Sender Dictionary Has 127 ASCII characters initially\nReading Text from .txt file";
FILE *fp;
fp=fopen("input.txt","r");
cout<<"\nRead\tTransmitted(Code)\tExtended Dictionary";
cout<<"\n----------------------------------------------------";
while(ch!=EOF)
{
i=0;
while(1)
{
ch=fgetc(fp);
if(ch==' ' || ch==EOF)
break;
str[i]=ch;
i++;
}
str[i]='\0';
if(!finddict(str))
{
adddict(str,ch);
}
else
{
indexloc=findindex(str);
for(int i=0;i<strlen(str);i++)
cout<<"\n'"<<str[i]<<"'";
if(ch == ' ')
cout<<"\n' '";
else
cout<<"\nEOF";
cout<<"\t"<<str<<"("<<indexloc<<")";
}
}
fclose(fp);
getch();
return 0;
}
No comments:
Post a Comment