input as function

Hi , i'm confuse about function thing
normaly , a normal function is
Math_plus(int a,int b) then a + b something ...
now, instead of asking user for input a , input b , input operator +
can we just let user input thing like plus(a,b) ?

Ex :
old style : input a >> input operator >> input b >> use switch thing
new style : input operator(a,b) like plus(a,b)

that would reduce time consuming
That's an interesting concept, but don't count me in for typing the header file.
> can we just let user input thing like plus(a,b) ?

1. read in the input as a string
2. parse the string to extract its components: operation, first number, second number
3. evaluate the expression

Regular expressions would come in handy.
https://en.wikipedia.org/wiki/Regular_expression
http://en.cppreference.com/w/cpp/regex
If you are an absolute beginner, you may want to put this off for later:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <regex>
#include <cctype>
#include <sstream>

void eval_print( const std::string& input )
{
    std::cout << "evaluate '" << input << "'\n" ;

    static const std::string ws = "\\s*" ; // zero or more white spaces
    static const std::string word =  "(\\w+)"; // one or more alphanumeric characters (capture)
    static const std::string integer = "([+-]?\\d+)" ; // optional sign followed by one or more decimal digits (capture)
    static const std::regex command_re( ws + word + ws + "\\(" + ws + integer + ws + ',' + ws + integer + ws + "\\)" + ws ) ;

    std::smatch matches ;
    if( std::regex_match( input, matches, command_re ) )
    {
        std::string operation = matches[1] ; // capture 1: operation
        for( char& c : operation ) c = std::tolower(c) ; // convert to all lower case

        const int a = std::stoi( matches[2] ) ; // capture 2: integer, convert to int
        const int b = std::stoi( matches[3] ) ; // capture 3: integer, convert to int

        if( operation == "plus" ) std::cout << a << " + " << b << " == " << a+b << "\n\n" ;
        else if( operation == "minus" ) std::cout << a << " - " << b << " == " << a-b << "\n\n" ;
        // else other operations ...
        else std::cout << "unsupported operation '" << matches[1] << "'\n\n" ;
    }
    else std::cout << "badly formed input '" << input << "'\n\n" ;
}

int main()
{
    // you may want to ignore this part (this simulates user input for testing)
    std::stringbuf input_buffer( "Plus(54,-17)\n  MINUS( +54 ,   17 )  \n multiply(10,20)\n plus((10,20)\n" ) ;
    std::cin.rdbuf( std::addressof(input_buffer) ) ;
    /////////////////////////////////////////////////////////////

    std::string input ;
    while( std::cout << "? " && std::getline( std::cin, input ) ) eval_print(input) ;
}

http://coliru.stacked-crooked.com/a/1dd7816295b119c7
this trick is cool but hard
i would rather switch back to if condition ...
Topic archived. No new replies allowed.