Você está na página 1de 31

Programming languages by example 

Introduction:
The programming languages that have compilers operating under Linux OS (we shall be concerned 
here with openSuse and/or simplyMEPIS VERSION 6.5 or 7.0) and will be covered here in this article 
are: C++, fortran77, gfortran, pascal (free pascal compiler), SmallEiffel, ADA, Haskell, Ocaml and 
Java. Example programs linking with the following graphical libraries will also be presented: gtkmm, 
OSG and free GLUT.

Command line 
Compiler type & 
Language compiling command or  Example
version
statement
C++ (GNU, 
g++ g++ hellocpp.cpp ­o hello1 gcc v. 4.0.3
gcc)
fortran77 
g77 g77 ­o hellog77 hello.for gcc v. 3.4.6
(GNU, gcc)
gfortran (GNU, 
gfortran gfortran ­o hellogfor hello.for gcc v. 4.0.3
GCC)
pascal (free 
fpc fpc hello.pas v. 2.2.0
pascal)
gnu eiffel 
SmallEiffel 
compile compile hello.e ­o hellose compiler release 
(GNU)
0.74
HASKELL 
ghc ghc ­­ make ­o hellohaskell hello.hs v. 6.4.1
(GHC)
ADA (GNU,  gcc ­c helloada.adb then gnatbl ­o hello2 
gcc then gnatbl gcc v. 4.0.3
gcc) helloada.ali
ocamlopt ­c amodule.ml, ocamlopt ­c 
OCaml ocamlopt bmodule.ml, ocamlopt ­o hellooc  version 3.10.2
amodule.cmx bmodule.cmx
javac helloj.java, to run type java + the  JDK, version 
Java javac
class name (eg. HelloClassApp) "1.5.0_13"
Let's get graphical:For example, GTK+ is a highly usable, toolkit for creating graphical user interfaces 
(GUI) which boasts cross platform compatibility and an easy to use API. GTK+ it is written in C, but 
has bindings to many other popular programming languages such as C++, Perl, Python and C# among 
others. GTKMM is the wrapper for C++ language (a library used to create graphical user 
interfaces).gtkmm speaks arabic.GTKMM programs can be compiled and run under Windows 
operating system. To do so, you may follow the steps given hereafter:
1. Download and install MingW. 
2. Download and install Dev­Cpp. 
3. Download and unzip GTK library under c:\MingW. 
4. Download and install gtkmm library under c:\Dev­Cpp. 
5. Download and install msys. 
6. Copy the source code into /home/user_name/ directory of C:\msys. 
7. Compile source code, example for command line compiling statements in msys window:
• export PKG_CONFIG_PATH=c:/Dev­Cpp/gtkmm/pkgconfig 
• g++ 'pkg­config ­­cflags gtkmm­2.4' example.cc ­o example 'pkg­config ­­libs gtkmm­
2.4' 
8. Run executable (.exe) Windows file
Command line compiling  Compiler type & 
Library Example
command or statement version
g++ gtkmareas.cc ­o gtkmareas `pkg­config 
gtkmm g++ g++ (GCC) 4.2.1 
gtkmm­2.4 ­­cflags ­­libs`
There are certain libraries that are written for example in C (or C++) that can be added to the 
microcomputer hard drve. With these libraries and the appropriate compiler, eg. C compiler, linking to 
such libraries and compiling your source, you can produce an output file (2D or 3D graphics one). 
Examples for such libraries are Cairo and OpenSceneGraphics (OSG).
Command line 
compiling  Compiler type 
Library Example
command or  & version
statement
cc ­o hellocairo $(pkg­config ­­cflags ­­libs  cc (GCC) 
cairo cc
cairo) hellocairo.c 4.2.1 
g++ ­c HelloMain.cpp, g++ ­c HelloSG.cpp, 
OpenSceneGraph  g++ ­o hello HelloSG.o HelloMain.o  g++ (GCC) 
g++
(OSG) ­I/usr/include ­losg ­losgDB ­losgUtil  4.2.1 
­losgViewer ­losgText 
Free GL utility  gcc ­o helloglut helloglut.c ­lm ­lGL ­lGLU  gcc (GCC) 
gcc
toolkit (GLUT) ­lglut  4.2.1 

General:
1. Make a new directory under /home/user­name (eg. hany)/Documents and call it executables. 
2. Add to this directory each executable (compiled) file. 
3. Go to the desktop, click Documents (Konquerer will open with a list of files & folders plus the 
menu and probably a tree­like files & folders on your PC), click View on the main menu and 
then choose Show hidden files. The hidden files that start with a dot (.) will appear. 
4. Go to /home/user­name (eg.han)y/ directory and click on .bashrc file. It will open in a text 
editor, probably kwrite. Add the following line to the end of the file export PATH=/home/user­
name (eg.hany)/Documents/executables:$PATH. Then save the file and cose (exit) the editor. 
5. Go to konquerer, click View in the menu list, then choose and click one more time Show hidden 
files, the hidden ones will disappear. 
Saying hello in different languages:
C++: 
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
#include <stdio.h>
int main ()
{
printf ("Hello world from gcc/g++\n");

3. Save the file as hellocpp.cpp 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hello1 
6. Open the konsole window and run hello1 (i.e. type hello1 at the prompt). 
Fortran 77:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
       program hello
c This program prints hello, leave at least 7 spaces before typing program hello above
      write (*, *) 'hello from g77, fortran77'
      stop
      end 
3. Save the file as hello.for 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hellog77 
6. Open the konsole window and run hellog77 (i.e. type hellog77 at the prompt). 
gFortran:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
       program hello
c This program prints hello, leave at least 7 spaces before typing program hello above
      write (*, *) 'hello from g77, fortran77'
      stop
      end 
3. Save the file as hello.for 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hellogfor 
6. Open the konsole window and run hellogfor (i.e. type hellogfor at the prompt). 
Pascal:
1. Open a new document in any text editor (kwrite, kedit or kate) or open a trminal (konsole) 
window and type fp to open the FreePascal IDE for Linux. 
2. Type the following lines:
program hello;
begin
writeln('hello from pascal');
end.
3. Save the file as hello.pas 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hello 
6. Open the konsole window and run hello (i.e. type hello at the prompt). 
SmallEiffel: 
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
 class SIMPLE
  creation
   make
  feature
   make is
   do
    io.put_string("Hello from SmallEiffel.%N");
   io.put_new_line
   end;
 end
3. Save the file as hello.e 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hellose 
6. Open the konsole window and run hellose (i.e. type hellose at the prompt). 
Haskell:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
main = do
putStrLn "hello, world, from haskell ghc!"
3. Save the file as hello.hs 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hellohaskell 
6. Open the konsole window and run hellohaskell (i.e. type hellohaskell at the prompt). 
ADA:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
­­ Print a simple message.
with Ada.Text_IO;
procedure Helloada is
begin
Ada.Text_IO.Put_Line("Hello, world from ada!");
end Helloada;
3. Save the file as helloada.adb 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hello2 
6. Open the konsole window and run hello2 (i.e. type hello2 at the prompt). 
OCaml:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following line:
let hello () = print_endline "Hello from OCaml"
Save this file as amodule.ml 
3. Open a new document in any text editor (kwrite, kedit or kate) and type the following line:
Amodule.hello ()
Save this file as bmodule.ml 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. hellooc 
6. Open the konsole window and run hellooc (i.e. type hellooc at the prompt). 
Java:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
/** 
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World from Java!"); // Display the string.
}
}
3. Save the file as hellojava.java 
4. Compile it per the above table 
5. Run the file, while you are in the directory where hellojava.java and HelloWorldApp.class are 
residing, by typing java HelloWorldApp 
gtkmm:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following line:
#include <gtkmm/button.h>
#include <gtkmm/window.h>
#include <gtkmm/main.h>
#include <iostream>
class HelloWorld : public Gtk::Window
{

public:
HelloWorld();
virtual ~HelloWorld();

protected:
//Signal handlers:
virtual void on_button_clicked();

//Member widgets:
Gtk::Button m_button;
};

int main (int argc, char *argv[])


{
Gtk::Main kit(argc, argv);
HelloWorld helloworld;
//Shows the window and returns when it is closed.
Gtk::Main::run(helloworld);

return 0;
}

HelloWorld::HelloWorld()
: m_button("Hello World") // creates a new button with label "Hello
World".
{
// Sets the border width of the window.
set_border_width(10);

// When the button receives the "clicked" signal, it will call the
// on_button_clicked() method defined below.
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&HelloWorld::on_button_clicked));

// This packs the button into the Window (a container).


add(m_button);

// The final step is to display this newly created widget...


m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
std::cout << "Hello World" << std::endl;
}

3. Compile it per the above table 
4. Copy the executable (compiled) file to the executables folder (directory), eg. hellogtk 
5. Open the konsole window and run hellogtk (i.e. type hellogtk at the prompt). 
A simple mathematical program (accepting user input, making simple calculatins and outputing results 
to screen):
C++:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
#include <istream>
#include <ostream>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a,b,c;
cout << ("enter a number\n") ;
cin >> a;
cout <<("enter another number\n") ;
cin >> b;
c = a*b;
cout << "multiplication result = " << c << endl;
cout << "division result = " << a/b << endl;
cout << "a ­ b result = " << a­b <<endl;
cout << "a + b result = " << a+b <<endl;
cout << "square root a and b result = " << sqrt(a) <<"; " << sqrt(b) <<endl;

3. Save the file as test00cpp.cpp 
4. Compile it: g++ test00cpp.cpp ­o simplemthc 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simplemathc 
6. Open the konsole window and run simplemathc (i.e. type simplemathc at the prompt). 
Fortran 77:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
       program simathg77
      real a, b, add, sub, mul, divis
c This program accepts 2 numbers and calculates few values
      write (*,*) 'enter a number a:'
      read (*,*) a
      write (*,*) 'enter a number b:'
      read (*,*) b
      add = a+b
      sub = a­b
      mul = a*b
      divis = a/b
      write (*,*) 'a+b = ', add, ' a­b = ', sub, ' a times b = ', mul
      write (*,*) 'a by b = ', divis, ' square root of a = ', sqrt(a)
      write (*,*) 'square root of b = ', sqrt(b)
      stop
      end 
3. Save the file as simathg77.for 
4. Compile it: g77 ­o simath77 simathg77.for 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simath77 
6. Open the konsole window and run simath77 (i.e. type simath77 at the prompt). 
gFortran:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
       program simathg77
      real a, b, add, sub, mul, divis
c This program accepts 2 numbers and calculates few values
      write (*,*) 'enter a number a:'
      read (*,*) a
      write (*,*) 'enter a number b:'
      read (*,*) b
      add = a+b
      sub = a­b
      mul = a*b
      divis = a/b
      write (*,*) 'a+b = ', add, ' a­b = ', sub, ' a times b = ', mul
      write (*,*) 'a by b = ', divis, ' square root of a = ', sqrt(a)
      write (*,*) 'square root of b = ', sqrt(b)
      stop
      end 
3. Save the file as simathg77.for 
4. Compile it: gfortran ­o simathgf simathg77.for 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simathgf 
6. Open the konsole window and run simathgf (i.e. type simathgf at the prompt). 
Pascal:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
program test1 (input,output);
var a,b,add,sub,mul,divis,sr1,sr2:real;
begin
writeln('a = ');
readln(a);
writeln('b = ');
readln(b);
add := a+b;
sub := a­b;
mul := a*b;
divis := a/b;
sr1 := sqrt(a);
sr2 := sqrt (b);
writeln('a + b =', add:4:2);
writeln('a ­ b =', sub:4:2);
writeln('a times b =', mul:4:4);
writeln('a by b =', divis:4:4);
writeln('suare root of a =', sr1:4:4);
writeln('suare root of b =', sr2:4:4);
end. 
3. Save the file as simathp.pas 
4. Compile it: fpc simathp.pas 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simathp 
6. Open the konsole window and run simathp (i.e. type simathp at the prompt). 
SmallEiffel:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
class SIMATHSE
creation
make
feature
a,b:real;
make is
do
io.put_string("enter value of first variable: ");
io.read_real;
a:=io.last_real;
io.put_string("enter second variable: ");
io.read_real;
b:=io.last_real;
io.put_string("a + b = ");
io.put_real(a+b);
io.put_string("%N")
io.put_string("a ­ b = ");
io.put_real(a­b);
io.put_string("%N")
io.put_string("a times b = ");
io.put_real(a*b);
io.put_string("%N")
io.put_string("a by b = ");
io.put_real(a/b);
io.put_string("%N")
end;
end 
3. Save the file as simathp.e 
4. Compile it: compile simathse.e ­o simathse 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simathse 
6. Open the konsole window and run simathse (i.e. type simathse at the prompt). 
Haskell:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
main = do
putStrLn "Please a number: "
name <­ getLine
let x = read name :: Double
putStrLn "Please enter another number: "
name1 <­ getLine
let y = read name1 :: Double
let z = x + y
let a = x * y
let b = x / y
let c = x ­ y
let d = sqrt x
let e = sqrt y
putStrLn ("first number = " ++ show x)
putStrLn ("second number = " ++ show y)
putStrLn ("addition of 2 numbers = " ++ show z)
putStrLn ("multiplication of 2 numbers = " ++ show a)
putStrLn ("first number by second number = " ++ show b)
putStrLn ("subtracting second number from first = " ++ show c)
putStrLn ("square root of first number = " ++ show d)
putStrLn ("suare root of second number = " ++ show e) 
3. Save the file as simathaskell.hs 
4. Compile it: ghc ­­ make ­o simathhs simathaskell.hs 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simathhs 
6. Open the konsole window and run simathhs (i.e. type simathhs at the prompt). 
ADA:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
with Gnat.Io; use Gnat.Io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
procedure simathada is
a, b, c, d ,e, f: float := 0.0;
begin
Put("Enter a numbere, a: ");
Get(a);
Put("Enter another numbere, b: ");
Get(b);
c := a + b;
Put("a + b = ");
Put(c);
New_Line;
d := a ­ b;
Put("a ­ b = ");
Put(d);
New_Line;
e := a * b;
Put("a times b = ");
Put(e);
New_Line;
f := a / b;
Put("a divided by b = ");
Put(f);
New_Line;
end simathada;
3. Save the file as simpmthada.adb 
4. Compile it: gcc ­c simathada.adb; then: gnatbl ­o simpmthada simathada.ali 
5. Copy the executable (compiled) file to the executables folder (directory), eg. simpmthada 
6. Open the konsole window and run simpmthada (i.e. type simpmthada at the prompt). 
Java:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
import java.io.*;
class SiMathJava {
public static void main(String[] args) throws Exception{
String n, n1;
Double m1, m, m2, m3, m4, m5, m6, m7;
System.out.print("Enter a number: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = br.readLine();
m = Double.valueOf(n);
System.out.print("Enter another number: "); 
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
n1 = br.readLine();
m1 = Double.valueOf(n1);
m2 = m + m1;
System.out.print("first number + second one = ");
String s = String.valueOf(m2);
System.out.println(s);
m3 = m ­ m1;
System.out.print("first number ­ second one = ");
String s1 = String.valueOf(m3);
System.out.println(s1);
m4 = m * m1;
System.out.print("first number times second one = ");
String s2 = String.valueOf(m4);
System.out.println(s2);
m5 = m / m1;
System.out.print("first number by second one = ");
String s3 = String.valueOf(m5);
System.out.println(s3);
m6 = Math.sqrt(m);
System.out.print("square root of first number = ");
String s4 = String.valueOf(m6);
System.out.println(s4);
m7 = Math.sqrt(m1);
System.out.print("square root of second number = ");
String s5 = String.valueOf(m7);
System.out.println(s5);
}
}
3. Save the file as simathjava.java 
4. Compile it per the above table 
5. Run the file, while you are in the directory where simathjava.java and SiMathJava.class are 
residing, by typing java SiMathJava 
gtkmm:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following line:
#include <gtkmm.h>
#include <gtkmm/main.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
char filename[20];
gchar buf[32];
double x, y;
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();

protected:
//Signal handlers:
virtual void on_button_add();
virtual void on_button_close();
virtual void on_button_subtract();
virtual void on_button_multiply();
virtual void on_button_divide();
//Child widgets:
Gtk::Frame m_Frame_Accelerated;
Gtk::HBox m_HBox_Accelerated, m_HBox_Buttons;
Gtk::VBox m_VBox_Main, m_VBox, m_VBox_Accelerated, m_VBox_Value;
Gtk::Label m_Label_Value1, m_Label_Value, m_Label_ShowValue;
Gtk::Adjustment m_adjustment_value, m_adjustment_value1;
Gtk::SpinButton m_SpinButton_Value, m_SpinButton_Value1;
Gtk::Button m_Button_Add, m_Button_Close, m_Button_Subtract,
m_Button_Multiply, m_Button_Divide;
};

int main(int argc, char *argv[])


{
Gtk::Main kit(argc, argv);

ExampleWindow window;
//Shows the window and returns when it is closed.
Gtk::Main::run(window);

return 0;
}

ExampleWindow::ExampleWindow()
:
m_Frame_Accelerated("Accelerated"),
m_VBox_Main(false, 5),
m_Label_Value("Value of x: "),
m_Label_Value1("Value of y: "),
m_adjustment_value(0.0, -10000.0, 10000.0, 0.1, 100.0, 0.0),
m_adjustment_value1(0.0, -10000.0, 10000.0, 0.1, 100.0, 0.0),
m_SpinButton_Value(m_adjustment_value, 1.0, 2),
m_SpinButton_Value1(m_adjustment_value1, 1.0, 2),
m_Button_Add("addition: x + y"),
m_Button_Subtract("Subtraction: x - y"),
m_Button_Multiply("Multiplication: x times y"),
m_Button_Divide("Division: x / y"),
m_Button_Close("Close")
{
set_title("Math");

m_VBox_Main.set_border_width(10);
add(m_VBox_Main);

m_VBox.set_border_width(5);

//Accelerated:
m_VBox_Main.pack_start(m_Frame_Accelerated);

m_VBox_Accelerated.set_border_width(5);
m_Frame_Accelerated.add(m_VBox_Accelerated);

m_VBox_Accelerated.pack_start(m_HBox_Accelerated, Gtk::PACK_EXPAND_WIDGET,
5);

m_HBox_Accelerated.pack_start(m_VBox_Value, Gtk::PACK_EXPAND_WIDGET, 5);

m_Label_Value.set_alignment(Gtk::ALIGN_LEFT);
m_VBox_Value.pack_start(m_Label_Value);

m_Label_Value1.set_alignment(Gtk::ALIGN_LEFT);
m_VBox_Value.pack_start(m_Label_Value1);

m_SpinButton_Value.set_wrap();
m_SpinButton_Value.set_size_request(100, -1);
m_VBox_Value.pack_start(m_SpinButton_Value);

m_SpinButton_Value1.set_wrap();
m_SpinButton_Value1.set_size_request(100, -1);
m_VBox_Value.pack_start(m_SpinButton_Value1);

//Buttons:
m_VBox_Accelerated.pack_start (m_HBox_Buttons, Gtk::PACK_SHRINK, 5);

m_VBox_Accelerated.pack_start(m_Label_ShowValue);
m_Label_ShowValue.set_text("0");

m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_close) );
m_VBox_Main.pack_start(m_Button_Close, Gtk::PACK_SHRINK);

m_Button_Add.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_add) );
m_VBox_Main.pack_start(m_Button_Add, Gtk::PACK_SHRINK);

m_Button_Subtract.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_subtract) );
m_VBox_Main.pack_start(m_Button_Subtract, Gtk::PACK_SHRINK);
m_Button_Multiply.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_multiply) );
m_VBox_Main.pack_start(m_Button_Multiply, Gtk::PACK_SHRINK);

m_Button_Divide.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_divide) );
m_VBox_Main.pack_start(m_Button_Divide, Gtk::PACK_SHRINK);

show_all_children();
}

ExampleWindow::~ExampleWindow()
{std::cout << "bye." << std::endl;
}

void ExampleWindow::on_button_close()
{
hide();
std::cout<<"file name: ";
std::cin>>filename;
std::ofstream fout(filename);
fout<<"x, y, x + y: "<<x<<","<<y<<","<<x+y<<std::endl;
fout<<"x, y, x - y: "<<x<<","<<y<<","<<x-y<<std::endl;
fout<<"x, y, x times y: "<<x<<","<<y<<","<<x*y<<std::endl;
fout<<"x, y, x / y: "<<x<<","<<y<<","<<x/y<<std::endl;
fout.close();
std::cout<<"x + y, result: "<<x<<","<<y<<","<<x+y<<std::endl;
std::cout<<"x - y, result: "<<x<<","<<y<<","<<x-y<<std::endl;
std::cout<<"x times y, result: "<<x<<","<<y<<","<<x*y<<std::endl;
std::cout<<"x / y, result: "<<x<<","<<y<<","<<x/y<<std::endl;
}

void ExampleWindow::on_button_add()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()+m_SpinButton_Value1.get_value());
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

}
void ExampleWindow::on_button_subtract()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()-m_SpinButton_Value1.get_value());
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

void ExampleWindow::on_button_multiply()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()*m_SpinButton_Value1.get_value());
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

void ExampleWindow::on_button_divide()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()/m_SpinButton_Value1.get_value());
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

3. Compile it per the above table 
4. Copy the executable (compiled) file to the executables folder (directory), eg. gtkmmath 
5. Open the konsole window and run gtkmmath (i.e. type gtkmmath at the prompt). 
A simple program to calculate the areas of a square, a rectangle and a triangle:
C++: 
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
#include<iostream>
#include<fstream>
using namespace std;
int main() {
char mainsel;
float x, y, b1, b3;
int quit = 0;
while (quit==0) {
cout<<"enter:a for area of a square, b for area of a rectangle"<<endl;
cout<<"c for the area of a triangle and q to quit:"<<endl;
cin>>mainsel;
if (mainsel == 'a') {
cout<< "length (base) of the square: " ;
cin >> x ;
b3= x*x ;
cout << "the area of the square = "<< b3 << endl;
continue;
}
if (mainsel == 'b') {
cout<< "length (base) of the rectangle: " ;
cin >> x ;
cout<< "height of the rectangle: " ;
cin >> y;
b3= x * y;
cout << "area of rectangle = "<< b3 << endl;
continue;
}
if (mainsel == 'c') {
cout<< "base of the triangle: " ;
cin >> x ;
cout<< "height of the triangle: " ;
cin >> y;
b1 = (x*y/2);
cout << "area of triangle = "<< b1 << endl;
continue;
}
if (mainsel == 'q') {
quit = 1;
break;
}
}

3. Save the file as areascpp.cpp 
4. Compile it per the above table 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areascpp 
6. Open the konsole window and run areascpp (i.e. type areascpp at the prompt). 
Fortran 77:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
       program areas
      real a, b, area
      integer x
100      write (*,*) 'enter 1 for the area of a square, 2 a rectangle,'
      write (*,*) '3 a triangle and 4 to quit:'
      read (*,*) x
      IF(x .eq. 1)THEN
      write (*,*) 'Give legnht of square:'
      read (*,*) a
      area = a*a
      write (*,*) 'Area = ', area
      goto 100
      endif
      IF(x .eq. 2)THEN
      write (*,*) 'Give base of rectangle:'
      read (*,*) a
      write (*,*) 'Give height of rectangle:'
      read (*,*) b
      area = a*b
      write (*,*) 'Area = ', area
      goto 100
      endif
      IF(x .eq. 3)THEN
      write (*,*) 'Give legnht of square:'
      read (*,*) a
      write (*,*) 'Give height of rectangle:'
      read (*,*) b
      area = a*b/2
      write (*,*) 'Area = ', area
      goto 100
      endif
      end
3. Save the file as areas.for 
4. Compile it: g77 ­o areasg77 areas.for 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areasg77 
6. Open the konsole window and run areasg77 (i.e. type areasg77 at the prompt). 
gFortran:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
       program areas
      real a, b, area
      integer x
100      write (*,*) 'enter 1 for the area of a square, 2 a rectangle,'
      write (*,*) '3 a triangle and 4 to quit:'
      read (*,*) x
      IF(x .eq. 1)THEN
      write (*,*) 'Give legnht of square:'
      read (*,*) a
      area = a*a
      write (*,*) 'Area = ', area
      goto 100
      endif
      IF(x .eq. 2)THEN
      write (*,*) 'Give base of rectangle:'
      read (*,*) a
      write (*,*) 'Give height of rectangle:'
      read (*,*) b
      area = a*b
      write (*,*) 'Area = ', area
      goto 100
      endif
      IF(x .eq. 3)THEN
      write (*,*) 'Give legnht of square:'
      read (*,*) a
      write (*,*) 'Give height of rectangle:'
      read (*,*) b
      area = a*b/2
      write (*,*) 'Area = ', area
      goto 100
      endif
      end
3. Save the file as areas.for 
4. Compile it: gfortran ­o areasgfor areas.for 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areasgfor 
6. Open the konsole window and run areasgfor (i.e. type areasgfor at the prompt). 
Pascal:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
program areas (input,output);
var choice,q:char;
var el1,el3,result:real;
begin
while (choice<>'q') do
begin
writeln ('enter a for area of a square, b for rectangle');
writeln ('enter c for triangle & enter q to exit');
readln(choice);
if choice = 'a' then
begin
writeln('base length of square');
readln(el1);
writeln('area = ');
result:=(el1*el1);
writeln(result:4:2);
end;
if choice = 'b' then
begin
writeln('base length of rectangle = ');
readln(el1);
writeln('height of rectangle = ');
readln(el3);
writeln('area of rectangle = ');
result:=(el1*el3);
writeln(result:4:2);
end;
if choice = 'c' then
begin
writeln('base length of triangle = ');
readln(el1);
writeln('height of triangle = ');
readln(el3);
writeln('area of triangle = ');
result:=(el1*el3/2);
writeln(result:4:2);
end
end
end.
3. Save the file as areasp.pas 
4. Compile it: fpc areasp.pas 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areasp 
6. Open the konsole window and run areasp (i.e. type areasp at the prompt). 
SmallEiffel:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
class AREASSE
creation
make
feature
noe:integer;
el1,el3:real;
first:character;
make is
do
from
until first ='q'
loop
menu
end ­­loop
end ­­make
menu is
do
io.put_string("%Nenter a for the area of a square, b for a rectangle,%Nc for a triangle and q to 
quit: %N");
io.read_character;
first:=io.last_character;
if first = 'a' then do_selectiona
end ­­first='a'
if first = 'b' then do_selectionb
end ­­first='b'
if first = 'c' then do_selectionc
end ­­first='c'
end ­­menu
do_selectiona is
do
io.put_string("enter the base length of the square: ");
io.read_real;
el1:=io.last_real;
io.put_string("the result: ");
io.put_real((el1*el1));
io.put_string("%N")
end ­­do_selectiona
do_selectionb is
do
io.put_string("enter the base length of the rectangle: ");
io.read_real;
el1:=io.last_real;
io.put_string("enter the height: ");
io.read_real;
el3:=io.last_real;
io.put_string("the result: ");
io.put_real(el1*el3);
io.put_string("%N")
end ­­do_selectionb
do_selectionc is
do
io.put_string("enter the base length of the triangle: ");
io.read_real;
el1:=io.last_real;
io.put_string("enter the height of the triangle: ");
io.read_real;
el3:=io.last_real;
io.put_string("the area (result): ");
io.put_real((el1*el3)/2);
io.put_string("%N")
end ­­do_selectionc
end ­­AREASSE 
3. Save the file as simathp.e 
4. Compile it: compile areasse.e ­o areasse 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areasse 
6. Open the konsole window and run areasse (i.e. type areasse at the prompt). 
Haskell:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
main = do
putStrLn "Please a number: "
choice <­ getLine
let ch = read choice::Int
if (ch == 1)
then do respond
else 
if (ch == 2)
then do respond1 
else 
if (ch == 3)
then do respond2 
else putStrLn"end"
respond = do
putStrLn "square"
putStrLn "base length of square: "
name <­ getLine
let x = read name :: Double
let a = x * x
putStrLn ("area of square = " ++ show a)
respond1 = do

putStrLn "rectangle"
putStrLn "base length of rectangle: "
name <­ getLine
putStrLn "height of rectangle: "
name1 <­ getLine
let y = read name1 :: Double
let x = read name :: Double
let a = x * y
putStrLn ("area of rectangle = " ++ show a)
respond2 = do
putStrLn "triangle"
putStrLn "base length of triangle: "
name <­ getLine
putStrLn "height of triangle: "
name1 <­ getLine
let y = read name1 :: Double
let x = read name :: Double
let a = x * y/2
putStrLn ("area of triangle = " ++ show a)
3. Save the file as areashaskell.hs 
4. Compile it: ghc ­­make ­o areashaskell areashaskell.hs 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areashaskell 
6. Open the konsole window and run areashaskell (i.e. type areashaskell at the prompt). 
ADA:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
with Gnat.Io; use Gnat.Io;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
procedure areasada is
a: integer;
b, c, d, e, f: float; 
begin
Put("Enter a numbere: 1 for area of a square, 2 area of rectangle, 3 area of triangle, 4 to quit ");
Get(a);
if A = 1 then
Put("Enter length of square: ");
Get(b);
e := b * b;
Put("area of square = ");
Put(e);
New_Line;
elsif a = 2 then
Put("Enter length of rectangle base: ");
Get(c);
Put("Enter height of rectangle base: ");
Get(d);
e := c * d;
Put("area of rectangle = ");
Put(e);
New_Line;
elsif a = 3 then
Put("Enter length of triangle base: ");
Get(d);
Put("Enter height of triangle base: ");
Get(f);
e := f * d/ 2.0;
Put("area of triangle = ");
Put(e);
New_Line;
end if;
end areasada;
3. Save the file as simpmthada.adb 
4. Compile it: gcc ­c areasada.adb; then: gnatbl ­o areasada areasada.ali 
5. Copy the executable (compiled) file to the executables folder (directory), eg. areasada 
6. Open the konsole window and run areasada (i.e. type areasada at the prompt). 
Java:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following lines:
import java.io.*;
import java.io.*;
class AreasJava {
public static void main(String[] args) throws Exception {
Integer n1, q;
String n, m1c, m2c;
Double m1,m2, m3;
q = 0;
while (q == 0) {
System.out.print("Enter 1 for area of square, 2 for rectangle or 3 for triangle and 4 to quit: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = br.readLine();
n1 = Integer.valueOf(n);
if (n1 == 1) {
System.out.print("square: ");
} else if (n1 == 2) {
System.out.print("rectangle: ");
System.out.print("Enter base length: ");
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
m1c = br2.readLine();
m1 = Double.valueOf(m1c);
System.out.print("Enter height: "); 
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
m2c = br1.readLine();
m2 = Double.valueOf(m2c);
m3 = m2 * m1;
System.out.print("area of rectangle = ");
String s = String.valueOf(m3);
System.out.println(s);
} else if (n1 == 3) {
System.out.print("triangle: ");
} else if (n1 == 4) { q =1;
}
}
}
}
3. Save the file as areasjava.java 
4. Compile it per the above table: javac areasjava.java 
5. Run the file, while you are in the directory where areasjava.java and SiMathJava.class are 
residing, by typing java AreasJava 
gtkmm:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following line:
#include <gtkmm.h>
#include <gtkmm/main.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
char filename[20];
gchar buf[32];
double x, y;
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();

protected:
//Signal handlers:
virtual void on_button_close();
virtual void on_button_triangle();
virtual void on_button_square();
virtual void on_button_rectangle();
//Child widgets:
Gtk::Frame m_Frame_Accelerated;
Gtk::HBox m_HBox_Accelerated, m_HBox_Buttons;
Gtk::VBox m_VBox_Main, m_VBox, m_VBox_Accelerated, m_VBox_Value;
Gtk::Label m_Label_Value1, m_Label_Value, m_Label_ShowValue;
Gtk::Adjustment m_adjustment_value, m_adjustment_value1;
Gtk::SpinButton m_SpinButton_Value, m_SpinButton_Value1;
Gtk::Button m_Button_Triangle, m_Button_Close, m_Button_Square,
m_Button_Rectangle;
};

int main(int argc, char *argv[])


{
Gtk::Main kit(argc, argv);

ExampleWindow window;
//Shows the window and returns when it is closed.
Gtk::Main::run(window);

return 0;
}

ExampleWindow::ExampleWindow()
:
m_Frame_Accelerated("Accelerated"),
m_VBox_Main(false, 5),
m_Label_Value("Value of x (length or height): "),
m_Label_Value1("Value of y (width or base): "),
m_adjustment_value(0.0, -10000.0, 10000.0, 0.1, 100.0, 0.0),
m_adjustment_value1(0.0, -10000.0, 10000.0, 0.1, 100.0, 0.0),
m_SpinButton_Value(m_adjustment_value, 1.0, 4),
m_SpinButton_Value1(m_adjustment_value1, 1.0, 4),
m_Button_Triangle("area = x*y/2"),
m_Button_Square("area = x * y"),
m_Button_Rectangle("area = x * y"),
m_Button_Close("Close")
{
set_title("Math");

m_VBox_Main.set_border_width(10);
add(m_VBox_Main);

m_VBox.set_border_width(5);

//Accelerated:
m_VBox_Main.pack_start(m_Frame_Accelerated);

m_VBox_Accelerated.set_border_width(5);
m_Frame_Accelerated.add(m_VBox_Accelerated);

m_VBox_Accelerated.pack_start(m_HBox_Accelerated, Gtk::PACK_EXPAND_WIDGET,
5);

m_HBox_Accelerated.pack_start(m_VBox_Value, Gtk::PACK_EXPAND_WIDGET, 5);

m_Label_Value.set_alignment(Gtk::ALIGN_LEFT);
m_VBox_Value.pack_start(m_Label_Value);

m_Label_Value1.set_alignment(Gtk::ALIGN_LEFT);
m_VBox_Value.pack_start(m_Label_Value1);

m_SpinButton_Value.set_wrap();
m_SpinButton_Value.set_size_request(100, -1);
m_VBox_Value.pack_start(m_SpinButton_Value);

m_SpinButton_Value1.set_wrap();
m_SpinButton_Value1.set_size_request(100, -1);
m_VBox_Value.pack_start(m_SpinButton_Value1);

//Buttons:
m_VBox_Accelerated.pack_start (m_HBox_Buttons, Gtk::PACK_SHRINK, 5);

m_VBox_Accelerated.pack_start(m_Label_ShowValue);
m_Label_ShowValue.set_text("0");

m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_close) );
m_VBox_Main.pack_start(m_Button_Close, Gtk::PACK_SHRINK);

m_Button_Triangle.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_triangle) );
m_VBox_Main.pack_start(m_Button_Triangle, Gtk::PACK_SHRINK);

m_Button_Square.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_square) );
m_VBox_Main.pack_start(m_Button_Square, Gtk::PACK_SHRINK);

m_Button_Rectangle.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_rectangle) );
m_VBox_Main.pack_start(m_Button_Rectangle, Gtk::PACK_SHRINK);

show_all_children();
}

ExampleWindow::~ExampleWindow()
{std::cout << "bye." << std::endl;
}

void ExampleWindow::on_button_close()
{
hide();
std::cout<<"file name: ";
std::cin>>filename;
std::ofstream fout(filename);
fout<<"area of triangle = "<<x*y/2<<std::endl;
fout<<"area of square = "<<x*y<<std::endl;
fout<<"area of rectangle = "<<x*y<<std::endl;
fout.close();
std::cout<<"x, y and area (x*y / 2): "<<x<<","<<y<<","<<x*y/2<<std::endl;
std::cout<<"x, y and area (x*y): "<<x<<","<<y<<","<<x*y<<std::endl;
std::cout<<"x, y and area (x*y): "<<x<<","<<y<<","<<x*y<<std::endl;
}

void ExampleWindow::on_button_triangle()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()*m_SpinButton_Value1.get_value()/2
);
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

}
void ExampleWindow::on_button_square()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()*m_SpinButton_Value1.get_value());
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

void ExampleWindow::on_button_rectangle()
{
sprintf (buf, "%0.*f", m_SpinButton_Value.get_digits(),
m_SpinButton_Value.get_value()*m_SpinButton_Value1.get_value());
x = m_SpinButton_Value.get_value();
y = m_SpinButton_Value1.get_value();

m_Label_ShowValue.set_text(buf);

3. Compile it per the above table 
4. Copy the executable (compiled) file to the executables folder (directory), eg. gtkmmath 
5. Open the konsole window and run gtkmmath (i.e. type gtkmmath at the prompt). 
Graphics libraries examples:
Cairo:
Example 1: Saying hello:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. #include

int
main (int argc, char *argv[])
{
cairo_surface_t *surface;
cairo_t *cr;

surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);


cr = cairo_create (surface);

cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL,


CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 16.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, "Hello, world from Cairo");
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "hello.png");
cairo_surface_destroy (surface);

return 0;
}

3. Compile it per the above table 
4. Run the executable (compiled) file, eg. ./hellocairo 
5. You will get hello.png graph file having the following picture (graphics): Hello, world from 
Cairo. 
Example 2: Drawing (stroke) lines & rectangles (fill) including text:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following:
#include

int
main (int argc, char *argv[])
{
cairo_surface_t *surface;
cairo_t *cr;

surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100,


100);
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_move_to (cr, 0, 0);
cairo_line_to (cr, 100, 100);
cairo_move_to (cr, 100, 0);
cairo_line_to (cr, 0, 100);
cairo_set_line_width (cr, 2);
cairo_stroke (cr);

cairo_rectangle (cr, 0, 0, 50, 50);


cairo_set_source_rgba (cr, 1, 0, 0, 0.80);
cairo_fill (cr);

cairo_rectangle (cr, 0, 50, 50, 50);


cairo_set_source_rgba (cr, 0, 1, 0, 0.60);
cairo_fill (cr);

cairo_rectangle (cr, 50, 0, 50, 50);


cairo_set_source_rgba (cr, 0, 0, 1, 0.40);
cairo_fill (cr);

cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL,


CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size (cr, 16.0);
cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, "HAK HAK");
cairo_destroy (cr);
cairo_destroy (cr);
cairo_surface_write_to_png (surface, "test1.png");

cairo_surface_destroy (surface);
return 0;
}

3. Compile it per the above table 
4. Run the executable (compiled) file, eg. ./test1 
5. You will get hello.png graph file having the following picture (graphics): 

OSG:
Example 1: Saying hello:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following: 
#include <osg/Geode>
#include <osg/Geometry>
#include <osgText/Font>
#include <osgText/Text>

osg::ref_ptr
createSceneGraph()
{
// Create the root (and only) node.
osg::ref_ptr geode = new osg::Geode;

osg::ref_ptr font = osgText::readFontFile( "fonts/arial.ttf" );

osg::Vec4 white( 1.f, 1.f, 1.f, 1.f );

{
osg::ref_ptr text = new osgText::Text;
text->setFont( font.get() );
text->setFontResolution( 128, 128 );
text->setColor( white );
text->setCharacterSize( .4f );
text->setPosition( osg::Vec3( 0.f, 0.f, -1.04f ) );
text->setAxisAlignment( osgText::Text::XZ_PLANE );
text->setAlignment( osgText::Text::CENTER_TOP );
text->setText( "Hello from OSG World" );
geode->addDrawable( text.get() );
}

return geode.get();
}
and save the file as HelloSG.cpp.
Type the following in a new file and save as HelloMain.cpp:
#include <osg/ref_ptr>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <iostream>

using std::endl;

osg::ref_ptr createSceneGraph();

int
main( int argc, char** argv )
{
osg::ref_ptr root = createSceneGraph();
if (!root.valid())
{
osg::notify(osg::FATAL) << "Failed in createSceneGraph()." << endl;
return 1;
}

std::string out( "hello.osg" );


if ( !(osgDB::writeNodeFile( *(root.get()), out )) )
{
osg::notify(osg::FATAL) << "Failed in osgDB::writeNodeFile()." <<
endl;
return 1;
}

osg::notify(osg::ALWAYS) << "Successfully wrote \"" << out << "\".


Execute \"osgviewer " << out << "\" to view." << endl;
}

3. Compile per above table and run hello i.e. ./hello, and follow the prompt
that appears, Execute "osgviewer hello.osg" to view.
4. To add a viewer, open a text editor, add the following lines and save the
file as Viewerhello.cpp:

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>

int
main( int, char ** )
{
// Create a Viewer.
osgViewer::Viewer viewer;

// Load a model and add it to the Viewer.


viewer.setSceneData( osgDB::readNodeFile( "hello.osg" ) );
if (!viewer.getSceneData())
{
osg::notify( osg::FATAL ) << "Unable to load data file. Exiting." <<
std::endl;
return 1;
}

return viewer.run();
}

Compile using the following statement at the console prompt:


g++ -o viewerh Viewerhello.cpp -I/usr/include -losg -losgDB -losgUtil
-losgViewer -losgText.
Then run the output file with ./viewerh.

GLUT:
Example 1: Saying hello:
1. Open a new document in any text editor (kwrite, kedit or kate). 
2. Type the following:
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <GL/glut.h>

void
bitmap_output(int x, int y, char *string, void *font)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}

void
stroke_output(GLfloat x, GLfloat y, char *format,...)
{
va_list args;
char buffer[200], *p;

va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(0.005, 0.005, 0.005);
for (p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}

void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
bitmap_output(40, 35, "Hello from glut.",
GLUT_BITMAP_TIMES_ROMAN_24);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(40.0, 1.0, 0.1, 20.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(0.0, 0.0, 4.0, /* eye is at (0,0,30) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in postivie Y direction */
glPushMatrix();
glTranslatef(0, 0, -4);
glRotatef(50, 0, 1, 0);
stroke_output(-2.5, 1.1, " one more time");
stroke_output(-2.5, 0, " hello from");
stroke_output(-2.5, -1.1, "GLUT.");
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();
}

void
reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
glMatrixMode(GL_MODELVIEW);
}

int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(465, 250);
glutCreateWindow("Hello from GLUT example");
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0, 0, 0);
glLineWidth(3.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

3. Compile it per the above table 
4. Run the executable (compiled) file, eg. ./helloglut, to get the hello window. 
More programs and examples (related to electrical power systems): 
•  C++ examples.  
•  Fortran 77 programs.  
•  gFortran examples.  
•  Pascal examples.  
•  SmallEiffel programs.  
•  Haskell examples.  
•  Ada examples.  
•  Java programs.  
•  gtkmm programs.  

Você também pode gostar