Advertisement
ScorpS

Sum Subtraction Multiplication

Jan 29th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1.  
  2. //Extend the program to support also subtraction and multiplication of polynomials.
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. class SubtractionMultiplication12
  11. {
  12.     static List<int> firstPolynom = new List<int> { 2, 3, 4, 8 };    // this is (8x^3 + 4x^2 + 3x + 2)
  13.     static List<int> secondPolynom = new List<int> { 1, 0, -5 };  // (-5x^2 + 0x + 1)
  14.     static List<int> newPolynom = new List<int>();
  15.  
  16.     private static List<int> AddsTwoPolynomials(List<int> firstPolynom, List<int> secondPolynom)
  17.     {
  18.         int maxLength = Math.Max(firstPolynom.Count, secondPolynom.Count);
  19.         int minLength = Math.Min(firstPolynom.Count, secondPolynom.Count);
  20.  
  21.         // sum numbers of polynoms until the shorter ended
  22.         for (int i = 0; i < minLength; i++)
  23.         {
  24.             newPolynom.Add(firstPolynom[i] + secondPolynom[i]);
  25.         }
  26.  
  27.         // here check which of polynoms is bigger and copy the numbers that are stay from bigger one
  28.         if (firstPolynom.Count > secondPolynom.Count)
  29.         {
  30.             for (int i = minLength; i < maxLength; i++)
  31.             {
  32.                 newPolynom.Add(firstPolynom[i]);
  33.             }
  34.         }
  35.  
  36.         if (firstPolynom.Count < secondPolynom.Count)
  37.         {
  38.             for (int i = minLength; i < maxLength; i++)
  39.             {
  40.                 newPolynom.Add(secondPolynom[i]);
  41.             }
  42.         }
  43.  
  44.         return newPolynom;
  45.     }
  46.  
  47.  
  48.     private static void SubtractionTwoPolynoms()
  49.     {
  50.         int maxLength = Math.Max(firstPolynom.Count, secondPolynom.Count);
  51.         int minLength = Math.Min(firstPolynom.Count, secondPolynom.Count);
  52.  
  53.         // suntracts numbers of polynoms until the shorter ended
  54.         for (int i = 0; i < minLength; i++)
  55.         {
  56.             newPolynom.Add(firstPolynom[i] - secondPolynom[i]);
  57.         }
  58.  
  59.         // here check which of polynoms is bigger and copy the numbers that are stay from bigger one
  60.         if (firstPolynom.Count > secondPolynom.Count)
  61.         {
  62.             for (int i = minLength; i < maxLength; i++)
  63.             {
  64.                 newPolynom.Add(firstPolynom[i]);
  65.             }
  66.         }
  67.  
  68.         if (firstPolynom.Count < secondPolynom.Count)
  69.         {
  70.             for (int i = minLength; i < maxLength; i++)
  71.             {
  72.                 newPolynom.Add(secondPolynom[i]);
  73.             }
  74.         }
  75.     }
  76.  
  77.  
  78.     private static void MultiplicationTwoPolynoms()
  79.     {
  80.         List<List<int>> polynomSums = new List<List<int>>();
  81.         int count = 0;
  82.  
  83.         // here we multiply polynoms. Example (x + 2) * (x + 3) = (3x + 6) + (x^2 + 2x + 0)
  84.         // we add 0 to be easy after that to sum each other
  85.         for (int i = 0; i < secondPolynom.Count; i++)
  86.         {
  87.             count++;
  88.             polynomSums.Add(new List<int>());
  89.             for (int k = 0; k < firstPolynom.Count; k++)                                                                                                                                                
  90.             {
  91.                 polynomSums[i].Add(firstPolynom[k] * secondPolynom[i]);
  92.                 if (k == firstPolynom.Count - 1)
  93.                 {
  94.                     for (int p = 1; p < count; p++)
  95.                     {
  96.                         polynomSums[i].Insert(0, 0);
  97.                     }
  98.                 }
  99.             }
  100.             // prints all steps for easier to understand -> uncomment next row (all comment console.writeline rows are for help to understand)
  101.             //Console.WriteLine(string.Join(",  ", polynomSums[i]).PadRight(60));  
  102.         }
  103.         //Console.WriteLine("".PadLeft(60, '-'));
  104.  
  105.         // here added zeros, in that way we'll easy make sums after that
  106.         int lastIndex = polynomSums.Count - 1;
  107.         for (int k = 0; k < polynomSums.Count - 1; k++)
  108.         {
  109.             for (int i = polynomSums[k].Count - 1; i < (polynomSums[lastIndex].Count - 1); i++)
  110.             {
  111.                 polynomSums[k].Add(0);
  112.             }
  113.         }
  114.  
  115.         // makes sums in order every member that is for example x^2 is sum with all other x^2
  116.         for (int i = 0; i < polynomSums[0].Count; i++)
  117.         {
  118.             newPolynom.Add(0);
  119.  
  120.             for (int k = 0; k < polynomSums.Count; k++)
  121.             {
  122.                 newPolynom[i] += (polynomSums[k][i]);
  123.             }
  124.         }
  125.  
  126.         // prints sum of rows for help in addition to previous help row
  127.         //Console.WriteLine(string.Join(",  ", newPolynom).PadRight(60));
  128.     }
  129.  
  130.     static void Main()
  131.     {
  132.         Console.WriteLine("Choose action you want to do.");
  133.         Console.WriteLine("1 for adds");
  134.         Console.WriteLine("2 for substraction");
  135.         Console.WriteLine("3 for multiplication");
  136.         int choice = int.Parse(Console.ReadLine());
  137.  
  138.         switch (choice)
  139.         {
  140.             case 1: newPolynom = AddsTwoPolynomials(firstPolynom, secondPolynom); break;
  141.             case 2: SubtractionTwoPolynoms(); break;
  142.             case 3: MultiplicationTwoPolynoms(); break;
  143.         }
  144.  
  145.         firstPolynom.Reverse();
  146.         secondPolynom.Reverse();
  147.         newPolynom.Reverse();
  148.         Console.WriteLine();
  149.         Console.WriteLine("here prints the two polynoms which type is (x^n + ... + x^2 + x + 1)");
  150.         Console.WriteLine(string.Join("   ", firstPolynom).PadLeft(60));
  151.         Console.WriteLine(string.Join("   ", secondPolynom).PadLeft(60));
  152.         Console.WriteLine("".PadLeft(60, '-'));
  153.         Console.WriteLine(string.Join("   ", newPolynom).PadLeft(60));
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement