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

Time arithmatic

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/* Time arithmatic */ #include<stdio.h> #include<conio.h> typedef struct { int hh,mm,ss; }time; time add(time f,time s); time sub(time f,time s); time input(void); void main() { time f,s,ans; int ch; clrscr(); do { printf("\n <1> enter first operand "); printf("\n <2> enter second operand "); printf("\n <3> time addition "); printf("\n <4> time subtraction "); printf("\n <5> Exit \n\n"); do { printf("enter your choice "); scanf("%d",&ch); }while(ch<1 || ch>5); switch (ch) { case 1: printf("\n enter the time(hh mm ss) "); f=input(); break; case 2: printf("\n enter the time(hh mm ss) "); s=input(); break; case 3: ans=add(f,s); printf("\n time addition is %d %d %d",ans.hh,ans.mm,ans.ss); break; case 4: ans=sub(f,s); printf("\n time subtraction is %d %d %d",ans.hh,ans.mm,ans.ss); break; } }while(ch!=5); } time input(void) { time temp; do { printf("\n enter the hours : "); scanf("%d",&temp.hh); } while(temp.hh<0 || temp.hh>12); do { printf("\n enter the minutes : "); scanf("%d",&temp.mm); } while(temp.mm<0 || temp.mm>=60); do { printf("\n enter the seconds : "); scanf("%d",&temp.ss); } while(temp.ss<0 || temp.ss>=60); return temp; } time add(time x,time y) { time a; if(x.ss+y.ss>60) ++x.mm,a.ss=(x.ss+y.ss)%60; else a.ss=x.ss+y.ss; if(x.mm+y.mm>60) ++x.hh,a.mm=(x.mm+y.mm)%60; else a.mm=x.mm+y.mm; if(x.hh+y.hh>12) a.hh=(x.hh+y.hh)%12; else a.hh=x.hh+y.hh; if(a.hh==0) a.hh=12; return a; } time sub(time x,time y) { time a; if(x.ss-y.ss<0) --x.mm,a.ss=60+x.ss-y.ss; else a.ss=x.ss-y.ss; if(x.mm-y.mm<0) --x.hh,a.mm=60+x.mm-y.mm; else a.mm=x.mm-y.mm; if(x.hh-y.hh<0) a.hh=12+x.hh-y.hh; else a.hh=x.hh-y.hh; if(a.hh==0) a.hh=12; return a; }

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:

Get current time. Get the current calendar time as a value of type time_t. The function returns this value, and if the argument is not a null pointer, it also sets this value to the object pointed by timer. The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). Although libraries may use a different representation of time: Portable programs should not use the value returned by this function directly, but always rely on calls to other elements of the standard library to translate them to portable types (such as localtime, gmtime or difftime).

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

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.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

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

Put character. Inserts character c into the stream. Internally, the function accesses the output sequence by first constructing a sentry object. Then (if good), it inserts c into its associated stream buffer object as if calling its member function sputc, and finally destroys the sentry object before returning. Function returns the ostream object (*this).

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

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 C++, namespace can be defined in different parts of the program. All the objects that are in different parts of a program, but if in same namespace will form one namespace. Better

Using Templates so that any type of data can be stored in stack without multiple defination of class. Contains "Location of Topmost data" pushed onto stack. Sets the top location to -1