Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- using static System.Console;
- using static System.Math;
- namespace Zahlen {
- static class Zahlen {
- static void Main(string[] args) {
- string[] ergebnisse = new string[2];
- #if DEBUG
- string function = "((x * x) - 2 / (x + 8 * x) + 15)";
- ergebnisse[0] = Aufgabe1(function, -15);
- #else
- string function = "";
- do {
- WriteLine("Bitte geben Sie einen Funktionsterm ein.");
- Write("f(x) = ");
- function = ReadLine();
- } while (String.IsNullOrWhiteSpace(function));
- string value = "";
- double result;
- do {
- WriteLine("Bitte geben Sie einen Wert für 'x' ein.");
- Write("x = ");
- value = ReadLine();
- } while (!Double.TryParse(value, out result));
- ergebnisse[0] = Aufgabe1(function, result);
- #endif
- ergebnisse[1] = Aufgabe2(23245);
- for (int i = 0; i < ergebnisse.Length; i++) {
- WriteLine($@"Ergebnis für Aufgabe #{i + 1}: {ergebnisse[i]}");
- }
- WriteLine($"\nFunktionswerte für f(x) = {function}\n");
- for (int i = -10; i <= 10; i++) {
- WriteLine($@"f({i}) = {Aufgabe1(function, i)}");
- }
- WriteLine("\nEnde des Programms");
- ReadKey();
- }
- static string Aufgabe1(string function, double value) {
- function = function.ToLower().Replace("x", value.ToString()).Replace(" ", "");
- string filterDigits = @"-?([0-9]*\,[0-9]+|[0-9]+)";
- string filterOperators = @"*/+-";
- string filterTestSubterm = $@"\(({filterDigits}[{filterOperators}]{filterDigits})[^)]*\)";
- var valuesInBraches = Regex.Matches(function, filterTestSubterm);
- foreach (var valueInBrache in valuesInBraches) {
- string temp = valueInBrache.ToString().Replace("(", "").Replace(")", "");
- function = function.Replace(valueInBrache.ToString(), Aufgabe1(temp, value));
- }
- function = function.Replace("(", "").Replace(")", "");
- foreach (char filterOperator in filterOperators) {
- do {
- string filter = $@"{filterDigits}\{filterOperator}{filterDigits}";
- var subTerm = Regex.Match(function, filter);
- var subTermValues = Regex.Matches(subTerm.Value, filterDigits);
- if (subTerm.Length <= 0)
- break;
- Func<double, double, double> calc = null;
- switch (filterOperator) {
- case '*':
- calc = (x, y) => x * y;
- break;
- case '/':
- calc = (x, y) => x / y;
- break;
- case '+':
- calc = (x, y) => x + y;
- break;
- case '-':
- calc = (x, y) => x - Abs(y);
- break;
- default:
- break;
- }
- if (filterOperator == '/' && Double.Parse(subTermValues[1]?.Value) == 0)
- return "Asymptote";
- double ergebnis = calc(Double.Parse(subTermValues[0]?.Value), Double.Parse(subTermValues[1]?.Value));
- function = function.Replace(subTerm.Value, ergebnis >= 0 ? "+" + ergebnis.ToString() : ergebnis.ToString());
- function = function.Replace("+-", "-").Replace("-+", "-").Replace("++", "+");
- } while (true);
- }
- return function.TrimStart('+');
- }
- static string Aufgabe2(long input) {
- string temp = input.ToString();
- for (int i = 0; i < temp.Length - 1; i++) {
- if (temp[i] <= temp[i + 1]) {
- continue;
- } else {
- /*
- * Übertrag mittels Modulo zur Basis 10^i bestimmen.
- * Sei { z, i aus N | z = 12345 und i = [0..5] }. Dann gilt,
- * 00005 := 12345 mod (10^0)
- * 00045 := 12345 mod (10^1)
- * ...
- * 12345 := 12345 mod (10^5)
- */
- long diff = Int64.Parse(temp.Substring(i, temp.Length - i));
- diff %= (long)Pow(10, temp.Length - i - 1);
- input -= diff + 1;
- // Schleife Zurücksetzen
- i = -1;
- temp = input.ToString();
- }
- }
- return input.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement