private static MyLinkList <item> newPolynomial() { MyLinkList <item> polynomial = new MyLinkList <item>(); //实例化一个单链表 string str = Console.ReadLine(); str = str.Trim(); string[] nums = str.Split(new string[] { " " }, StringSplitOptions.None); int n = Convert.ToInt32(nums[0]) * 2 + 1; if (Convert.ToInt32(nums[0]) == 0) { return(null); } int[] input = new int[n]; int j = 0; for (int i = 0; i <= nums.Length - 1; i++) { if (nums[i] != "") { input[j] = Convert.ToInt32(nums[i]); j++; } } for (int i = 1; i <= input[0]; i++) { item a = new item(); a.coef = input[2 * i - 1]; a.expo = input[2 * i]; polynomial.Append(a); } return(polynomial); }
static MyLinkList <item> Mult(MyLinkList <item> P1, MyLinkList <item> P2) { Node <item> t1, t2; MyLinkList <item> newP = new MyLinkList <item>(); t1 = P1.Head; t2 = P2.Head; while (t1 != null) { MyLinkList <item> tempP = new MyLinkList <item>(); while (t2 != null) { item temp = new item { coef = t1.Data.coef * t2.Data.coef, expo = t1.Data.expo + t2.Data.expo }; tempP.Append(temp); t2 = t2.Next; } newP = Add(newP, tempP); t1 = t1.Next; t2 = P2.Head; } return(newP); }
static MyLinkList <item> Add(MyLinkList <item> P1, MyLinkList <item> P2) { Node <item> t1, t2; MyLinkList <item> newP = new MyLinkList <item>(); t1 = P1.Head; t2 = P2.Head; while (t1 != null && t2 != null) { if (t1.Data.expo == t2.Data.expo) { item temp = new item { coef = t1.Data.coef + t2.Data.coef, expo = t1.Data.expo }; if (temp.coef != 0) { newP.Append(temp); } t1 = t1.Next; t2 = t2.Next; } else if (t1.Data.expo > t2.Data.expo) { item temp = new item { coef = t1.Data.coef, expo = t1.Data.expo }; newP.Append(temp); t1 = t1.Next; } else if (t1.Data.expo < t2.Data.expo) { item temp = new item(); temp.coef = t2.Data.coef; temp.expo = t2.Data.expo; newP.Append(temp); t2 = t2.Next; } } while (t1 != null) { item temp = new item { coef = t1.Data.coef, expo = t1.Data.expo }; newP.Append(temp); t1 = t1.Next; } while (t2 != null) { item temp = new item(); temp.coef = t2.Data.coef; temp.expo = t2.Data.expo; newP.Append(temp); t2 = t2.Next; } return(newP); }