C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Ziffern finden in 'Zahlenkette'

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Alexander Dietz
Guest





PostPosted: Mon Sep 01, 2003 12:50 pm    Post subject: Ziffern finden in 'Zahlenkette' Reply with quote



Hallo,


ich habe zwei int-Variablen, z.B.

int a=1245780; // beliebig lang
int b=4; // nur eine Ziffer


Jetzt möchte ich prüfen, ob der Wert von b (die Ziffer '4') in der
Zahl von a (Ziffernfolge '1245780') vorkommt oder nicht.
In dem beschriebenen Beispiel wäre die Antwort positiv, wäre b=3 dann
wäre sie negativ.

Kennt jemand einen Algorithmus um solche Abfragen durchzuführen? Oder
ist dieses Problem nur zu klären, in dem man z.B. beide Zahle in
Zeichenketten umwandelt und dann einen Byte-für-Byte - Vergleich
durchführt?

Gruss

Alexander Dietz

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Pether Hubert
Guest





PostPosted: Mon Sep 01, 2003 4:43 pm    Post subject: Re: Ziffern finden in 'Zahlenkette' Reply with quote



[email]alexd4351 (AT) compuserve (DOT) de[/email] (Alexander Dietz) writes:
Quote:
ich habe zwei int-Variablen, z.B.
int a=1245780; // beliebig lang
int b=4; // nur eine Ziffer
Jetzt möchte ich prüfen, ob der Wert von b (die Ziffer '4') in der
Zahl von a (Ziffernfolge '1245780') vorkommt oder nicht. In dem
beschriebenen Beispiel wäre die Antwort positiv, wäre b=3 dann wäre
sie negativ.

Ungetestet:

--8<------------------------------------------------------------------

bool Ziffer_drin(int a, int b)
{
if (a < 0)
a = -a;
while (a) {
if (a % 10 == b)
return true;
a /= 10;
}
return false;
}

--8<------------------------------------------------------------------

Meintest Du sowas?

Ciao,

Pether
--
Hab immer Sex mit dem Boß. (Erwerbsregel 113)

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Volker Glave
Guest





PostPosted: Tue Sep 02, 2003 2:10 pm    Post subject: Re: Ziffern finden in 'Zahlenkette' Reply with quote



Pether Hubert <p.hubert (AT) gmx (DOT) de> wrote

Quote:
alexd4351 (AT) compuserve (DOT) de (Alexander Dietz) writes:
ich habe zwei int-Variablen, z.B.
int a=1245780; // beliebig lang
int b=4; // nur eine Ziffer
Jetzt möchte ich prüfen, ob der Wert von b (die Ziffer '4') in der
Zahl von a (Ziffernfolge '1245780') vorkommt oder nicht. In dem
beschriebenen Beispiel wäre die Antwort positiv, wäre b=3 dann wäre
sie negativ.

Ungetestet:

--8<------------------------------------------------------------------

bool Ziffer_drin(int a, int b)
{
if (a < 0)
a = -a;
while (a) {
if (a % 10 == b)
return true;
a /= 10;
}
return false;
}

--8<------------------------------------------------------------------

Liefert jedoch bei a = b = 0 und bei a = INT_MIN falsche Ergebnisse.
Hab's aufgegriffen und komme auf folgendes (nur wenig getestet):

template T rest(T num, T div)
{
const T r(num % div);
return r < T() ? -r : r;
}

template bool ziffer_drin(T a, T b, T base = 10) {
while (rest(a, base) != b)
if (!(a /= base))
return false;
return true;
}

Gruß
Volker

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.