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

Use C++ language Create a program which will ask the user to input three songs for...
Use C++ language Create a program which will ask the user to input three songs for a playlist (you may use TV shows or movies, if you prefer). Declare three strings to store each of the songs. Use getline to receive the input. Display output which lists each of the songs (or movies or tv shows), on separate lines, with a title on the first line: My Playlist. Insert three lines of comments at the beginning of the program for...
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...
Construct this program in C programming Please. Using a do/while loop, your program will ask/prompt the...
Construct this program in C programming Please. Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. If the user enters in a 0 or negative number the program should exit with a message to the user indicating they chose to exit. If the user has entered in a valid positive number, your program should pass...
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...
Use C++ language Ask the user for their favorite number. If it isn't 10 and isn't...
Use C++ language Ask the user for their favorite number. If it isn't 10 and isn't divisible by 3, tell them they made a good choice. Ask the user how many guests will be arriving, use a while loop to ensure they don't give you negative number of guests. Ask them the name of each guest and output "hello guest" with their name. 3. Ask the user to pick a number between 1 and 100. You (the program) should try...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT