Da ich zur Zeit C++
anfang bzw. grade lerne hoffe ich das ihr mir ein paar tipps geben könnt
oder vielleicht auch ein paar Rätsel stellt die ich lösen muss im laufe meines c++ kapitel :)
Danke schon mal
Druckbare Version
Da ich zur Zeit C++
anfang bzw. grade lerne hoffe ich das ihr mir ein paar tipps geben könnt
oder vielleicht auch ein paar Rätsel stellt die ich lösen muss im laufe meines c++ kapitel :)
Danke schon mal
int i = 30;
i += i++ + ++i;
welches Ergebnis liefert das unter c++ und warum ist das Ergebnis bei Java anders? =) (bzw. wie wird es bei C++ berechnet und wie bei Java?)
ok ich werd die Lösung dann posten. Nächste Rätsel bitte! :D
Nimm eine Zahl * 8. Nur mit binären Operatoren.
Überprüfe mit einem binären Operator, ob eine Zahl gerade ist oder nicht.
Und wie soll das bitte gehen? Der Ausdruck ist undefiniert und es ist compilerabhängig was dabei rauskommt.
Siehe: Stroustrup: C++ Style and Technique FAQ
Oder hättest du eine andere Antwort erwartet? Was soll da bitte die Begründung sein?
hierbei gehts ihm denke ich um das Pre bzw eben postfix... was mich aber interessieren würde ist warum das in java anders berechnet wird... und vor allem wies da abläuft...gleich nochmal back 2 basics ... =/
int i = 30;
i += i++ + ++i;
i wird mit 30 initialisiert.
"+=" heisst, dass auf das gesetzte i (30) noch was dazu kommt.
i++ => i+1 (31)
+ => normaler operator
++i => i+1 (31)
Also im Ergebnis: 30 + 31 + 31= 92
@Invincible
Hier verweise ich nochmal auf Stroustrups FAQ: "Similarly, the order of evaluation of arguments is unspecified."
i += i++ + ++i;
Habs also mal für folgende Szenarien durchgespielt:
1) Erst ++i, dann i++, dann i
31 + 31 + 32 = 94
2) Erst i, dann ++i, dann i++
30 + 31 + 31 = 92
3) Erst i, dann i++, dann ++i
30 + 30 + 32 = 92
4) Erst i++, dann ++i, dann i
30 + 32 + 32 = 94
^^ ok, dann helfe ich mal:
Hier der Code:
int i = 30;
i += i++ + ++i;
In C++ liefert es als Ergebnis: 94
In Java liefert es als Ergebnis: 92
So jetzt die große Frage: WARUM?
Fangen wir mit C++ an. Ausdrücke werden immer von rechts nach links ausgewertet.
1. Schritt: ++i wird ausgeführt und dann Ergebnis eingesetzt:
i += i++ + 31;
2. Schritt: Als nächstes wird i++ ausgeführt, d.h. zuerst i (=31) eingesetzt und danach i erhöht, also:
i += 31 + 31;
3. Schritt: Nun lösen wir das += auf in nur = und setzen für i beim summieren 32 ein:
i = 32 + 31 + 31
i = 94
So und jetzt kommt die große Frage, was macht Java anders?
Ich bin damals 1 Stunde gesessen und hab überlegt ^^ bin aber auf eine Lösung gekommen ;) Lol nur leider fällt sie mir grad jetzt nicht ein, dazu müsste ich wieder n Stück länger nachdenken, aber vll. fällt sie ja einem von euch auch ein. Also hf damit =) =)
(ps: natürlich ist der Ausdruck oben definiert, da c++ Ausdrücke immr von rechts nach links auflöst)
edit: Eines noch vorweg, die Reihenfolge bei Java ist auch von rechts nach links, es hat mit der Reihenfolge des Einsetzens zu tun. Bei C++ wirds immer kurz davor eingesetzt, bei Java glaube ich, gleich am Anfang, wodurch sich später ein Wert überschreibt, was dann zum Ergebnis führt.
Manchmal frage ich mich wirklich, ob die Leute meine Posts mit Absicht übergehen, oder einfach nur zufälliger Weise nicht zuhören.
Also nochmal auf deutsch: wenn dein C++ Compiler die Ausdrücke von rechts nach links auswertet, dann mag das zwar schön sein, aber es ist keines Falls irgendwo vorgeschrieben und könnte ebenso gut von links nach rechts, oder von der mitte nach rechts und dann von links wieder zur Mitte sein.Zitat:
Zitat von http://www2.research.att.com/~bs/bs_faq2.html#undefined
oh stimmt sry, dann hab ich das mit was verwechselt oO
In C/C++ ist die Reihenfolge undefiniert, mein Compiler hats aber von rechts nach links ausgewertet. (100% tig so, wie ich es geschrieben habe, weil ichs mir in assembler angesehen habe).
Java allerdings gibt eine Reihenfolge vor, nämlich links nach rechts.
rubyquiz.com - Funktioniert prima in jeder Sprache. Hardcore OOP-Design, los geht's.
Und wenn hier einer wegen OOP rumweint, den bitte ich doch, sich den Thread-Titel genauer anzusehen. Tru! :)
Edith: Rifler, nur weil es in einem Fall so war, ist es natürlich nicht immer so. Zum Einen nicht, weil du nur einen Ausdruck getestet hast, zum Anderen schon mal gar nicht, weil du von einem einzelnen Compiler auf C und C++ als Sprache schließt.
mh ok dann wäre das wohl gelöst^^ Bitte noch weitere rätsel vielleicht diesmal nicht ganz soooooooo schwer :)
Danke aber bisher