Books
in black and white
Main menu
Home About us Share a book
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Linux for dummies - Klimas M.

Klimas M. Linux for dummies - Wiley publishing , 2002. - 169 p.
Download (direct link): linuxfordummies2002.pdf
Previous << 1 .. .. 104105 106 107 108 109 110 < 111 > 112 113 114 115 116 117

After saving the file, I make it executable: chmod a+x try_python.py
after which I can run it by typing:
./try_python.py
Python is an excellent, and very modern programming language. Give it a try, particularly if you like object
oriented programming. There are numerous libaries/extensions available on the Internet. For example, scientific
python (http://starship.python.net/crew/hinsen/scientific.html) and numeric python (http://sourceforge.net/projects/numpy) are popular libraries used in engineering.
Here is a slightly longer, but still (hopefully) self-explanatory python code. A quick note: python flow control depends on the code indentation--it makes it very natural looking and forcing legibility, but takes an hour to get used to.
#!/usr/bin/env python
# All comments start with a the character "#"
# This program converts human years to dog years
# get the original age
age = input("Enter your age (in human years): ") print # print a blank line
Part l: Learning with Linux
151
Linux Newbie Guide by Stan, Peter and Marie Klimas 01/08/2003
# check if the age is valid using a simple if statement if age < 0:
print "A negative age is not possible." elif age < 3 or age > 110:
print "Frankly, I don't believe you."
else:
print "That's the same as a", age/7, "year old dog."
tcl
(Pronounced "tickle".) Popular scripting language.
A simple tcl program?
#!/usr/bin/tclsh
puts stdout {Hello World!}
wish
(type in X-terminal ) A front-end to Tk, an X-windows extension of tcl. Often used for building front-ends of a program.
How do I write a simple GUI program (using Tk)?
Tk is a GUI extension of the easy yet powerful tcl programming language. For example, I may use pico to create a text file that will contain a simple tk program:
pico try_tk
and type in a simple example of tk code to see if it works:
#!/usr/bin/wish
button .my_button -text "Hello World" -command exit pack .my_button
The first line (starting with the "#!" pound-bang) tells the shell what utility to use to execute my text file. The next two lines are an example of a simple tk program. First, I created a button called "my_button" and placed it at the root of my class hierarchy (the dot in front of "my_button"). To the button, I tied the text "Hello World" and a command that exits the program (when the button is pressed). Last line makes my program's window adjust its size to just big enough to contain my button.
After saving the file, I make it executable: chmod a+x try_tk
after which I can run it by typing (in the X-terminal, because it requires X-windows to run):
./try_tk
Tk is very popular for building GUI front ends.
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"
Part 7: Learning with Linux
152
Linux Newbie Guide by Stan, Peter and Marie Klimas 01/08/2003
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
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
g++ filename.C
GNU C++ compiler. The capital "C" is often used for C++ sources. If you need an "integrated development envrionment" (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;
Part 7: Learning with Linux
Previous << 1 .. .. 104105 106 107 108 109 110 < 111 > 112 113 114 115 116 117