Thread: pass mathematical expression as a command line argument

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    82

    pass mathematical expression as a command line argument

    Hi

    I have a simple program to evaluate integrals via monte-carlo.

    Currently the program has a hard coded expression on which to perform the integration, but I'd like to be able to pass in a string as an argument to the program so that I don't have to edit the source each time.

    Actually I can come up with a perl or sed script to change the line in question fairly easily, but then I'd have to save under another name, and also it didn't appear very elegant.

    I could also grab the string from the command line and try matching to predefined strings in a switch statement, which would tell the program what to do if the string matched. But I'd be very limited in the range of expressions I could use.

    Any quick ideas from anybody?

    Many thanks!

  2. #2
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by stabu View Post
    Hi

    I have a simple program to evaluate integrals via monte-carlo.

    Currently the program has a hard coded expression on which to perform the integration, but I'd like to be able to pass in a string as an argument to the program so that I don't have to edit the source each time.

    Actually I can come up with a perl or sed script to change the line in question fairly easily, but then I'd have to save under another name, and also it didn't appear very elegant.

    I could also grab the string from the command line and try matching to predefined strings in a switch statement, which would tell the program what to do if the string matched. But I'd be very limited in the range of expressions I could use.

    Any quick ideas from anybody?

    Many thanks!
    you could start from here.
    Do not stare at my avatar.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    Thanks for your response, cph.

    That article is indeed a start, it talks about accepting arguments on the command line at the most basic level.

    However, I'd hate to start that far back. Also, I'm not a beginner, so I'd be reading stuff I already know. In fact, I did read it. In any case, that material is reproduced many times all over the internet and in many books.

    May be I didn't explain properly. I have a variable in my program, called x. The program evaluates exp(-x*x) and prints the results of that mathematical function.

    Instead of exp() and the negative of the square, I want it to evaluate sin(x)/x for example, and I want to this function identified at the command line. As is, C, will not allow a string to be interpreted, so a common solution is to match the string on the command line to an internally held string. If the match occurs, then the program can have code corresponding to sin(x)/x and will evaluate it.

    However, I want to try out very many functions, with small changes each time, and the "string match; then do this" method becomes very awkward. I believe it is a sort of reflective programming I want to do, and C doesn't allow this, so I was looking for other alternatives.

  4. #4
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by stabu View Post
    Thanks for your response, cph.

    That article is indeed a start, it talks about accepting arguments on the command line at the most basic level.

    However, I'd hate to start that far back. Also, I'm not a beginner, so I'd be reading stuff I already know. In fact, I did read it. In any case, that material is reproduced many times all over the internet and in many books.

    May be I didn't explain properly. I have a variable in my program, called x. The program evaluates exp(-x*x) and prints the results of that mathematical function.

    Instead of exp() and the negative of the square, I want it to evaluate sin(x)/x for example, and I want to this function identified at the command line. As is, C, will not allow a string to be interpreted, so a common solution is to match the string on the command line to an internally held string. If the match occurs, then the program can have code corresponding to sin(x)/x and will evaluate it.

    However, I want to try out very many functions, with small changes each time, and the "string match; then do this" method becomes very awkward. I believe it is a sort of reflective programming I want to do, and C doesn't allow this, so I was looking for other alternatives.
    hmm...you probably could combine that (command line args) with this
    Do not stare at my avatar.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    More specifically, you're probably looking for an implementation of something like this : Shunting-yard algorithm - Wikipedia, the free encyclopedia. Adding function such as trig or log are, as they say, an exercise for the reader. As the example code on that page shows, this isn't a trivial problem.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    hmm .. yes, I see where this is going now guys, thanks, ... a mini-project.

    I think I might do a perl script to replace the line for the time being. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-03-2010, 10:34 AM
  2. use of command line argument
    By natrajdreams in forum C Programming
    Replies: 2
    Last Post: 04-19-2009, 11:07 AM
  3. command line argument
    By csit in forum C Programming
    Replies: 4
    Last Post: 09-19-2007, 11:35 AM
  4. command line argument
    By Lord CyKill in forum Linux Programming
    Replies: 1
    Last Post: 04-11-2003, 02:05 PM
  5. '*' as a command line argument
    By kooma in forum C Programming
    Replies: 6
    Last Post: 02-26-2002, 02:18 PM