Question

In: Computer Science

Please use C language and use link list to do this program. This program should ask...

Please use C language and use link list to do this program.

This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it.

I need you please to test it to see if it work with these two fraction polynomials

1-  Left Poly Pointer:

1/1x2 + 3/4x + 5/12

2-Right Poly Pointer:

1/1x4 – 3/7x2 + 4/9x + 2/11

AND AFTER ADDING

3- Resulting Poly Pointer:

1/1x4 + 4/7x2 + 43/36x + 79/132

And if the user choice to Multiple

3- Resulting Poly Pointer:

1/1x6 + 3/4x5 – 1/84x4 + 31/252x3 + 871/924x2 + 191/594x + 5/66

Solutions

Expert Solution

C program to Add and multiply two polynomials using Link List

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link{
int coeff_n;
int coeff_d;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n\nenter coeff numerator :");
scanf("%d",&node->coeff_n);
printf("\n\nenter coeff denominator :");
scanf("%d",&node->coeff_d);
printf("\nenter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\ncontinue(y/n):");
ch=getch();
}
while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{

printf("(%d/%d)x^%d",node->coeff_n,node->coeff_d,node->pow);
node=node->next;
if(node->next!=NULL)
printf(" + ");


}
}


void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
int a, b,c,d,x,y,i,gcd;
  
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff_n=poly1->coeff_n;
poly->coeff_d=poly1->coeff_d;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff_n=poly2->coeff_n;
poly->coeff_d=poly2->coeff_d;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;


a=poly1->coeff_n;
b=poly1->coeff_d;

c=poly2->coeff_n;
d=poly2->coeff_d;

x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}

poly->coeff_n=x/gcd;
poly->coeff_d=y/gcd;



poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff_n=poly1->coeff_n;
poly->coeff_d=poly1->coeff_d;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff_n=poly2->coeff_n;
poly->coeff_d=poly2->coeff_d;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}


void polymul(struct link *n1, struct link *n2, struct link *n)
{
   int x,y,i,gcd;
struct link * n2beg=n2;

while (n1)
{
struct link * temp=(struct link *)malloc(sizeof(struct link));
temp->next=NULL;
n2=n2beg;
while (n2)
{
  
  
x= n1->coeff_n * n2->coeff_n;
y= n1->coeff_d * n2->coeff_d;
  
for(i=1; i <= x && i <= y; ++i)
                           {
                           if(x%i==0 && y%i==0)
                           gcd = i;
                           }
                          
                          
                           temp->coeff_n = x/gcd;
                           temp->coeff_d = y/gcd;

temp->pow = n1->pow + n2->pow;

n2 = n2->next;
temp->next=(struct link *)malloc(sizeof(struct link));
temp=temp->next;
temp->next=NULL;

}

polyadd(temp,n,n);
n1 = n1->next;
free(temp);
}
}


int main()
{
int op;
char ch;
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\n\nWhat do you want to do?\n1.Addition\n2.Multiplication\n0.Exit\nEnter your choice:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n\nenter 1st polynomial:");
create(poly1);
printf("\n\nenter 2nd polynomial:");
create(poly2);
printf("\n1st Polynomial:\t");
show(poly1);
printf("\n2nd Polynomial:\t");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:\t");
show(poly);
break;
  
case 2:
printf("\n\nenter 1st polynomial:");
create(poly1);
printf("\n\nenter 2nd polynomial:");
create(poly2);
printf("\n\n1st Polynomial:\t");
show(poly1);
printf("\n\n2nd Polynomial:\t");
show(poly2);
polymul(poly1,poly2,poly);
printf("\n\nMultiplied polynomial:\t");
show(poly);
break;
  
}
  
}
while(op);
  
return 0;
}


Related Solutions

Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program...
Solve this question in C++ language. DO NOT use loops. Use recursive function. Keep the program simple. Q (5) Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely inserted_text (of type string), characters_count (of type unsigned int), words_count...
Write a program in C++ that calculates the sum of two fractions. The program should ask...
Write a program in C++ that calculates the sum of two fractions. The program should ask for the numerators and denominators of two fractions and then output the sum of the two fractions. You will need to write four functions for this program, one to read the inputted data, one to calculate the sum of the two fractions, one to find the Greatest Common Divider (GCD) between the numerator and denominator and a function that will display the end result....
Assembly Language for x86 processors You are to write a program which should first ask for...
Assembly Language for x86 processors You are to write a program which should first ask for 4 random numbers from 0-20 (user will inpute these numbers in no preset order). Input these 5 numbers in variables called num1, num2, num3, num4, and num5. When done, your program should sort these numbers (you will use lots of conditions to check order). num1 should contain smallest number while num5 should contain the biggest. display the contents of num1 through num5 on the...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
IN ASSEMLY LANGUAGE MASM! please show output run. Write a program that ask the user to...
IN ASSEMLY LANGUAGE MASM! please show output run. Write a program that ask the user to write a string and reverse a string using indirect addressing (may not use the stack - push/pop). The string will be given by the user and be up to 64 characters long. INCLUDE Irvine32.inc INCLUDE macros.inc MAX = 64 .data source BYTE MAX DUP('#'),0 destination BYTE LENGTHOF source DUP('*'),0 actual_length DWORD ? ask BYTE "Enter a String: ",0 .code main proc ; ask user...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two file names. You will read the characters from each file and put them in separate queues. Then, you will read each character from each queue and compare it. If every character from both queues is the same, you will print “The files are identical.” Otherwise, you will print “The files are not identical.” Step-by-Step Instructions Create a character queue (named queue1) using the Standard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT