How to change handle range of integer

Discussion in 'C Programming' started by moonhkt, Feb 26, 2011.

  1. moonhkt

    moonhkt Guest

    Hi All

    How to using C to change input value "0-4,10,20,40-45" to to
    "0,1,2,3,4,10,20,40,41,42,43,44,45" ?

    moonhkt
     
    moonhkt, Feb 26, 2011
    #1
    1. Advertising

  2. moonhkt

    Tom St Denis Guest

    On Feb 26, 2:09 am, moonhkt <> wrote:
    > Hi All
    >
    > How to using C to change input value "0-4,10,20,40-45" to  to
    > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?
    >
    > moonhkt


    By finishing your homework assignment yourself.

    Tom
     
    Tom St Denis, Feb 26, 2011
    #2
    1. Advertising

  3. moonhkt <> writes:
    > How to using C to change input value "0-4,10,20,40-45" to to
    > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?


    By writing a program to do it.

    Show us what you've done, and we'll be glad to help.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 26, 2011
    #3
  4. moonhkt

    moonhkt Guest

    On Feb 26, 6:37 pm, Keith Thompson <> wrote:
    > moonhkt <> writes:
    > > How to using C to change input value "0-4,10,20,40-45" to  to
    > > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?

    >
    > By writing a program to do it.
    >
    > Show us what you've done, and we'll be glad to help.
    >
    > --
    > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something.  This is something.  Therefore, we must do this."
    >     -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Try to read crontab file , build run schedule by month.


    /* gcc str_split.c -o str_split */

    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>

    #define MAX_FIELD_SIZE 1023


    typedef unsigned char bool;

    enum { UNSELECTED=0, COLUMN, FIELD } ModeType;

    bool field_list[MAX_FIELD_SIZE]; /* list of selected fields */
    bool SupressFlag = 0; /* supress lines without delimiter symbol */
    // num ModeType mode = UNSELECTED; /* scan mode: column vs. field */
    unsigned char delimiter = '\t'; /* delimiter symbol for field
    separation */


    int Set_list();

    void main (int argc, char **argv)
    {
    int error = 0;

    char *p;
    p = "0-4,10,20,40-45";
    printf("main() %-30s\n",p); // p only
    error = Set_list(p); // p only
    printf("error = %3d\n", error);

    printf("# %-30s\n",field_list[1]);


    }

    /*
    * Set_list - Parse field argument list and set selected fields
    */
    int Set_list (char *list)
    {
    bool setFieldFlag = 0; /* flag to indicate if any fields were set */
    bool contFlag = 0; /* flag to continue setting fields for a range (i-
    j) */
    unsigned prevField = 0; /* previous field selected */
    unsigned field; /* field position */

    while (*list)
    {
    switch (*list) {
    case '-':
    if (contFlag) return(1);
    contFlag = 1; /* turn on continue flag */
    list++;
    break;
    case ',':
    contFlag = 0;
    list++;
    break;
    default:
    field = 0;
    while (*list >= '0' && *list <= '9')
    {
    field *= 10;
    field += *list - '0';
    ++list;
    }
    if (field == 0) return(1);
    else if (field > MAX_FIELD_SIZE) return(2);
    --field; /* decrement field # for array index */
    if (!contFlag) prevField = field;
    else contFlag = 0;
    while (prevField <= field)
    {
    field_list[prevField++] = 1;
    }
    setFieldFlag = 1; /* set field selected flag
    */
    } /* end switch */
    } /* end while */

    if (contFlag)
    {
    while (prevField <= MAX_FIELD_SIZE)
    {
    field_list[prevField++] = 1;
    }
    }

    if (setFieldFlag == 0) return 1; /* error - nothing selected
    */
    else return 0; /* okay */
    } /* end Set_list */
     
    moonhkt, Feb 26, 2011
    #4
  5. moonhkt <> writes:

    > On Feb 26, 6:37 pm, Keith Thompson <> wrote:
    >> moonhkt <> writes:
    >> > How to using C to change input value "0-4,10,20,40-45" to  to
    >> > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?

    >>
    >> By writing a program to do it.
    >>
    >> Show us what you've done, and we'll be glad to help.
    >>
    >> --
    >> Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    >> Nokia
    >> "We must do something.  This is something.  Therefore, we must do this."
    >>     -- Antony Jay and Jonathan Lynn, "Yes Minister"


    It's better to snip sig blocks.

    > Try to read crontab file , build run schedule by month.


    Why are you using C to do this? It looks like an ideal fit for a
    scripting language.

    > /* gcc str_split.c -o str_split */
    >
    > #include <stdio.h>
    > #include <io.h>
    > #include <fcntl.h>


    These look odd. A simple text processing program like this should not
    need anything other than standard headers.

    > #define MAX_FIELD_SIZE 1023
    >
    >
    > typedef unsigned char bool;
    >
    > enum { UNSELECTED=0, COLUMN, FIELD } ModeType;
    >
    > bool field_list[MAX_FIELD_SIZE]; /* list of selected fields */
    > bool SupressFlag = 0; /* supress lines without delimiter symbol */
    > // num ModeType mode = UNSELECTED; /* scan mode: column vs. field */
    > unsigned char delimiter = '\t'; /* delimiter symbol for field
    > separation */


    It's a shame to use globals. If you stick with C for scripts like this,
    one day you'll want to put this code into a bigger program and a clean
    functional API will make that easier.

    > int Set_list();


    This is not a prototype and prototypes are a Good Thing.

    > void main (int argc, char **argv)


    s/void/int/

    > {
    > int error = 0;
    >
    > char *p;
    > p = "0-4,10,20,40-45";
    > printf("main() %-30s\n",p); // p only
    > error = Set_list(p); // p only
    > printf("error = %3d\n", error);
    >
    > printf("# %-30s\n",field_list[1]);
    >
    >
    > }
    >
    > /*
    > * Set_list - Parse field argument list and set selected fields


    At least tell reader what that returned int means. BTW, "argument list"
    usually means something else in C. I'd say "parse fields from a
    string".

    > */
    > int Set_list (char *list)
    > {
    > bool setFieldFlag = 0; /* flag to indicate if any fields were set */
    > bool contFlag = 0; /* flag to continue setting fields for a range (i-
    > j) */
    > unsigned prevField = 0; /* previous field selected */
    > unsigned field; /* field position */
    >
    > while (*list)
    > {
    > switch (*list) {
    > case '-':
    > if (contFlag) return(1);
    > contFlag = 1; /* turn on continue flag */
    > list++;
    > break;
    > case ',':
    > contFlag = 0;
    > list++;
    > break;
    > default:
    > field = 0;
    > while (*list >= '0' && *list <= '9')
    > {
    > field *= 10;
    > field += *list - '0';
    > ++list;
    > }
    > if (field == 0) return(1);


    I have not looked at the rest in detail, but this jumps out at me.
    return 1 is an error, yes? field can be zero quite legitimately. In
    fact it is the first field in your test case.

    > else if (field > MAX_FIELD_SIZE) return(2);
    > --field; /* decrement field # for array index */
    > if (!contFlag) prevField = field;
    > else contFlag = 0;
    > while (prevField <= field)
    > {
    > field_list[prevField++] = 1;
    > }
    > setFieldFlag = 1; /* set field selected flag
    > */
    > } /* end switch */
    > } /* end while */
    >
    > if (contFlag)
    > {
    > while (prevField <= MAX_FIELD_SIZE)
    > {
    > field_list[prevField++] = 1;
    > }
    > }
    >
    > if (setFieldFlag == 0) return 1; /* error - nothing selected
    > */
    > else return 0; /* okay */
    > } /* end Set_list */
    >
    >
    >


    --
    Ben.
     
    Ben Bacarisse, Feb 26, 2011
    #5
  6. In article <>,
    moonhkt <> wrote:
    >On Feb 26, 6:37 pm, Keith Thompson <> wrote:
    >> moonhkt <> writes:
    >> > How to using C to change input value "0-4,10,20,40-45" to  to
    >> > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?

    >>
    >> By writing a program to do it.
    >>
    >> Show us what you've done, and we'll be glad to help.
    >>
    >> --
    >> Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    >> Nokia
    >> "We must do something.  This is something.  Therefore, we must do this."
    >>     -- Antony Jay and Jonathan Lynn, "Yes Minister"

    >
    >Try to read crontab file , build run schedule by month.


    (Rest snipped)

    Why do this in C at all?

    Assuming you're doing it because you want it done and not just as an
    exercise (and a stupid one at that), you should be doing it in AWK or
    Perl (or Python or Ruby or whatever).

    --
    Faced with the choice between changing one's mind and proving that there is
    no need to do so, almost everyone gets busy on the proof.

    - John Kenneth Galbraith -
     
    Kenny McCormack, Feb 26, 2011
    #6
  7. On Feb 26, 7:09 am, moonhkt <> wrote:
    > Hi All
    >
    > How to using C to change input value "0-4,10,20,40-45" to  to
    > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?
    >
    > moonhkt


    If you use Perl, you can just *eval* ranges containing hyphenated
    spans. There is a module called Set::IntSpan that is built on this
    principle:

    http://search.cpan.org/dist/Set-IntSpan/IntSpan.pm

    Martin
     
    MartinBroadhurst, Feb 26, 2011
    #7
  8. moonhkt

    moonhkt Guest

    On 2月27æ—¥, 上åˆ5時53分, MartinBroadhurst <>
    wrote:
    > On Feb 26, 7:09 am, moonhkt <> wrote:
    >
    > > Hi All

    >
    > > How to using C to change input value "0-4,10,20,40-45" to  to
    > > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?

    >
    > > moonhkt

    >
    > If you use Perl, you can just *eval* ranges containing hyphenated
    > spans. There is a module called Set::IntSpan that is built on this
    > principle:
    >
    > http://search.cpan.org/dist/Set-IntSpan/IntSpan.pm
    >
    > Martin



    Due to I try using C.
     
    moonhkt, Feb 27, 2011
    #8
  9. moonhkt <> might have writ, in news:185214db-457c-4f20-
    :

    In addition to what Ben pointed out, your problem is here:

    > bool field_list[MAX_FIELD_SIZE]; /* list of selected fields */
    > printf("# %-30s\n",field_list[1]);


    %s wants char *, you're giving it bool (unsigned char).
    When you fix this, remember that it is '1' (not 1) that prints as 1.

    (Kenny McCormack) might have writ, in
    news:ikbge2$k1$:
    > Why do this in C at all?
    >
    > Assuming you're doing it because you want it done and not just as an
    > exercise (and a stupid one at that), you should be doing it in AWK or
    > Perl (or Python or Ruby or whatever).


    Am I the only one who finds this suggestion particularly annoying?
    I've never learned Perl (or whatever), and am happy to use C even
    when better languages exist. I'm not fully too lazy to learn; the last
    several languages I learned included LaTex, Mathematica, Html, Css
    and Javascript. I also enjoy learning some linguistics, ancient
    history, etc. I wish I weren't so lazy and had learned even more.
    I'd have liked to learn juggling, piano playing, and several more of
    the Kama Sutra positions.

    To pretend that everyone who wants to be an adept C programmer need
    also be adept at Perl (or whatever) is almost preciously pretentious.

    And why is this a "stupid" exercise? It's an easy exercise, yes, but
    for its level of difficulty I don't know why it's particularly "stupid."


    ---
    James Dow Allen
     
    James Dow Allen, Feb 27, 2011
    #9
  10. On Sat, 26 Feb 2011 08:29:40 -0800 (PST), moonhkt <>
    wrote:

    >Try to read crontab file , build run schedule by month.
    >
    >
    >/* gcc str_split.c -o str_split */
    >
    >#include <stdio.h>
    >#include <io.h>
    >#include <fcntl.h>
    >
    >#define MAX_FIELD_SIZE 1023
    >
    >
    >typedef unsigned char bool;
    >
    >enum { UNSELECTED=0, COLUMN, FIELD } ModeType;
    >
    >bool field_list[MAX_FIELD_SIZE]; /* list of selected fields */
    >bool SupressFlag = 0; /* supress lines without delimiter symbol */
    >// num ModeType mode = UNSELECTED; /* scan mode: column vs. field */
    >unsigned char delimiter = '\t'; /* delimiter symbol for field
    >separation */
    >
    >
    >int Set_list();
    >
    >void main (int argc, char **argv)
    >{
    > int error = 0;
    >
    > char *p;
    > p = "0-4,10,20,40-45";
    > printf("main() %-30s\n",p); // p only
    > error = Set_list(p); // p only
    > printf("error = %3d\n", error);
    >
    > printf("# %-30s\n",field_list[1]);


    This can't be what you want. %s requires a pointer to char.
    field_list[1] is an unsigned char. This invokes undefined behavior.

    Furthermore, Set_list stores integer values in field_list which are
    not printable as characters. 1 is not the same a '1'.

    >
    >
    >}
    >
    >/*
    > * Set_list - Parse field argument list and set selected fields
    > */
    >int Set_list (char *list)
    >{
    > bool setFieldFlag = 0; /* flag to indicate if any fields were set */
    > bool contFlag = 0; /* flag to continue setting fields for a range (i-
    >j) */
    > unsigned prevField = 0; /* previous field selected */
    > unsigned field; /* field position */
    >
    > while (*list)
    > {
    > switch (*list) {
    > case '-':
    > if (contFlag) return(1);
    > contFlag = 1; /* turn on continue flag */
    > list++;
    > break;


    Your indenting is not achieving its intended purpose. It should
    provide a visible clue to the "scope" of a "block" of statements. All
    four of these statements are part of the case '-' "block and should be
    indented at the same level/

    > case ',':
    > contFlag = 0;
    > list++;
    > break;
    > default:
    > field = 0;
    > while (*list >= '0' && *list <= '9')
    > {
    > field *= 10;
    > field += *list - '0';
    > ++list;
    > }
    > if (field == 0) return(1);
    > else if (field > MAX_FIELD_SIZE) return(2);
    > --field; /* decrement field # for array index */
    > if (!contFlag) prevField = field;
    > else contFlag = 0;
    > while (prevField <= field)


    This is an even worse example. The while statement is not part of the
    range of the else. Your indenting makes it appear as if the while is
    only executed when the if is false.

    > {
    > field_list[prevField++] = 1;
    > }
    > setFieldFlag = 1; /* set field selected flag
    >*/
    > } /* end switch */
    > } /* end while */
    >
    > if (contFlag)
    > {
    > while (prevField <= MAX_FIELD_SIZE)
    > {
    > field_list[prevField++] = 1;


    This invokes undefined behavior when prevField equals MAX_FIELD_SIZE.
    The highest allowed index for field_list is MAX_FIELD_SIZE-1. You
    probably want <, not <=, in the preceding while statement.

    > }
    > }
    >
    > if (setFieldFlag == 0) return 1; /* error - nothing selected
    >*/
    > else return 0; /* okay */
    >} /* end Set_list */
    >
    >


    --
    Remove del for email
     
    Barry Schwarz, Feb 27, 2011
    #10
  11. moonhkt <> writes:
    > On 2月27æ—¥, 上åˆ5時53分, MartinBroadhurst <>
    > wrote:
    >> On Feb 26, 7:09 am, moonhkt <> wrote:
    >>
    >> > Hi All

    >>
    >> > How to using C to change input value "0-4,10,20,40-45" to  to
    >> > "0,1,2,3,4,10,20,40,41,42,43,44,45" ?

    >>
    >> > moonhkt

    >>
    >> If you use Perl, you can just *eval* ranges containing hyphenated
    >> spans. There is a module called Set::IntSpan that is built on this
    >> principle:
    >>
    >> http://search.cpan.org/dist/Set-IntSpan/IntSpan.pm

    >
    > Due to I try using C.


    I can't even figure out how many words you left out of that sentence.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 27, 2011
    #11
  12. moonhkt

    Hans Vlems Guest

    > > Due to I try using C.
    >
    > I can't even figure out how many words you left out of that sentence.
    >

    Possibly none when translated back to the OP's native language <g>
    Hans
     
    Hans Vlems, Feb 27, 2011
    #12
  13. In article <>,
    Hans Vlems <> wrote:
    >> > Due to I try using C.

    >>
    >> I can't even figure out how many words you left out of that sentence.
    >>

    >Possibly none when translated back to the OP's native language <g>
    >Hans


    And yet, for all that, it is perfectly clear what he meant.

    --
    Faced with the choice between changing one's mind and proving that there is
    no need to do so, almost everyone gets busy on the proof.

    - John Kenneth Galbraith -
     
    Kenny McCormack, Feb 27, 2011
    #13
  14. moonhkt

    moonhkt Guest

    On Feb 27, 9:44 pm, (Kenny McCormack)
    wrote:
    > In article <..com>,
    > Hans Vlems  <> wrote:
    >
    > >> > Due to I try using C.

    >
    > >> I can't even figure out how many words you left out of that sentence.

    >
    > >Possibly none when translated back to the OP's native language <g>
    > >Hans

    >
    > And yet, for all that, it is perfectly clear what he meant.
    >
    > --
    > Faced with the choice between changing one's mind and proving that there is
    > no need to do so, almost everyone gets busy on the proof.
    >
    >     - John Kenneth Galbraith -



    I found 'cut' source code in Internet, 'cut' using -c parameter. But I
    can not understanding, how Set_list and set field_list[prevField++]
    works. I try to add code to display field_list array. But, I still can
    not understand. (I am not a C programmer, I am UNIX script Writer)
     
    moonhkt, Feb 27, 2011
    #14
  15. moonhkt

    Hans Vlems Guest

    On 27 feb, 14:44, (Kenny McCormack) wrote:
    > In article <..com>,
    > Hans Vlems  <> wrote:
    >
    > >> > Due to I try using C.

    >
    > >> I can't even figure out how many words you left out of that sentence.

    >
    > >Possibly none when translated back to the OP's native language <g>
    > >Hans

    >
    > And yet, for all that, it is perfectly clear what he meant.
    >


    Absolutely :)
     
    Hans Vlems, Feb 27, 2011
    #15
  16. Hans Vlems <> writes:
    >> > Due to I try using C.

    >>
    >> I can't even figure out how many words you left out of that sentence.
    >>

    > Possibly none when translated back to the OP's native language <g>
    > Hans


    Hans, your newsreader should add attribution lines when you post
    a followup, such as the "Hans Vlems <> writes:"
    lines above. Please leave those lines in place. Knowing who said
    what makes it easier to follow the discussion.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 27, 2011
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    985
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    412
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    158
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. Randy Kramer
    Replies:
    12
    Views:
    385
    Robert Klemme
    Oct 25, 2007
  5. David Bird
    Replies:
    1
    Views:
    221
    Tiago Macedo
    Jun 23, 2008
Loading...

Share This Page