Advertisement
Teodor92

11. 12. Polinomal

Jan 15th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class PolynomalMultiplication
  6. {
  7.     static void PolyAddition(int[] firstPoly, int[] secondPoly)
  8.     {
  9.         List<int> finallPoly = new List<int>();
  10.  
  11.         if (firstPoly.Length >= secondPoly.Length)
  12.         {
  13.             for (int i = 0; i < firstPoly.Length; i++)
  14.             {
  15.                 finallPoly.Add(firstPoly[i] + secondPoly[i]);
  16.             }
  17.         }
  18.         else
  19.         {
  20.             for (int i = 0; i < secondPoly.Length; i++)
  21.             {
  22.                 finallPoly.Add(firstPoly[i] + secondPoly[i]);
  23.             }
  24.         }
  25.  
  26.         StringBuilder finalPolyStr = new StringBuilder();
  27.         if (finallPoly[0] != 0)
  28.         {
  29.             finalPolyStr.AppendFormat("{0} ", finallPoly[0]);
  30.         }
  31.         for (int i = 1; i < finallPoly.Count; i++)
  32.         {
  33.             if (finallPoly[i] != 0 && finallPoly[i] > 0)
  34.             {
  35.                 finalPolyStr.AppendFormat("+ {0}^x{1} ", finallPoly[i], i);
  36.             }
  37.             if (finallPoly[i] < 0)
  38.             {
  39.                 finalPolyStr.AppendFormat("{0}^x{1} ", finallPoly[i], i);
  40.             }
  41.         }
  42.         Console.WriteLine(finalPolyStr.ToString());
  43.     }
  44.     static void PolySubtraction(int[] firstPoly, int[] secondPoly)
  45.     {
  46.         List<int> finallPoly = new List<int>();
  47.  
  48.         if (firstPoly.Length >= secondPoly.Length)
  49.         {
  50.             for (int i = 0; i < firstPoly.Length; i++)
  51.             {
  52.                 finallPoly.Add(firstPoly[i] - secondPoly[i]);
  53.             }
  54.         }
  55.         else
  56.         {
  57.             for (int i = 0; i < secondPoly.Length; i++)
  58.             {
  59.                 finallPoly.Add(firstPoly[i] - secondPoly[i]);
  60.             }
  61.         }
  62.  
  63.         StringBuilder finalPolyStr = new StringBuilder();
  64.         if (finallPoly[0] != 0)
  65.         {
  66.             finalPolyStr.AppendFormat("{0} ", finallPoly[0]);
  67.         }
  68.         for (int i = 1; i < finallPoly.Count; i++)
  69.         {
  70.             if (finallPoly[i] != 0 && finallPoly[i] > 0)
  71.             {
  72.                 finalPolyStr.AppendFormat("+ {0}^x{1} ", finallPoly[i], i);
  73.             }
  74.             if (finallPoly[i] < 0)
  75.             {
  76.                 finalPolyStr.AppendFormat("{0}^x{1} ", finallPoly[i], i);
  77.             }
  78.         }
  79.         Console.WriteLine(finalPolyStr.ToString());    
  80.     }
  81.     static void PolyMultiplication(int[] firstPoly, int[] secondPoly)
  82.     {
  83.         int[] finalPoly = new int[firstPoly.Length + secondPoly.Length];
  84.         int startingCell = 0;
  85.         for (int i = 0; i < firstPoly.Length; i++)
  86.         {
  87.             startingCell = i;
  88.             for (int j = 0; j < secondPoly.Length; j++)
  89.             {
  90.                 finalPoly[startingCell] = finalPoly[startingCell] + (secondPoly[j] * firstPoly[i]);
  91.                 startingCell++;
  92.             }
  93.         }
  94.  
  95.         StringBuilder finalPolyStr = new StringBuilder();
  96.         if (finalPoly[0] != 0)
  97.         {
  98.             finalPolyStr.AppendFormat("{0} ", finalPoly[0]);
  99.         }
  100.         for (int i = 1; i < finalPoly.Length; i++)
  101.         {
  102.             if (finalPoly[i] != 0 && finalPoly[i] > 0)
  103.             {
  104.                 finalPolyStr.AppendFormat("+ {0}^x{1} ", finalPoly[i], i);
  105.             }
  106.             if (finalPoly[i] < 0)
  107.             {
  108.                 finalPolyStr.AppendFormat("{0}^x{1} ", finalPoly[i], i);
  109.             }
  110.         }
  111.         Console.WriteLine(finalPolyStr.ToString());
  112.     }
  113.     static void Main()
  114.     {
  115.         int[] firstPoly = { 3, -1, 4, -3, -2 };
  116.         int[] secondPoly = { 8, 1, 3, 0, 0 };
  117.  
  118.         Console.WriteLine("Addition");
  119.         PolyAddition(firstPoly, secondPoly);
  120.  
  121.         Console.WriteLine("Substracion");
  122.         PolySubtraction(firstPoly, secondPoly);
  123.  
  124.         Console.WriteLine("Multiplication");
  125.         PolyMultiplication(firstPoly, secondPoly);
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement