implement atoi

V

Victor Bazarov

puzzlecracker said:
does anyone know how to implement this function?

My guess is that somebody must know, since we have a relatively
well working 'atoi' in the standard library... Why do you ask?
 
K

Karl Heinz Buchegger

puzzlecracker said:
does anyone know how to implement this function?

Why do you ask

int myAtoi( const char* )
{
int Tmp = 0;
sscanf( "%d", &Tmp );
return Tmp;
}
 
H

Howard

Karl Heinz Buchegger said:
Why do you ask

int myAtoi( const char* )
{
int Tmp = 0;
sscanf( "%d", &Tmp );
return Tmp;
}

Geez, Karl, you gonna do *all* this guy's work for him? :)

-Howard
 
P

puzzlecracker

Karl said:
Why do you ask

int myAtoi( const char* )
{
int Tmp = 0;
sscanf( "%d", &Tmp );
return Tmp;
}


dnt you need to put sscanf (string, "%d", &tmp);?


but hey can you do that yourself? I got this question on the interview
 
J

Jonathan Turkanis

Howard said:
Geez, Karl, you gonna do *all* this guy's work for him? :)

Not quite. The function parameter should be named, and the value used in the
call to sscanf.

Jonathan
 
D

Dietmar Kuehl

Karl said:
Why do you ask

int myAtoi( const char* )
{
int Tmp = 0;
sscanf( "%d", &Tmp );
return Tmp;
}

.... and in the implementation of 'sscanf()' I assume you use
'myAtoi()' to convert a string into an integer - well, that's
a smart move: delegation is generally a good approach to make
problems go away.
 
W

White Wolf

Dietmar said:
... and in the implementation of 'sscanf()' I assume you use
'myAtoi()' to convert a string into an integer - well, that's
a smart move: delegation is generally a good approach to make
problems go away.

Hey! It works for the EU governments. ;-)
 
T

Thomas Matthews

puzzlecracker said:
does anyone know how to implement this function?

Search using your favorite search engine.
Hint: to convert an ASCII numeric character
to its internal (integral) value, subtract
'0' from it:
'9' - '0' == 9

By the way, do you research anything or read
any books?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
H

Howard

Thomas Matthews said:
Search using your favorite search engine.
Hint: to convert an ASCII numeric character
to its internal (integral) value, subtract
'0' from it:
'9' - '0' == 9

By the way, do you research anything or read
any books?

I'm not familiar with any system that this doesn't work with, but I don't
think it's required (by the standard) to work that way, is it?

A more sure approach would be a switch statement for each numeric character,
I'd think.

-Howard
 
W

White Wolf

Howard said:
I'm not familiar with any system that this doesn't work with, but I don't
think it's required (by the standard) to work that way, is it?

A more sure approach would be a switch statement for each numeric
character, I'd think.

I was told it is required:

In C 99 standard 5.2.1 Character sets/Par3 Simon says:

"Both the basic source and basic execution character sets shall have the
following members: the 26 uppercase letters of the Latin alphabet"

blablabla

"In both the source and execution basic character sets, the value of each
character after 0 in the above list of decimal digits shall be one greater
than the value of the previous."

I say this is fairly clear. It is not in the C++ standard although it
references the C standard as a normative reference. We could look around
for a standardeeze guru to tell which part actually connects this. If it is
missing, then it is unintentional, since once upon a time such a standard's
guru told me this. I mean the fact that all character sets are defined on a
way to make this "minus 0 trick" work.
 
D

Dietmar Kuehl

White said:
interview

I can.

I can, too, but this is trivial: let's make it a quest! Can you do it
without a loop and without recursion, i.e. using algorithms? This is
still trivial but I don't know the answer to this one: no loop, no
class (or struct), no function. Just standard C++ library features
including TR1 stuff. Well, thinking of it, I would guess it is doable
but I don't yet have a solution...
 
D

Dietmar Kuehl

Dietmar Kuehl wrote:
[incomplete spec]

Of course, this is still trivial using 'strtol()', 'sscanf()', etc.
The solution should create the key algorithm based on standard
library tools: I guess, you know what I mean :)
 
W

White Wolf

Dietmar said:
Dietmar Kuehl wrote:
[incomplete spec]

Of course, this is still trivial using 'strtol()', 'sscanf()', etc.
The solution should create the key algorithm based on standard
library tools: I guess, you know what I mean :)

Nope. You have lost me. Do you mean functional approach? But wouldn't
that work only on compile time constant input? Ehh, I really have a bad
brain day today. I mean just the fact that I am posting here at 0028hours
when I wanted to go to sleep early today... :)
 
D

Dietmar Kuehl

White said:
Nope. You have lost me. Do you mean functional approach? But wouldn't
that work only on compile time constant input? Ehh, I really have a bad
brain day today. I mean just the fact that I am posting here at 0028hours
when I wanted to go to sleep early today... :)

Nah, I mean using only algorithms, functors, etc. Here is what I mean
(i.e. it is still too easy; maybe the next step is to avoid the
auxiliary
vector):

| int atoi(std::string s)
| {
| if (s.empty())
| return 0;
| std::string::iterator b = s.begin();
| int sign = *b == '-'? (++b, -1): *b == '+'? (++b, 1): 1;
| std::string::iterator e = std::transform(b,
| std::find_if(b, s.end(),
| std::not1(std::ptr_fun(
| static_cast<int(*)(int)>(&std::isdigit)))), b,
| std::bind2nd(std::minus<char>(), '0'));
| std::vector<unsigned int> aux(b == e? 0: e - b - 1, 10);
| std::partial_sum(aux.rbegin(), aux.rend(), aux.rbegin(),
| std::multiplies<int>());
| aux.push_back(1);
| return sign * std::inner_product(b, e, aux.begin(), 0,
| std::plus<int>(), std::multiplies<int>());
| }

.... and before somebody thinks I have become incredible stupid (*):
no, this is not intended for production code!

(*) that is, even more stupid than is commonly known.
 
W

White Wolf

Dietmar said:
Nah, I mean using only algorithms, functors, etc. Here is what I mean
(i.e. it is still too easy; maybe the next step is to avoid the
auxiliary
vector):


... and before somebody thinks I have become incredible stupid (*):
no, this is not intended for production code!

(*) that is, even more stupid than is commonly known.

Oh my god.
 
K

Karl Heinz Buchegger

Dietmar said:
... and in the implementation of 'sscanf()' I assume you use
'myAtoi()' to convert a string into an integer - well, that's
a smart move: delegation is generally a good approach to make
problems go away.

:)
Actually, sscanf should use whatever it wants to do the conversion.
:)
Just in case: I wouldn't use that approach anyway
:)
 
K

Karl Heinz Buchegger

puzzlecracker said:
dnt you need to put sscanf (string, "%d", &tmp);?

but hey can you do that yourself? I got this question on the interview

Sure I can. But that is not the question.
The question is: Can *you* do it?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top