Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- //Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below:
- // x2 + 5 = 1x2 + 0x + 5
- //Extend the program to support also subtraction and multiplication of polynomials.
- class Program
- {
- static void Main()
- {
- //7x^3 + 4x^2 -3x + 2
- decimal[] pol1 = {2,-3,4,7};
- //1x2 + 0x + 5
- decimal[] pol2 = { 5, 0, 1 };
- //swap arrays to set shortest array at first position
- SetShortestArrayFirst(ref pol1, ref pol2);
- decimal[] result = new decimal[pol2.Length];
- //add
- result = AddPolynomials(pol1, pol2);
- PrintResult(result);
- Array.Clear(result,0,result.Length);
- //substract
- result = SubstractPolynomials(pol1, pol2);
- PrintResult(result);
- //multiplay
- decimal[] multiplayResult = new decimal[pol1.Length + pol2.Length];
- multiplayResult = MultiplayPolynomials(pol1, pol2);
- PrintResult(multiplayResult);
- }
- private static decimal[] MultiplayPolynomials(decimal[] pol1, decimal[] pol2)
- {
- decimal[] result = new decimal[pol1.Length + pol2.Length];
- int pos;
- for (int i = 0; i < pol1.Length; i++)
- {
- for (int j = 0; j < pol2.Length; j++)
- {
- pos = i + j;
- result[pos] += (pol1[i] * pol2[j]);
- }
- }
- return result;
- }
- private static decimal[] SubstractPolynomials(decimal[] pol1, decimal[] pol2)
- {
- decimal[] result = new decimal[pol2.Length];
- for (int i = 0; i < pol2.Length; i++)
- {
- if (i < pol1.Length)
- {
- result[i] = pol2[i] - pol1[i];
- }
- else
- {
- result[i] = pol2[i];
- }
- }
- return result;
- }
- private static void PrintResult(decimal[] result)
- {
- for (int i = result.Length-1; i >= 0; i--)
- {
- if (i > 1)
- {
- if (i == result.Length - 1)
- {
- Console.Write("{0}x^{1}", result[i], i);
- }
- else
- {
- if (result[i] > 0)
- {
- Console.Write("+{0}x^{1}", result[i], i);
- }
- else
- {
- Console.Write("{0}x^{1}", result[i], i);
- }
- }
- }
- else if (i == 1)
- {
- if (result[i] > 0)
- {
- Console.Write("+{0}x",result[i]);
- }
- else
- {
- Console.Write("{0}x",result[i]);
- }
- }
- else if (i == 0)
- {
- if (result[i] > 0)
- {
- Console.Write("+{0}", result[i]);
- }
- else
- {
- Console.Write("{0}", result[i]);
- }
- }
- }
- Console.WriteLine();
- }
- private static decimal[] AddPolynomials(decimal[] pol1, decimal[] pol2)
- {
- decimal[] result = new decimal[pol2.Length];
- for (int i = 0; i < pol2.Length; i++)
- {
- if (i < pol1.Length)
- {
- result[i] = pol1[i] + pol2[i];
- }
- else
- {
- result[i] = pol2[i];
- }
- }
- return result;
- }
- private static void SetShortestArrayFirst(ref decimal[] pol1,ref decimal[] pol2)
- {
- if (pol1.Length > pol2.Length)
- {
- //swap arrays
- decimal[] temp = (decimal[])pol1.Clone();
- pol1 = new decimal[pol2.Length];
- Array.Copy(pol2, pol1, pol2.Length);
- pol2 = new decimal[temp.Length];
- Array.Copy(temp, pol2, temp.Length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment