/ Published in: C++
dok rok cetong boh
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Effy-20111021.cpp : Defines the entry point for the console application. // This program written using Visual C++ 2010 bish!! // printf(); Reference: http://www.cplusplus.com/reference/clibrary/cstdio/printf/ // scanf(); Reference: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/ // Modulo splitting digits: http://www.daniweb.com/software-development/c/threads/310740 /********************************************************************************* d1 = r % 10 * Thinking from right to left, we first store d1 * r /= 10 * Now we drop the last digit of r, as we don't need it anymore * * By the time we've got all of d1, d10 and d100, r will be equal to r / 1000 * **********************************************************************************/ //#include <stdio.h> // Include standard libs instead of stdafx.h (the windows shit) #include "stdafx.h" // Windows precompiled library and shits. #include <iostream> // Include standard C++ Libs. //#include <iomanip> // Reference on setw + setfill http://msdn.microsoft.com/en-us/library/daeb650d(VS.80).aspx using namespace std; int _tmain() { // We gonna use some variables. int usertime = 0; int hours = 0; int minutes = 0; printf ("Enter time in 24hrs format: "); //scanf_s ("%d", &usertime); cin >> usertime; //newline setting printf("\n\n"); // Splitting minutes and hours using modulo operation. minutes = usertime % 100; hours = usertime / 100; // Validating Minutes first. (There's workaround for it actually) if (minutes >= 0 && minutes < 60) { // 12 AM Flag if (hours == 0 || hours == 24){ printf ("24hrs time is: %d.\n", usertime); printf ("12hrs time is: 12:%02d AM.\n", minutes); // 1 AM to 11AM } else if (hours >= 1 && hours <= 11) { printf ("24hrs time is: %04d.\n", usertime); printf ("12hrs time is: %02d:%02d AM.\n", hours, minutes); // 12PM to 11PM (On-fly conversion. woohoo } else if (hours >= 12 && hours <= 23) { printf ("24hrs time is: %04d.\n", usertime); printf ("12hrs time is: %02d:%02d PM.\n", hours%12 , minutes); } else { printf ("Invalid time!"); } } else { printf ("Please reconsider your minute value!\n\n"); } system("Pause"); return 0; }