Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Text.RegularExpressions;
- namespace Курсовая_Часть_5
- {
- //exceptions about empty stack - user is dumbass can't use brackets properly
- public class Parser
- {
- public double parse(string input, double x1)
- {
- List<string> opz = OPZ(input, x1);
- double result = Calculate(opz);
- //Console.WriteLine("\nResult is {0}", result);
- return result;
- }
- private static bool IsOperation(char c)
- {
- if (c == '+' ||
- c == '-' ||
- c == '*' ||
- c == '/' ||
- c == '^')
- return true;
- else
- return false;
- }
- private static int GetOperationPriority(char c)
- {
- switch (c)
- {
- case '+': return 1;
- case '-': return 1;
- case '*': return 2;
- case '/': return 2;
- case '^': return 3;
- default: return 0;
- }
- }
- private static double ApplyOperation(string operation, double op1, double op2)
- {
- switch (operation)
- {
- case "+": return (op1 + op2);
- case "-": return (op1 - op2);
- case "*": return (op1 * op2);
- case "/": return (op1 / op2);
- case "^": return Math.Pow(op1, op2);
- default: return 0;
- }
- }
- private static List<string> OPZ(string input, double x1)
- {
- input = input.Replace("x", Convert.ToString(x1));
- /*if (input.Contains("--"))
- {
- input = input.Replace("--", "+");
- }*/
- Stack<char> operationsStack = new Stack<char>();
- char lastOperation;
- List<string> result = new List<string>();
- string temp;
- input = input.Replace(" ", "");
- int iterator = 0;
- for (int i = 0; i < input.Length; i++)
- {
- if (Char.IsDigit(input[i]) || Char.IsLetter(input[i]))
- {
- temp = Convert.ToString(input[i]);
- iterator = i+1;
- while (iterator <input.Length && (Char.IsDigit(input[iterator]) || Char.IsLetter(input[iterator]) || input[iterator] == '.'))
- {
- temp += Convert.ToString(input[iterator]);
- iterator++;
- }
- result.Add(temp);
- i = iterator-1;
- }
- else if (IsOperation(input[i]))
- {
- if (input[i].Equals('-'))
- {
- if (i == 0 || IsOperation(input[i - 1]) || input[i-1].Equals('('))
- result.Add("0");
- }
- if (!(operationsStack.Count == 0))
- {
- lastOperation = operationsStack.Peek();
- if (GetOperationPriority(lastOperation) < GetOperationPriority(input[i]))
- {
- operationsStack.Push(input[i]);
- }
- else
- {
- result.Add(Convert.ToString(operationsStack.Pop()));
- operationsStack.Push(input[i]);
- }
- }
- else
- {
- operationsStack.Push(input[i]);
- }
- }
- else if (input[i].Equals('('))
- {
- operationsStack.Push(input[i]);
- }
- else if (input[i].Equals(')'))
- {
- while (operationsStack.Peek() != '(')
- {
- result.Add(Convert.ToString(operationsStack.Pop()));
- }
- operationsStack.Pop();
- }
- }
- while (!(operationsStack.Count == 0))
- {
- result.Add(Convert.ToString(operationsStack.Pop()));
- }
- /*foreach (String strings in result) {
- Console.Write(strings);
- }*/
- return result;
- }
- private static double Calculate(List<string> opzstring)
- {
- Stack<double> numbersStack = new Stack<double>();
- double op1, op2;
- /*for (int i = 0; i < opzstring.Count; i++)
- {
- if (Char.IsDigit(opzstring[i]))
- numbersStack.Push(double.Parse(opzstring[i].ToString()));
- else
- {
- op2 = numbersStack.Pop();
- op1 = numbersStack.Pop();
- numbersStack.Push(ApplyOperation(opzstring[i], op1, op2));
- }
- }*/
- foreach(String strings in opzstring)
- {
- if (!(IsOperation(Convert.ToChar(strings[0]))))
- numbersStack.Push(double.Parse(strings));
- else
- {
- op2 = numbersStack.Pop();
- op1 = numbersStack.Pop();
- numbersStack.Push(ApplyOperation(strings, op1, op2));
- }
- }
- return numbersStack.Pop();
- }
- }
- //в фибоначчи нужно вычислять значение функции в той или иной точке
- //парсер заключается в просто поиске необходимых выражений и перегрузке, а потом возвращении нужного
- class Fibonacci
- {
- public void MethodFibonacci()
- {
- //Parser parser = new Parser();
- Console.WriteLine("Enter left and right values of interval: ");
- double a = Convert.ToDouble(Console.ReadLine());
- double b = Convert.ToDouble(Console.ReadLine());
- double Ln = 0.00000001;
- List<int> fib = calculateNumbers(a, b, Ln);
- double xmin = methodFibonacci(a, b, Ln, fib);
- Console.WriteLine("xmin = {0}", xmin);
- Console.ReadKey();
- }
- private static List<int> calculateNumbers(double a, double b, double Ln)
- {
- List<int> listOfNumbers = new List<int>();
- listOfNumbers.Add(1);
- listOfNumbers.Add(1);
- while(listOfNumbers.ElementAt(listOfNumbers.Count()-1) < ((b - a) / Ln))
- {
- listOfNumbers.Add(listOfNumbers.ElementAt(listOfNumbers.Count()-1) + listOfNumbers.ElementAt(listOfNumbers.Count() - 2));
- }
- return listOfNumbers;
- }
- private static double methodFibonacci(double a, double b, double Ln, List<int> fib)
- {
- Parser parser = new Parser();
- double EPS = (b - a) / (fib.ElementAt(fib.Count()-1)+1);
- double L = Math.Abs(b - a);
- int n = fib.Count();
- int k = 1;
- double fibelem1 = fib.ElementAt(n - 3);
- double fibelem2 = fib.ElementAt(n - 2);
- double fibelem3 = fib.ElementAt(n - 1);
- double x1 = a + (fibelem1 / fibelem3 * (b - a));
- double x2 = a + (fibelem2 / fibelem3 * (b - a));
- double a1 = a;
- double b1 = b;
- double xmin;
- Console.WriteLine("Enter function: ");
- string function = Console.ReadLine();
- x1 = 2.5;
- double y1 = parser.parse(function, x1);
- double y2 = parser.parse(function, x2);
- while (k != n - 2)
- {
- if(parser.parse(function,x1) < parser.parse(function,x2))
- {
- b1 = x2;
- L = Math.Abs(b1 - a1);
- x2 = x1;
- //y2 = y1;
- //if mistakes first check indexes cause there they are starts from 0 and in matlab they are starts from 1
- fibelem1 = fib.ElementAt(n - k - 3);
- fibelem2 = fib.ElementAt(n - k - 1);
- x1 = a1 + L * fibelem1 / fibelem2;
- //y1 = parser.parse(function, x1);
- }
- else
- {
- a1 = x1;
- L = Math.Abs(b1 - a1);
- x1 = x2;
- y1 = y2;
- fibelem1 = fib.ElementAt(n - k - 2);
- fibelem2 = fib.ElementAt(n - k - 1);
- x2 = a1 + L * fibelem1 / fibelem2;
- y2 = parser.parse(function, x2);
- }
- k++;
- }
- x2 = x1 + EPS;
- if (parser.parse(function, x1) > parser.parse(function, x2))
- {
- xmin = (x1 + b1) / 2;
- }
- else
- xmin = (a1 + x2) / x2;
- return xmin;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //Parser pars = new Parser();
- Fibonacci fib = new Fibonacci();
- //string func = Console.ReadLine();
- //pars.parse(func);
- //Console.ReadKey();
- fib.MethodFibonacci();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement