GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, December 31, 2010

Reading File In Python

To read a text file, use:
fp = open("file.txt","r");
print fp.read()

For writing to a file, use:
fp = open("data.txt","w");
write("Hello World");
fp.close();
       
Share/Bookmark

Monday, December 27, 2010

C++ Template Function

Templeate function used for flexibility of the type. So you can do programming like in scripting language. See this code:

templace
myType getMin( myType a, myType b){   
   if(a
        return a;
   }else{
        return b;
   }
}

The you call it by:
int x = 2;
int y = 3;
int c = getMin(x,y);
cout << c << endl;
output>> 2

You can use this function for long data type:
long x = 456789;
long y = 456788;
int c = getMin(x,y);
cout << c << endl;
output>> 456788

More example:
template
Lol addTwo(Lol x){
    Lol r = x + 2;
    return r;
}

then call it by
cout << addTwo(2) << endl;
output>> 5
       
For using two kind of different data type, use
template
A addTwo(A a, B b){
     A r = a+b;
    return r;
}

then call it by:
int x = 4;
long y = 45;
cout << addTwo(x,y) << endl;
output>> 49
          
Share/Bookmark

Iterating Method In Java

For a given array, to iterate over the whole elements in the array, use:

String[] names = new String[]{"lady gaga","barack obama","larry page"};
for(String s: names){
    System.out.println(s);
}

output:
lady gaga
barack obama
larry page
     
Share/Bookmark

Saturday, December 25, 2010

Batch Basic

Batch file is a file that consift of DOS command. What you can do in command prompt, will also can be done in batch file. Type this code in hello.bat, and then execute it with
C:\hello.bat lady gaga

@ECHO OFF
ECHO hello, %1


To make a variable, type:
SET name=Lady Gaga
echo %name%
PAUSE


IF Statements:
@ECHO OFF
SET a=luna
SET b=luna

IF %a%==%b% GOTO luna

ECHO Sorry, Lady!

:luna
ECHO You may win, Luna!
         
Share/Bookmark

Thursday, December 23, 2010

Procedure In Pascal

Here's a code on how write a procedure in Pascal:

Without Parameters

Program UsingProc;

uses wincrt;

procedure hello;
var
    name : string;
begin
    name := 'lady gaga';
    writeln(name);
end;

begin
    hello;
end.

With Parameters:

Program UsingProcParam;

uses wincrt;

procedure hello(name : integer);
begin
    writeln(name);
end;

begin
    hello('lady gaga');
end.
          
Share/Bookmark

Looping In Pascal

Here's a code on how to loop over a number of times;

For Loop:

Program forLoop;

uses wincrt;

var
    i : integer;
    c : char;

begin
    
    for i := 0 to 100 do
    begin
        writeln(i,'. Holla World');
    end;

    writeln('==========================');
    for i := 100 downto 10 do
    begin
        writeln(i,'. Holla World Again');
    end;

    writeln('===========================');

    for c := 'a' to 'z' do
    begin
        writeln(c,'. characters');
    end;

end.


While Loop:

Program whileLoop; 

uses wincrt;

var
    i : integer;

begin

    i := 0;
    while i<100 do
    begin
        writeln(i,'. Less than 100');
        i := i+1;
    end;

end.






Repeat Until

Program untilLoop;

uses wincrt;

var 
    i : integer;

begin   
    i := 1;
    Repeat
        writeln(i,'. Holla World');
        writeln('This holla comes from turbo pascal');
        i := i + 1;
    until i = 100;

end.
         

Share/Bookmark

If Else In Pascal

Here's a code on how to write a decision programs.

Program Sleeping;

uses wincrt;

var
    sleep : boolean;

begin

    sleep := true;

    if (not sleep) then
    begin
        writeln('Lady gaga still sleep');
        writeln('So please keep silent');
    end
    else
    begin
        writeln('You can do what you want');
        writeln('And this is a free area');
    end;

end.
Share/Bookmark