Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/polystr

Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/polystr

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras
Filename: Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/polystr
Read OK:
File size: 02E3 bytes
Load address: 0000
Exec address: 0000
File contents
/* string representation of polynomial with coefficients
   in c and indeterminate x.

   Uses bob:h.string.string
*/

poly2str(c,x)
{
 local n,i,prev,s,a;
 n = sizeof(c);
 prev = FALSE;
 s = "";
 for ( i = 0; i < n; i++)
   if (a = c[i])
   {
    s += coeff(a,prev,i)+monom(x,i);
    prev = TRUE;
   }
 return (prev)?s:"0";
}

coeff(a,prev,deg)
{
 local s;
 s = (prev && a>0)?"+":"";
 switch (a)
 {
  case 1:
    if (deg == 0) s += "1";
    break;
  case -1:
    s += (deg == 0)?"-1":"-";
    break;
  default:
    s += string(a,10);
    break;
 }
 return s;
}
 
monom(x,deg)
{
 local s;
 switch (deg)
 {
  case 0:
    s = "";
    break;
  case 1:
    s = x;
    break;
  default:
    s = x+"^"+string(deg,10);
    break;
 }
 return s;
}
00000000  2f 2a 20 73 74 72 69 6e  67 20 72 65 70 72 65 73  |/* string repres|
00000010  65 6e 74 61 74 69 6f 6e  20 6f 66 20 70 6f 6c 79  |entation of poly|
00000020  6e 6f 6d 69 61 6c 20 77  69 74 68 20 63 6f 65 66  |nomial with coef|
00000030  66 69 63 69 65 6e 74 73  0a 20 20 20 69 6e 20 63  |ficients.   in c|
00000040  20 61 6e 64 20 69 6e 64  65 74 65 72 6d 69 6e 61  | and indetermina|
00000050  74 65 20 78 2e 0a 0a 20  20 20 55 73 65 73 20 62  |te x...   Uses b|
00000060  6f 62 3a 68 2e 73 74 72  69 6e 67 2e 73 74 72 69  |ob:h.string.stri|
00000070  6e 67 0a 2a 2f 0a 0a 70  6f 6c 79 32 73 74 72 28  |ng.*/..poly2str(|
00000080  63 2c 78 29 0a 7b 0a 20  6c 6f 63 61 6c 20 6e 2c  |c,x).{. local n,|
00000090  69 2c 70 72 65 76 2c 73  2c 61 3b 0a 20 6e 20 3d  |i,prev,s,a;. n =|
000000a0  20 73 69 7a 65 6f 66 28  63 29 3b 0a 20 70 72 65  | sizeof(c);. pre|
000000b0  76 20 3d 20 46 41 4c 53  45 3b 0a 20 73 20 3d 20  |v = FALSE;. s = |
000000c0  22 22 3b 0a 20 66 6f 72  20 28 20 69 20 3d 20 30  |"";. for ( i = 0|
000000d0  3b 20 69 20 3c 20 6e 3b  20 69 2b 2b 29 0a 20 20  |; i < n; i++).  |
000000e0  20 69 66 20 28 61 20 3d  20 63 5b 69 5d 29 0a 20  | if (a = c[i]). |
000000f0  20 20 7b 0a 20 20 20 20  73 20 2b 3d 20 63 6f 65  |  {.    s += coe|
00000100  66 66 28 61 2c 70 72 65  76 2c 69 29 2b 6d 6f 6e  |ff(a,prev,i)+mon|
00000110  6f 6d 28 78 2c 69 29 3b  0a 20 20 20 20 70 72 65  |om(x,i);.    pre|
00000120  76 20 3d 20 54 52 55 45  3b 0a 20 20 20 7d 0a 20  |v = TRUE;.   }. |
00000130  72 65 74 75 72 6e 20 28  70 72 65 76 29 3f 73 3a  |return (prev)?s:|
00000140  22 30 22 3b 0a 7d 0a 0a  63 6f 65 66 66 28 61 2c  |"0";.}..coeff(a,|
00000150  70 72 65 76 2c 64 65 67  29 0a 7b 0a 20 6c 6f 63  |prev,deg).{. loc|
00000160  61 6c 20 73 3b 0a 20 73  20 3d 20 28 70 72 65 76  |al s;. s = (prev|
00000170  20 26 26 20 61 3e 30 29  3f 22 2b 22 3a 22 22 3b  | && a>0)?"+":"";|
00000180  0a 20 73 77 69 74 63 68  20 28 61 29 0a 20 7b 0a  |. switch (a). {.|
00000190  20 20 63 61 73 65 20 31  3a 0a 20 20 20 20 69 66  |  case 1:.    if|
000001a0  20 28 64 65 67 20 3d 3d  20 30 29 20 73 20 2b 3d  | (deg == 0) s +=|
000001b0  20 22 31 22 3b 0a 20 20  20 20 62 72 65 61 6b 3b  | "1";.    break;|
000001c0  0a 20 20 63 61 73 65 20  2d 31 3a 0a 20 20 20 20  |.  case -1:.    |
000001d0  73 20 2b 3d 20 28 64 65  67 20 3d 3d 20 30 29 3f  |s += (deg == 0)?|
000001e0  22 2d 31 22 3a 22 2d 22  3b 0a 20 20 20 20 62 72  |"-1":"-";.    br|
000001f0  65 61 6b 3b 0a 20 20 64  65 66 61 75 6c 74 3a 0a  |eak;.  default:.|
00000200  20 20 20 20 73 20 2b 3d  20 73 74 72 69 6e 67 28  |    s += string(|
00000210  61 2c 31 30 29 3b 0a 20  20 20 20 62 72 65 61 6b  |a,10);.    break|
00000220  3b 0a 20 7d 0a 20 72 65  74 75 72 6e 20 73 3b 0a  |;. }. return s;.|
00000230  7d 0a 20 0a 6d 6f 6e 6f  6d 28 78 2c 64 65 67 29  |}. .monom(x,deg)|
00000240  0a 7b 0a 20 6c 6f 63 61  6c 20 73 3b 0a 20 73 77  |.{. local s;. sw|
00000250  69 74 63 68 20 28 64 65  67 29 0a 20 7b 0a 20 20  |itch (deg). {.  |
00000260  63 61 73 65 20 30 3a 0a  20 20 20 20 73 20 3d 20  |case 0:.    s = |
00000270  22 22 3b 0a 20 20 20 20  62 72 65 61 6b 3b 0a 20  |"";.    break;. |
00000280  20 63 61 73 65 20 31 3a  0a 20 20 20 20 73 20 3d  | case 1:.    s =|
00000290  20 78 3b 0a 20 20 20 20  62 72 65 61 6b 3b 0a 20  | x;.    break;. |
000002a0  20 64 65 66 61 75 6c 74  3a 0a 20 20 20 20 73 20  | default:.    s |
000002b0  3d 20 78 2b 22 5e 22 2b  73 74 72 69 6e 67 28 64  |= x+"^"+string(d|
000002c0  65 67 2c 31 30 29 3b 0a  20 20 20 20 62 72 65 61  |eg,10);.    brea|
000002d0  6b 3b 0a 20 7d 0a 20 72  65 74 75 72 6e 20 73 3b  |k;. }. return s;|
000002e0  0a 7d 0a                                          |.}.|
000002e3