sashomaga

Polynomials

Jan 17th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. //Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below:
  4. //        x2 + 5 = 1x2 + 0x + 5
  5. //Extend the program to support also subtraction and multiplication of polynomials.
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         //7x^3 + 4x^2 -3x + 2
  11.         decimal[] pol1 = {2,-3,4,7};
  12.         //1x2 + 0x + 5
  13.         decimal[] pol2 = { 5, 0, 1 };
  14.         //swap arrays to set shortest array at first position
  15.         SetShortestArrayFirst(ref pol1, ref pol2);
  16.        
  17.         decimal[] result = new decimal[pol2.Length];
  18.  
  19.         //add
  20.         result = AddPolynomials(pol1, pol2);
  21.         PrintResult(result);
  22.         Array.Clear(result,0,result.Length);
  23.        
  24.         //substract
  25.         result = SubstractPolynomials(pol1, pol2);
  26.         PrintResult(result);
  27.  
  28.         //multiplay
  29.         decimal[] multiplayResult = new decimal[pol1.Length + pol2.Length];
  30.         multiplayResult = MultiplayPolynomials(pol1, pol2);
  31.         PrintResult(multiplayResult);    
  32.     }
  33.  
  34.     private static decimal[] MultiplayPolynomials(decimal[] pol1, decimal[] pol2)
  35.     {
  36.         decimal[] result = new decimal[pol1.Length + pol2.Length];
  37.         int pos;
  38.         for (int i = 0; i < pol1.Length; i++)
  39.         {
  40.             for (int j = 0; j < pol2.Length; j++)
  41.             {
  42.                 pos = i + j;
  43.                 result[pos] += (pol1[i] * pol2[j]);
  44.             }
  45.         }
  46.         return result;
  47.     }
  48.  
  49.     private static decimal[] SubstractPolynomials(decimal[] pol1, decimal[] pol2)
  50.     {
  51.         decimal[] result = new decimal[pol2.Length];
  52.  
  53.         for (int i = 0; i < pol2.Length; i++)
  54.         {
  55.             if (i < pol1.Length)
  56.             {
  57.                 result[i] = pol2[i] - pol1[i];
  58.             }
  59.             else
  60.             {
  61.                 result[i] = pol2[i];
  62.             }
  63.         }
  64.         return result;
  65.     }
  66.  
  67.     private static void PrintResult(decimal[] result)
  68.     {
  69.         for (int i = result.Length-1; i >= 0; i--)
  70.         {
  71.             if (i > 1)
  72.             {
  73.                 if (i == result.Length - 1)
  74.                 {
  75.                     Console.Write("{0}x^{1}", result[i], i);
  76.                 }
  77.                 else
  78.                 {  
  79.                     if (result[i] > 0)
  80.                     {
  81.                         Console.Write("+{0}x^{1}", result[i], i);
  82.                     }
  83.                     else
  84.                     {
  85.                         Console.Write("{0}x^{1}", result[i], i);
  86.                     }                
  87.                 }                
  88.             }
  89.             else if (i == 1)
  90.             {
  91.                 if (result[i] > 0)
  92.                 {
  93.                     Console.Write("+{0}x",result[i]);
  94.                 }
  95.                 else
  96.                 {
  97.                     Console.Write("{0}x",result[i]);
  98.                 }              
  99.             }
  100.             else if (i == 0)
  101.             {
  102.                 if (result[i] > 0)
  103.                 {
  104.                     Console.Write("+{0}", result[i]);
  105.                 }
  106.                 else
  107.                 {
  108.                     Console.Write("{0}", result[i]);
  109.                 }
  110.             }
  111.         }
  112.         Console.WriteLine();
  113.     }
  114.  
  115.     private static decimal[] AddPolynomials(decimal[] pol1, decimal[] pol2)
  116.     {
  117.         decimal[] result = new decimal[pol2.Length];
  118.  
  119.         for (int i = 0; i < pol2.Length; i++)
  120.         {
  121.             if (i < pol1.Length)
  122.             {
  123.                 result[i] = pol1[i] + pol2[i];
  124.             }
  125.             else
  126.             {
  127.                 result[i] = pol2[i];
  128.             }          
  129.         }
  130.         return result;
  131.     }
  132.  
  133.     private static void SetShortestArrayFirst(ref decimal[] pol1,ref decimal[] pol2)
  134.     {
  135.        
  136.         if (pol1.Length > pol2.Length)
  137.         {  
  138.             //swap arrays
  139.             decimal[] temp = (decimal[])pol1.Clone();
  140.             pol1 = new decimal[pol2.Length];
  141.             Array.Copy(pol2, pol1, pol2.Length);
  142.             pol2 = new decimal[temp.Length];
  143.             Array.Copy(temp, pol2, temp.Length);
  144.         }
  145.     }
  146.  
  147.    
  148. }
Advertisement
Add Comment
Please, Sign In to add comment