Ciao a tutti, dovevo fare un programma in visual basic che calcolava le disposizioni con ripetizione di N elementi in classe K. Girovagando in internet ho trovato un JavaScript che effettua perfettamente ciò che avevo in mente, sto cercando di tradurlo per effettuarci un lavoro in VB 2010 solo che non riesco perchè ci sono alcune funzioni che non so come tradurle.
Il sito da cui ho prelevato il JavaScript è questo
http://members.chello.nl/k.ijntema/download.html
Lo script è quello nella voce "Permutations and combinations" (http://members.chello.nl/k.ijntema/combi2dir.zip).
La funzione che mi interessa è il terzo radiobutton della pagina, quello con n^k.
il codice singolo è questo:
PARTE 1
var res=Math.pow(varn,vark);
var resstr=res.toString();
var reslen=resstr.length;
if(reslen>=13){
var msg="P("+varn+","+vark+") [ordered, repetition allowed] = n^k has too many digits (programmed at 12 digits at most).";
ok=false;
show(msg);
}
else{
var ok=true;
for(var i=0;i<reslen;i++){
var lit=resstr.charAt(i);
if (lit<'0'||lit>'9'){
ok=false;
}
}
if(ok==false){
var msg="P("+varn+","+vark+") [ordered, repetition allowed] = n^k has too many digits (programmed at 12 digits at most).";
show(msg);
}
else{
form1.area1.value="P("+varn+","+vark+") [ordered, repetition allowed] = "+res
}
PARTE 2
if(res>=10001||varn>=27){
var msg="For practical reasons enumeration of the arrangements of this type is limited to 10000 and n < 27.";
ok=false;
form1.area1.style.backgroundColor='#FF6060';
form1.area1.value+="\n\n"+msg;
}
else{
var ar=new Array();
for(i=0;i<=vark-1;i++){
ar[i]=new Array();
}
ar=enumer3(varn,vark,res);
var shw=form1.area1.value+"\n\n";
var xy=vark-1;
ar[xy].sort();
for(k=0;k<res;k++){
shw+=ar[xy][k]+" "
}
form1.area1.value=shw;
}
Io ho provato a tradurlo così, ma mi da molti errori, sopratutto nella parte finale.
Dim resstr, msg As String
Dim res, varn, vark, reslen, i, lit As Integer
Dim ok As Boolean
varn = Val(TextBox1.Text)
vark = Val(TextBox2.Text)
res = Math.Pow(varn, vark)
TextBox3.Text = res
''-----------------------------------------------------------??
resstr = res.ToString()
TextBox4.Text = resstr
''-----------------------------------------------------------
reslen = resstr.Length()
TextBox5.Text = reslen
''-----------------------------------------------------------
If (reslen >= 10) Then
MsgBox("P(" & varn & "," & vark & ") [ordered, repetition allowed] = n^k has too many digits (programmed at 9 digits at most).")
ok = False
Else
ok = True
For i = 1 To reslen + 1
lit = Val(Mid(resstr, i, i))
If lit < 0 Or lit > 9 Then
ok = False
End If
Next
If ok = False Then
MsgBox("P(" & varn & "," & vark & ") [ordered, repetition allowed] = n^k has too many digits (programmed at 9 digits at most).")
Else
TextBox6.Text = "P(" & varn & "," & vark & ") [ordered, repetition allowed] = " & res
End If
End If
''-----------------------------------------
Dim ar() As Array
Dim enumer3(3)
Dim shw As String
Dim k As Integer
Dim xy As Object
If res >= 10001 Or varn >= 27 Then
Msg = "For practical reasons enumeration of the arrangements of this type is limited to 10000 and n < 27."
ok = False
TextBox7.Text = "\n\n" & msg
Else
For i = 0 To vark - 1
Dim Ar(i) As Array
Next
ar = enumer3(varn, vark, res)
shw = TextBox7.Text & " "
xy = vark - 1
ar((xy).sort())
For k = 0 To res
shw = shw & ar(xy)(k) + " "
Next
End if
TextBox7.Text = shw
PS: Gli errori sono quelli in grassetto, comunque non so se quello precedente funziona, perchè non riesco a compilarlo.