Exemples

Algorithme de Fibonacci itératif
Passer des arguments à un script

Comparer deux fichiers
Charger et afficher un document XML
Compter les occurences d'une chaîne
Classe et héritage
Utiliser JAVA
Utiliser GTK


Algorithme de Fibonacci itératif

int fibmax = 20

int fibonacci(int n)
    int u = 0
    int v = 1
    int t
    for int i in 2 .. n
        t = u + v
        u = v
        v = t
    /for
return v

for int x in 1..fibmax echo "fib(" , x , ") ", fibonacci(x), "\n"


Passer des arguments à un script

Usage: sol ex_main [some parameters]
Unlike the for .. in, that reads each element of a sequence into a variable, scan points out each of them, and each may be read or modified, as the [] empty subscripting designates the current element.

int main(array arglist, int argnum)
  print argnum, "arguments"
  int i = 0
  scan arglist                    ` scan the array
    print "$i)", arglist[]     ` display the next argument
    i + 1
  /scan
return
0

main($argv, $argc) ` argv and argc are Php variables directly used.


fcomp - Comparer deux fichiers

die() sends a message and exits the program

int main(int argnum, array arglist)
  if argnum <> 3 ? die("usage: php fcomp.php file1 file2")
  print "Comparing", arglist[1], arglist[2]
  array f1, f2
  f1.load(arglist[1])        ` array filled with a file
  f2.load(arglist[2])
  if f1.size() <> f2.size() ? die("sizes differ")
  for int i in 0..f1.size() - 1
    if f1[i] <> f2[i] ? die("files differ")
  /for
  print "No difference found."
return 0

main($argv) ` argv is a direct Php variable.


Charger et afficher un document XML

`` Ce petit programme teste le chargeur XML sax
`` et affiche les noms de pilotes F1.


xml dom
/xml

text fname = "demos\f1-drivers.xml"
print "loading", fname, "..."
if not dom.load(fname)
  print fname, "file not found"
exit()
/if

print dom.document.driver.at()
while dom.isFound()
  dom.down() // set current to name
  text driver = dom.getData()
  print driver.trim()
  dom.up()
let dom.inc()


Compter les occurences d'une chaîne

Text, array, dict, file, are virtual classes, and each instance may use methods of the virtual class, as find here, method of text.

class String

int count(text base, text sea) `one of the methods of the String class
  int ctr = 0
  int i = 0
  while forever
    i = base.find(sea, i)   ` return the position, if found
    if i = nil break        ` else, return nil (not in list).
    i + 1      ` advancing the pointer inside the base text
    ctr + 1   ` counter of occurences
  /while
return ctr

/class

String str           `declaring an instance of the String class
print str.count("somestring", "string")


Classe et héritage (ex_car.sol)

From the base class "Car", we can inherit other classes that use its methods, and make instances either of "Car" or the subclasses.

class Car     ` this is a base class

  int power = 850
  int getPower() return power

  text color(int c)
    text tc = "other"
    if c
    = 1: tc = "blue"
    = 2: tc = "green"
    = 4: tc = "red"
    /if
  return "color is " + tc
/class

class FormulaOne is Car     ` FormulaOne is a kind of Car
  int speed
  int getSpeed()
    speed = getPower() * 2 / 4
  return speed
/class

FormulaOne f1                   // instance of FormulaOne
print f1.power                   // attribute of the superclass
print f1.getPower()           // method of the superclass
print f1.getSpeed()            // method of the class itself
print FormulaOne.color(4) // static method of the superclass (Car.color() works also).


Utiliser JAVA

` You can view and download the class JavaDemo.java

import java.lang.System
import java JavaDemo

System infos          ` instance of the java.lang.System class
JavaDemo demo    ` instance of the JavaDemo class

demo.myVar = infos.getProperty("java.version")
print "Java version" , demo.getVar()

demo.setVar(infos.getProperty("os.name") + " " + infos.getProperty("os.version"))
print "OS", demo.myVar
demo.exit(0)

The JavaDemo class
public class JavaDemo
{
 public String myVar;

 // Assign a value to a variable
 public void setVar(String str)
 {
   myVar = str;
 }

 // Return the value of a variable
 public String getVar()
 {
   return myVar;
 }
}



Utiliser GTK

include "gtk.sol"
include <gtk/gtk.h>

int main(int argc, array argv)
  GtkWidget window = null
  gtk_init(null, null)
  window = gtk_window_new($GTK_WINDOW_TOPLEVEL)
  gtk_widget_show(window)
  gtk_main()
return 0

XML



Voir Algorithmes et scripts pour d'autres programmes...

Page de téléchargement