Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling sets of numbers from the command line with argv[]

Tags:

c

**If you're not interested in the background information explaining my reasoning skip to the very bottom for the end of my question.

I have a set of polynomial equations i need to read into my program to perform unit tests of polynomials such as

  • Synthetic division
  • Addition
  • Subtraction
  • Scale
  • Multiplication
  • Roots

so i need to be able to input a wide range of numbers to test the functions used in BRLCAD, though i am having trouble coming up with an efficient solution to read in numbers. My current approach:

Given the command line:

./unit_test sd 2 3 4 8 1 4 5

  • sd - perform synthetic division
  • 2 - degree of first poly
  • 3,4,8 - 3x^2 + 4x + 8
  • 1 - degree of second poly

writing an algorithm to read a set of numbers in this format is cumbersome and quite confusing for others to understand, plus i have yet to implement two other polynomials to be read in as the answers to be compared with.(as this is an open source file, i would like my work to have some transparency).

if(strcmp(argv[1],"sd") == 0){

    poly_eqn1.dgr = atoi(argv[2]);
    /* start at argv[3], run degree count plus one times */
    for(counter = 0; counter < (eqn1.dgr + 1); counter++)
        poly_eqn1.cf[counter] = atof(argv[counter+3]);

    poly_eqn2.dgr = atoi(argv[4 + poly_eqn1.dgr]);
    /* start at end of degree one counter */
    for(counter = 0; counter < (dgr2 + 1); counter++)
        poly_eqn2.cf[counter] = atof(argv[counter+5+dgr]);

    /* grab the answer from end of data */

    return test_synthetic(//input proper data...);
}

Would using sscanf be more efficent to read in my polynomial given a maximum degree is set? After writing the initial method i thought about using sscanf, but i'm unsure if using it in such a manner would be worth it as opposed to writing in two more polynomial read in's from above:

Given the maximum degree is four for a polynomial

./unit_test sd 2,0,0,2,4,5 1,0,0,0,2,3

  • 2,0,0,2,4,5 - degree 2, 0x^4 + 0x^3 + 2x^2 + 4x + 5
like image 461
Syntactic Fructose Avatar asked Dec 20 '12 02:12

Syntactic Fructose


2 Answers

If I were you, I would try hard to support your parameters in human readable format.

For example, it should be possible to parse input in following form:

./unit_test "(3*x^2+4*x+8)/(4*x+5)"

Granted, you may have to write lexical parser to understand this input, but it would be easiest to understand from user perspective.

like image 138
mvp Avatar answered Sep 24 '22 23:09

mvp


You really need not worry about efficiency when simply processing the command line arguments. Do so in the simplest, most straightforward fashion appropriate for your task. Spend your time worrying about efficiency where it matter - in tight loops with the actual number crunching algorithms.

like image 22
Jonathon Reinhart Avatar answered Sep 22 '22 23:09

Jonathon Reinhart