Friday, May 13, 2011
Simple C++ Program
Before looking at how to write C++ programs consider the following simple example program.
The following points should be noted in the above program:
// Sample program // IEA September 1995 // Reads values for the length and width of a rectangle // and returns the perimeter and area of the rectangle. #includeDownload program.void main() { int length, width; int perimeter, area; // declarations cout << "Length = "; // prompt user cin >> length; // enter length cout << "Width = "; // prompt user cin >> width; // input width perimeter = 2*(length+width); // compute perimeter area = length*width; // compute area cout << endl << "Perimeter is " << perimeter; cout << endl << "Area is " << area << endl; // output results } // end of main program
The following points should be noted in the above program:
- Any text from the symbols
//
until the end of the line is ignored by the compiler. This facility allows the programmer to insert Comments in the program. Every program should at least have a comment indicating the programmer's name, when it was written and what the program actually does. Any program that is not very simple should also have further comments indicating the major steps carried out and explaining any particularly complex piece of programming. This is essential if the program has to be amended or corrected at a later date. - The line
#include
must start in column one. It causes the compiler to include the text of the named file (in this caseiostream.h
) in the program at this point. The fileiostream.h
is a system supplied file which has definitions in it which are required if the program is going to use stream input or output. All your programs will include this file. This statement is a compiler directive -- that is it gives information to the compiler but does not cause any executable code to be produced. - The actual program consists of the function
main
which commences at the linevoid main()
main
. Note that the opening brace ({
) marks the beginning of the body of the function, while the closing brace (}
) indicates the end of the body of the function. The wordvoid
indicates thatmain
does not return a value. Running the program consists of obeying the statements in the body of the functionmain
. - The body of the function
main
contains the actual code which is executed by the computer and is enclosed, as noted above, in braces{}
. - Every statement which instructs the computer to do something is terminated by a semi-colon. Symbols such as
main()
,{
}
etc. are not instructions to do something and hence are not followed by a semi-colon. - Sequences of characters enclosed in double quotes are literal strings. Thus instructions such as
cout << "Length = "
cout
. The special identifierendl
when sent to an output stream will cause a newline to be taken on output. - All variables that are used in a program must be declared and given a type. In this case all the variables are of type
int
, i.e. whole numbers. Thus the statementint length, width;
length
andwidth
are going to be used by the program. The compiler reserves space in memory for these variables. - Values can be given to variables by the assignment statement, e.g. the statement
area = length*width;
length
andwidth
and assigns the resulting value to the variablearea
. - Layout of the program is quite arbitrary, i.e. new lines, spaces etc. can be inserted wherever desired and will be ignored by the compiler. The prime aim of additional spaces, new lines, etc. is to make the program more readable. However superfluous spaces or new lines must not be inserted in words like
main
,cout
, in variable names or in strings (unless you actually want them printed).
Source : http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node42.html
Friday, July 23, 2010
Create Simple Calculator With Java
This is my first article on this blog. Because i have some interest on java, so with google i find some code about simple calculator, and i found it.
Here is some code to create simple calculator with Java, please click the picture for a large view.
This program implements a GUI calculator using java.awt The calculator takes the order of operations into account.
Code Snippet 1.
Calculator.java - variable assignment & constructor
Code Snippet 2.
Calculator.java - launch frame
Code Snippet 3.
Calculator.java - button action listeners
Code Snippet 4.
Calculator.java - button action listeners (cont...)
Code Snippet 5.
Calculator.java - "equal btn" listener & multiplication routine
For more code you can visit this site : www.monctoncomputerservice.com
Here is some code to create simple calculator with Java, please click the picture for a large view.
This program implements a GUI calculator using java.awt The calculator takes the order of operations into account.
Code Snippet 1.
Calculator.java - variable assignment & constructor
Code Snippet 2.
Calculator.java - launch frame
Code Snippet 3.
Calculator.java - button action listeners
Code Snippet 4.
Calculator.java - button action listeners (cont...)
Code Snippet 5.
Calculator.java - "equal btn" listener & multiplication routine
For more code you can visit this site : www.monctoncomputerservice.com
Subscribe to Posts [Atom]