Advertisement
stak441

Methods - 11,12 - OperationsWithPolynomials

Jan 21st, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _11.AddPolynomials
  7. {
  8.     class AddPolynomials
  9.     {
  10.         static int[] SumOfArrays(int[] arrayA, int[] arrayB, bool substraction)
  11.         {
  12.             int lengthA = arrayA.Length;
  13.             int lengthB = arrayB.Length;
  14.             if (lengthA < lengthB)
  15.             {
  16.                 return SumOfArrays(arrayB, arrayA, substraction);         //To ensure that the first array is always the bigger
  17.             }
  18.             int[] resultArray = new int[lengthA];
  19.  
  20.             for (int i = 0; i < lengthA; i++)
  21.             {
  22.                 if (i >= lengthB)
  23.                 {
  24.                     resultArray[i] = arrayA[i];
  25.                 }
  26.                 else
  27.                 {
  28.                     if (substraction)
  29.                     {
  30.                         resultArray[i] = arrayA[i] - arrayB[i];
  31.                     }
  32.                     else
  33.                     {
  34.                         resultArray[i] = arrayA[i] + arrayB[i];
  35.                     }
  36.                    
  37.                 }
  38.             }
  39.  
  40.             return resultArray;
  41.         }
  42.  
  43.  
  44.         static int[] MultiplicationOfArrays(int[] arrayA, int[] arrayB)
  45.         {
  46.             int[] multipliedArray = new int[arrayA.Length + arrayB.Length];
  47.  
  48.             for (int i = 0; i < arrayA.Length; i++)
  49.             {
  50.                 for (int j = 0; j < arrayB.Length; j++)
  51.                 {
  52.                     int position = i + j;
  53.                     multipliedArray[position] += arrayA[i] * arrayB[j];
  54.                 }
  55.             }
  56.             return multipliedArray;
  57.         }
  58.  
  59.  
  60.         static void PrintArray(int[] sumArray)
  61.         {
  62.             for (int i = sumArray.Length - 1; i >= 0; i--)
  63.             {
  64.                 if (sumArray[i] == 0)
  65.                 {
  66.                     continue;
  67.                 }
  68.  
  69.                 if (i == 0)
  70.                 {
  71.                     Console.Write(sumArray[i]);
  72.                 }
  73.                 else if (i == 1)
  74.                 {
  75.                     Console.Write(sumArray[i] + "x + ");
  76.                 }
  77.                 else
  78.                 {
  79.                     Console.Write(sumArray[i] + "x^" + i + " + ");
  80.                 }
  81.             }
  82.             Console.WriteLine();
  83.         }
  84.  
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.             int[] arrayA = { 5, 0, 1 };
  89.             int[] arrayB = { 2, 1, 4, 6 };
  90.             bool substraction = false;
  91.  
  92.             int[] sumArray = SumOfArrays(arrayA, arrayB, substraction);      //result for addition or substraction
  93.             PrintArray(sumArray);
  94.  
  95.             int[] multiplyArray = MultiplicationOfArrays(arrayA, arrayB);    //result for multiplication
  96.             PrintArray(multiplyArray);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement