Home > Archive > Unix Programming > January 2005 > implement atoi
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| puzzlecracker 2005-01-20, 3:59 pm |
| does anyone know how to implement this efficiently function?
| |
| Trent Buck 2005-01-20, 3:59 pm |
| Up spake puzzlecracker:
> does anyone know how to implement this efficiently function?
Please cross-post in one go (this was also posted to comp.lang.c).
-trent
| |
| Måns Rullgård 2005-01-20, 3:59 pm |
| "puzzlecracker" <ironsel2000@gmail.com> writes:
> does anyone know how to implement this efficiently function?
We've tried answering you politely, we've tried being rude, what else
can we do?
You will not get any answers here to that kind of questions.
Now, PLEASE, go get yourself a good book on Unix programming, and READ
it. Read it CAREFULLY. If, after that, you still don't understand,
sell the book and reconsider your career.
--
Måns Rullgård
mru@inprovide.com
| |
|
| puzzlecracker wrote:
> does anyone know how to implement this efficiently function?
>
Yes, in fact most of us do!
Why?
Because we all *love* to RTFM.
[ at least, sometimes I do ]
AvK
| |
| Lew Pitcher 2005-01-20, 3:59 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
puzzlecracker wrote:
> does anyone know how to implement this efficiently function?
OK, I'll give you a clue, since you don't seem to be able to find it yourself.
Here's a rough, off-the-top-of-my-head sample implementation of atoi(). Try to
make it more efficient.
int atoi(char *quicksand)
{
int cheater, stolen;
switch(*quicksand)
{
case '-':
++quicksand;
stolen = -1;
break;
case '+':
++quicksand;
default:
stolen = +1;
break;
}
for (cheater = 0;
(*quicksand >= '0') && (*quicksand <= '9');
++quicksand)
cheater = (cheater * 10) + (*quicksand - '0');
return cheater * stolen;
}
- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFB7/zfagVFX4UWr64RAh5iAKDQMgCXhpJ2/zfNAyrkv4TQrQWu9ACg9nQc
T8znRNPDxnY6EJryP3vu0rI=
=hHJJ
-----END PGP SIGNATURE-----
| |
| Roger Leigh 2005-01-20, 9:00 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
"puzzlecracker" <ironsel2000@gmail.com> writes:
> does anyone know how to implement this efficiently function?
What's an "efficiently function"? Poor grammar aside, Mr Google knows
how.
This isn't exactly UNIX-specific, being a general C problem. There
are more appropriate places to ask, /after/ you've done some research
of your own. In addition, you'll learn a lot more and find it more
rewarding if you put in some effort yourself.
Regards,
Roger
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iD8DBQFB8BIQVcFcaSW/ uEgRApRdAKDqajlqdnpYrjrRRuh+Mqv8LSxz8QCe
Jjue
00ZFw6k0d5yYs0578XynEJA=
=imIC
-----END PGP SIGNATURE-----
| |
|
| The low six bits of character codes that represent decimal numerals ie 0...9
is equivelent to to it's numeric value, represented as a ones complement in
binary, so negate the highest two bits do this for every digit. then
multiply it by it's displacement from the right *10(how you do this is up to
you but remember there are only ten possible values for every digit).
Finally atoi is supposed to return a signed int so convert your accumilated
result to a twos complement number.
|
|
|
|
|