nelle corrispondenti lettere di testo. Dopo varie ore di lavoro, all'inizio il lavoro sembrava più facile, la lingua italiana è piena di eccezioni, pur scrivendo in modo barbaro sono riuscito nell'intento. Gradirei suggerimenti e consigli su come occupare meno memoria di programma oppure ricevere una funzione scritta meglio. Allego il programma.
- Codice: Seleziona tutto
//17/2/023 scrive un numero di tre cifre decimali in lettere
const String U1 = "uno", U2="due",U3="tre",U4="quattro",U5="cinque",U6 = "sei",U7 = "sette",U8="otto",U9="nove",U10="dieci";
const char *vet_U[] = {"zero","uno", "due","tre","quattro","cinque","sei","sette","otto","nove","dieci"} ;
const String DICI = "dici", ANT = "ant";
const String U20 = "vent",U30 = "trent", U40 = "quar",U50 = "cinqu", U60="sess", U70="sett",U80="ott",U90="nov";
//const char *vet_D[] = {"null", "dec", "vent", "trent", "quar", "cinqu", "sess","sett", "ott","nov"};
const String CENTO = "cento", MILA = "mila";
int N,NI,NU,ND,NC,NDU, N1,N5;
String testo,testU,testoC;
void selezunita() { if(NU>0 && NU<=9){testU = vet_U[NU];} }
void legginumero()
{
//cerca se ci sono delle centnaia
NC = NI/100;
N = NI - NC*100;// numero (decine e unità) inferiore a 100
if (NC > 0)
{ if (NC == 1) {testoC = CENTO;} else {testoC = vet_U[NC]+CENTO;} } else {testoC = "";}
if (NC == 0 && N==0) { testo = vet_U[N];}else{testo="";} //non scrive zero se il numero è solo centinaia
if (N> 0 && N <=10) { testo = vet_U[N];} //
if (N == 11){testo = "un"+DICI;}
if (N == 12){testo = "do"+DICI;}
if (N == 13){testo = "tre"+DICI;}
if (N == 14){testo = "quattor"+DICI;}
if (N == 15){testo = "quin"+DICI;}
if (N == 16){testo = "se"+DICI;}
if (N == 17){testo = DICI+"as"+U7;}
if (N == 18){testo = DICI+U8;}
if (N == 19){testo = DICI+"ann"+U9;}
ND = N/10; //numero delle decine
if (N == 20){testo = U20+"i";}
if (N>20 && N< 30) { NU = N-20; selezunita(); if (NU==1 || NU==8){ testo = U20+testU;} else {testo= U20+"i"+testU;} }
/*
for (N1= 30;N1<= 90;N1=N1+10)
{
if (N == N1){testo =vet_D[N1/10] +"a";}
if (N>N1 && N<(N1+10)) {NU = N-N1; selezunita();if (NU==1 || NU==8){ testo = vet_D[N1/10]+testU;} else { testo = vet_D[N1/10]+"a"+testU;} }
}
// questa variante da errore di compilazione
*/
N1=30;
if (N == N1){testo = U30+"a";}
if (N>N1 && N< (N1+10)) {NU = N-N1; selezunita();if (NU==1 || NU==8){ testo = U30+testU;} else { testo = U30+"a"+testU;} }
N1 = 40;
if (N == N1){testo = U40+ANT+"a";}
if (N>N1 && N< (N1+10)) { NU = N-N1;selezunita(); if (NU==1 || NU==8){ testo = U40+ANT+testU;} else { testo = U40+ANT+"a"+testU;} }
N1 = 50;
if (N == N1){testo = U50+ANT+"a";}
if (N>N1 && N<( N1+10)) { NU = N-N1; selezunita(); if (NU==1 || NU==8){ testo = U50+ANT+testU;} else { testo = U50+ANT+"a"+testU;} }
N1 = 60;
if (N == N1){testo = U60+ANT+"a";}
if (N>N1 && N<( N1+10)) { NU = N-N1; selezunita(); if (NU==1 || NU==8){ testo = U60+ANT+testU;} else { testo = U60+ANT+"a"+testU;} }
N1 = 70;
if (N == N1){testo = U70+ANT+"a";}
if (N>N1 && N<( N1+10)) { NU = N-N1; selezunita(); if (NU==1 || NU==8){ testo = U70+ANT+testU;} else { testo = U70+ANT+"a"+testU;} }
N1 = 80;
if (N == N1){testo = U80+ANT+"a";}
if (N>N1 && N<( N1+10)) { NU = N-N1; selezunita(); if (NU==1 || NU==8){ testo = U80+ANT+testU;} else { testo = U80+ANT+"a"+testU;} }
N1 = 90;
if (N == N1){testo = U90+ANT+"a";}
if (N>N1 && N<( N1+10)) { NU = N-N1; selezunita(); if (NU==1 || NU==8){ testo = U90+ANT+testU;} else { testo = U90+ANT+"a"+testU;} }
testo = testoC+testo;
}
void setup()
{ Serial.begin(9600); // open the serial port at 9600 bps:
//legginumero();
for ( NI = 0; NI<500; NI=NI+1)
{ legginumero();
Serial.print( testo);Serial.print(" ");
N5 = N5 +1; if (N5==10){N5=0; Serial.println(" ");}
}
}
void loop()
{ //non utilizzato