Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleApp2
- {
- class Program
- {
- static void Main(string[] args)
- {
- string shit = "19.5*x + (-15)";
- Console.WriteLine(Parser(shit));
- }
- public static bool IsZero(double number)
- {
- return System.Math.Abs(number) <= 10e-6;
- }
- public static string SolvePolynom(double first, double second, double third)
- {
- if (IsZero(first))
- {
- if (IsZero(second))
- {
- if (IsZero(third))
- return "1";
- return "no roots";
- }
- else
- {
- return (-third) / second + "";
- }
- }
- else
- {
- var discriminant = second * second - 4 * first * third;
- if (discriminant < 0)
- {
- return "no roots";
- }
- else
- {
- return (System.Math.Sqrt(discriminant) - second) / (2 * first) + "";
- }
- }
- }
- static string Parser(string text)
- {
- var coefs = new double[3];
- var shit = text.Split('+');
- foreach (var s in shit)
- {
- var power = 2;
- var number = new StringBuilder();
- for (int i = 0; i < s.Length; i++)
- {
- if (char.IsLetter(s[i]))
- {
- if (i == s.Length || s[i + 1] != '^')
- power = 1;
- else power = 0;
- break;
- }
- if (char.IsDigit(s[i]) || s[i] == '.' || s[i] == '-')
- {
- number.Append(s[i]);
- }
- }
- var numberStr = number.ToString();
- if (string.IsNullOrEmpty(numberStr)) coefs[power] = 1;
- else if (numberStr == "-") coefs[power] = -1;
- else coefs[power] = double.Parse(numberStr, System.Globalization.CultureInfo.InvariantCulture);
- }
- return SolvePolynom(coefs[0], coefs[1], coefs[2]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement