C-Bär
Hält's aus hier
Beiträge: 2
|
Verfasst: Do 02.02.12 19:15
Wäre cool, wenn da mal jemand drüberschauen würde und evt. Optimerungsvorschläge machen könnte
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
| private void Calculate_Click(object sender, EventArgs e) { string functioninput, saveinput; functioninput = Input.Text; if (functioninput.Trim() == "") { functioninput = "0"; } saveinput = functioninput.Replace("+1x", "+x").Replace("-1x", "-x").Replace("f(x)", "").Replace("=", ""); functioninput = MakeStringReadable(functioninput); functioninput = functioninput.Replace("-", "+-"); string[] singleaddends = functioninput.Trim().Split(new[] { "+" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < singleaddends.Length; i++) { if (!singleaddends[i].Contains("x^")) { singleaddends[i] = singleaddends[i] + "x^0"; } } double[,] factorexponent = new double[2, singleaddends.Length]; for (int i = 0; i < singleaddends.Length; i++) { string[] ram = singleaddends[i].Split(new[] { "x^" }, StringSplitOptions.RemoveEmptyEntries); factorexponent[0, i] = Convert.ToDouble(ram[0]); factorexponent[1, i] = Convert.ToDouble(ram[1]); } double[,] f1arr = new double[2, factorexponent.Length]; double[,] f2arr = new double[2, factorexponent.Length]; double[,] f3arr = new double[2, factorexponent.Length]; double[,] Farr = new double[2, factorexponent.Length]; Farr = Sortieren(Stammfunktion(factorexponent)); f1arr = Sortieren(Ableiten(factorexponent, 1)); f2arr = Sortieren(Ableiten(factorexponent, 2)); f3arr = Sortieren(Ableiten(factorexponent, 3)); GradLabel.Text = "Es liegt eine Funktion " + Grade(factorexponent) + ". Grades vor.\n"; StammfunktionLabel.Text = StammfunktionToString(Farr); InputFunction.Text = "f(x) = " + saveinput; A1Label.Text = AbleitungsfunktionToString(f1arr, 1); A2Label.Text = AbleitungsfunktionToString(f2arr, 2); A3Label.Text = AbleitungsfunktionToString(f3arr, 3); } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
| static string MakeStringReadable(string functioninput) { functioninput = functioninput.Replace("f(x)", ""); functioninput = functioninput.Replace("=", ""); functioninput = functioninput.Trim(); functioninput = functioninput.Replace(" ", ""); functioninput = "Beginn:" + functioninput; functioninput = functioninput.Replace("Beginn:x", "1x"); functioninput = functioninput.Replace("+x", "+1x"); functioninput = functioninput.Replace("-x", "-1x"); functioninput = functioninput.Replace("Beginn:", ""); for (int i = 0; i < functioninput.Length; i++) { if ((functioninput[i] == Convert.ToChar("x")) && (i + 1 == functioninput.Length)) { functioninput = functioninput.Remove(i, 1); functioninput = functioninput.Insert(i, "x^1"); } else { if ((functioninput[i] == Convert.ToChar("x")) && (functioninput[i + 1] != Convert.ToChar("^"))) { functioninput = functioninput.Remove(i, 1); functioninput = functioninput.Insert(i, "x^1"); } } } return functioninput; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
| static double[,] Ableiten(double[,] function, int times) { double[,] output = new double[2, function.GetLength(1)]; for (int i = 0; i < function.GetLength(1); i++) { if (function[1, i] == 0) { output[0, i] = 0; function[1, i] = 0; } else { output[0, i] = function[0, i] * function[1, i]; output[1, i] = function[1, i] - 1; } } if (times > 1) { return Ableiten(output, times - 1); } else { return output; } } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
| static double[,] Stammfunktion(double[,] function) { double[,] output = new double[2, function.GetLength(1)]; for (int i = 0; i < function.GetLength(1); i++) { output[0, i] = function[0, i] / (function[1, i] + 1); output[1, i] = function[1, i] + 1; } output = Zusammenfassen(output); return output; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
| static int Grade(double[,] function) { int counter = Convert.ToInt32(function[1, 0]); for (int i = 1; i < function.GetLength(1); i++) { if (function[1, i] > counter) { counter = Convert.ToInt32(function[1, i]); } } return counter; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
| static double[,] Zusammenfassen(double[,] function) { int count = 1; int size = function.GetLength(1); for (int i = 0; i < size; i++) { for (int j = count; j < size; j++) { if (function[1, i] == function[1, j]) { function[0, i] = function[0, i] + function[0, j]; function[0, j] = 0; function[1, j] = 0; } } count = count + 1; } return function; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
| static double[,] Sortieren(double[,] function) { double[,] output = new double[2, function.GetLength(1)]; double[] exp = new double[function.GetLength(1)]; double[] bases = new double[function.GetLength(1)]; for (int i = 0; i < function.GetLength(1); i++) { exp[i] = function[1, i]; } Array.Sort(exp); Array.Reverse(exp); for (int i = 0; i < function.GetLength(1); i++) { for (int j = 0; j < function.GetLength(1); j++) { if (exp[i] == function[1, j]) { bases[i] = function[0, j]; } } } for (int i = 0; i < function.GetLength(1); i++) { output[0, i] = bases[i]; output[1, i] = exp[i]; } return output; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
| static string AbleitungsfunktionToString(double[,] arr, int grad) { string strich = new String(Convert.ToChar("\'"), grad); string output = "f" + strich + "(x) = "; arr = Zusammenfassen(arr); for (int i = 0; i < arr.GetLength(1); i++) { output += "+" + Math.Round(arr[0, i], 2) + "x^" + Math.Round(arr[1, i], 2); } output = output.Replace("+1x", "+x"); output = output.Replace("-1x", "-x"); output = output.Replace("x^0", ""); output = output.Replace("x^1", "x"); output = output.Replace("+0", ""); output = output.Replace("-0", ""); output = output.Replace("+-", "-"); output = output.Replace(" +", " "); output = output.TrimEnd('-', '+'); if (output == "f" + strich + "(x) = ") { output = output + "0"; } return output; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
| static string StammfunktionToString(double[,] arr) { string output = "F(x) = "; for (int i = 0; i < arr.GetLength(1); i++) { output += "+" + Math.Round(arr[0, i], 2) + "x^" + Math.Round(arr[1, i], 2); } output = output.Replace("+1x", "+x"); output = output.Replace("-1x", "-x"); output = output.Replace("+0x", ""); output = output.Replace("-0x", ""); output = output.Replace("+-", "-"); output = output.Replace(" +", " "); output = output.Replace("x^1", "x"); output = output.Replace("x^0", ""); output = output.Replace("^0", ""); return output; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
| private void Clear_Click(object sender, EventArgs e) { Input.Text = ""; StammfunktionLabel.Text = ""; A1Label.Text = ""; A2Label.Text = ""; A3Label.Text = ""; InputFunction.Text = ""; GradLabel.Text = ""; } |
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9:
| private void Form1_Load(object sender, EventArgs e) { Input.Focus(); }
private void Abbrechen_Click(object sender, EventArgs e) { Application.Exit(); } |
Danke
|