中文网站
  Advanced Search
Read the latest Blogs from IT professionals in the field. Read and write community created documents. Need IT help? Ask our staff. Connect with your peers. Check our Tech Shop for posters, books and software tools. Home

7.9 Simple Programming under Linux(3)

ruby

ruby
A purely object-oriented scripting language. This language is a relative newcomer, but it is rapidly gaining popularity, and may well be the flavour of the future of programming.

To write a simple program in ruby, I open my favorite text editor and start a program with the following first line:
#!/usr/bin/ruby

Here is an example of a program that I wrote to help me understand the basics of the ruby language:
#!/usr/bin/ruby
#This is a comment
a = Array.new
print "Please enter a few words (type EXIT to stop):\n"
i = 0
while enterWord = STDIN.gets
enterWord.chop!
if enterWord == "EXIT"
break
end
a[i] = enterWord
i += 1
end
#sort the array
for i in 0...a.length-1 do
for j in i+1...a.length do
if a[j] < a[i]
tmp = a[i]
a[i] = a[j]
a[j] = tmp
end
end
end
#Output the results
print "You entered "  + a.length.to_s + " entries.\n\n"
for i in 0...a.length do
print "Entry " + (i+1).to_s + ": "+ a[i] + "\n"
end

I save my ruby script to file "myprogram". To execute it, I need to type on the command line:
./myprogram

C

gcc filename.c
GNU C compiler. Quite straight-forward if you know C. Extensive free manuals are available on the net.

How do I compile a simple C program?

Start your favourite text editor and type in your source code. For example, I may use pico:
pico hello.c
and type in the Kerningham and Richie (the creators of "c") intro example of a C program:
#include <stdio.h>
void main(void) {
printf("hello world\n");
}
I save the file and then invoke the GNU C compiler to compile the file "hello.c":
gcc hello.c
The gcc compiler produces an executable binary file "a.out", which I can run:
./a.out

C++

g++ filename.C
GNU C++ compiler. The capital "C" is often used for C++ sources.  If you need an "integrated development environment" (IDE), kdevelop is really something you would probably like to look at.

How do I compile a simple C++ program?

Just like in c, I open a text editor and write my program. For example, using pico, I write the following program:
//This is a comment (to the end of line, C++ style)
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
//define a function
double wheeldrop (double dGap, double dDiameter) {
double dDrop, dRadius, dNotDrop;

    dRadius = dDiameter * 0.5;
dDrop = dRadius - sqrt( (dRadius*dRadius)-(0.25*dGap*dGap) );

    return (dDrop);
}   //end of the function

//The function main is the entry point to the program
void main(void) {
double dGap, dDiameter, dDrop, dRadius, dNotDrop;  //variables

    for (;;) {                                         //infinite loop
cout << "Please enter gap between track segments and \n"
<< "diameter of train wheel in inches (-1 -1 to exit): ";
cin >> dGap >> dDiameter;

        if ((dGap == -1) && (dDiameter == -1))
break;
else if (dGap < dDiameter) {   //do calculations
dDrop = wheeldrop (dGap, dDiameter);
printf ("The wheel will drop %f inches.\n\n", dDrop);
}
else {
printf ("Error, your train is going to crash.\n Gap bigger then wheel!\n\n");
}
}
}

I save the source to the file "train.c", and then invoke the GNU C++ compiler to compile the file "train.c" to an executable called "traincalc":
g++ -o traincalc train.c
I can then run the executable by typing:
./traincalc

kdevelop
(type in X-terminal) Integrated development environment for K. It is really worth downloading (if it does not come with your distribution).

glade
(type in X-terminal) A graphical builder of user interfaces.
"Glade is an interface builder developed by Damon Chaplin. It allows graphical and interactive construction of Gnome/Gtk graphical user interfaces. From Glade, the generated interface can be saved in a xml file or directly exported to C code to be included in a C source tree. Glade also allows to define the name of the handlers - functions - to be attached to the various event of the interface. For example the function (name) to be called when a specific menu item is pressed." (From: http://linuxtoday.com/news_story.php3?ltsn=2000-07-16-013-04-PS-GN)

Related Terms:linux,Simple Programming,Programming,ruby,GNU C compiler ,GNU C,C,g++ filename.C,GNU C++ compiler ,GNU C++,C++ ,kdevelop,glade