using System;
using System.Collections.Generic;
using System.Text;
namespace PolynomialArithmetic
{
class Node
{
public int coeff;
public int expo;
public Node next;
}
class Polynomial
{
Node root;
public Polynomial()
{
root = null;
}
public void readPoly()
{
char ch = 'y';
while (ch == 'y')
{
Node newNode = new Node();
Console.Write("\nEnter coefficient:");
newNode.coeff = Convert.ToInt32 (Console.ReadLine());
Console.WriteLine("\nEnter exponent:");
newNode.expo = Convert.ToInt32(Console.ReadLine());
newNode.next = null;
if (root == null)
root = newNode;
else
{
Node prev, curr;
curr = root;
prev = null;
while ((curr != null) && (curr.expo > newNode.expo))
{
prev = curr;
curr = curr.next;
}
if ((curr!=null) && (curr.expo == newNode.expo))
{
curr.coeff = curr.coeff + newNode.coeff;
Console.Write("\nDo you want to continue? (y/n):");
ch = Convert.ToChar(Console.ReadLine());
if (ch == 'y')
continue;
else
return;
}
if (prev == null)
{
newNode.next = root;
root = newNode;
}
else
{
prev.next = newNode;
newNode.next = curr;
}
}
Console.Write("\nDo you want to continue? (y/n):");
ch = Convert.ToChar(Console.ReadLine());
}
}
public Polynomial add(Polynomial p)
{
Node ptr1,ptr2;
Polynomial result = new Polynomial();
ptr1 = root;
ptr2 = p.root;
result.root = null;
while ((ptr1 != null) && (ptr2 != null))
{
Node newNode = new Node();
newNode.next = null;
if (ptr1.expo > ptr2.expo)
{
newNode.coeff = ptr1.coeff;
newNode.expo = ptr1.expo;
ptr1 = ptr1.next;
}
else if (ptr2.expo > ptr1.expo)
{
newNode.coeff = ptr2.coeff;
newNode.expo = ptr2.expo;
ptr2 = ptr2.next;
}
else
{
newNode.coeff = ptr1.coeff + ptr2.coeff;
newNode.expo = ptr1.expo;
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
if (result.root == null)
result.root = newNode;
else
{
Node ptr3;
ptr3 = result.root;
while (ptr3.next != null)
ptr3 = ptr3.next;
ptr3.next = newNode;
}
}
while (ptr1 != null)
{
Node newNode = new Node();
newNode.next = null;
newNode.coeff = ptr1.coeff;
newNode.expo = ptr1.expo;
Node ptr3;
ptr3 = result.root;
while (ptr3.next != null)
ptr3 = ptr3.next;
ptr3.next = newNode;
ptr1 = ptr1.next;
}
while (ptr2 != null)
{
Node newNode = new Node();
newNode.next = null;
newNode.coeff = ptr2.coeff;
newNode.expo = ptr2.expo;
Node ptr3;
ptr3 = result.root;
while (ptr3.next != null)
ptr3 = ptr3.next;
ptr3.next = newNode;
ptr2 = ptr2.next;
}
return (result);
}
public void display()
{
Node ptr = root;
while (ptr != null)
{
if ((ptr != root) && (ptr.coeff >= 0))
Console.Write("+");
Console.Write(ptr.coeff + "x^" + ptr.expo + " ");
ptr = ptr.next;
}
}
}
class PolynomialAddition
{
static void Main(string[] args)
{
Polynomial p1, p2,p3;
p1 = new Polynomial();
p2 = new Polynomial();
Console.WriteLine("Enter first polynomial :\n");
p1.readPoly();
Console.WriteLine("\nThe first polynomial is:\n");
p1.display();
Console.WriteLine();
Console.WriteLine("\nEnter second polynomial :\n");
p2.readPoly();
Console.WriteLine("\nThe second polynomial is:\n");
p2.display();
p3 = p1.add(p2);
Console.WriteLine();
Console.WriteLine("\nThe result is:\n");
p3.display();
Console.ReadLine();
}
}
}
No comments:
Post a Comment