|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
[C++] Aiutatemi ad accorciare questo if-else
oggi pomeriggio scrivendo un codice mi sono accorto di aver scritto un if-else spropositato e vorrei sapere come è possibile accorciare una struttura di questo tipo.
questo è il codice Codice:
if(data>1582) { if(data%4==0&&(!(data%100==0))) { cout << "E' bisestile.\n"; } if(data%100==0) { if (data%400==0) { cout << "E' bisestile.\n"; } else { cout << "Non e' bisestile.\n"; } } } else { cout << "Non e' bisestile.\n"; } |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
bool bisestile = (data>1582) && ((!(data % 4) && (data%100)) || (!data%400))
__________________
In God we trust; all others bring data |
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
lho ridotto usando una variabile e uno switch ma è ancora grande :\
Codice:
int a; if(data%1582==0) { if(data%400==0) { a=1; } else { if(data%4==0) { a = 1; } } } switch(a) { case "1": cout << "E' bisestile.\n";break; default: cout << "Non e' bisestile.\n;break; } ![]() grazie 1000 |
![]() |
![]() |
![]() |
#4 |
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
non sono sicuro che funzioni, ma se ho capito bene dovrebbe essere equivalente a:
Codice:
if(data>1582) { if(!((data % 4 == 0) ^ (data % 100 == 0))) { cout << "E' bisestile"; } else { cout << "Non e' bisestile.\n"; } } else { cout << "Non e' bisestile.\n"; } Ultima modifica di 71104 : 26-06-2006 alle 16:54. |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
^ che significa? (elevato a?)
|
![]() |
![]() |
![]() |
#6 |
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
no, è lo XOR:
false ^ false = false false ^ true = true true ^ false = true true ^ true = false |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Io l'avevo fatta cosí:
Codice:
bool IsLeap( int Y ) { bool Leap = false; if ( (Y % 4 ) == 0 ) Leap = true; if ( (Y % 100) == 0 ) Leap = false; if ( (Y % 400) == 0 ) Leap = true; return Leap; } Ma i bresciani stanno avanti ![]()
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
il codice + corto lha fatto sottovento, 1 riga
![]() io non sarei mai riuscito a fare una cosa del genere ![]() |
![]() |
![]() |
![]() |
#9 | |
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
ma la bravura non é inversamente proporzionale al numero di righe ![]() Io quelle le chiamo "mandrakate" ... interventi geniali, sicuramente, ma poi ti fanno stare 2 ore a capirli quando riprendi il programma 1 mese dopo ![]()
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
vabbe basta un commentuccio
![]() |
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
[domandascema]ma -?-:- si puo concatenare? tipo -?-: (-?-:-) (- sono le parti di codice^^)[/domandascema]
|
![]() |
![]() |
![]() |
#12 | ||
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
![]() Quote:
![]() un esempio ? perché fai 'ste domande ar cane ? ![]()
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
||
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
intendevo:
anzike fare Codice:
if(x) {y} else if(z) {a} Codice:
(x)? //if(x) (y) //{y} : //else ( (z)? //if(z) (a) //{a} :() //null ) |
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
l'operatore ternario é una if con ritorno di valore quindi si.
esempio Codice:
z = (x == 1) ? x : (y == 2) ? y : 0;
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
una dritta
![]() puoi anche scegliere di fare un qualcosa o no con il ternario: Codice:
(++x) >= MAX_LEN ? PrintHeader() : (void)NULL Peró devi castare il NULL a void perché PrintHeader é void ![]()
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
![]() |
![]() |
![]() |
#16 | |
Senior Member
Iscritto dal: Dec 2000
Città: bologna
Messaggi: 1309
|
Quote:
![]() |
|
![]() |
![]() |
![]() |
#17 | |
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
![]()
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
![]() |
![]() |
![]() |
#18 | |
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Quote:
![]() High Flying Sottovento
__________________
In God we trust; all others bring data |
|
![]() |
![]() |
![]() |
#19 | |
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
in una riga é "geniale". Non ho detto che sei Einstein dai ![]()
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: May 2006
Città: Salerno
Messaggi: 936
|
non ho capito quella cosa con ++x max_num ...
![]() |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:41.