Вы находитесь на странице: 1из 18

Program 1: File Comparison #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> int main() { FILE *f1,*f2; char str1[100],str2[100]; f1=fopen("file1.txt","r"); f2=fopen("file2.

txt","r"); fgets(str1,100,f1); fgets(str2,100,f2); if(strcmp(str1,str2)==0) printf("EQUAL"); else printf("NOT EQUAL"); fclose(f1); fclose(f2); getch(); return 0; } /* FILE1: Hi. My name is Sakshi. FILE2: Hi. My name is Daksh. OUTPUT NOT EQUAL*/

Program 2: RUN LENGTH ENCODING #include<stdio.h> #include<conio.h> #include<string.h> char str[50]; void encode() { int i,j,cnt,l,count[50]={0}; l = strlen(str); for(i=0;i< l;i*=1) { j = 0; count[i] = 1; do { j++; if(str[i+j] == str[i]) count[i]++; }while(str[i+j]==str[i]); if(count[i]<=3) printf("%c",str[i++]); else { printf("!%d%c",count[i],str[i]); i += count[i]; } } } void decode() { int l, count; l=strlen(str); for(int i=0;i<l;i*=1) { if( str[i]=='!') { count= str[i+1]-48; for( int j=0; j<count; j++) printf("%c",str[i+2]); i=i+3; } else { printf("%c",str[i]); i=i+1; } } } int main() { int choice; printf("MENU\n1. for encoding \n2. for decoding\nenter your choice...."); scanf( "%d", &choice); if (choice==1)

{ printf("\nEnter the string: "); scanf("%s",str); printf("\nOriginal String is: %s",str); printf("\nEncoded String is: "); encode(); } else { printf("\nEnter the string: "); scanf("%s",str); printf("\nEncoded String is: %s",str); printf("\nDecoded String is: "); decode(); } getch(); return 0; } /*OUTPUT MENU 1. for encoding 2. for decoding enter your choice....1 Enter the string: dddddddffffffjjkkkkkl Original String is: dddddddffffffjjkkkkkl Encoded String is: !7d!6fjj!5kl

MENU 1. for encoding 2. for decoding enter your choice....!5g!4fssss Enter the string: Encoded String is: !5g!4fssss Decoded String is: gggggffffssss*

Program 3: Huffman encoding #include <iostream> #include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> #define MAX 10 using namespace std; int finalnum=-1; struct link { int freq; char ch[MAX]; struct link* right; struct link* left; }; struct finalcode { char alpha; int code[MAX]; int codelen; }final[MAX]; typedef struct link node; node* create(char a[], int x) { node* ptr; ptr = (node *) malloc(sizeof(node)); ptr->freq = x; strcpy( ptr->ch , a); ptr->right = ptr->left = NULL; return(ptr); } void sort(node* a[], int n) { int i, j; node* temp; for (i = 0; i < n - 1; i++) for (j = i; j < n; j++) if (a[i]->freq > a[j]->freq) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } void sright(node* a[], int n) { int i; for (i = 1; i < n - 1; i++) a[i] = a[i + 1]; } void Assign_Code(node* tree, int c[], int n) { int i;

if ((tree->left == NULL) && (tree->right == NULL)) { finalnum++; printf("%s code:", tree->ch); for (i = 0; i < n; i++) { final[finalnum].code[i]=c[i]; printf("%d", c[i]); } final[finalnum].codelen=i; final[finalnum].alpha=tree->ch[0]; printf("\n"); } else { c[n] = 1; n++; Assign_Code(tree->left, c, n); c[n - 1] = 0; Assign_Code(tree->right, c, n); } } void encode() { char str[MAX]; cout<<"Enter A Statement:"; cin>>str; cout<<"\nThe Encoded String Is :"; for(int i=0;i<strlen(str);i++) { for(int j=0;j<=finalnum;j++) { if(final[j].alpha==str[i]) for(int k=0;k<final[j].codelen;k++) cout<<final[j].code[k]; } } } void Delete_Tree(node * root) { if(root!=NULL) { Delete_Tree(root->left); Delete_Tree(root->right); free(root); } } int main() { node* ptr, * head; int i, n, total = 0, u, c[15]; char str[MAX]; node* a[12]; int freq; printf( "Huffman Algorithm\n"); printf("\nEnter the no. of letter to be coded:");

/*input the no. of letters*/ scanf("%d", &n); for (i = 0; i < n; i++) { printf("Enter the letter & frequency:"); /*input the letter & frequency*/ scanf("%s %d", str, &freq); a[i] = create(str, freq); } while (n > 1) { sort(a, n); u = a[0]->freq + a[1]->freq; strcpy(str,a[0]->ch); strcat(str,a[1]->ch); ptr = create(str, u); ptr->right = a[1]; ptr->left = a[0]; a[0] = ptr; sright(a, n); n--; } Assign_Code(a[0], c, 0); getch(); encode(); Delete_Tree(a[0]); return 0; }

OUTPUT:
Huffman Algorithm Enter the no. of letter to be coded:4 Enter the letter & frequency:a 10 Enter the letter & frequency:b 20 Enter the letter & frequency:c 15 Enter the letter & frequency:d 9 b code:1 c code:01 d code:001 a code:000

Program 4: ARITHMETIC CODING TECHNIQUE #include<conio.h> #include<stdio.h> #include <iostream.h> #include<string.h> #define MAX 3 struct code { char ch; float prob; float st; float end; }ARR[MAX]; void sort_data() { int i,j; struct code temp; for(i=0;i<MAX;i++) for(j=0;j<MAX-i-1;j++) { if(ARR[j].prob<ARR[j+1].prob) //sort in descending order of probability { temp=ARR[j]; ARR[j]=ARR[j+1]; ARR[j+1]=temp; }

} //sorting completes here ARR[0].st=0; ARR[0].end=ARR[0].prob; for(i=1;i<MAX;i++) { ARR[i].st=ARR[i-1].end; ARR[i].end=ARR[i].st+ARR[i].prob; } } void display() { int i,j; cout<<"\nCharacter\tProbabilty\t start Point \t End Point"; for(i=0;i<MAX;i++) { cout<<"\n"<<ARR[i].ch<<"\t"<<ARR[i].prob<<"\t\t"<<ARR[i].st<<"\t"<<ARR[i].end; }

} int main() { int i=0; int j=0,k;

struct code temp; char chtemp; char str[10]; //inp_data(); for(i=0;i<MAX;i++) { cout<<"Enter character - "; fflush(stdin); cin>>ARR[i].ch; cout<<"Enter probability - "; fflush(stdin); cin>>ARR[i].prob; //cout<<i<<"\n"; //getch(); //fflush(stdout); } fflush(stdin); cout<<"Input data successful....Loading "; fflush(stdin); sort_data(); cout<<"Sorting done "; fflush(stdin); display(); cout<<"\nEnter the string - "; gets(str); //fflush(stdin); i=0; int len; float tempprob = 1; len = strlen(str); //fflush(stdin); for(i=0;i<=len-1;i++) { cout<<"\n CAsE for i = "<<i<<"\n"; //fflush(stdin); chtemp = str[i]; for(j=0;j<MAX;j++) { if(ARR[j].ch == chtemp) { temp=ARR[j]; ARR[0].st=temp.st; tempprob = (tempprob * temp.prob );

///

fflush(stdin); ARR[0].end=(ARR[0].st + ((ARR[0].prob)*(tempprob))); for(k=1;k<MAX;k++) { ARR[k].st=ARR[k-1].end; ARR[k].end=(ARR[k].st+((ARR[k].prob)*(tempprob))); } ARR[MAX].end=temp.end; display();

} } } //fflush(stdin); cout<<"\n"; cout<<ARR[0].st<<"<=CODEWORD<="<<ARR[MAX].end; getch(); return 0; }

/*OUTPUT Enter character - a Enter probability - 0.5 Enter character - b Enter probability - 0.1 Enter character - c Enter probability - 0.3 Input data successful....Loading Sorting done Character Probabilty start Point End Point a 0.5 0 0.5 c 0.3 0.5 0.8 b 0.1 0.8 0.9 Enter the string - abbcbbaaaaaa CAsE for i = 0 Character Probabilty a 0.5 0 0.25 c 0.3 0.25 0.4 b 0.1 0.4 0.45 CAsE for i = 1 start Point End Point

Character Probabilty start Point a 0.5 0.4 0.425 c 0.3 0.425 0.44 b 0.1 0.44 0.445 CAsE for i = 2 Character Probabilty start Point a 0.5 0.44 0.4425 c 0.3 0.4425 0.444 b 0.1 0.444 0.4445 CAsE for i = 3 Character Probabilty start Point a 0.5 0.4425 0.44325 c 0.3 0.44325 0.4437 b 0.1 0.4437 0.44385 CAsE for i = 4 Character a 0.5 c 0.3 b 0.1 Probabilty start Point 0.4437 0.443775 0.443775 0.44382 0.44382 0.443835

End Point

End Point

End Point

End Point

CAsE for i = 5 Character Probabilty start Point a 0.5 0.44382 0.443828 c 0.3 0.443828 0.443832 b 0.1 0.443832 0.443834 CAsE for i = 6 Character Probabilty start Point a 0.5 0.44382 0.443824 c 0.3 0.443824 0.443826 b 0.1 0.443826 0.443827 CAsE for i = 7 Character Probabilty start Point a 0.5 0.44382 0.443822 c 0.3 0.443822 0.443823 b 0.1 0.443823 0.443823 CAsE for i = 8 Character Probabilty start Point a 0.5 0.44382 0.443821 c 0.3 0.443821 0.443822 b 0.1 0.443822 0.443822 CAsE for i = 9 Character Probabilty start Point a 0.5 0.44382 0.443821 c 0.3 0.443821 0.443821 b 0.1 0.443821 0.443821 CAsE for i = 10 Character Probabilty start Point a 0.5 0.44382 0.44382 c 0.3 0.44382 0.44382 b 0.1 0.44382 0.44382 CAsE for i = 11 Character Probabilty start Point a 0.5 0.44382 0.44382 c 0.3 0.44382 0.44382 b 0.1 0.44382 0.44382 0.44382<=CODEWORD<=0.44382*/ End Point

End Point

End Point

End Point

End Point

End Point

End Point

Program 5: DPCM #include<stdio.h> #include<conio.h> #define max 10 //global const int max=10; int orgarray[max]; int finalarray[max]={0}; int n; void dpcmencode(int orgarray[],int n) { int i,j,k=0,l; int temp=orgarray[0]; finalarray[k++]=temp; for(i=1;i<n;i++) { finalarray[k++]=orgarray[i]-orgarray[i-1]; } printf("\nThe compressed version is as follows :\n"); for(j=0;j<k;j++) { printf("%d\t",finalarray[j]); } // return finalarray; } void dpcmdecode(int tarray[],int n) { int newarray[max]; int temp=tarray[0]; int i,j,k=0; newarray[k++]=temp; for(i=0;i<n-1;i++) { newarray[k++]=temp+tarray[i+1]; temp= temp+tarray[i+1]; } printf("\nThe decoded version is as follows:\n"); for(j=0;j<k;j++) printf("\t%d",newarray[j]); }

int main() { int i; int ch,newch; printf("Enter the number of elements\t"); scanf("%d",&n);

do { printf("\nMENU"); printf("\n1.Encode dpcm"); printf("\n2.Decode dpcm"); printf("\nEnter your choice\t"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the data\n"); for(i=0;i<n;i++) scanf("%d",&orgarray[i]); dpcmencode(orgarray,n);break; case 2: printf("Enter the data\n"); for(i=0;i<n;i++) scanf("%d",&orgarray[i]); dpcmdecode(orgarray,n);break; default:printf("Wrong Choice"); } printf("\nDo you want to enter again\t"); scanf("%d",&newch); }while(newch==1); getch(); return 0; }

Program 6: LZW #include<stdio.h> #include<fstream.h> #include<string.h> #include<conio.h> #include <iostream> #define MAX 128 using namespace std; char dictionary[MAX][10]; int dictionarylen=-1; int array[MAX]; int arraylen=-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[]) { cout<<"\n-------------------------------------------------------------\nSending The Alphabets of '"<<str<<"'\n"; char ch; int i; while(1) { ch=str[i++]; if(ch=='\0') break; cout<<ch<<":"<<int(ch)<<"\n"; arraylen++; array[arraylen]=int(ch); } dictionarylen++; cout<<"Adding '"<<str<<"' to the Dictionary at Index "<< dictionarylen+128; strcpy(dictionary[dictionarylen],str); } 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,flag; float uncompressed=0.0; cout<<"The Sender Dictionary Has 127 ASCII characters initially"; cout<<"\nReading Text...\n\n"; FILE *fp; fp=fopen("input.txt","r"); while(ch!=EOF) { i=0; flag=0; while(1) { ch=fgetc(fp); if(ch==' ' || ch==EOF) { flag=1; break; } str[i]=ch; i++; uncompressed++; } str[i]='\0'; if(!finddict(str)) { adddict(str); } else { indexloc=findindex(str); cout<<"\n-------------------------------------------------------------\nWord '"<<str<<"' Found\n"; cout<<"Transmitting Index :"<<indexloc; arraylen++; array[arraylen]=indexloc; } } fclose(fp); cout<<"\n-------------------------------------------------------------\n"; cout<<"\n\nTHE DICTIONARY VALUES (ELIMINATING ASCII CHARACTERS) ARE :\n"; for(i=0;i<dictionarylen;i++) cout<<i+128<<" : "<<dictionary[i]<<"\n"; cout<<"\n-------------------------------------------------------------\n"; cout<<"\n\nTHE TRANSMETTED INDICES ARE :\n"; for(i=0;i<arraylen;i++) cout<<array[i]<<"\n"; cout<<"\n-------------------------------------------------------------\n"; cout<<"\nCOMPRESSED DATA IS OF LENGTH :"<<arraylen; cout<<"\nUNCOMPRESSED DATA IS OF LENGTH :"<<uncompressed; cout<<"\nCOMPRESSION RATIO IS :"<<uncompressed/float(arraylen); return 0; }

Program 7: HAAR WAVELET TRANSFORM imgdata=imread('testimage.png'); imgdata=rgb2gray(imgdata); figure, imshow(imgdata); [ht, wt]=size(imgdata); numlevel=1; htindex=1;

for level=1:numlevel for i=1:ht-1 htindex=2; for j=1:2:wt-1 htrnsr(i,htindex)=(imgdata(i,j)+imgdata(i,j+1))/2; htrnsr(i,round(htindex+wt/2))=imgdata(i,j)-imgdata(i,j+1); htindex=htindex+1; end end figure, imshow(htrnsr, []); htindex=1; for i=1:wt-1 htindex=2; for j=1:2:ht-1 htrnsc(htindex,i)=(htrnsr(j,i)+htrnsr(j+1,i))/2; htrnsc(round(htindex+ht/2),i)= htrnsr(j,i)-htrnsr(j+1,i); htindex=htindex+1; end end

for i=1:ht-1 for j=1:wt-1 imgdata(i,j) = htrnsc(i,j); % wt=wt>>1; % ht=ht>>1; end end wt=bitshift(wt,1); ht=bitshift(ht,1); end figure, imshow(imgdata, []);

OUTPUT

Program 8: DCT close all; clear all; map=rand(256,1); map=[map map map]; picsize=8; I=rand(picsize,picsize)*256; I=round(I); figure;imshow(imresize(I,[512 512]),map);title('Input Image'); % figure;imshow(I,map);title('Input Image'); PI=22/7; for i=1:picsize, for j1=1:picsize, temp=0; for k=1:picsize, for l=1:picsize, temp=temp+I(k,l)*cos((PI/picsize)*(k-0.5)*(i-1))*cos((PI/picsize)*(l-0.5)*(j1-1)); end end if i==1 alpha1=1/sqrt(picsize); else alpha1=sqrt(2/picsize); end if j1==1 alpha2=1/sqrt(picsize); else alpha2=sqrt(2/picsize); end F(i,j1)=alpha1*alpha2*temp; end end figure;imshow(imresize(F,[512 512]),map);title('DCT 2D Image'); % figure;imshow(F,map);title('DCT 2D Image');

OUTPUT Input Image

Output Image

Вам также может понравиться