enclose a negative number in columns

Discussion in 'C++' started by puzzlecracker, Dec 19, 2008.

  1. Say I have,

    char buffer[50];
    double d=-205;
    that contains a number (negative or positive), and I know what number
    it is, Now I need to enclose the number in the buffer with columns if
    it negative.

    sprintf(buffer,"%s", d);

    Here is the twist, the format varies, it can be "%s", can also be
    something else. So, after I constructed that buffer with number, want
    to enclose it with columns.

    Any suggestion how to enclose the end buffer with them?

    Thanks
    puzzlecracker, Dec 19, 2008
    #1
    1. Advertising

  2. On Dec 19, 11:38 am, Christian Hackl <> wrote:
    > puzzlecracker ha scritto:
    >
    > > Say I have,

    >
    > >   char buffer[50];
    > >  double d=-205;
    > > that contains a number (negative or positive), and I know what number
    > > it is, Now I need to enclose the number in the buffer with columns if
    > > it negative.

    >
    > > sprintf(buffer,"%s", d);

    >
    > > Here is the twist,  the format varies, it can be "%s", can also be
    > > something else. So, after I constructed that buffer with number, want
    > > to enclose it with columns.

    >
    > I fear I do not understand the problem. Why don't you simply use
    >
    > (if d < 0)
    >    sprintf(buffer, "%s", d)
    > else
    >    sprintf(buffer, "|%s|", d)
    >
    > ?
    >
    > --
    > Christian Hackl
    >



    well, I am given a final string with which contains a number. It's
    constructed based on various criteria, and it would be painful to
    modify every format (around 100 in total) that a number is
    constructed with.

    So I just want to take the final product and enclose it with columns
    puzzlecracker, Dec 19, 2008
    #2
    1. Advertising

  3. puzzlecracker

    LR Guest

    puzzlecracker wrote:
    > On Dec 19, 11:38 am, Christian Hackl <> wrote:
    >> puzzlecracker ha scritto:
    >>
    >>> Say I have,
    >>> char buffer[50];
    >>> double d=-205;
    >>> that contains a number (negative or positive), and I know what number
    >>> it is, Now I need to enclose the number in the buffer with columns if
    >>> it negative.
    >>> sprintf(buffer,"%s", d);
    >>> Here is the twist, the format varies, it can be "%s", can also be
    >>> something else. So, after I constructed that buffer with number, want
    >>> to enclose it with columns.

    >> I fear I do not understand the problem. Why don't you simply use
    >>
    >> (if d < 0)
    >> sprintf(buffer, "%s", d)
    >> else
    >> sprintf(buffer, "|%s|", d)
    >>
    >> ?
    >>
    >> --
    >> Christian Hackl
    >>

    >
    >
    > well, I am given a final string with which contains a number. It's
    > constructed based on various criteria, and it would be painful to
    > modify every format (around 100 in total) that a number is
    > constructed with.
    >
    > So I just want to take the final product and enclose it with columns


    I'm completely confused by this problem.

    1) What kind of number is in buffer after sprintf(buffer,"%s",d); ? This
    seems unwise to me.

    2) Is this any different than putting some other characters around the
    contents of whatever is in buffer if what's in buffer is not a number?

    3) What if buffer were empty, ie buffer[0] == 0

    4) Can you use std::string make the thing you want and copy it back?
    Before you start you'll have to test the length of whatever is in buffer
    to make sure that strlen(buffer) < sizeof(buffer)-3

    5) Or alternatively, append whatever character you want to the end of
    buffer, move all the characters in buffer to the "right", make buffer[0]
    = whatever character you want. Same tests for length apply.

    I feel like I must be missing some fundamental point about your question.

    LR
    LR, Dec 19, 2008
    #3
  4. puzzlecracker schrieb:
    > Say I have,
    >
    > char buffer[50];
    > double d=-205;
    > that contains a number (negative or positive), and I know what number
    > it is, Now I need to enclose the number in the buffer with columns if
    > it negative.
    >
    > sprintf(buffer,"%s", d);
    >
    > Here is the twist, the format varies, it can be "%s", can also be
    > something else. So, after I constructed that buffer with number, want
    > to enclose it with columns.
    >
    > Any suggestion how to enclose the end buffer with them?
    >
    > Thanks


    If I understand your problem correctly, how about:

    char buffer[50];
    double d=-205;

    char *p = buffer;

    // opening bracket
    if (d < 0)
    *p++ = '(';

    // format your number
    p += sprintf(p, "%s", d); // format mismatch?

    // closing bracket
    if (d < 0)
    *p++ = ')';


    Of course, as this is a C++ newsgroup, some std::string solution would
    be more appropriate, and here you might have to worry about the buffer
    length.
    Peter Remmers, Dec 20, 2008
    #4
  5. Peter Remmers schrieb:

    > If I understand your problem correctly, how about:
    >
    > char buffer[50];
    > double d=-205;
    >
    > char *p = buffer;
    >
    > // opening bracket
    > if (d < 0)
    > *p++ = '(';
    >
    > // format your number
    > p += sprintf(p, "%s", d); // format mismatch?
    >
    > // closing bracket
    > if (d < 0)
    > *p++ = ')';
    >
    >
    > Of course, as this is a C++ newsgroup, some std::string solution would
    > be more appropriate, and here you might have to worry about the buffer
    > length.


    I was too quick. *p++ only works for the opening bracket. Would have to
    use sprintf here, too. Otherwise the terminating zero would be messed up.

    if (d < 0)
    p += sprintf(p, "|");

    p += sprintf(p, "%s", d);

    if (d < 0)
    p += sprintf(p, "|");

    But the basic idea is the same.
    Peter Remmers, Dec 20, 2008
    #5
  6. puzzlecracker

    Stefan Ram Guest

    Peter Remmers <> writes (shortened):
    >if (d < 0)
    > *p++ = '(';
    >p += sprintf(p, "%s", d);
    >if (d < 0)
    > *p++ = ')';


    Or,

    p += ::std::sprintf( p, d < 0 ? "(%s)" : "%s", d );

    But when using »sprintf« one must always be aware of
    the possibility of a buffer overflow. I am not sure
    whether the OP can proove that the size of p will suffice.

    In C, one can use a special form of sprintf to obtain
    the size needed and then allocate this, like:

    char * salloc( char * const f, ... )
    { va_list a; char * b = 0;
    va_start(a,f); int const s = vsnprintf( 0, 0, f, a ); va_end(a);
    if( s >= 0 ){ size_t const k = 1 + s; if( b = malloc( k ))
    { va_start(a,f); vsprintf( b, f, a ); va_end(a); }}
    return b; }

    In C++, using ::std::string, as you wrote, will take
    care of this.
    Stefan Ram, Dec 20, 2008
    #6
  7. puzzlecracker

    James Kanze Guest

    On Dec 21, 2:52 am, (blargg) wrote:
    > Peter Remmers wrote:


    > [...]
    > > If I understand your problem correctly, how about:


    > > char buffer[50];
    > > double d=-205;


    > > char *p = buffer;


    > > // opening bracket
    > > if (d < 0)
    > > *p++ = '(';


    > > // format your number
    > > p += sprintf(p, "%s", d); // format mismatch?


    > > // closing bracket
    > > if (d < 0)
    > > *p++ = ')';


    > Will <0 catch negative zero?


    Of course not.

    > I'd use <=-0.0 above.


    Which is the same as <= 0.0.

    > Remember, FP is usually not two's complement, so it has
    > negative zero.


    Which is irrelevant in most cases. Positive and negative 0 are
    required to compare equal, not less than or greater than.
    They're just artifacts of the representation: a positive 0 isn't
    strictly positive, and a negative 0 isn't strictly
    negative---both are zero.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 21, 2008
    #7
  8. puzzlecracker <> writes:

    > On Dec 19, 11:38 am, Christian Hackl <> wrote:
    >> puzzlecracker ha scritto:
    >>
    >> > Say I have,

    >>
    >> >   char buffer[50];
    >> >  double d=-205;
    >> > that contains a number (negative or positive), and I know what number
    >> > it is, Now I need to enclose the number in the buffer with columns if
    >> > it negative.

    >>
    >> > sprintf(buffer,"%s", d);

    >>
    >> > Here is the twist,  the format varies, it can be "%s", can also be
    >> > something else. So, after I constructed that buffer with number, want
    >> > to enclose it with columns.

    >>
    >> I fear I do not understand the problem. Why don't you simply use
    >>
    >> (if d < 0)
    >>    sprintf(buffer, "%s", d)
    >> else
    >>    sprintf(buffer, "|%s|", d)
    >>
    >> ?


    sprintf(buffer,((d<0)?"|%s|":"%s"),d);

    > well, I am given a final string with which contains a number. It's
    > constructed based on various criteria, and it would be painful to
    > modify every format (around 100 in total) that a number is
    > constructed with.
    >
    > So I just want to take the final product and enclose it with columns


    Then why do you lie to us?
    You do not have a number, you have a string!

    And it looks like you want to concatenate this string with some other
    when it starts with a '-' character.

    #include <ciso646>
    std::string f(std::string number){
    return(((number.size()>0) and (number[0]=='-')) ? "|"+number+"|" : number);
    }


    Or perhaps the string may be left-padded:

    std::string f(std::string number){
    return((number.position('-')!=std::string::npos) ? "|"+number+"|" : number);
    }


    Or perhaps your string may be anything, and you will still have to
    modify all the places where the number is formated:

    class SignedString {
    private:
    std::string formatedNumber;
    bool negative;
    public:
    SignedString(std::string aFormatedNumber,bool aNegativeFlag):formatedNumber(aFormatedNumber),negative(aNegativeFlag){}
    inline std::string getFormatedNumber(){return(formatedNumber);}
    inline bool getNegative(){return(negative);};
    };

    in places where you format your numbers:

    sprintf(buffer,"...",d);
    return SignedString(buffer,d<0);


    then:

    std::string f(SignedString number){
    return(number.getNegative() ? "|"+number.getFormatedNumber()+"|" : number.getFormatedNumber());
    }


    But in any case, do not lie, when you have a string don't say you have a number!

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Dec 22, 2008
    #8
  9. puzzlecracker

    James Kanze Guest

    On Dec 22, 2:20 am, (blargg) wrote:
    > James Kanze wrote:
    > > On Dec 21, 2:52 am, (blargg) wrote:

    > [...]
    > > > Will <0 catch negative zero?


    > > Of course not.


    > > > I'd use <= -0.0 above.


    > > Which is the same as <= 0.0.


    > > > Remember, FP is usually not two's complement, so it has
    > > > negative zero.


    > > Which is irrelevant in most cases. Positive and negative 0
    > > are required to compare equal, not less than or greater
    > > than. They're just artifacts of the representation: a
    > > positive 0 isn't strictly positive, and a negative 0 isn't
    > > strictly negative---both are zero.


    > So it seems. Maybe it's just my implementation, but printf
    > does put a negative sign before a negative zero (cout
    > doesn't).


    I would consider that a very serious bug. It seems to be
    universal, however:

    int main()
    {
    double d = 0.0 ;
    d *= -1.0 ;
    printf( "%f\n", d ) ;
    std::cout << std::fixed << d << std::endl ;
    return 0 ;
    }

    Outputs -0.000000 twice with all compilers/libraries I have
    access too. That's three different librarys for printf, and
    four for std::cout. And it leads to the interesting point that
    two values can compare equal, but not result in the same output.

    (Note that if the 0.0 is a result of rounding, there might be
    some justification. If, for example, I was outputting something
    like -1E20 with "%f" or std::fixed. Even then, however, I doubt
    it.)

    [...]
    > Assuming the goal is to parenthesize values with a negative
    > sign, how would one do the conditional in print()? As you
    > said, negative and positive zero both compare <= -0.0.


    Portably, of course, you can't, since there's no guarantee that
    you have a negative zero:). But the C99 standard provides the
    macro "signbit()", with a footnote specifying that "The signbit
    macro reports the sign of all values, including infinities,
    zeros, and NaNs. If zero is unsigned, it is treated as
    positive." So if your implementation supports C99 (and this
    will be in C++0x), you can use that.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Dec 22, 2008
    #9
  10. James Kanze wrote:
    > On Dec 22, 2:20 am, (blargg) wrote:
    >> James Kanze wrote:


    >> > They're just artifacts of the representation: a
    >> > positive 0 isn't strictly positive, and a negative 0 isn't
    >> > strictly negative---both are zero.

    >
    >> So it seems. Maybe it's just my implementation, but printf
    >> does put a negative sign before a negative zero (cout
    >> doesn't).

    >
    > I would consider that a very serious bug. It seems to be
    > universal, however:


    Signed zero is there for a reason. See
    http://en.wikipedia.org/wiki/-0_(number)


    Martin

    --
    Quidquid latine scriptum est, altum videtur.
    Martin Eisenberg, Dec 22, 2008
    #10
    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. prem_eda
    Replies:
    5
    Views:
    7,845
    Pieter Hulshoff
    Oct 11, 2004
  2. Andreas N

    Regexp to enclose text with P-tag

    Andreas N, May 18, 2004, in forum: Perl
    Replies:
    1
    Views:
    515
    Jürgen Exner
    May 18, 2004
  3. Cram TeXeD
    Replies:
    0
    Views:
    363
    Cram TeXeD
    Apr 6, 2004
  4. Dead RAM
    Replies:
    20
    Views:
    1,107
    John Harrison
    Jul 14, 2004
  5. SubZane

    Regexp to enclose text with P-tag

    SubZane, May 18, 2004, in forum: Python
    Replies:
    1
    Views:
    279
    Heiko Wundram
    May 18, 2004
Loading...

Share This Page