-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminor_2.java
More file actions
125 lines (117 loc) · 2.65 KB
/
minor_2.java
File metadata and controls
125 lines (117 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.*;
class nod{
int coeff,pow;
nod next;
}
class LinkList{
nod first,last;
public LinkList() {
first=null;
last=null;
}
public void Insert(int coeff,int pow) {
nod q=new nod();
q.coeff=coeff;
q.pow=pow;
if(first==null) {
first=last=q;
}
else {
last.next=q;
last=q;
}
}
public void Display() {
nod x=first;
while(x!=null) {
if(x.coeff>0 && x.pow!=0) {
System.out.println(" "+x.coeff+"x^"+x.pow);
}
else if(x.coeff<0 && x.pow!=0) {
System.out.println(" "+x.coeff+"x^"+x.pow);
}
if(x.pow==0) {
if(x.coeff>0) {
System.out.println(" "+x.coeff);
}
else if(x.coeff<0) {
System.out.println(" "+x.coeff);
}
else System.out.println(" +0");
}
x=x.next;
}
System.out.println();
}
public void add(LinkList a,LinkList b){
nod x1,x2;
x1=a.first;
x2=b.first;
while(x1!=null && x2!=null) {
if(x1.pow>x2.pow) {
Insert(x1.coeff,x1.pow);
x1=x1.next;
}
else if(x1.pow<x2.pow) {
Insert(x2.coeff,x2.pow);
x2=x2.next;
}
else {
Insert(x1.coeff+x2.coeff,x1.pow);
x1=x1.next;
x2=x2.next;
}
}
if (x1==null && x2!=null) {
while(x2!=null) {
Insert(x2.coeff,x2.pow);
x2=x2.next;
}
}
else if(x1!=null && x2==null) {
while(x1!=null) {
Insert(x1.coeff,x1.pow);
x1=x1.next;
}
}
}
}
public class minor_2 {
public static void main(String[] args){
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int coeff,exp,i,y,z;
LinkList a=new LinkList();
LinkList b=new LinkList();
LinkList c=new LinkList();
System.out.print("Enter the degree of Polynomial 1:\n");
y=sc.nextInt();
System.out.print("Enter coefficients in descending order:\n");
for(i=y;i>0;i--) {
System.out.print("Coefficient of x^"+i+":");
coeff=sc.nextInt();
a.Insert(coeff,i);
}
System.out.print("Constant:");
coeff=sc.nextInt();
a.Insert(coeff,0);
System.out.print("Enter the degree of Polynomial 2:\n");
z=sc.nextInt();
System.out.print("Enter coefficients in descending order:\n");
for(i=z;i>0;i--) {
System.out.print("Coefficient of x^"+i+":");
coeff=sc.nextInt();
b.Insert(coeff,i);
}
System.out.print("Constant:");
coeff=sc.nextInt();
b.Insert(coeff,0);
c.add(a, b);
System.out.println("Polynomial 1:");
a.Display();
System.out.println("Polynomial 2:");
b.Display();
System.out.println("Addition of both the polynomials:");
c.Display();
}
}