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++ > Miscellaneous Code Examples

Dos based TSR program to Disable-Enable floppy drives

/* Dos based TSR program to Disable-Enable floppy drives */ #pragma inline /* dodn't remove this line */ #include "dos.h" #include "stdio.h" #include "conio.h" #define SUCCESS 0XC0FF #define FAILURE 0XC000 union REGS i,o; void interrupt (*old)(); struct INTERRUPT { unsigned bp,di,si,ds,es,dx,cx,bx,ax,ip,cs,fl; }; void interrupt disk (struct INTERRUPT); int tsrc; unsigned our_psp; void interrupt our_1c(); void interrupt (*prev_1c)(); void interrupt our_2f(struct INTERRUPT); void interrupt (*prev_2f)(); main(int argc,char *argv[]) { int far *ebptr; strupr(argv[1]); tsrc=strcmp(argv[1],"UNLOAD"); if(tsrc!=0) { i.x.ax=0xc000; int86(0x2f,&i,&o); if(o.x.ax!=SUCCESS) { our_psp=getpsp(); res(); ebptr=MK_FP(our_psp,0x2c); freemem(*ebptr); printf("\Nofloppy v(1.0) Installed Successfully....\n"); keep(0,2500); /* if u want to use automatic memory allocation use next line*/ /*keep(0,(_SS+(_SP/16)-_psp));*/ } printf("\nfor unloading nofloppy use -> nofloppy unload \n"); printf("\n Nofloppy v(1.0) already Loaded....."); } else { i.x.ax=0xc000; int86(0x2f,&i,&o); if(o.x.ax==SUCCESS) { i.x.ax=0xc001; int86(0x2f,&i,&o); if(o.x.ax==FAILURE) printf("\nCannot uninstall Nofloppy v(1.0),another program on top\n"); else printf("\nNofloppy ver(1.0) Uninstalled"); } else printf("\nNofloppy ver(1.0) NOT IN MEMORY"); } return 0; } res() { prev_1c=getvect(0x1c); setvect(0x1c,our_1c); prev_2f=getvect(0x2f); setvect(0x2f,our_2f); old=getvect(0x13); setvect(0x13,disk); return 0; } void interrupt disk(struct INTERRUPT r) { if (_DL==0 || _DL==1) { asm clc; asm pushf asm pop r.fl return; } _ES=r.es; _DX=r.dx; _CX=r.cx; _BX=r.bx; _AX=r.ax; (*old)(); asm pushf asm pop r.fl; r.ax=_AX; r.bx=_BX; r.cx=_CX; r.dx=_DX; r.es=_ES; } void interrupt our_1c() { (*prev_1c)(); } void interrupt our_2f(struct INTERRUPT r) { if(r.ax==0xc000) { r.ax=SUCCESS; return; } if(r.ax==0xc001) { r.ax=uninstall(); return; } asm pop bp asm pop di asm pop si asm pop ds asm pop es asm pop dx asm pop cx asm pop bx asm pop ax asm jmp cs:_prev_2f; } int uninstall() { if(our_1c==getvect(0x1c) && our_2f==getvect(0x2f)&&disk==getvect(0x13)) { setvect(0x1c,prev_1c); setvect(0x2f,prev_2f); setvect(0x13,old); freemem(our_psp); return(SUCCESS); } else return(FAILURE); }

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.

The function in C++ language is also known as procedure or subroutine in other programming languages. To perform any task, we can create function. A function can be called many times. It provides modularity and code reusability. Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check... Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them:

#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.

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.

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 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

Compare two strings. Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters. For a function that takes into account locale-specific rules, see strcoll. The strcmp() function in C++ compares two null-terminating strings (C-strings). The comparison is done lexicographically. It is defined in the cstring header file.

In C++ programming language, the strupr() is string manipulation function defined in the cstring header file, which is used to convert all the characters in a string to uppercase letters i.e. a string which is stored in a c-style char array. The strupr(string) function returns string characters in uppercase.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

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.


C++ program displays the iterative solution to the Tower of Hanoi problem. Tower Of Hanoi consists of 'three rods' and a number of disks of different sizes which can "Slide Onto" any







Program checks whether an year ("integer") entered by the user is a leap year or not. All years which are "perfectly divisible" by 4 are leap years except for century years which is


Using Fibonacci numbers we calculate mid of data array to search the data item. 'Calculate' the mid of the array. Divide the array into two subarray. 'Compare the item' by mid element