Advertisement
stanevplamen

02.03.12.Polynomials

Jul 16th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2.  
  3. class Polynomials
  4. {
  5.     static int[] SumCoeffs(int[] firstCoeffs, int[] secondCoeffs)
  6.     {
  7.         int[] newCoeffs = new int[firstCoeffs.Length];
  8.         for (int i = 0; i < firstCoeffs.Length; i++)
  9.         {
  10.             newCoeffs[i] = firstCoeffs[i] + secondCoeffs[i];
  11.         }
  12.         return newCoeffs;
  13.     }
  14.  
  15.     static int[] SubstractCoeffs(int[] firstCoeffs, int[] secondCoeffs)
  16.     {
  17.         int[] newCoeffs = new int[firstCoeffs.Length];
  18.         for (int i = 0; i < firstCoeffs.Length; i++)
  19.         {
  20.             newCoeffs[i] = firstCoeffs[i] - secondCoeffs[i];
  21.         }
  22.         return newCoeffs;
  23.     }
  24.  
  25.     static int[] MultiplyCoeffs(int[] firstCoeffs, int[] secondCoeffs)
  26.     {
  27.         int[] newCoeffs = new int[firstCoeffs.Length + 2];
  28.  
  29.         newCoeffs[0] = firstCoeffs[0] * secondCoeffs[0];
  30.         newCoeffs[1] = firstCoeffs[1] * secondCoeffs[0] + firstCoeffs[0] * secondCoeffs[1];
  31.         newCoeffs[2] = firstCoeffs[2] * secondCoeffs[0] + firstCoeffs[0] * secondCoeffs[2] + firstCoeffs[1] * secondCoeffs[1];
  32.         newCoeffs[3] = firstCoeffs[2] * secondCoeffs[1] + firstCoeffs[1] * secondCoeffs[2];
  33.         newCoeffs[4] = firstCoeffs[2] * secondCoeffs[2];
  34.  
  35.         return newCoeffs;
  36.     }
  37.  
  38.     static void Main()
  39.     {
  40.         int[] firstCoeffs = (LoadArray());
  41.         int[] secondCoeffs = (LoadArray());
  42.  
  43.         int[] sumCoeffs = SumCoeffs(firstCoeffs, secondCoeffs);
  44.         int[] substract = SubstractCoeffs(firstCoeffs, secondCoeffs);
  45.         int[] multiplication = MultiplyCoeffs(firstCoeffs, secondCoeffs);
  46.  
  47.         Print(firstCoeffs);
  48.         Print(secondCoeffs);
  49.         Console.Write("\nThe sum is: ");
  50.         Print(sumCoeffs);
  51.         Console.WriteLine("\nThe substraction is: ");
  52.         Print(substract);
  53.         Console.WriteLine("\nThe multiplication is: ");
  54.         PrintMulti(multiplication);
  55.     }
  56.  
  57.     static int counter = 0;
  58.     static int counterPrint = 0;
  59.  
  60.     private static int[] LoadArray()
  61.     {
  62.         counter++;
  63.         Console.WriteLine("Please enter the coefficients (integer numbers) of the {0} polynomial nx2 + mx + k:", counter);
  64.         int[] array = new int[3];
  65.         Console.Write("k = ");
  66.         array[0] = int.Parse(Console.ReadLine());
  67.         Console.Write("m = ");
  68.         array[1] = int.Parse(Console.ReadLine());
  69.         Console.Write("n = ");
  70.         array[2] = int.Parse(Console.ReadLine());
  71.         return array;
  72.     }
  73.  
  74.     private static void Print(int[] polynomToPrint)
  75.     {
  76.         counterPrint++;
  77.         Console.WriteLine("Polynomial {0} is: {1}x2 + {2}x +{3}", counterPrint, polynomToPrint[2], polynomToPrint[1], polynomToPrint[0]);
  78.     }
  79.  
  80.     private static void PrintMulti(int[] polynomToPrint)
  81.     {
  82.         counterPrint++;
  83.         Console.WriteLine("Polynomial {5} is: {4}x4 + {3}x3 + {2}x2 + {1}x +{0}", polynomToPrint[0], polynomToPrint[1], polynomToPrint[2], polynomToPrint[3], polynomToPrint[4], counterPrint);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement