Hi Dominique. This is a new subject but I think you are the correct person. hope this can result interesting.
I usually concatenate strings to create polynoms. Example:
Answer = join("", a, "/", b, " x^3 + ", c, "/", d, " x^2 + ", e , "/" , f, " x", g , "/", h) ;
So ... frecuently the students see an answer like:
"a possible answer is 0/3 x^3 + 4/4 x^2 + -2/5 x + -1/-8"
I supposse you have the same problem.
Well, I created a code to generate nice string. similar to function: poly.
There are some differences between mine and poly:
a) Mine doesn't generate HTML style, so it is no very useful to use it in Main Question
b) Mine considers fractions
c) Mine is limited to 5 terms because I didn't know how put for ( i : [0 : CantTerm])
I am contacting you because maybe you know a way to create a function to call it, just like function poly().
By the way, I made the code thanks to the last example you sent. It was not only useful to solve my problem, but to teach me how to use the for statement.
I add the code; maybe you want it; maybe someone else does.
---------------------------------------------------------------------------------------------------------------------------
# ----------------- Variables de entrada ---------------------
Variable = "x";
CadenaIniNum = [ 0 , 1 , 0 , 2 , 4 ];
CadenaIniDen = [ 1 , 1 , -2 , 2 , 2 ];
# ----------------- Procedimiento ---------------------
Signos = [ "+" , "-" , ""];
Cadena = "";
CantTerm = len(CadenaIniNum);
# ----- Detecta primer término diferente de 0 -------
PrimerTerm = 0;
for ( i : [0 : 5])
PrimerTerm = (PrimerTerm == 0 && CadenaIniNum[i] <> 0)? i : PrimerTerm;
for ( i : [ 0 : 5] ) {
# ------------------ Coeficiente ----------------------
iNum = CadenaIniNum[i];
iDen = (CadenaIniDen[i] == 0)? 1 : CadenaIniDen[i];
iNumAbs = abs(iNum);
iDenAbs = abs(iDen);
MCD = gcd( iNumAbs , iDenAbs);
iNumAbs = iNumAbs / MCD;
iDenAbs = iDenAbs / MCD;
iCadCoefPos = [ "", join("" , iNumAbs) , join("" , iNumAbs, "/", iDenAbs) ];
Pos = (iNumAbs == 0)? 0 : ( ( iNumAbs == iDenAbs )? 0: ((iDenAbs == 1)? 1 : 2) );
iCadCoef = iCadCoefPos[Pos];
# ------------------- Signo -------------------------
iSignoPos = (( i <= PrimerTerm && iNum >= 0) || iNum == 0 )? 2: (( (iNum > 0 && iDen > 0) || (iNum < 0 && iDen < 0) )? 0 : 1);
iSigno = Signos[iSignoPos];
# ------------------ Expresión --------------------------
iExp = CantTerm - i - 1;
iExpresPos = [ "" , Variable, join("", Variable, "^", iExp) ];
Pos = (iNum == 0 || iExp == 0)? 0 : (( iExp == 1)? 1 : 2 ) ;
iExpres = iExpresPos[Pos];
Cadena = join(" ", Cadena, iSigno, iCadCoef , iExpres);
}
Respuesta = Cadena;