diff --git a/c/Question1_part1.c b/c/Question1_part1.c new file mode 100644 index 0000000..e448ba3 --- /dev/null +++ b/c/Question1_part1.c @@ -0,0 +1,77 @@ +#include +#include + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ +struct term +{ + int coeff; + int exp; +}; +struct polynomial +{ + int n; + struct term *terms; +}; +void create(struct polynomial *p) +{ + printf("No of terms:"); + scanf("%d",&p->n); + p->terms=(struct term*)malloc(p->n*sizeof(struct term)); + + printf("Enter terms\n"); + int i; + for( i=0;in;i++) + scanf("%d%d",&p->terms[i].coeff,&p->terms[i].exp); +} + +void display(struct polynomial p) +{ + int i; + for(i=0;iterms=(struct term*)malloc((p1->n+p2->n)*sizeof(struct term)); + int i,j,k; + i=k=j=0; + while(in && jn) + { + if(p1->terms[i].exp>p2->terms[j].exp) + sum->terms[k++]=p1->terms[i++]; + else if(p1->terms[i].expterms[j].exp) + sum->terms[k++]=p2->terms[j++]; + else + { + sum->terms[k].exp=p1->terms[i].exp; + sum->terms[k++].coeff=p1->terms[i++].coeff+p2->terms[j++].coeff; + } + + + } + for(;in;i++)sum->terms[k++]=p1->terms[i]; + for(;jn;j++)sum->terms[k++]=p2->terms[j]; + + sum->n=k; + return sum; +} +int main() { + + struct polynomial p1,p2,*p3; + create(&p1); + create(&p2); + p3=add(&p1,&p2); + printf("\n"); + display(p1); + printf("\n"); + display(p2); + printf("\n"); + display(*p3); + + return 0; + getch(); +} diff --git a/c/Question1_part2.c b/c/Question1_part2.c new file mode 100644 index 0000000..a232292 --- /dev/null +++ b/c/Question1_part2.c @@ -0,0 +1,200 @@ +#include +#include + /* run this program using the console pauser or add your own getch, system("pause") or input loop */ +struct node { + int coefficient, exponent; + struct node *next; +}; + +struct node *hPtr1, *hPtr2, *hPtr3; + +struct node * buildNode(int coefficient, int exponent) { + struct node *ptr = (struct node *) malloc(sizeof (struct node)); + ptr->coefficient = coefficient; + ptr->exponent = exponent; + ptr->next = NULL; + return ptr; +} + + +void polynomial_insert(struct node ** myNode, int coefficient, int exponent) { + struct node *lPtr, *pPtr, *qPtr = *myNode; + lPtr = buildNode(coefficient, exponent); + + + if (*myNode == NULL || (*myNode)->exponent < exponent) { + *myNode = lPtr; + (*myNode)->next = qPtr; + return; + } + + + while (qPtr) { + pPtr = qPtr; + qPtr = qPtr->next; + if (!qPtr) { + pPtr->next = lPtr; + break; + + } + else if ((exponent < pPtr->exponent) && (exponent > qPtr->exponent)){ + lPtr->next = qPtr; + pPtr->next = lPtr; + break; + } + } + return; + } + + + void polynomial_add(struct node **n1, int coefficient, int exponent) { + struct node *x = NULL, *temp = *n1; + if (*n1 == NULL || (*n1)->exponent < exponent) { + + *n1 = x = buildNode(coefficient, exponent); + (*n1)->next = temp; + } else { + while (temp) { + if (temp->exponent == exponent) { + + temp->coefficient = temp->coefficient + coefficient; + return; + } + if (temp->exponent > exponent && (!temp->next || temp->next->exponent < exponent)) { + + x = buildNode(coefficient, exponent); + x->next = temp->next; + temp->next = x; + return; + } + temp = temp->next; + } + x->next = NULL; + temp->next = x; + } + } + + void polynomial_multiply(struct node **n1, struct node *n2, struct node *n3) { + struct node * temp; + int coefficient, exponent; + + temp = n3; + + + if (!n2 && !n3) + return; + + + if (!n2) { + *n1 = n3; + } else if (!n3) { + + + *n1 = n2; + } else { + while (n2) { + while (n3) { + + coefficient = n2->coefficient * n3->coefficient; + exponent = n2->exponent + n3->exponent; + n3 = n3->next; + + polynomial_add(n1, coefficient, exponent); + } + n3 = temp; + n2 = n2->next; + } + } + return; + } + + struct node * polynomial_deleteList(struct node *ptr) { + struct node *temp; + while (ptr){ + temp = ptr->next; + free(ptr); + ptr = temp; + } + return NULL; + } + + void polynomial_view(struct node *ptr) { + int i = 0; + int flag=0; + while (ptr) { + if(ptr->exponent != 0 || ptr->exponent != 1 ){ + if(ptr->coefficient > 0 && flag==0 ){ + printf("%dx^%d", ptr->coefficient,ptr->exponent); + flag++; + } + else if (ptr->coefficient > 0 && flag==1 ) + printf("+%dx^%d", ptr->coefficient,ptr->exponent); + else if(ptr->coefficient < 0) + printf("%dx^%d", ptr->coefficient,ptr->exponent); + } + else if (ptr->exponent == 0){ + if(ptr->coefficient > 0 && flag==0 ){ + printf("%d", ptr->coefficient); + flag++; + } + else if (ptr->coefficient > 0 && flag==1 ) + printf("+%d", ptr->coefficient); + else if(ptr->coefficient < 0) + printf("%d", ptr->coefficient); + } + else if( ptr->exponent == 1 ){ + if(ptr->coefficient > 0 && flag==0 ){ + printf("%dx", ptr->coefficient); + flag++; + } + else if (ptr->coefficient > 0 && flag==1 ) + printf("+%dx", ptr->coefficient); + else if(ptr->coefficient < 0) + printf("%dx", ptr->coefficient); + } + ptr = ptr->next; + i++; + } + printf("\n"); + return; + } + + int main (int argc, char *argv[]) { + int coefficient, exponent, i, n; + int count; + + printf("Enter the number of coefficients in the multiplicand:"); + scanf("%d",&count); + for(i=0;i +#include +#include +#include +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ +void swap(char *str1, char *str2) +{ + char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char)); + strcpy(temp, str1); + strcpy(str1, str2); + strcpy(str2, temp); + free(temp); +} + +void reverse(char *str) +{int i=0; + for( i=0;i0 && bt>0) + { + val=((a[strlen(a)-at]-'0')+(b[strlen(b)-bt]-'0'))%10+carry; + temp[i++]=val+'0'; + carry=((a[strlen(a)-at]-'0')+(b[strlen(b)-bt]-'0'))/10; + at--; bt--; + } + while(at>0) + { + if(carry!=0) + { + val=(((a[strlen(a)-at]-'0')+carry))%10; + carry=(((a[strlen(a)-at]-'0')+carry))/10; + temp[i++]=val+'0'; + } + else + temp[i++]=(a[strlen(a)-at]-'0')+'0'; + at--; + } + while(bt>0) + { + if(carry!=0) + { + val=(((b[strlen(b)-bt]-'0')+carry))%10; + carry=(((b[strlen(b)-bt]-'0')+carry))/10; + temp[i++]=val+'0'; + } + else + temp[i++]=(b[strlen(b)-bt]-'0')+'0'; + at--; + } + if(carry!=0) + temp[i++]=carry+'0'; + temp[i]='\0'; + reverse(temp); +} + +char * multiply(char a[],char b[]){ + static char mul[1000000]; + char c[1000000]; + char temp[1000000]; + int la,lb; + int i,j,k=0,x=0,y; + long int r=0; + long sum = 0; + la=strlen(a)-1; + lb=strlen(b)-1; + + for(i=0;i<=la;i++){ + a[i] = a[i] - 48; + } + + for(i=0;i<=lb;i++){ + b[i] = b[i] - 48; + } + + for(i=lb;i>=0;i--){ + r=0; + for(j=la;j>=0;j--){ + temp[k++] = (b[i]*a[j] + r)%10; + r = (b[i]*a[j]+r)/10; + } + temp[k++] = r; + x++; + for(y = 0;y=0;i--){ + mul[j++]=c[i] + 48; + } + reverse(mul); + mul[strlen(mul)-1]='\0'; + return mul; +} + +int main() +{ + char A[1000],B[1000]; + char sum[1000]; + char *product; + printf("Enter your 1st number : "); + scanf("%s",A); + printf("Enter ypur 2nd number : "); + scanf("%s",B); + if(strlen(A)