Rectangle 27 20

I needed to do this myself and since I couldn't find any answers online, here's what I worked out. This uses a simple SOAP calculator service with an 'Add' method that takes two numbers and returns the sum.

public async Task<int> AddNumbersAsync(Uri uri, int a, int b)
{
    var soapString = this.ConstructSoapRequest(a, b);
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("SOAPAction", "http://CalculatorService/ICalculatorService/Add");
        var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
        using (var response = await client.PostAsync(uri, content))
        {
            var soapResponse = await response.Content.ReadAsStringAsync();
            return this.ParseSoapResponse(soapResponse);
        }
    }
}

private string ConstructSoapRequest(int a, int b)
{
    return String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
    <s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
        <s:Body>
            <Add xmlns=""http://CalculatorService/"">
                <a>{0}</a>
                <b>{1}</b>
            </Add>
        </s:Body>
    </s:Envelope>", a, b);
}

private int ParseSoapResponse(string response)
{
    var soap = XDocument.Parse(response);
    XNamespace ns = "http://CalculatorService/";
    var result = soap.Descendants(ns + "AddResponse").First().Element(ns + "AddResult").Value;
    return Int32.Parse(result);
}

Great answer, please note that HttpClient is intended to be created once and reused for many requests with the same configuration. So I suggest that you create a static member and reuse it across multiple calls to your sample AddNumbersAsync

@Alireza Valid point. I was only using my code for testing some things so it didn't really matter in my case but for a more formal service that's certainly the right decision.

HttpClient & SOAP (C#) - Stack Overflow

c# soap httpclient
Rectangle 27 3

In a simple terms if you have a web service of calculator. WSDL tells about the functions that you can implement or exposed to the client. For example: add, delete, subtract and so on. Where as using SOAP you actually perform actions like doDelete(), doSubtract(), doAdd(). So SOAP and WSDL are apples and oranges. We should not compare them. They both have their own different functionality.

web services - Difference between a SOAP message and a WSDL? - Stack O...

web-services soap wsdl
Rectangle 27 3

In a simple terms if you have a web service of calculator. WSDL tells about the functions that you can implement or exposed to the client. For example: add, delete, subtract and so on. Where as using SOAP you actually perform actions like doDelete(), doSubtract(), doAdd(). So SOAP and WSDL are apples and oranges. We should not compare them. They both have their own different functionality.

java - Difference between a SOAP message and a WSDL? - Stack Overflow

java web-services soap wsdl
Rectangle 27 4

Since you didn't provide file sample, I'll show the simple (1st column) approach, using Unix's calculator bc and transforming newlines into "+" using tr command - you need an extra 0 at the end since the newline after the last number will create an extra "+" at the very end:

$ cat a
1
2
3
$ cat a | tr "\012" "+" ; echo "0"
1+2+3+0
$ (cat a | tr "\012" "+" ; echo "0") |bc
6

However, I'm not quite sure why you need to avoid Perl, as it's very easy to do as a one-liner:

> perl5.8 -ae '{map {$sum+=$_} <>; print "$sum\n"}' a
6

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

perl - Linux shell sum columns - Stack Overflow

linux perl shell sum
Rectangle 27 4

Since you didn't provide file sample, I'll show the simple (1st column) approach, using Unix's calculator bc and transforming newlines into "+" using tr command - you need an extra 0 at the end since the newline after the last number will create an extra "+" at the very end:

$ cat a
1
2
3
$ cat a | tr "\012" "+" ; echo "0"
1+2+3+0
$ (cat a | tr "\012" "+" ; echo "0") |bc
6

However, I'm not quite sure why you need to avoid Perl, as it's very easy to do as a one-liner:

> perl5.8 -ae '{map {$sum+=$_} <>; print "$sum\n"}' a
6

perl - Linux shell sum columns - Stack Overflow

linux perl shell sum
Rectangle 27 1

using System;

namespace Lab_Ex_12
{
    class SimpleCalculator
    {
        double num1, num2;
        public void read()
        {
            Console.WriteLine("\n Enter any two numbers:");
            Console.Write("\n Number1 : ");
            num1 = double.Parse(Console.ReadLine());
            Console.Write("\n Number2 : ");
            num2 = double.Parse(Console.ReadLine());
        }
        public void add()
        {
            double sum = num1 + num2;
            Console.WriteLine("\n Result : ({0}) + ({1}) = {2}", num1, num2, sum);
        }
        public void subtract()
        {
            double diff = num1 - num2;
            Console.WriteLine("\n Result : ({0}) - ({1}) = {2}", num1, num2, diff);
        }
        public void multiply()
        {
            double prod = num1 * num2;
            Console.WriteLine("\n Result : ({0}) X ({1}) = {2}", num1, num2, prod);
        }
        public void divide()
        {
            double qt = num1 / num2;
            Console.WriteLine("\n Result : ({0}) / ({1}) = {2}", num1, num2, qt);
        }
    }
    class ArithmeticOperations
    {
        public static void Main()
        {
            SimpleCalculator SC = new SimpleCalculator();
            int ch, i=1;
            while(i==1)
            {
                Console.Clear();
                Console.WriteLine("\n *************************");
                Console.WriteLine("\n   ZAHID SIMPLE CALCULATOR.");
                Console.WriteLine("\n *************************");
                Console.WriteLine("\n 1-----> ADDITION");
                Console.WriteLine("\n 2-----> SUBTRACTION");
                Console.WriteLine("\n 3-----> MULTIPLICATION");
                Console.WriteLine("\n 4-----> DIVISION");
                Console.WriteLine("\n 5-----> EXIT");
                Console.WriteLine("\n *************************");
                Console.Write("\n\n Enter your choice: ");
                ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1: SC.read();
                            SC.add();
                            break;
                    case 2: SC.read();
                            SC.subtract();
                            break;
                    case 3: SC.read();
                            SC.multiply();
                            break;
                    case 4: SC.read();
                            SC.divide();
                            break;
                    case 5: Environment.Exit(-1);
                            break;
                    default: Console.WriteLine(" Sorry !!! Wrong  choice.");
                            break;
                }
                Console.Write("\n Press ENTER to Continue. ");
                Console.ReadLine();
            }
            Console.WriteLine("\n Cannot continue... Bye");
        }
    }
}

c# - Math.Sin, Math.Cos and Math.Tan precision and way to display them...

c# .net winforms math
Rectangle 27 4

I would suggest cheating and using the Shunting Yard Algorithm. It's an easy means of writing a simple calculator-type parser and takes precedence into account.

If you want to properly tokenise things and have variables, etc. involved then I would go ahead and write a recursive descent parser as suggested by others here, however if you simply require a calculator-style parser then this algorithm should be sufficient :-)

algorithm - Equation (expression) parser with precedence? - Stack Over...

algorithm parsing equation
Rectangle 27 0

Yes, but it doesn't matter. Any one extract a square root lately, by using paper and pencil? We use a calculator or simple call a function in our favorite programming language. How many of us know what the calculator does or even the function?

You should know how things work, The better programmer, or anything else, you want to be, the more you should know. But there are only so many hours in day.

Better tools make us better. The trade off is we lose little control over the process.

Do smarter compilers, languages, and frameworks make dumber programmer...

language-design