Showing posts with label Polynomial. Show all posts
Showing posts with label Polynomial. Show all posts

crc-ccitt is based on the polynomial x^16+x^12+x^5+1

How to write a C Program crc-ccitt which based on the polynomial x^16+x^12+x^5+1 in C Programming Language ?

The prescription for determining the mask to use for a given polynomial is as follows:

                1.  Represent the polynomial by a 17-bit number
                2.  Assume that the most and least significant bits are 1
                3.  Place the right 16 bits into an integer

Solution:

  1. /* compute crc's */
  2. /* crc-ccitt is based on the polynomial x^16+x^12+x^5+1 */
  3. /* The prescription for determining the mask to use for a given polynomial
  4.         is as follows:
  5.                 1.  Represent the polynomial by a 17-bit number
  6.                 2.  Assume that the most and least significant bits are 1
  7.                 3.  Place the right 16 bits into an integer
  8. */
  9. #include        <stdio.h>
  10. #include        <stdlib.h>
  11. #include        <string.h>
  12. #include        <time.h>
  13. #define         MTT     0x1021          /* crc-ccitt mask */
  14. #define     CLK_TCK CLOCKS_PER_SEC
  15. /* function declarations */
  16. unsigned short int updcrc(unsigned short int,int,unsigned short int);
  17. void perr(char *);
  18. /* variables */
  19. char filename[100];
  20. unsigned short int crctt;
  21. int ch;
  22. unsigned long num;
  23. FILE *fp;
  24. /* driver */
  25. int main(argc,argv)
  26. int argc; char **argv;
  27. {
  28.  clock_t start,end;
  29.         if(argc>2) perr("Usage:  ccitt [filename]");
  30.         if(argc==2) strcpy(filename,argv[1]);
  31.         else
  32.         {
  33.                 printf("\nEnter filename:  ");
  34.                 gets(filename);
  35.         }
  36.         if((fp=fopen(filename,"rb"))==NULL) perr("Can't open file");
  37.         num=0L; crctt=0;
  38.         start=clock();
  39.        
  40.         while((ch=fgetc(fp))!=EOF)
  41.         {
  42.                 //if (ch == '\n')
  43.                 //      continue;
  44.                 num++;
  45.                 //int i; for (i=0;i<10000;i++){
  46.                 crctt=updcrc(crctt,ch,MTT);
  47.                 //}
  48.         }
  49.         end=clock();
  50.         fclose(fp);
  51.         printf("\nNumber of bytes = %lu\n CRCTT = %04X\n",      num,crctt);
  52.         printf("Czas trwania: %f\n",(double)(end-start)/CLK_TCK);
  53.         return 0;
  54. }
  55. /* update crc */
  56. unsigned short int updcrc(crc,c,mask)
  57. unsigned short int crc,mask; int c;
  58. {
  59.         int i;
  60.         c<<=8;
  61.         for(i=0;i<8;i++)
  62.         {
  63.                 if((crc ^ c) & 0x8000) crc=(crc<<1)^mask;
  64.                 else crc<<=1;
  65.                 c<<=1;
  66.         }
  67.         return crc;
  68. }
  69. /* error abort */
  70. void perr(s)
  71. char *s;
  72. {
  73.         printf("\n%s",s); exit(1);
  74. }

C Program To Multiply Two Polynomials

How to in Write a  C program to multiply two polynomials in C Programming Language ?


Solution:
/*
Write a C program to multiply two polynomials.
*/

Adding two polynomial functions C Program Using Structure

How to write a C Program to Adding Two Polynomial Functions in C Programming Language ?


Solution:
/*Adding two polynomial functions*/

#include <stdio.h>
#include<stdlib.h>

struct poly* link(struct poly*);
void display(struct poly*);
struct poly* addlink(struct poly*,struct poly*, struct poly*);

struct poly
{
int coeff, pow;
struct poly *ptr;
};

int main()
{
struct poly *head1,*head2,*head3;
int num;
head1=NULL;
head2=NULL;
head3=NULL;

while(1)
{
printf("\nEnter\n1->To create first polynomial function\n2->To create second polynomial function\n3->To display first polynomial function\n4->To display second polynomial function\n5->To add both the polynomial functions\n6->To display the new polynomial created after addition\n");
scanf("%d",&num);

switch(num)
{

case 1: head1=link(head1);
break;

case 2: head2=link(head2);
break;

case 3: display(head1);
break;

case 4: display(head2);
break;

case 5: head3=addlink(head1,head2,head3);
break;

case 6: display(head3);
break;

default: printf("\nBoss ! Enter the correct value\n");

}
}
}

struct poly* link(struct poly *head1)
{
struct poly *newnode,*temp;
int value,degree;
newnode=temp=NULL;
printf("\nEnter the coefficient of the polynomial\n");
scanf("%d",&value);
printf("\nEnter the degree for variable x\n");
scanf("%d",&degree);

newnode=(struct poly*) malloc (sizeof (struct poly));

newnode->pow=degree;
newnode->coeff=value;
temp=head1;

if(temp==NULL)
{
printf("\nThe first node is created\n");
newnode->ptr=NULL;
return newnode;
}
else
{
printf("\nThe next node is being linked\n");
temp->ptr=newnode;
temp=temp->ptr;
return head1;
}

}

void display(struct poly* head)
{
if(head == NULL)
{
printf("\nThe polynomial function is not created.\nPlease create a new polynomial function.\n");
}

while(head != NULL && head->ptr != NULL)
{
printf("The polynomial function has been detected\n");
printf("%dx^%d+",head->coeff,head->pow);
head=head->ptr;
}

if(head->ptr == NULL)
{
// printf("\nEntering the last node\n");
printf("%dx^%d",head->coeff,head->pow);
head=head->ptr;
}
}


struct poly* addlink(struct poly* head,struct poly* tail,struct poly *head3 )
{
struct poly *temp1,*temp,*newnode;
temp=newnode=temp1=head3=NULL;

newnode=( struct poly*) malloc(sizeof(struct poly));

if( head == NULL || tail == NULL)
{
printf("\nYou have no node to get added\n");
}
if(head !=NULL && tail == NULL)
{
printf("\nThe existing first polynomial is the final polynomial\n");
return head;
}
if(head == NULL && tail !=NULL)
{
printf("\nThe existing second polynomial is the final polynomial\n");
return tail;
}

head3=newnode;

if(head3==NULL)
{
printf("\nEnter a node first\n");
}

if(head3 != NULL)
{
while(head !=NULL && tail !=NULL)
{
if(head->pow > tail->pow)
{
temp=tail;
temp1=head;
while((head->pow) > (temp->pow))
{
printf("\nThe element's degree in first poly. is greater than the second\n");

if(temp->ptr == NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;

}

temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}

if(tail->pow > head->pow)
{
temp=head;
temp1=tail;
while((tail->pow) > (temp->pow))
{
printf("\nThe element's degree in second poly. is greater than the first\n");

if(temp->ptr ==NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}

temp=temp->ptr;
}

printf("\nYou have come to the end of the tail pointer\n");
}

if(head->pow == tail->pow)
{
temp=head;
temp1=tail;

while((temp->pow == temp1->pow))
{
newnode->coeff = (temp->coeff + tail->coeff);
newnode->pow=temp->pow;
head3=newnode;
return head3;
temp=temp->ptr;
temp1=temp1->ptr;
}
}
}
}

}

Program to Add Two Polynomials Using Linked List C Program

How to write a C Program to add two polynomials using linked list in C Programming Language ?


Solution:
/*Program to add two polynomials using linked list*/

#include <stdio.h>
#include<stdlib.h>

struct poly* link(struct poly*);
void display(struct poly*);
struct poly* addlink(struct poly*,struct poly*, struct poly*);

struct poly
{
int coeff, pow;
struct poly *ptr;
};

int main()
{
struct poly *head1,*head2,*head3;
int num;
head1=NULL;
head2=NULL;
head3=NULL;

while(1)
{
printf("\nEnter\n1->To create first polynomial function\n2->To create second polynomial function\n3->To display first polynomial function\n4->To display second polynomial function\n5->To add both the polynomial functions\n6->To display the new polynomial created after addition\n");
scanf("%d",&num);

switch(num)
{

case 1: head1=link(head1);
break;

case 2: head2=link(head2);
break;

case 3: display(head1);
break;

case 4: display(head2);
break;

case 5: head3=addlink(head1,head2,head3);
break;

case 6: display(head3);
break;

default: printf("\nBoss ! Enter the correct value\n");

}
}
}

struct poly* link(struct poly *head1)
{
struct poly *newnode;
int value,degree;
newnode=NULL;
printf("\nEnter the coefficient of the polynomial\n");
scanf("%d",&value);
printf("\nEnter the degree for variable x\n");
scanf("%d",&degree);

newnode=(struct poly*) malloc (sizeof (struct poly));

newnode->pow=degree;
newnode->coeff=value;

if(head1==NULL)
{
printf("\nThe first node is created\n");
newnode->ptr=NULL;
}
else
{
printf("\nThe next node is being linked\n");
newnode->ptr=head1;
}
return newnode;
}

void display(struct poly* head)
{
if(head == NULL)
{
printf("\nThe polynomial function is not created.\nPlease create a new polynomial function.\n");
}

while(head != NULL && head->ptr != NULL)
{
printf("The polynomial function has been detected\n");
printf("%dx^%d+",head->coeff,head->pow);
head=head->ptr;
}

if(head->ptr == NULL)
{
printf("\nEntering the last node\n");
printf("%dx^%d",head->coeff,head->pow);
head=head->ptr;
}
}


struct poly* addlink(struct poly* head,struct poly* tail,struct poly *head3 )
{
struct poly *temp1,*temp,*newnode;
temp=newnode=temp1=head3=NULL;

newnode=( struct poly*) malloc(sizeof(struct poly));

if( head == NULL || tail == NULL)
{
printf("\nYou have no node to get added\n");
}
if(head !=NULL && tail == NULL)
{
printf("\nThe existing first polynomial is the final polynomial\n");
return head;
}
if(head == NULL && tail !=NULL)
{
printf("\nThe existing second polynomial is the final polynomial\n");
return tail;
}

head3=newnode;

if(head3==NULL)
{
printf("\nEnter a node first\n");
}

if(head3 != NULL)
{
while(head !=NULL && tail !=NULL)
{
if(head->pow > tail->pow)
{
temp=tail;
temp1=head;
while((head->pow) > (temp->pow))
{
printf("\nThe element's degree in first poly. is greater than the second\n");

if(temp->ptr == NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;

}

temp=temp->ptr;
}
printf("\nYou have come to the end of the tail pointer\n");
}

if(tail->pow > head->pow)
{
temp=head;
temp1=tail;
while((tail->pow) > (temp->pow))
{
printf("\nThe element's degree in second poly. is greater than the first\n");

if(temp->ptr ==NULL)
{
newnode->coeff=temp->coeff;
newnode->pow=temp->pow;
newnode->ptr=NULL;
head3=newnode;
return head3;
temp1=temp1->ptr;
}

temp=temp->ptr;
}

printf("\nYou have come to the end of the tail pointer\n");
}

if(head->pow == tail->pow)
{
temp=head;
temp1=tail;

while((temp->pow == temp1->pow))
{
newnode->coeff = (temp->coeff + tail->coeff);
newnode->pow=temp->pow;
head3=newnode;
return head3;
temp=temp->ptr;
temp1=temp1->ptr;
}
}
}
}

}