Willkommen ~Gast!
Registrieren || Einloggen || Hilfe/FAQ || Staff
Probleme mit der Registrierung im Forum? Melde dich unter registerEin Bild.
Autor Beitrag
000
11.07.2010, 22:52
bennifilm



Ich probier mich gerade ein bisschen an C++ und wollte ein Programm schreiben
das die Fakultät von Zahlen berechnet. Das klappt eigendlich alles wunderbar
doch bei Zahlen über 30 krieg ich nur noch falsche Ergebnisse.

Weil das warscheinlich an der definition meiner Variabeln liegt hab ich es mit
"unsigned long long int" probiert um mit größeren Zahlen zu rechnen. Leider
klappt das auch nicht.

Ich hoffe jemand kann mir helfen.

Quellcode:
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>

using namespace std;

long fak(unsigned long long int x)
{
    unsigned long long int i = 1;
    unsigned long long int loop = 1;
    int y = x;
    while (loop != y)
    {
          x = x * i;
          ++i;
          ++loop;
    }
    return x;        
}
    

int main()
{
    unsigned long long int zahl;
    cout <<"Faku von :"<< endl;
    cin >> zahl;
    cout << "Ergebnis :" << endl;
    cout << fak(zahl)<< endl;
    while (!kbhit()) //läuft solange keine Taste gedrückt wird
    {
    }
}

--

::Alien Swarm Sunstation 583::
::Left 4 Dead:: b_alley 95% | b_office 65%
::Battlemod:: steht still

http://www.iop-b.de/

zum Seitenanfang zum Seitenende Profil || Suche
001
11.07.2010, 22:58
Adrian_Broher
Admin


Der Rueckgabewert ist nur ein long breit.

--

There is nothing wrong with high standards. It's your problem that you don't meet them.
If you think it's simple, then you have misunderstood the problem.
When a customer says "nothing has changed", assume they're lying.

zum Seitenanfang zum Seitenende Profil || Suche
002
11.07.2010, 23:36
bennifilm



Danke für die Antwort , leider hilft mir das nicht ganz weiter.

Der Rückgabewert ist x und ist doch so definiert "unsigned long long int x"

Oder meinst du was anderes ?

--

::Alien Swarm Sunstation 583::
::Left 4 Dead:: b_alley 95% | b_office 65%
::Battlemod:: steht still

http://www.iop-b.de/

zum Seitenanfang zum Seitenende Profil || Suche
003
11.07.2010, 23:43
Master Pegasus



Er meint die Funktion: long fak(unsigned long long int x)

--

Was immer auch geschieht: Nie sollt Ihr so tief sinken, von dem Kakao, durch den man Euch zieht, auch noch zu trinken! -- Erich Kästner

zum Seitenanfang zum Seitenende Profil || Suche
004
12.07.2010, 00:07
bennifilm



Wunderbar vielen Dank. Daran hatte ich garnicht mehr gedacht.

Jetzt ist 65 das Maximum , aber das reicht locker :D

--

::Alien Swarm Sunstation 583::
::Left 4 Dead:: b_alley 95% | b_office 65%
::Battlemod:: steht still

http://www.iop-b.de/

zum Seitenanfang zum Seitenende Profil || Suche
005
12.07.2010, 00:43
Bluthund



Dem waere noch hinzuzufuegen, dass long long kein im C++-Standard vorgesehener Datentyp (anders als in C99) ist, auch wenn die meisten aktuellen Compiler ihn trotzdem in C++-Code zulassen.
In C++ heissen die Includes der C-Standard-Bibliothek <cfoo> und nicht <foo.h>. Also bspw.: <cstdio> und <cstring>. </Standard-Nazi>
Ist natuerlich fuer so eine Uebung irrelevant, aber das solltest du auf jeden Fall im Hinterkopf behalten wenn du vorhast weiter in die Richtung zu gehen (in C++0x ist dann zumindestens std::tr1::[u]int64_t ueber den Include von <cstdint> vorgesehen).
Wenn du willkuerlich lange Ganzzahlen brauchst, sei dir die libgmp ans Herz gelegt.

Du kannst auch etwas Platz (auf dem Stack und im Code) sparen indem dich in Richtung Null bewegst:
Quellcode:uint64_t fak(unsigned int x)
{
    uint64_t retval = 1;

    while (x > 0) retval *= x--; // post decrement, damit x erst nach dem Auswerten des Ausdrucks dekrementiert wird

    /* Alternative Langform:
    while (x > 0) {
        retval *= x;
        --x;
    }
    */

    return retval;
}
Bei vorzeichenlosen 64bit-Integers ist uebrigens schon bei 20! Schluss, da 21! mit 5.1E19 bereits ausserhalb des Wertebereichs von 0..2^64 - 1 (etwas ueber 1.8E19) liegt. Selbst bei einem 128bit Integer-Datentyp ohne Vorzeichen wuerdest du hoechstens bis 34! kommen. Es ist also sehr unwahrscheinlich, dass du erst bei 65! falsche Ergebnisse bekommst.

--

The C language combines all the power of assembly language with all the ease-of-use of assembly language.
"humorig is n blödwort :>" by -CarniGGeLjumpR-


Dieser Beitrag wurde am 12.07.2010 um 01:25 von Bluthund bearbeitet.
zum Seitenanfang zum Seitenende Profil || Suche
006
12.07.2010, 15:20
bennifilm



Danke auch noch mal an dich für die Lehrstunde :)

Nur noch eine kurze Frage : Kann es sein das der ein oder andere Compiler
nicht mit "long int" umgehen kann ? oder bezieht sich das nur auf "long long".

Noch ein Frage am Ende , muss die main() Funktion immer als "int" definiert werden oder geht auch "void"

Nochmal der fertige Code :
Quellcode:
#include <iostream>
#include <string>
#include <conio>
#include <stdio>

using namespace std;

unsigned long long fak(unsigned long long int x)
{
    unsigned long long int i = 1;
    unsigned long long int loop = 1;
    int y = x;
    while (loop != y)
    {
          x = x * i;
          ++i;
          ++loop;
    }
    return x;        
}
    

int main()
{
    unsigned long long int zahl;
    cout <<"Faku von :"<< endl;
    cin >> zahl;
    cout << "Ergebnis :" << endl;
    cout << fak(zahl)<< endl;
    while (!kbhit()) //läuft solange keine Taste gedrückt wird
    {
    }
}

--

::Alien Swarm Sunstation 583::
::Left 4 Dead:: b_alley 95% | b_office 65%
::Battlemod:: steht still

http://www.iop-b.de/


Dieser Beitrag wurde am 12.07.2010 um 15:23 von bennifilm bearbeitet.
zum Seitenanfang zum Seitenende Profil || Suche
007
12.07.2010, 15:39
Raziel



Bin kein C++ Profi, in deinem Fall fehlt noch ein "return 0;" am Ende der "main()" Funktion, schließlich hast du ja einen Rückgabewert definiert.

Zu deiner Frage, bezüglich "void" oder "int":

Zitat:
Zitat aus dem C99 Final Draft:

5.1.2.2.1 Program startup
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;8) or in some other implementation-defined manner.
If they are declared, the parameters to the main function shall obey the following
constraints:
— The value of argc shall be nonnegative.
— argv[argc] shall be a null pointer.
— If the value of argc is greater than zero, the array members argv[0] through
argv[argc-1] inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The
intent is to supply to the program information determined prior to program startup
from elsewhere in the hosted environment. If the host environment is not capable of
supplying strings with letters in both uppercase and lowercase, the implementation
shall ensure that the strings are received in lowercase.
— If the value of argc is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0] shall be the null character if the
program name is not available from the host environment. If the value of argc is
greater than one, the strings pointed to by argv[1] through argv[argc-1]
represent the program parameters.
— The parameters argc and argv and the strings pointed to by the argv array shall
be modifiable by the program, and retain their last-stored values between program
startup and program termination.

Ergo: int main() { return 0; }

--


Dieser Beitrag wurde am 12.07.2010 um 15:41 von Raziel bearbeitet.
zum Seitenanfang zum Seitenende Profil || Suche
008
12.07.2010, 17:07
Bluthund



@bennifilm:
Mit long int kann jeder ISO-C++-kompatible Compiler umgehen. Bei den meisten macht es aber keinen Unterschied ob du long int oder int verwendest, da beide meistens die gleiche Breite haben (edit: zumindest solange du dich im Bereich von 32/64Bit x86 bewegst). Es muss naemlich lediglich sizeof(int) <= sizeof(long) garantiert sein.
Ja, main muss in C++ als Rueckgabetyp int haben (siehe unten).

@Raziel:
Wie oben schon gesagt basiert ISO-C++ nicht auf C99, sondern auf C89 mit den '95er Anhaengen (der Standard wurde '98 verabschiedet; also vor der Standardisierung von C99). Das Zitat unterstreicht deine Aussage also nicht wirklich.
In C++ kann das return-Statement in der main-Funktion entfallen. In so einem Fall wird automatisch "Programm wurde ohne Fehler beendet" an die rufende Umgebung zurueck gegeben.

Zitat:
TCPPPL §3.2:
The int value returned by main(), if any, is the program's return value to "the system." If no value is returned, the system will receive a value indicating successful completion. A nonzero value from main() indicates failure.
oder auch http://www.csci.csusb.edu/dick/c++std/cd2/basic.html#basic.start.main fuer das uebliche Standardisierungskomitee-Gebrabbel.

--

The C language combines all the power of assembly language with all the ease-of-use of assembly language.
"humorig is n blödwort :>" by -CarniGGeLjumpR-


Dieser Beitrag wurde am 12.07.2010 um 17:51 von Bluthund bearbeitet.
zum Seitenanfang zum Seitenende Profil || Suche
009
12.07.2010, 19:41
Raziel



Das passiert, wenn man mit Halbwissen um sich wirft, jetzt weiß ich's besser ;)

--

zum Seitenanfang zum Seitenende Profil || Suche