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 = "2.6*x^2 + (-0.9)*x + (-3.5)";
- 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 List<double>();
- var shit = text.Split('+');
- foreach (var s in shit)
- {
- var number = new StringBuilder();
- for (int i = 0; i < s.Length; i++)
- {
- if (char.IsLetter(s[i])) break;
- if (char.IsDigit(s[i]) || s[i] == '.' || s[i] == '-')
- {
- number.Append(s[i]);
- }
- }
- var numberStr = number.ToString();
- if (string.IsNullOrEmpty(numberStr)) coefs.Add(1);
- else if (numberStr == "-") coefs.Add(-1);
- else coefs.Add(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