Thread: Looking to give script-like interface to program. (or something)

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Looking to give script-like interface to program. (or something)

    Basically I'm doing a small project for a math course that does root finding of functions, both polynomial and standard functions (using at most cos, sin, x, etc). Now I have a library with a class that acts as standard integer polynomial which takes in an array of coefficients and spreads it across an nth degree polynomial. It also calculates the polynomial at a given value (also integer). For the polynomial root finding algorithm it's pretty straightforward since the most I'm given is an array or vector of numbers.

    On the other hand I am to create a library for other functions such as cos(x) + x-2 - x^2, and the like.

    The problem is with the input. I can make the library take as parameters function objects as callbacks and just have the main program(mer) modify the source files. The only thing is I'm giving this as a program to the teacher, I don't want him to have to test if it works with different functions and modify and recompile everything himself (considering it's spread across three seperate projects).

    What I imagine is one of two things.
    1) design a "function" class that like the polynomial class represents an arbitrary function that's not a polynomial, and can evaluate it at x
    2) take a file that has functions typed in it and my program parses it for what it is and can evaluate it.

    I personally don't know what to choose as I don't know how I can represent an arbitrary function as completely as I can a polynomial outside of a string.

    Anyone have an idea if I can have input as a script, and it could just use basic <cmath> library functions, or do I need to parse everything myself?
    Last edited by indigo0086; 01-26-2008 at 03:40 PM.

  2. #2
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    I worked on a very similar problem before, as far as I knew I had to parse the mathematical equation myself, break it down and evaluate as appropriate. However, I took the method of having the user type the equation in to the program (console based) and then the program would output the equation (line-by-line) as it was being evaluated until it was solved for the intended variable.

    I'm not sure how strong your skills are in character array manipulating, but unless you find a better way then your up for a little bit of a challenge. I wish I could just simply post the code, but I didn't end up keeping the source (wasn't fully complete and I had no reason to continue the project after the class was complete). If you want to discuss anything further, since I have at least done something very similar in the past, just shoot me a msg and I'll be happy to discuss it with ya.

    Regards

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I was thinking I had to do that as well.

    What I'm now thinking is that in the algorithms class I would make a basic shell class with a virtual overloaded function call operator like this

    Code:
    class funCallback
    {
        string name;
    
        public:
            funCallback(string label) : name(label) {}
            string getName() { return name; }
            virtual ~funCallback() {}
    };
    
    class cosCallback : public funCallback
    {
        public:
            cosCallback(): funCallback("cos") {}
            float operator()(int a) { return cos(a); }
    };
    
    class sinCallback : public funCallback
    {
        public:
            sinCallback(): funCallback("sin") {}
            float operator()(int a) { return sin(a); }
    };
    Then when building the function they would go into an array of funCallback pointers.

    The only thing is I'm getting funky output when trying to call the operator

    Code:
        std::vector<funCallback*> a;
    
        a.push_back(new cosCallback());
        a.push_back(new sinCallback());
    
        int val = 4;
    
        for(int i = 0; i < a.size(); ++i)
        {
            funCallback* current = a.at(i);
    
            std::cout << current->getName()
                      << "(" << val << ")="
                      << *current(val)   //49: error: `current' cannot be used as a function
                      << std::endl;
        }
    Last edited by indigo0086; 01-26-2008 at 05:48 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You never point out where you get those errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    ah I did but I erased ii. It's where I make the call to funcallback in the vector loop routine.

    also can you not call function pointers polymorphically. I just tried it and it's saying the function object can't be used as a function.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    funCallback is a function pointer declaration I take it.
    So why are you trying to call a function pointer declaration? You need to create a variable and assign an address to the function you want to call!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    OK i corrected the code, but get the error that I can't use it as a function.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not show the code and the declaration of funCallback?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    It's all up there in the original post.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, I see.
    So what exactly is the code trying to do?
    funCallback is a class, but due to the odd naming, it appeared as a function pointer due to the way you tried to use it. That's why I typically add C before all classes, but whatever.
    So instead you were trying to create a new funCallback class instance and show... what? The constructor returns nothing?
    Are you trying to do a callback to somewhere?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    It was my sample code to see if my callback system will work

    The funCallback is a base class for different types of function objects. The derived classes will be stored in a vector of funCallback pointers. During runtime the appropriate derived class would be called. The only thing is it's not seeing the vector as holding pointers to function objects.

    something like

    Code:
    funCallback* a = new cosCallback;
    
    float i = *a(4);
    Last edited by indigo0086; 01-26-2008 at 06:03 PM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Surely you need to tell the compiler that you want the float variety of the conversion operator.

    Code:
                       << (float)*current(val)   //49: error: `current' cannot be used as a function
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It still won't explain how you were planning on using the code.
    The original code was
    Code:
    << *funCallback(val)
    I believe.
    But since funCallback is a class, a type, a * before it would be illegal.
    If you had a pointer, like now
    Code:
    << *current(val)
    Then it still wouldn't be correct since it's not a function pointer and calling a function pointer doesn't require the * before the variable name.
    You also tried to output the return value of the constructor.

    Perhaps what you need is to call a member function (probably virtual) that does the actual function call and returns though that function what the actual function call returned. That way your system with callback objects might work properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    current is a function object pointer.

    funCallback* current = a.at(i);

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, it's a pointer to a funCallback object.
    Based on what you want to do, why not overload a function called DoCallback or similar that takes an int, string or whatever you want the function to process?
    Then you can do
    Code:
    funCallback* a = new cosCallback;
    float i = a->DoCallback(4);
    I believe what you want is the class to do a callback to a function that processes the math and returns the result? The class itself should just provide as a wrapper to the actual function call, right?
    I don't know if it's possible to overload an operator to return a function pointer. Let me try that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  4. Execute Program Using VB Script
    By brett2 in forum Windows Programming
    Replies: 0
    Last Post: 08-30-2003, 12:40 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM