C++ Program to Display Calendar














































C++ Program to Display Calendar



#include <iostream>

class Calender {
	bool leap;
	int year,month,start_day;
	std::pair<std::string,int> getMonth(int m);
	void isLeapYear();
	public:
	Calender(int year,int start_day);
	void printCalender();

};

//Constructor
Calender::Calender(const int y,const int sd) : year(y),start_day(sd) {
	month = 12;
	isLeapYear();
}

//Function for finding wether the year is leap year or not
void Calender::isLeapYear() {
/*
   *if year is divided by 4 but not 100 than it is a leap year
   *if year is divided by both 100 and 400 than it is a leap year
   *if year is divided by 400 than it is a leap year
   * Else not a leap year
*/
	if ( ( year % 400 ==0 || year % 100 !=0) && (year%4 == 0))
		leap = true;
	else
		leap = false;
}

//Prints the calender
void Calender::printCalender() {
	std::string showCalender("");
	for(int i = 1 ; i <= month ; ++i) {
		auto mn = getMonth(i);
		//start_day will remember where the months end
		int monthStart = start_day;
		showCalender.append(mn.first);
		showCalender.append("\033[1;34mSun\tMon\tTue\tWed\tThu\tFri\tSat\n\033[0m");
		for(int j = 1 ; j < monthStart ; ++j) 
			showCalender.append("      \t");
		for(int k = 1 , numOfDays = mn.second; k <= numOfDays ; ++k) {
			if((( k+ monthStart -2) % 7 == 0) && (k != 1))
				showCalender.append("\n");
			showCalender.append("\033[1;33m");
			showCalender.append(std::to_string(k));
			showCalender.append("\033[0m");
			showCalender.append("\t");
			start_day++;
			if(start_day > 7) 
				start_day = 1;
		}
	}
	std::cout << showCalender << std::endl;
}

//Function finds number of day in the month
std::pair<std::string,int> Calender::getMonth(int m) {
	switch(m) {
		case 1 : return (std::make_pair("\n\n\033[1;32mJanuary\033[0m\n",31));
		case 2 : if(leap)
				return (std::make_pair("\n\n\033m[1;32mFeburay\033[0m\n",29));
			 else
				return (std::make_pair("\n\n\033[1;32mFeburay\033[0m\n",28));
		case 3 : return (std::make_pair("\n\n\033[1;32mMarch\033[0m\n",   31));
		case 4 : return (std::make_pair("\n\n\033[1;32mApril\033[0m\n",	  30));
		case 5 : return (std::make_pair("\n\n\033[1;32mMay\033[0m\n",	  31));
		case 6 : return (std::make_pair("\n\n\033[1;32mJune\033[0m\n",	  30));
		case 7 : return (std::make_pair("\n\n\033[1;32mJuly\033[0m\n",     31));
		case 8 : return (std::make_pair("\n\n\033[1;32mAugust\033[0m\n",   31));
		case 9 : return (std::make_pair("\n\n\033[1;32mSeptember\033[0m\n",30));
		case 10 : return (std::make_pair("\n\n\033[1;32mOctober\033[0m\n", 31));
		case 11 : return (std::make_pair("\n\n\033[1;32mNovember\033[0m\n",30));
		case 12 : return (std::make_pair("\n\n\033[1;32mDecember \033[0m\n",31));
	}
}

//Function for getting year
std::pair<int,int> getYearNStartDay() {
	int year,sd;
	std::cout << "Enter the year you want to Display\n";
	std::cin >> year;

	do {
	std::cout << "Enter the first day of January (Ex. 1 for Sunday ,2 for Monday ..)\n";
	std::cin >> sd;
	}while (sd < 0 && sd > 8);

	return (std::make_pair(year,sd));

}


int main() {

	for(;;) {
		std::cout << "C-c to Quit" << std::endl;
		auto i = getYearNStartDay();
		Calender c(i.first,i.second);
		c.printCalender();
	}

}

More Articles of Hatim Master:

Name Views Likes
Check if removing an edge can divide a Binary Tree in two halves 717 18
C++11 std::has_virtual_destructor 758 18
C++ program to find inorder successor in binary search tree using templates 1400 18
C++ future std::future::future (Constructor) 823 23
C++ Program to Merge Mails 717 21
C++ program for swapping two strings 521 19
C++ Program to count number of lines in a file 887 23
C++ Progam for checking prime numbers 578 15
C++ std::atomic::load 945 22
C++ Program to Find Hash of File 2914 11
C++ program to check if an array represents Inorder of Binary Search tree or not 511 19
C++ future std::future::wait_until 1251 19
C++ future std::future::valid 755 22
C++ program for merge sort 870 18
C++ program to find median of binary search tree in O(n) using templates 629 19
C++11 std::genrate with std::array 685 17
C++ program to convert Binary to decimal,octal,hex 640 19
C++ Program to Check Vowel or Not 502 20
C++ std::find_first_of with vector 895 20
C++ program to find if there is a triplet in a Balanced binary search tree that adds to zero 545 16
C++ Program to Find the Factorial of a Number 621 21
C++ Exception std::exception(Constructor) 793 13
C++ atomic std::atomic::fetch_and 733 20
Check if a Binary Tree contains duplicate subtrees of size 2 or more 1066 17
C++ Program to Print the Fibonacci sequence 603 13
C++ future std::async 868 14
C++ program to check if a given binary tree is height balanced like a red-black tree 670 13
C++ program to insert an element into red black tree using template 2669 20
C++ program for Bubble sort using template 2625 13
C++11 std::genrate_n with std::vector 715 14
C++ program for copying string 456 11
C++ Program to Lowercase to Uppercase 590 15
C++ program to copy the content of the file 603 18
C++ program to count and display the number of lines not starting with the given alphabet in the file 1229 16
Finding Distance between two nodes 962 12
C++ program for removing Punctuation 81 19
C++ program to find distance between two nodes of a binary search tree using templates 547 27
C++ program for solving Quadratic equation 694 26
C++ Program to Display Powers of 2 Using Anonymous Function 608 20
C++ program to add page at the beginning of the PDF 796 14
Print root to leaf paths without using recursion 671 26
C++ Program to Display Calendar 1214 29
C++11 std::genrate with std::forward_list 669 22
C++ std::find_if_not with std::deque 631 19
C++ program to query for ancestor-descendant relationship in a tree 735 13
C++ Program to Find ncR & nPr 753 21
Create loops of even and odd values in a binary tree 716 21
C++ atomic std::atomic::fetch_or 883 23
C++ program to convert kilometers to miles 631 27
C++ Program for comparing string 604 11
C++ Program to Find Factors of Number 554 24
C++ std::genrate with std::vector 688 14
C++ program to find vertical sum in binary tree 705 14
Program for shutdown or Restart PC. 659 20
C++ Program to Remove Words from Sentence 644 17
C++ program for Transpose matrix 668 22
C++ Constructor 777 20
C++ atomic std::atomic::compare_exchange_strong 748 16
Combining two sorted list and creating another sorted list 499 13
C++11 std::is_arithmetic 668 16
C++ program for adding matrices 624 20
C++ program to find check if given sorted sub-sequence exists in binary search tree 614 16
C++ Program to Convert Decimal to Binary, Octal and Hexadecimal 5095 22
C++ Program to Convert Fahrenheit to Celsius 656 21
C++ Program for finding size of string 551 24
C++ program to find a word in the file and print that line along with line number 1280 19
C++ program to reverse a path in binary search tree using queue using templates 859 12
C++ Program to Remove Spaces from String 686 21
C++ Program to Shuffle Deck of Cards 8153 18
C++ is_lock_free 627 16
C++ program to convert image/images to PDF 2349 24
C++ atomic std::atomic::compare_exchange_weak 739 19
C++ program for removing vowels from a string 696 25
C++ program for swapping two variable 565 18
Find Numbers Divisible by Number 1268 17
std::find_if_not with std::deque 654 18
C++ Program to Check Reverse equal Original 588 14
C++ program to count pairs from two binary search trees whose sum is equal to a given value x 498 14
C++ program for Insertion Sort 1110 18
C++ Program to Illustrate Different Set Operations 589 20
C++ Program to One Dimension Array Program 591 20
C++ program to count the number occurance of the given charactor in a file. 59 21
C++ program to print unique words 1479 16
C++11 std::is_abstract 641 26
C++ Program to Count the Number of Each Vowel 508 16
C++ program to find all the prime number in the given interval 518 26
C++ Program to Add Digits of Number 558 18
C++ Program to Print ASCII Values 544 15
Convert a Binary Tree into Doubly Linked List in spiral fashion 728 13
C++11 std::genrate with std::deque 581 20
C++ future std::future::get 865 21
C++ program to count the number of spaces present in a file. 562 19
C++ future std::future::share 753 18
C++ atomic std::atomic::fetch_xor 676 17
C++ program to check for identical binary serach trees without building the trees 464 13
C++ program to convert hexadecimal to binary,decimal,octal 738 22
C++ atomic std::atomic::operator-- 698 14
C++ program for multiplying matrices 523 27
C++ Program to append the file 711 24
C++ program for radix sort 1657 11
C++ program to read the content from a file, count and display the number of alphabets present in it. 517 26
std::find_first_of with std::deque 580 20
C++ program to construct ancestor matrix from a given binary tree using templates 517 15
C++ program to find list of files present in the directory 583 16
C++ std::atomic::store 1207 11
C++ program to find median of binary search tree 729 26
Print all root to leaf paths with there relative positions 654 24
Convert a tree to forest of even nodes 552 17
Convert a Binary Tree to a Circular Doubly Link List 564 16
Merge Two Binary Trees by doing Node Sum 721 23
C++ program to implement linux grep command. 2235 21
C++ Program to Check Leap Year 598 15
C++11 std::genrate_n with std::list 641 28
C++ atomic std::atomic::operator++ 813 23
C++ program to count the number of word in the file 1033 18
C++ Program for Counting sort 793 28
C++ program to get number of page of the pdf file 1212 19
C++ Program to Check Alphabet or Not 421 12
C++11 std::genrate_n with std::array 645 18
C++ future std::future::wait_for 1588 18
C++ program for concatenate String 668 28
std::find_if_not with std::array 719 26
std::find_if_not with std::vector 717 30
C++ Program to Uppercase to Lowercase 517 18
C++ Program to Count Word in Sentence 683 11
C++ program for Subtracting matrices 465 26
C++ Program to Make a Simple Calculator 433 17
Random Number 531 14
C++ program for checking the number is even or odd 652 27
std::find_if_not with std::list 636 28
C++ Program to convert octal to binary,decimal and hex 534 11
C++ program to encrypt a PDF file 1693 13
c++ program to get size of the file 502 15
C++ program to create a doubly linked list from a ternary tree 508 27
std::find_first_of with std::list 983 20
C++ future std::future::wait 792 27
C++ Program to Display Fibonacci Sequence Using Recursion 584 16
C++ Program to Find Armstrong Number in an Interval 684 16
C++ program for Bucket Sort 4230 26
C++ std::genrate with std::list 521 18
C++ program to create a tree with left-child right-sibling representation using templates 1204 20
C++ program to shuffle words of each line of the given file 2053 21
C++ atomic std::atomic::fetch_add 1014 25
C++ program to find simple recursive solution to check whether binary search tree contains dead end using templates 448 15
C++ atomic_flag 771 13
Maximum spiral sum in Binary Tree 613 27
C++ atomic std::atomic::fetch_sub 689 18
C++ program to read content of multiple files and generate one output file 749 22
C++ Program to print Pattern 556 22
C++ program to Decrypt a PDF File 1566 13
C++ Program to Merge Two Files 1700 19
C++ Program for reversing the String 513 24
C++ program to merge two files 679 14
Check if leaf traversal of two Binary Trees is same? 546 20
C++ program to get a page from PDF file 1059 19
C++ program for counting the character in the String 409 17
C++ Program to Check Armstrong Number 610 25
C++ program to check given array of size n can represent binary search tree of n levels or not 632 27
C++ Program to Sort Words in Alphabetic Order 600 24
C++11 std::alignment_of 601 13
C++ program to count the number of new lines present in a file. 583 16
C++ Program to Find HCF & LCM 582 20
C++ program to shuffle lines of the given file 555 12
C++ Program to Find the Size (Resolution) of a Image 1682 20
std::find_if_not with std::forward_list 636 23
C++ program to construct tree from ancestor matrix using templates 857 14
C++ std::atomic::exchange 684 19
std::find_fist_of with std::forward_list 566 15
C++11 std::extent 676 14
C++ program to merge PDF files 2874 22

Comments

  • Pankaj
    5-Jan-2019 12:59:03 PM
    Good Hatim.

    Here is the output of the program.