Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

ich soll ein Programm schreiben (Microsoft Visual C++) und brauch um da weiter zu kommen den Befehl, wo die stellen eines int's gezählt werden

(bsp: 12345678 = 8 Stellen)

Hat einer von euch eine Ahnung wie der Befehl heißt, ich hab schon im Netz gesucht, aber so gerade nichts gefunden.

mfg Akaria

Eine fertige Funktion gibt's nicht, du musst das ausprogrammieren.

Weitere Vorschläge:

Pragmatisch: Mitzählen, wie oft du durch 10 teilen musst, bis 0 herauskommt.

Simpel, aber verschwenderisch: In einen String umwandeln und dessen Länge ermitteln.

Performant: Binäre Suche in einer Lookup-Tabelle.


int DigitalCount(int x)

{

	int j=0;


	while(x>0)

	{

		x=x/10;

		j++;

	}


	return j;

}

Beide Varianten funktionieren nur für x > 0.

So könnte man es auch machen:

int digit_count( int x ) 
{
static const int table[] = {
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000 };

return std::upper_bound( table, table + 9, abs(x) ) - table + 1;
}
[/code]

Das klappt so allerdings nur, wenn int höchstens 32 Bit hat. Man könnte die Tabelle aber auf 64 Bit erweitern.

dann halt aus dem x noch ein abs(x) gemacht

Genau

Danke schön, das mit dem durch 10 teilen probier ich die tage mal aus.

das mit dem logarithmus auch mal (bin mal gespannt ob das funktioniert)

Mfg Akaria

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.