public CalculationResult Calculate(string equation)
 {
     CalculateAll(equation);
     foreach (var result in results.Where(result => result.HasResult))
     {
         return(result);
     }
     return(CalculationResult.NoResult());
 }
Esempio n. 2
0
        public override CalculationResult Calculate(string equation)
        {
            try
            {
                var requestString = "submit=Submit%20to%20Octave&commands=" + HttpUtility.UrlEncode(equation);

                var    encoding = new ASCIIEncoding();
                byte[] data     = encoding.GetBytes(requestString);

                var request = (HttpWebRequest)WebRequest.Create(URLBase);
                request.ServicePoint.Expect100Continue = false;
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                var stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Flush();

                var reader = new StreamReader(request.GetResponse().GetResponseStream());
                var query  = reader.ReadToEnd();


                var match = resultMatch.Match(query);

                var lines = match.Value.Split(new[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                if (lines.Length > 0)
                {
                    if (lines.Length > 1)
                    {
                        var result = ""; int i = 0;
                        foreach (var line in lines.Skip(1))
                        {
                            if (i == 0)
                            {
                                result += "[" + whiteSpaceMatch.Replace(line.Trim(), ",") + "]";
                            }
                            else
                            {
                                result += ",[" + whiteSpaceMatch.Replace(line.Trim(), ",") + "]";
                            }
                            i++;
                        }
                        return(new CalculationResult(result, equation + " = " + result, true));
                    }
                    var splitres = lines[0].Split(new[] { '=' });
                    return(new CalculationResult(splitres[1], equation + " = " + RemoveAns(lines[0]), false));
                }
                return(CalculationResult.NoResult());
            }
            catch (Exception)
            {
                return(CalculationResult.NoResult());
            }
        }
        public override CalculationResult Calculate(string equation)
        {
            try
            {
                var client = new WebClient();
                var eq     = HttpUtility.UrlEncode(equation);
                var query  = client.DownloadString(URLBase + eq);
                var match  = resultMatch.Match(query);

                if (match.Success)
                {
                    var result = match.Value;
                    result = ParseSpecial(result);
                    var splitres = result.Split(new[] { '=' });
                    return(new CalculationResult(splitres[1], result, false));
                }
                return(new CalculationResult());
            }
            catch (Exception)
            {
                return(CalculationResult.NoResult());
            }
        }