Wednesday, June 28, 2006


Hello World in Perl

Hello World in Perl

Friday, June 16, 2006


File Checking

File Checking

Thursday, December 15, 2005


member function implement

member function implement:

return type (which can be omitted when it is int)+ class name + :: member function name ( parameters, if any) {....}


class SimpleCircle
a) const SimpleCircle& operator ++(); // declare of a member function
b) const SimpleCircle operator ++ (int); //postfix

const SimpleCircle & SimpleCircle::operator ++ ( ) {...}

Sunday, November 20, 2005


Bit Counter

int n;
n&077, here 077 = 00..0 111 111, so "bitwise and" sets zero all but the low-order 6 bits of n.

int bitcounter (unsigned x)
int b;
for (b=0; x!=0; x >>=1) //x>>1, but after first loop x = x, then x=x>>1 after that.
if (x&01)

int bitcounter (unsigned x)
int b=0;
while (x !=0)
x &= (x-1) //delete the rightmost 1-bit in x;
return b;

Wednesday, August 24, 2005


VI Quick Introduction

Credit: Norm Matloff

1. Overview

A text editor is a program that can be used to create and modify text files. One of the most popular editors on Unix systems (it is also available on Windows and many other platforms) is vi.

2. 5-Minute Introduction

As a brief introduction to vi, go through the following:
First, type vi x
at the Unix prompt. Assuming you did not already have a file named x, this command will create one. (If you have tried this example before, x will already exist, and vi will work on it. If you wish to start the example from scratch, simply remove x first.)

The file will of course initially be empty. To put something in it, type the letter 'i' (it stands for "nsert-text mode''), and type the following (including hitting the Enter key at the end of each of the three lines):

The quick
fox will return.

Then hit the Escape key, to end insert-text-mode.

This mode-oriented aspect of the vi editor differs from many other editors in this respect. With modeless editors such as joe and emacs, for instance, to insert text at the cursor position,
one simply starts typing, and to stop inserting, one just stops typing! However, that means that in order to perform most commands, one needs to use the Control key (in order to distinguish a command from text to be inserted). This has given rise to jokes that heavy users of modeless editors develop gnarled fingers.

Now save the file and exit vi, by typing 'ZZ' (note the capitals).

Again, the key to learning vi is to keep in mind always the difference between insert-text mode and command mode. In the latter mode, as its name implies, one issues commands, such as the ZZ above, which we issued to save the file and exit vi. The characters you type will appear on the screen if you are in insert-text mode,
whereas they will not appear on the screen while you are in command mode. By far the most frequent problem new vi users have is that they forget they are in insert-text mode, and so their commands are not obeyed.

For example, suppose a new user wants to type ZZ, to save the file and exit vi, but he has forgotten to hit the Escape key to terminate insert-text mode. Then the ZZ will appear on the screen, and will become part of the text of the file-and the ZZ command will not
be obeyed.

You now have a file named x. You can check its contents by typing (at the Unix shell prompt)
more x
which will yield

The quick
fox will return.

just as expected.

Now let's see how we can use vi again to modify that file.
Type vi x
again, and make the following changes.

First, suppose we wish to say the fox will not return: We need to first move the cursor to the word "return''. To do this, type '/re' and hit the Enter key, which instructs vi to move the cursor to the first instance of `re' relative to the current cursor position. (Note that typing only '/r' would have moved the cursor to the first instance of `r', which would be the `r' in `brown', not what we want.)

Now use the 'i' command again: Hit 'i', then type 'not ' (note the space), and then hit Escape.

Next, let's delete the word 'brown'. Type '/b' to move the cursor there, and then hit `x' five times, to delete each of the five letters in 'brown'. (This will still leave us with a blank line. If we did not want this, we could have used the 'dd' command, which would have deleted the entire line.)
Now type 'ZZ' to save the file and exit vi. Use 'more' again to convince yourself that you did indeed modify the file.

3. Going Further: Other Frequently-Used Commands

You now know how to use vi to insert text, move the cursor to text, and delete text. Technically, the bare-bones set of commands introduced above is sufficient for any use of vi. However, if you limit yourself to these few commands, you will be doing a large amount of unnecessary, tiresome typing.

So, you should also learn at least some of these other frequently-used vi commands:

h move cursor one character to left
j move cursor one line down
k move cursor one line up
l move cursor one character to right
w move cursor one word to right
b move cursor one word to left
0 move cursor to beginning of line
$ move cursor to end of line
nG move cursor to line n
control-f scroll forward one screen
control-b scroll backward one screen

i insert to left of current cursor position (end with ESC)
a append to right of current cursor position (end with ESC)
dw delete current word (end with ESC)
cw change current word (end with ESC)
r change current character
~ change case (upper-, lower-) of current character

dd delete current line
D delete portion of current line to right of the cursor
x delete current character
ma mark currrent position
d'a delete everything from the marked position to here
'a go back to the marked position
p dump out at current place your last deletion ("paste'')

u undo the last command
. repeat the last command

J combine (``join'') next line with this one

:w write file to disk, stay in vi
:q! quit VI, do not write file to disk,
ZZ write file to disk, quit vi

:r filename read in a copy of the specified file to the current buffer

/string search forward for string (end with Enter)
?string search backward for string (end with Enter)
n repeat the last search (``next search'')

:s/s1/s2 replace ("substitute'') (the first) s1 in this line by s2
:lr/s/s1/s2/g replace all instances of s1 in the line range lr by s2 (lr is of form 'a,b', where a and b are either explicit line numbers, or . (current line) or $ (last line)
:map k s map the key k to a string of vi commands s (see below)
:abb s1 s2 expand the string s1 in append/insert mode to a string s2 (see below)
% go to the "mate," if one exists, of this parenthesis or brace or bracket (very useful for programmers!)

All of the ':' commands end with your hitting the Enter key. (By the way, these are called "ex'' commands, after the name of the simpler editor from which vi is descended.)

The 'a' command, which puts text to the right of the cursor, does put you in insert-text mode, just like the 'i' command does.

By the way, if you need to insert a control character while in append/insert mode, hit control-v first. For example, to insert control-g into the file being edited, type control-v then control-g.

One of vi's advantages is easy cursor movement. Since the keys h,j,k,l are adjacent and easily accessible with the fingers of your right hand, you can quickly reach them to move the cursor, instead of fumbling around for the arrow keys as with many other editors (though they can be used in vi too). You will find that this use of h,j,k,l become second nature to you very quickly, very much increasing your speed, efficiency and enjoyment of text editing.

Many of the commands can be prefixed by a number. For example, 3dd means to delete (consecutive) three lines, starting with the current one. As an another example, 4cw will delete the next four words.

The p command can be used for "cut-and-paste'' and copy operations. For example, to move three lines from place A to place B:

1. Move the cursor to A.

2. Type '3dd'.

3. Move the cursor to B.

4. Type 'p'.

The same steps can be used to copy text, except that p must be used twice, the first time being immediately after Step 2 (to put back the text just deleted).

Note that you can do operations like cut-and-paste, cursor movement, and so on, much more easily using a mouse. This requires a GUI version of vi, which we will discuss later in this document.

4. More

Saturday, June 25, 2005


UNIX Library Functions

The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others are very specialised in their application.
Wise programmers will check whether a library function is available to perform a task before writing their own version. This will reduce program development time. The library functions have been tested, so they are more likely to be correct than any function which the programmer might write. This will save time when debugging the program.

Finding Information about Library Functions

The UNIX manual has an entry for all available functions. Function documentation is stored in section 3 of the manual, and there are many other useful system calls in section 2. If you already know the name of the function you want, you can read the page by typing (to find about strcat).
man 3 strcatIf you don't know the name of the function, a full list is included in the introductory page for section 3 of the manual. To read this, type
man 3 intro
There are approximately 700 functions described here. This number tends to increase with each upgrade of the system.
On any manual page, the SYNOPSIS section will include information on the use of the function. For example
char *ctime(time_t *clock)
This means that you must have
#include in your file before you call ctime. And that function ctime takes a pointer to type time_t as an argument, and returns a string (char *). time_t will probably be defined in the same manual page.
The DESCRIPTION section will then give a short description of what the function does. For example
ctime() converts a long integer, pointed to by clock, to a
26-character string of the form produced by asctime().
Further related reading is suggested in the SEE ALSO section.

Use of Library Functions

To use a function, ensure that you have made the required #includes in your C file. Then the function can be called as though you had defined it yourself.
It is important to ensure that your arguments have the expected types, otherwise the function will probably produce strange results. lint is quite good at checking such things.
Some libraries require extra options before the compiler can support their use. For example, to compile a program including functions from the math.h library the command might be
cc mathprog.c -o mathprog -lm
The final -lm is an instruction to link the maths library with the program. The manual page for each function will usually inform you if any special compiler flags are required.

Some Useful Library Functions

The following functions may be useful to you. Each manual page typically describes several functions, so if you see something similar to what you want, try looking in that manual page.

abs -------- integer absolute value
ctime ------ convert date and time
fopen ------ open a stream
printf ------ formatted output
fputc ------ put character or word on a stream
getwd ----- get current working directory path name
strcat ----- string concatenation
ctype ------ character classification and conversion macros and functions
maktemp -- make a unique file name
puts ------- put a string on a stream
sleep ------ suspend execution for interval
stdio ------ standard buffered input/output pakage

To get a full summary type
man 3 intro

This page is powered by Blogger. Isn't yours?