SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Linq; | |
| 4 | using System.Text; | |
| 5 | using System.Threading.Tasks; | |
| 6 | ||
| 7 | - | namespace AddSubstractMultiplyPolynomial |
| 7 | + | namespace AddSubtractMultiplyPolynomials |
| 8 | {
| |
| 9 | class Polynomial | |
| 10 | {
| |
| 11 | private List<double> coefficients; | |
| 12 | ||
| 13 | public Polynomial() | |
| 14 | {
| |
| 15 | coefficients = new List<double>(); | |
| 16 | } | |
| 17 | ||
| 18 | public int PolynomialDegree | |
| 19 | {
| |
| 20 | get | |
| 21 | {
| |
| 22 | return coefficients.Count - 1; | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | public double this[int index] | |
| 27 | {
| |
| 28 | get | |
| 29 | {
| |
| 30 | if (index >= 0 && index < coefficients.Count) | |
| 31 | {
| |
| 32 | return coefficients[index]; | |
| 33 | } | |
| 34 | else | |
| 35 | {
| |
| 36 | return 0.0; | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | set | |
| 41 | {
| |
| 42 | if (index >= 0 && index < coefficients.Count) | |
| 43 | {
| |
| 44 | coefficients[index] = value; | |
| 45 | } | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | private void AddCoefficient(double coef) | |
| 50 | {
| |
| 51 | coefficients.Add(coef); | |
| 52 | } | |
| 53 | ||
| 54 | public void ReadPolynomial() | |
| 55 | {
| |
| 56 | Console.Write("Enter the polynomial degree: ");
| |
| 57 | int polynomialDegree = int.Parse(Console.ReadLine()); | |
| 58 | ||
| 59 | Console.WriteLine("Enter the polynomial coefficients:");
| |
| 60 | for (int i = 0; i <= polynomialDegree; ++i) | |
| 61 | {
| |
| 62 | coefficients.Add(double.Parse(Console.ReadLine())); | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | private void Normalize() | |
| 67 | {
| |
| 68 | if (PolynomialDegree >= 0) | |
| 69 | {
| |
| 70 | while (coefficients[PolynomialDegree] == 0.0) | |
| 71 | {
| |
| 72 | coefficients.RemoveAt(PolynomialDegree); | |
| 73 | ||
| 74 | } | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | public static Polynomial operator +(Polynomial x, Polynomial y) | |
| 79 | {
| |
| 80 | int biggerDegree = Math.Max(x.PolynomialDegree, y.PolynomialDegree); | |
| 81 | Polynomial result = new Polynomial(); | |
| 82 | ||
| 83 | double tmp = 0.0; | |
| 84 | for (int i = 0; i <= biggerDegree; ++i) | |
| 85 | {
| |
| 86 | tmp = 0.0; | |
| 87 | if (i <= x.PolynomialDegree) | |
| 88 | {
| |
| 89 | tmp += x[i]; | |
| 90 | } | |
| 91 | if (i <= y.PolynomialDegree) | |
| 92 | {
| |
| 93 | tmp += y[i]; | |
| 94 | } | |
| 95 | ||
| 96 | result.AddCoefficient(tmp); | |
| 97 | } | |
| 98 | ||
| 99 | result.Normalize(); | |
| 100 | return result; | |
| 101 | } | |
| 102 | ||
| 103 | public static Polynomial operator -(Polynomial x) | |
| 104 | {
| |
| 105 | Polynomial result = new Polynomial(); | |
| 106 | for (int i = 0; i <= x.PolynomialDegree; ++i) | |
| 107 | {
| |
| 108 | result.AddCoefficient(-x[i]); | |
| 109 | } | |
| 110 | return result; | |
| 111 | } | |
| 112 | ||
| 113 | public static Polynomial operator -(Polynomial x, Polynomial y) | |
| 114 | {
| |
| 115 | Polynomial result = x + (-y); | |
| 116 | return result; | |
| 117 | } | |
| 118 | ||
| 119 | public static Polynomial operator *(Polynomial x, Polynomial y) | |
| 120 | {
| |
| 121 | Polynomial result = new Polynomial(); | |
| 122 | for (int i = 0; i <= x.PolynomialDegree; ++i) | |
| 123 | {
| |
| 124 | for (int j = 0; j <= y.PolynomialDegree; ++j) | |
| 125 | {
| |
| 126 | if (result.PolynomialDegree < i + j) | |
| 127 | {
| |
| 128 | result.AddCoefficient(0.0); | |
| 129 | } | |
| 130 | result[i + j] += x[i] * y[j]; | |
| 131 | } | |
| 132 | } | |
| 133 | result.Normalize(); | |
| 134 | return result; | |
| 135 | } | |
| 136 | ||
| 137 | public override string ToString() | |
| 138 | {
| |
| 139 | StringBuilder result = new StringBuilder("");
| |
| 140 | for (int i = PolynomialDegree; i >= 0; --i) | |
| 141 | {
| |
| 142 | if (i != PolynomialDegree) | |
| 143 | {
| |
| 144 | result.Append(" ");
| |
| 145 | } | |
| 146 | result.Append(coefficients[i].ToString()); | |
| 147 | } | |
| 148 | return result.ToString(); | |
| 149 | } | |
| 150 | } | |
| 151 | - | class AddSubstractMultiplyPolynomial |
| 151 | + | class AddSubtractMultiplyPolynomial |
| 152 | {
| |
| 153 | static void Main(string[] args) | |
| 154 | {
| |
| 155 | Polynomial a = new Polynomial(); | |
| 156 | Polynomial b = new Polynomial(); | |
| 157 | ||
| 158 | a.ReadPolynomial(); | |
| 159 | b.ReadPolynomial(); | |
| 160 | ||
| 161 | - | Polynomial r = a * b; |
| 161 | + | Polynomial r = a + b; |
| 162 | - | Console.WriteLine(r.ToString()); |
| 162 | + | Console.WriteLine("Addition: " + r.ToString());
|
| 163 | r = a - b; | |
| 164 | Console.WriteLine("Subtraction: " + r.ToString());
| |
| 165 | r = a * b; | |
| 166 | Console.WriteLine("Multiplication: " + r.ToString());
| |
| 167 | ||
| 168 | } | |
| 169 | } | |
| 170 | } |