I want to implement that using C#'s Generic LinkedList and the new features of C# 3.0. But I'm stuck now. the code add's the polynomials with matching exponents but doesn't add the rest of the nodes to the new LinkedList.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polynomials
{
class PolynomialExpression
{
public Int32 coefficient { get; set; }
public Int32 exponent { get; set; }
public override string ToString()
{
//return base.ToString();
return ((exponent==0)?(coefficient).ToString():(coefficient + "x^" + exponent).ToString());
}
}
class Program
{
public static LinkedList<PolynomialExpression> SortExponent(LinkedList<PolynomialExpression> a)
{
IEnumerable<PolynomialExpression> sortedExpression =
from x in a
orderby x.exponent descending
select x;
var r = new LinkedList<PolynomialExpression>(sortedExpression);
return r;
}
public static LinkedList<PolynomialExpression> CreateList()
{
Boolean accept = true;
LinkedList<PolynomialExpression> x = new LinkedList<PolynomialExpression>();
while (accept)
{
PolynomialExpression obj = new PolynomialExpression();
Console.Write("\nEnter the coefficient : ");
obj.coefficient = Int32.Parse(Console.ReadLine());
if (obj.coefficient == 0)
continue;
Console.Write("Enter the exponent : ");
obj.exponent = Int32.Parse(Console.ReadLine());
if (obj.exponent < 0)
continue;
var current = new LinkedListNode<PolynomialExpression>(obj);
x.AddLast(current);
Console.WriteLine("Continue? (y/n)");
accept = Console.ReadLine().StartsWith("y", true, System.Globalization.CultureInfo.CurrentCulture);
}
return x;
}
public static void TraverseList(LinkedList<PolynomialExpression> list)
{
foreach (var item in list)
{
Console.Write(item + (item.Equals(list.Last.Value) ? " " : " + "));
}
}
public static LinkedList<PolynomialExpression> AddExpressions(LinkedList<PolynomialExpression> a, LinkedList<PolynomialExpression> b)
{
//var currentA = a.First;
//var currentB = b.First;
var added = new LinkedList<PolynomialExpression>();
foreach (var itemA in a)
{
//currentA = a.Find(itemA);
foreach (var itemB in b)
{
//currentB = b.Find(itemB);
if (itemA.exponent == itemB.exponent)
{
PolynomialExpression obj = new PolynomialExpression
{
coefficient = itemA.coefficient + itemB.coefficient,
exponent = itemB.exponent
};
var x = new LinkedListNode<PolynomialExpression>(obj);
added.AddLast(x);
}
else if (itemA.exponent > itemB.exponent)
{
//if (!added.Contains(itemA))
//{
// var x = new LinkedListNode<PolynomialExpression>(itemA);
// added.AddLast(x);
//}
//break;
}
else if (itemA.exponent < itemB.exponent)
{
//if (!added.Contains(itemB))
//{
// var x = new LinkedListNode<PolynomialExpression>(itemB);
// added.AddLast(x);
//}
//continue;
}
}
}
return added;
}
static void Main(string[] args)
{
//Input the first expression
Console.WriteLine("Enter the First polynomial expression : ");
var exp1 = CreateList();
//Input the second expression
Console.WriteLine("\nEnter the Second polynomial expression : ");
var exp2 = CreateList();
//Display both expressions
Console.WriteLine("\n\nExpressions (sorted by exponent in decending order) ");
Console.Write("\nFirst Expression : ");
exp1 = SortExponent(exp1);
TraverseList(exp1);
Console.Write("\nSecond Expression : ");
exp2 = SortExponent(exp2);
TraverseList(exp2);
var added = AddExpressions(exp1, exp2);
added = SortExponent(added);
Console.Write("\n\nAdded Expression : ");
TraverseList(added);
Console.WriteLine("\n\n");
}
}
}
I also want to use the SortExponent function as an Exponent method, so I declare a static class Extentions in this namespace and add the static function, but I'm getting an error indicating that the scope of the function and the polynomialexpression class is not the same. I don't understand how to implement it.
public static class Expressions
{
public static LinkedList<PolynomialExpression> SortExponent(LinkedList<PolynomialExpression> a)
{
IEnumerable<PolynomialExpression> sortedExpression =
from x in a
orderby x.exponent descending
select x;
var r = new LinkedList<PolynomialExpression>(sortedExpression);
return r;
}
}
Any additional suggestion to improve the code will be helpful.