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...
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...
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...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
*In C++ language please* Create a table that converts temperatures from Celsius to Fahrenheit Ask the...
*In C++ language please* Create a table that converts temperatures from Celsius to Fahrenheit Ask the user if they would like to know today's temperatures When "yes": Ask or their name -Generate a random number: use the length of their name (nameLngth) to set the seed Use the length of their name (nameLngth) as the starting point, and 99 as the ending point for a range of Celsius temperatures Output the range of Celsius temperatures alongside the Fahrenheit temperature conversions...
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT