Velocity Reviews > Re: Formatting a number in a different way

# Re: Formatting a number in a different way

Noob
Guest
Posts: n/a

 01-20-2011
pozz wrote:

> I want to create a function, sprintfnum(), with the prototype:
> void sprintfnum(char *str, const char *fmt, int x, int div);
>
> I tried to write this function, but I encounter many difficulties.
> Someone can help me?

for us to send our submissions directly to him?

pozz
Guest
Posts: n/a

 01-20-2011
On 20 Gen, 10:25, Noob <r...@127.0.0.1> wrote:
> Could you provide the email address of your instructor, in order
> for us to send our submissions directly to him?

I'm sorry you think this is a school test. Firstly I don't remember
the last time I had an instructor.
And the most sad thing is that you are thinking the problem is
very simple, but I'm not able to find a good solution for it.

My implementation is long and complex and for simplicity I separated
prefix and suffix. Is there a simpler solution?

void
sprintfnum(char *str,
const char *prefix,
const char *num_fmt,
const char *suffix,
int x,
int div)
{
int n, nn;
int sign = 0, zero = 0, width = 0;

n = sprintf(str, "%s", prefix);
nn = 1;
if (num_fmt[nn] == '+') {
sign = 1;
nn++;
}
if (num_fmt[nn] == '0') {
zero = 1;
nn++;
}
width = strtol(&num_fmt[nn], NULL, 10);
if (!width) {
width = 1;
}
if (div > 0) {
char fmt[7];
if(!x && !zero) {
sprintf(fmt, "%%%s%dd",
sign ? "+" : "", width);
n += sprintf(&str[n], fmt, 0);
} else {
sprintf(fmt, "%%%s%s%dd",
sign ? "+" : "", zero ? "0": "", width - div);
n += sprintf(&str[n], fmt, x);
/* Aggiunge tanti zeri quanto č div */
while(div--) {
str[n++] = '0';
}
}
str[n] = '\0';
} else if (div == 0) {
n += sprintf(&str[n], num_fmt, x);
} else if (div < 0) {
char fmt[7];
div = -div;
sprintf(fmt, "%%%s%s%dd",
sign ? "+" : "", zero ? "0": "",
width - 1 < div + 1 + sign ? div + 1 + sign : width - 1);
n += sprintf(&str[n], fmt, x);
str[n + 1] = '\0';
while (div--) {
if (str[n - 1] == ' ') {
str[n] = '0';
} else if ((str[n - 1] == '+') || (str[n - 1] == '-')) {
str[n] = '0';
str[n - 2] = str[n - 1];
} else {
str[n] = str[n - 1];
}
n--;
}
str[n] = '.';
if (str[n - 1] == ' ') {
str[n - 1] = '0';
} else if ((str[n - 1] == '+') || (str[n - 1] == '-')) {
str[n - 2] = str[n - 1];
str[n - 1] = '0';
}
}
strcat(str, suffix);
}

Mark Bluemel
Guest
Posts: n/a

 01-20-2011
On 01/20/2011 09:57 AM, pozz wrote:
> On 20 Gen, 10:25, Noob<r...@127.0.0.1> wrote:
>> Could you provide the email address of your instructor, in order
>> for us to send our submissions directly to him?

>
> I'm sorry you think this is a school test. Firstly I don't remember
> the last time I had an instructor.
> And the most sad thing is that you are thinking the problem is
> very simple, but I'm not able to find a good solution for it.
>
> My implementation is long and complex and for simplicity I separated
> prefix and suffix. Is there a simpler solution?

I find the basic idea below easier to comprehend. I haven't tried to
achieve all you have, but I think the idea is clear enough and could
be extended to do what you want.

#include <stdio.h>
#include <stdlib.h>

void split(int num, int div, char *right, char *left);

int main(void) {
int div;
for (div = -3; div < 4; div++) {
char left[10]; /* arbitrary size, I'm afraid */
char right[10];
split(123456,div,right,left);
printf("%s.%s\n",right,left);
}
}

void split(int num, int div, char *right, char *left) {
int lp = 0;
if (div < 0) { /* there is a fractional part, get it */
char buff[10];
int bp = 0;
while(div) {
buff[bp++] = '0' + (num % 10);
num /= 10;
div++;
}
/* reverse the digits ... */
while (bp) {
left[lp++] = buff[--bp];
}
}
left[lp] = '\0';

while(div) { /* if there was a fractional part,
* div will now be zero
*/
num *= 10;
div --;
}
sprintf(right,"%d",num);
}

Malcolm McLean
Guest
Posts: n/a

 01-20-2011
On Jan 20, 11:25*am, Noob <r...@127.0.0.1> wrote:
>
> Could you provide the email address of your instructor, in order
> for us to send our submissions directly to him?
>

You can usually detect what's real problem and what's a learning
exercise.

This one has all the feel of a real problem about it. You can fake up
floating point on an integer-only machine by storing a decimal place
separately, but you then need to output the numbers for human
consumption. The fact he needs field widths and signs and padding
suggests he wants the output for real - it's not a good exercise
because it's fiddly rather than testing understanding of concepts.

Kenny McCormack
Guest
Posts: n/a

 01-20-2011
In article <ih92ea\$3al\$(E-Mail Removed)-september.org>,
Richard <(E-Mail Removed)> wrote:
>Noob <root@127.0.0.1> writes:
>
>> pozz wrote:
>>
>>> I want to create a function, sprintfnum(), with the prototype:
>>> void sprintfnum(char *str, const char *fmt, int x, int div);
>>>
>>> I tried to write this function, but I encounter many difficulties.
>>> Someone can help me?

>>
>> Could you provide the email address of your instructor, in order
>> for us to send our submissions directly to him?
>>

>
>This is a help group. If you can help please do else get lost with your

You raise an interesting question. Is comp.lang.c a "help group"?
Why do you (rgrdev_) think that it is? Do others agree with you?

I ask these questions because I think that at least some, probably most,
of the "regs" would disagree with you, saying that it is a discussion
group, not a form of an unpaid corporate help desk. They are lying, of
course, but that is what they will say.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

Mark Bluemel
Guest
Posts: n/a

 01-20-2011
On 01/20/2011 09:57 AM, pozz wrote:
> On 20 Gen, 10:25, Noob<r...@127.0.0.1> wrote:
>> Could you provide the email address of your instructor, in order
>> for us to send our submissions directly to him?

>
> I'm sorry you think this is a school test.

Given that you simply gave the problem specification and then said "I
tried to write this function, but I encounter many difficulties.", I'm
not totally surprised you got this reaction.

A summary of what you'd tried and the sorts of problems you encountered
would help people to give you relevant assistance. Otherwise, it sounds
rather like you want them to give you a fully worked solution from scratch.

Mark Bluemel
Guest
Posts: n/a

 01-20-2011
On 01/20/2011 10:23 AM, Richard wrote:

> This is a help group.

Given the lack of a formal charter, it's a bit optimistic to state that
so unequivocally...

> If you can help please do else get lost with your

Have you considered leading by example?

Mark Bluemel
Guest
Posts: n/a

 01-20-2011
On 01/20/2011 03:40 PM, Kenny McCormack wrote:
> In article<ih92ea\$3al\$(E-Mail Removed)-september.org>,
> Richard<(E-Mail Removed)> wrote:

>> This is a help group. If you can help please do else get lost with your

>
> You raise an interesting question. Is comp.lang.c a "help group"?
> Why do you (rgrdev_) think that it is? Do others agree with you?
>
> I ask these questions because I think that at least some, probably most,
> of the "regs" would disagree with you, saying that it is a discussion
> group, not a form of an unpaid corporate help desk. They are lying, of
> course, but that is what they will say.

"Lying" is a rather strong term - it's hard to lie about the
nature/purpose of comp.lang.c when there is no formal definition of it.

Empirically I'd suggest the observable nature of the newsgroup is more
discursive than problem-solving.

Seebs
Guest
Posts: n/a

 01-20-2011
On 2011-01-20, Mark Bluemel <(E-Mail Removed)> wrote:
> "Lying" is a rather strong term

But in context, self-referential.

> it's hard to lie about the
> nature/purpose of comp.lang.c when there is no formal definition of it.

You could easily lie about it; all you'd have to do is have an opinion
about the nature or purpose of the group, then say something contrary to
that opinion.

> Empirically I'd suggest the observable nature of the newsgroup is more
> discursive than problem-solving.

I'd mostly think so, but...

I guess I'd say I think it's perfectly appropriate to ask for help in a
discussion group. I don't think a group being a discussion group (which
all Usenet groups are by default unless stated otherwise, giving us our
clear answer) precludes straight requests for help from being appropriate.

However... Many of us are experienced programmers. One of the things
experienced programmers have usually learned is that the words in the
specification are frequently incorrect, because the person asking has
misunderstood the problem they need solved. As a result, if you come here
asking for help, it is reasonably well justified for people to second-guess

For a specific example, if something looks like homework, most experienced
programmers will conclude that, though the STATED problem was "I want someone
to do this for me", the REAL problem is "I need to learn how to do this so
I can not just get a passing grade but get one honestly".

That said, I didn't think the original looked like homework. It did, however,
look like a fairly arcane spec, and I'm still not sure what the intended
functionality is.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Kenny McCormack
Guest
Posts: n/a

 01-20-2011
In article <ih9lqc\$fnv\$(E-Mail Removed)-september.org>,
Mark Bluemel <(E-Mail Removed)> wrote:
>On 01/20/2011 03:40 PM, Kenny McCormack wrote:
>> In article<ih92ea\$3al\$(E-Mail Removed)-september.org>,
>> Richard<(E-Mail Removed)> wrote:

>
>>> This is a help group. If you can help please do else get lost with your

>>
>> You raise an interesting question. Is comp.lang.c a "help group"?
>> Why do you (rgrdev_) think that it is? Do others agree with you?
>>
>> I ask these questions because I think that at least some, probably most,
>> of the "regs" would disagree with you, saying that it is a discussion
>> group, not a form of an unpaid corporate help desk. They are lying, of
>> course, but that is what they will say.

>
>"Lying" is a rather strong term - it's hard to lie about the
>nature/purpose of comp.lang.c when there is no formal definition of it.

Well, there is a semantic difference between "lying" and "being wrong".
One, they say, is curable, the other is not.

I think that what you wrote would make more sense if I had said (and you had
quoted me as saying) that they (the "regs") were "wrong".

But (my use of) the term "lying" implies deceit and/or hypocrisy, which
is the target at which I was shooting. I guess my point is that if it
is a help group, it does a poor job of it (as shown by the frequent
requests for instructor's email addresses - a clear cut copout) but if
it is supposed to be a "discussion" group then it fails there because,
as I've shown elsewhere, nothing is "on topic" in comp.lang.c

>Empirically I'd suggest the observable nature of the newsgroup is more
>discursive than problem-solving.

It is many things, but none of them good.

--

Some of the more common characteristics of Asperger syndrome include:

* Inability to think in abstract ways (eg: puns, jokes, sarcasm, etc)
* Difficulties in empathising with others
* Problems with understanding another person's point of view
* Hampered conversational ability
* Problems with controlling feelings such as anger, depression
and anxiety
* Adherence to routines and schedules, and stress if expected routine
is disrupted
* Inability to manage appropriate social conduct
* Delayed understanding of sexual codes of conduct
* A narrow field of interests. For example a person with Asperger
syndrome may focus on learning all there is to know about
baseball statistics, politics or television shows.
* Anger and aggression when things do not happen as they want
* Sensitivity to criticism
* Eccentricity
* Behaviour varies from mildly unusual to quite aggressive
and difficult