Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Computer Graphics Code Examples

Tower of Hanoi - A Graphical Representation

/* Tower of Hanoi - A Graphical Representation */ // [ You can use more than 10 Disks too, just change the value of MAX ] // #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #define MAX 12 #define BegPos 105 #define AuxPos 305 #define EndPos 505 int width; typedef struct disc { char val1[MAX]; char top,pos; }; void push(disc *tt,int x); pop(disc *tt); void tower(int,disc *,disc *,disc *); void draw_stack(disc *beg,disc *,disc *); int main(void) { int gdriver = DETECT, gmode, errorcode; int i,x=2; disc beg,end,aux; printf(" TOWER OF HANOI "); printf("======================================================="); printf(" How Many Disks[1-10]:- "); scanf("%d",&x); initgraph(&gdriver, &gmode, "d:\TC\BGI"); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s ", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } width=50/x; beg.top=end.top=aux.top=0; beg.pos=1;end.pos=3;aux.pos=2; for(i=0;i<x;i++) push(&beg,(x-i)+1); draw_stack(&beg,&end,&aux); tower(x,&beg,&end,&aux); closegraph(); return 0; } void tower(int n,disc *beg,disc *aux,disc *end) { if(n>0) /* { push(end,pop(beg)); draw_stack(beg,end,aux); } else*/ { tower(n-1,beg,end,aux); push(end,pop(beg)); draw_stack(beg,end,aux); tower(n-1,aux,beg,end); } // } void push(disc *tt,int x) { tt->val1[tt->top]=x; tt->top++; } pop(disc *tt) { int a; tt->top--; a=tt->val1[tt->top]; tt->val1[tt->top]=0; return a; } void draw_stack(disc *beg,disc *end,disc *aux) { int ypos=295,i,height=10,xpos; int ver=0; cleardevice(); setfillstyle(1,2); bar(20,300,580,310); bar(100,100,110,300); bar(300,100,310,300); bar(500,100,510,300); rectangle(20,300,580,310); rectangle(100,100,110,300); rectangle(300,100,310,300); rectangle(500,100,510,300); /* END TOWER*/ ypos=295; if(end->pos==1) xpos=BegPos; else if(end->pos==2) xpos=AuxPos; else if(end->pos==3) xpos=EndPos; for(i=0;i<end->top;i++) { setfillstyle(end->val1[i],end->val1[i]); bar(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height); rectangle(xpos-(end->val1[i]*width),ypos,xpos+(end->val1[i]*width),ypos-height); ypos-=(height+2); } ver=end->pos; /* BEG TOWER*/ if(beg->pos==1) xpos=BegPos; else if(beg->pos==2) xpos=AuxPos; else if(beg->pos==3) xpos=EndPos; ypos=295; for(i=0;i<beg->top;i++) { setfillstyle(beg->val1[i],beg->val1[i]); bar(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height); rectangle(xpos-(beg->val1[i]*width),ypos,xpos+(beg->val1[i]*width),ypos-height); ypos-=(height+2); } /* AUX TOWER*/ ver=ver*10+beg->pos; if(ver<20) { if(ver%10==2) xpos=EndPos; else xpos=AuxPos; } else if(ver<30) { if(ver%10==1) xpos=EndPos; else xpos=BegPos; } else if(ver<40) { if(ver%10==1) xpos=AuxPos; else xpos=BegPos; } ypos=295; for(i=0;i<aux->top;i++) { setfillstyle(aux->val1[i],aux->val1[i]); bar(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height); rectangle(xpos-(aux->val1[i]*width),ypos,xpos+(aux->val1[i]*width),ypos-height); ypos-=(height+2); } getch(); }

The header file graphics.h contains closegraph() function which closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph. closegraph() function is used to re-enter in the text mode and exit from the graphics mode. If you want to use both text mode and graphics mode in the program then you have to use both initgraph() and closegraph() function in the program. This function deallocates all memory allocated by graphics system and restores the screen to that mode in which it was presented before you called the initgraph() function.

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

bar() function is a C graphics function that is used to draw graphics in the C programming language. The graphics.h header contains functions that work for drawing graphics. The bar() function is also defined in the header file. Bar function is used to draw a 2-dimensional, rectangular filled in bar. Coordinates of left top and right bottom corner are required to draw the bar. Left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner. Current fill pattern and fill color is used to fill the bar. To change fill pattern and fill color use setfillstyle.

The header file graphics.h contains cleardevice() function. cleardevice() is a function which is used to clear the screen by filling the whole screen with the current background color. It means that cleardevice() function is used to clear the whole screen with the current background color and it also sets the current position to (0,0). Both clrscr() and cleardevice() functions are used to clear the screen but clrscr() is used in text mode and cleardevice function is used in the graphics mode.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on)

The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value

The exit function terminates the program normally. Automatic objects are not destroyed, but static objects are. Then, all functions registered with atexit are called in the opposite order of registration. The code is returned to the operating system. An exit code of 0 or EXIT_SUCCESS means successful completion. If code is EXIT_FAILURE, an indication of program failure is returned to the operating system. Other values of code are implementation-defined. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

In C++, classes and structs are blueprints that are used to create the instance of a class. Structs are used for lightweight objects such as Rectangle, color, Point, etc. Unlike class, structs in C++ are value type than reference type. It is useful if you have data that is not intended to be modified after creation of struct. C++ Structure is a collection of different data types. It is similar to the class that holds different types of data. A structure is declared by preceding the struct keyword followed by the identifier(structure name). Inside the curly braces, we can declare the member variables of different types.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.






Function overloading & Operator overloading are examples of "Polymorphism". In The C++ programming In function overloading we can have more than one function by 'same name'