Advertisement
MBrendecke

Zahlen

Mar 23rd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.99 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. using static System.Console;
  5. using static System.Math;
  6.  
  7. namespace Zahlen {
  8.     static class Zahlen {
  9.         static void Main(string[] args) {
  10.             string[] ergebnisse = new string[2];
  11.  
  12. #if DEBUG
  13.             string function = "((x * x) - 2 / (x + 8 * x) + 15)";
  14.             ergebnisse[0] = Aufgabe1(function, -15);      
  15. #else
  16.             string function = "";
  17.             do {
  18.                 WriteLine("Bitte geben Sie einen Funktionsterm ein.");
  19.                 Write("f(x) = ");
  20.                 function = ReadLine();
  21.             } while (String.IsNullOrWhiteSpace(function));
  22.  
  23.             string value = "";
  24.             double result;
  25.             do {
  26.                 WriteLine("Bitte geben Sie einen Wert für 'x' ein.");
  27.                 Write("x = ");
  28.                 value = ReadLine();
  29.             } while (!Double.TryParse(value, out result));
  30.  
  31.             ergebnisse[0] = Aufgabe1(function, result);
  32. #endif
  33.  
  34.             ergebnisse[1] = Aufgabe2(23245);
  35.  
  36.             for (int i = 0; i < ergebnisse.Length; i++) {
  37.                 WriteLine($@"Ergebnis für Aufgabe #{i + 1}: {ergebnisse[i]}");
  38.             }
  39.  
  40.             WriteLine($"\nFunktionswerte für f(x) = {function}\n");
  41.             for (int i = -10; i <= 10; i++) {
  42.                 WriteLine($@"f({i}) = {Aufgabe1(function, i)}");
  43.             }
  44.  
  45.             WriteLine("\nEnde des Programms");
  46.             ReadKey();
  47.         }
  48.  
  49.         static string Aufgabe1(string function, double value) {
  50.             function = function.ToLower().Replace("x", value.ToString()).Replace(" ", "");
  51.  
  52.             string filterDigits = @"-?([0-9]*\,[0-9]+|[0-9]+)";
  53.             string filterOperators = @"*/+-";
  54.             string filterTestSubterm = $@"\(({filterDigits}[{filterOperators}]{filterDigits})[^)]*\)";
  55.  
  56.             var valuesInBraches = Regex.Matches(function, filterTestSubterm);
  57.  
  58.             foreach (var valueInBrache in valuesInBraches) {
  59.                 string temp = valueInBrache.ToString().Replace("(", "").Replace(")", "");
  60.                 function = function.Replace(valueInBrache.ToString(), Aufgabe1(temp, value));
  61.             }            
  62.  
  63.             function = function.Replace("(", "").Replace(")", "");            
  64.  
  65.             foreach (char filterOperator in filterOperators) {
  66.                 do {
  67.                     string filter = $@"{filterDigits}\{filterOperator}{filterDigits}";
  68.  
  69.                     var subTerm = Regex.Match(function, filter);
  70.                     var subTermValues = Regex.Matches(subTerm.Value, filterDigits);
  71.  
  72.                     if (subTerm.Length <= 0)
  73.                         break;
  74.  
  75.                     Func<double, double, double> calc = null;
  76.  
  77.                     switch (filterOperator) {
  78.                         case '*':
  79.                             calc = (x, y) => x * y;
  80.                             break;
  81.                         case '/':
  82.                             calc = (x, y) => x / y;
  83.                             break;
  84.                         case '+':
  85.                             calc = (x, y) => x + y;
  86.                             break;
  87.                         case '-':
  88.                             calc = (x, y) => x - Abs(y);
  89.                             break;
  90.                         default:
  91.                             break;
  92.                     }
  93.  
  94.                     if (filterOperator == '/' && Double.Parse(subTermValues[1]?.Value) == 0)
  95.                         return "Asymptote";
  96.  
  97.                     double ergebnis = calc(Double.Parse(subTermValues[0]?.Value), Double.Parse(subTermValues[1]?.Value));
  98.                     function = function.Replace(subTerm.Value, ergebnis >= 0 ? "+" + ergebnis.ToString() : ergebnis.ToString());
  99.                     function = function.Replace("+-", "-").Replace("-+", "-").Replace("++", "+");
  100.                 } while (true);
  101.             }
  102.            
  103.             return function.TrimStart('+');
  104.         }
  105.  
  106.         static string Aufgabe2(long input) {
  107.             string temp = input.ToString();
  108.  
  109.             for (int i = 0; i < temp.Length - 1; i++) {                
  110.                 if (temp[i] <= temp[i + 1]) {
  111.                     continue;
  112.                 } else {
  113.                     /*
  114.                      * Übertrag mittels Modulo zur Basis 10^i bestimmen.
  115.                      * Sei { z, i aus N | z = 12345 und i = [0..5] }. Dann gilt,
  116.                      * 00005 := 12345 mod (10^0)
  117.                      * 00045 := 12345 mod (10^1)
  118.                      * ...
  119.                      * 12345 := 12345 mod (10^5)
  120.                      */
  121.                     long diff = Int64.Parse(temp.Substring(i, temp.Length - i));
  122.                     diff %= (long)Pow(10, temp.Length - i - 1);
  123.                     input -= diff + 1;
  124.  
  125.                     // Schleife Zurücksetzen
  126.                     i = -1;
  127.                     temp = input.ToString();
  128.                 }
  129.             }
  130.  
  131.             return input.ToString();
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement