Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace Курсовая_Часть_5
  9. {
  10.     //exceptions about empty stack - user is dumbass can't use brackets properly
  11.  
  12.     public class Parser
  13.     {
  14.         public double parse(string input, double x1)
  15.         {
  16.             List<string> opz = OPZ(input, x1);
  17.             double result = Calculate(opz);
  18.             //Console.WriteLine("\nResult is {0}", result);
  19.             return result;
  20.         }
  21.         private static bool IsOperation(char c)
  22.         {
  23.             if (c == '+' ||
  24.                 c == '-' ||
  25.                 c == '*' ||
  26.                 c == '/' ||
  27.                 c == '^')
  28.                 return true;
  29.             else
  30.                 return false;
  31.         }
  32.  
  33.         private static int GetOperationPriority(char c)
  34.         {
  35.             switch (c)
  36.             {
  37.                 case '+': return 1;
  38.                 case '-': return 1;
  39.                 case '*': return 2;
  40.                 case '/': return 2;
  41.                 case '^': return 3;
  42.                 default: return 0;
  43.             }
  44.         }
  45.  
  46.         private static double ApplyOperation(string operation, double op1, double op2)
  47.         {
  48.             switch (operation)
  49.             {
  50.                 case "+": return (op1 + op2);
  51.                 case "-": return (op1 - op2);
  52.                 case "*": return (op1 * op2);
  53.                 case "/": return (op1 / op2);
  54.                 case "^": return Math.Pow(op1, op2);
  55.                 default: return 0;
  56.             }
  57.         }
  58.         private static List<string> OPZ(string input, double x1)
  59.         {
  60.             input = input.Replace("x", Convert.ToString(x1));
  61.             /*if (input.Contains("--"))
  62.             {
  63.                 input = input.Replace("--", "+");
  64.             }*/
  65.             Stack<char> operationsStack = new Stack<char>();
  66.             char lastOperation;
  67.             List<string> result = new List<string>();
  68.             string temp;
  69.             input = input.Replace(" ", "");
  70.             int iterator = 0;
  71.             for (int i = 0; i < input.Length; i++)
  72.             {
  73.                 if (Char.IsDigit(input[i]) || Char.IsLetter(input[i]))
  74.                 {
  75.                     temp = Convert.ToString(input[i]);
  76.                     iterator = i+1;
  77.                     while (iterator <input.Length && (Char.IsDigit(input[iterator]) || Char.IsLetter(input[iterator]) || input[iterator] == '.'))
  78.                     {
  79.                         temp += Convert.ToString(input[iterator]);
  80.                         iterator++;
  81.                     }
  82.                     result.Add(temp);
  83.                     i = iterator-1;
  84.                 }
  85.                 else if (IsOperation(input[i]))
  86.                 {
  87.                     if (input[i].Equals('-'))
  88.                     {
  89.                         if (i == 0 || IsOperation(input[i - 1]) || input[i-1].Equals('('))
  90.                             result.Add("0");
  91.                     }
  92.                     if (!(operationsStack.Count == 0))
  93.                     {
  94.                         lastOperation = operationsStack.Peek();
  95.                         if (GetOperationPriority(lastOperation) < GetOperationPriority(input[i]))
  96.                         {
  97.                             operationsStack.Push(input[i]);
  98.                         }
  99.                         else
  100.                         {
  101.                             result.Add(Convert.ToString(operationsStack.Pop()));
  102.                             operationsStack.Push(input[i]);
  103.                         }
  104.                     }
  105.                     else
  106.                     {
  107.                         operationsStack.Push(input[i]);
  108.                     }
  109.                 }
  110.                 else if (input[i].Equals('('))
  111.                 {
  112.                     operationsStack.Push(input[i]);
  113.                 }
  114.                 else if (input[i].Equals(')'))
  115.                 {
  116.                     while (operationsStack.Peek() != '(')
  117.                     {
  118.                         result.Add(Convert.ToString(operationsStack.Pop()));
  119.                     }
  120.                     operationsStack.Pop();
  121.                 }
  122.             }
  123.             while (!(operationsStack.Count == 0))
  124.             {
  125.                 result.Add(Convert.ToString(operationsStack.Pop()));
  126.             }
  127.             /*foreach (String strings in result) {
  128.                 Console.Write(strings);
  129.             }*/
  130.             return result;
  131.         }
  132.  
  133.         private static double Calculate(List<string> opzstring)
  134.         {
  135.             Stack<double> numbersStack = new Stack<double>();
  136.             double op1, op2;
  137.             /*for (int i = 0; i < opzstring.Count; i++)
  138.             {
  139.                 if (Char.IsDigit(opzstring[i]))
  140.                     numbersStack.Push(double.Parse(opzstring[i].ToString()));
  141.                 else
  142.                 {
  143.                     op2 = numbersStack.Pop();
  144.                     op1 = numbersStack.Pop();
  145.                     numbersStack.Push(ApplyOperation(opzstring[i], op1, op2));
  146.                 }
  147.             }*/
  148.             foreach(String strings in opzstring)
  149.             {
  150.                 if (!(IsOperation(Convert.ToChar(strings[0]))))
  151.                     numbersStack.Push(double.Parse(strings));
  152.                 else
  153.                 {
  154.                     op2 = numbersStack.Pop();
  155.                     op1 = numbersStack.Pop();
  156.                     numbersStack.Push(ApplyOperation(strings, op1, op2));
  157.                 }
  158.             }
  159.             return numbersStack.Pop();
  160.         }
  161.     }
  162.     //в фибоначчи нужно вычислять значение функции в той или иной точке
  163.     //парсер заключается в просто поиске необходимых выражений и перегрузке, а потом возвращении нужного
  164.     class Fibonacci
  165.     {
  166.         public void MethodFibonacci()
  167.         {
  168.             //Parser parser = new Parser();
  169.             Console.WriteLine("Enter left and right values of interval: ");
  170.             double a = Convert.ToDouble(Console.ReadLine());
  171.             double b = Convert.ToDouble(Console.ReadLine());
  172.             double Ln = 0.00000001;
  173.             List<int> fib = calculateNumbers(a, b, Ln);
  174.             double xmin = methodFibonacci(a, b, Ln, fib);
  175.             Console.WriteLine("xmin = {0}", xmin);
  176.             Console.ReadKey();
  177.         }
  178.         private static List<int> calculateNumbers(double a, double b, double Ln)
  179.         {
  180.             List<int> listOfNumbers = new List<int>();
  181.             listOfNumbers.Add(1);
  182.             listOfNumbers.Add(1);
  183.             while(listOfNumbers.ElementAt(listOfNumbers.Count()-1) < ((b - a) / Ln))
  184.             {
  185.                 listOfNumbers.Add(listOfNumbers.ElementAt(listOfNumbers.Count()-1) + listOfNumbers.ElementAt(listOfNumbers.Count() - 2));
  186.             }
  187.             return listOfNumbers;
  188.         }
  189.  
  190.         private static double methodFibonacci(double a, double b, double Ln, List<int> fib)
  191.         {
  192.             Parser parser = new Parser();
  193.  
  194.             double EPS = (b - a) / (fib.ElementAt(fib.Count()-1)+1);
  195.             double L = Math.Abs(b - a);
  196.  
  197.             int n = fib.Count();
  198.             int k = 1;
  199.             double fibelem1 = fib.ElementAt(n - 3);
  200.             double fibelem2 = fib.ElementAt(n - 2);
  201.             double fibelem3 = fib.ElementAt(n - 1);
  202.             double x1 = a + (fibelem1 / fibelem3 * (b - a));
  203.             double x2 = a + (fibelem2 / fibelem3 * (b - a));
  204.             double a1 = a;
  205.             double b1 = b;
  206.             double xmin;
  207.             Console.WriteLine("Enter function: ");
  208.             string function = Console.ReadLine();
  209.             x1 = 2.5;
  210.             double y1 = parser.parse(function, x1);
  211.             double y2 = parser.parse(function, x2);
  212.             while (k != n - 2)
  213.             {
  214.                 if(parser.parse(function,x1) < parser.parse(function,x2))
  215.                 {
  216.                     b1 = x2;
  217.                     L = Math.Abs(b1 - a1);
  218.                     x2 = x1;
  219.                     //y2 = y1;
  220.                     //if mistakes first check indexes cause there they are starts from 0 and in matlab they are starts from 1
  221.                     fibelem1 = fib.ElementAt(n - k - 3);
  222.                     fibelem2 = fib.ElementAt(n - k - 1);
  223.                     x1 = a1 + L * fibelem1 / fibelem2;
  224.                     //y1 = parser.parse(function, x1);
  225.                 }
  226.                 else
  227.                 {
  228.                     a1 = x1;
  229.                     L = Math.Abs(b1 - a1);
  230.                     x1 = x2;
  231.                     y1 = y2;
  232.                     fibelem1 = fib.ElementAt(n - k - 2);
  233.                     fibelem2 = fib.ElementAt(n - k - 1);
  234.                     x2 = a1 + L * fibelem1 / fibelem2;
  235.                     y2 = parser.parse(function, x2);
  236.                 }
  237.                 k++;
  238.             }
  239.  
  240.             x2 = x1 + EPS;
  241.             if (parser.parse(function, x1) > parser.parse(function, x2))
  242.             {
  243.                 xmin = (x1 + b1) / 2;
  244.             }
  245.             else
  246.                 xmin = (a1 + x2) / x2;
  247.  
  248.             return xmin;
  249.         }
  250.     }
  251.  
  252.     class Program
  253.     {
  254.         static void Main(string[] args)
  255.         {
  256.             //Parser pars = new Parser();
  257.             Fibonacci fib = new Fibonacci();
  258.             //string func = Console.ReadLine();
  259.             //pars.parse(func);
  260.             //Console.ReadKey();
  261.             fib.MethodFibonacci();
  262.             Console.ReadKey();
  263.         }
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement