Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <algorithm>
- #include <string>
- using namespace std;
- int main()
- {
- //paralle arrays for mont names and days
- string names[] = {"January","February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"};
- int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
- //parallel arrays for day name and number
- string weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- int week_num[] = {1,2,3,4,5,6,7};
- string month_name,day; //to get month name and day
- int year; //to store year
- //ask user to enter month and store it in month_name
- cout<<"\nEnter the name of a month> ";
- cin>>month_name;
- //loop till user enters stop
- while(true)
- {
- int month[6][7] = {0}; //initialize month array with all zeroes
- //ask user to enter year
- cout<<"\nEnter the year for "<<month_name<<"> ";
- cin>>year;
- //ask user to enter day name
- cout<<"\nEnter the name of the day on which this month starts> ";
- cin>>day;
- //print out month and year
- cout<<"\n "<<month_name<<" "<<year<<endl;
- int n, start;//n for no. of days in month and start for month starting day
- bool flag = false; //to cheack year is leap or not
- int i,j, k; //to itearate
- //get no. of days
- for(i=0;i<12;i++)
- {
- std::transform(month_name.begin(), month_name.end(), month_name.begin(), ::tolower);
- std::transform(names[i].begin(), names[i].end(), names[i].begin(), ::tolower);
- //if month found
- if(month_name == names[i])
- {
- //if february, check for leap year
- if(month_name == "february")
- {
- if(year%400==0)
- flag = true;
- else if(year%100==0)
- flag = false;
- else if(year%4==0)
- flag = true;
- }
- //if leap year and february then add 1 to days and assign to n
- if(flag==true)
- n = days[i]+1;
- //if not leap year or not february then assign days to n
- else
- n = days[i];
- break;
- }
- }
- //get starting day
- for(i=0;i<7;i++)
- {
- std::transform(day.begin(), day.end(), day.begin(), ::tolower);
- std::transform(weeks[i].begin(), weeks[i].end(), weeks[i].begin(), ::tolower);
- if(day==weeks[i])
- {
- start = week_num[i];
- break;
- }
- }
- //add values to month array
- for(i=0,j=0;(i<6&&j<(n+start));i++)
- {
- for(k=0;k<7;k++)
- {
- j++; //increment j which represents positions 0 to 41
- //if position is greater and less than end position (where last date to store)
- if(j>=start-1 && j<=(n+start-1))
- {
- month[i][k] = j-start+1; // add day
- }
- }
- }
- //print calendar
- for(i=0;i<6;i++)
- {
- for(j=0;j<7;j++)
- {
- //if 0 then print 3 spaces
- if(month[i][j]==0)
- cout<<" ";
- //if single digit add 1 space in front and at end
- else if(month[i][j]<10)
- cout<<" "<<month[i][j]<<" ";
- //if double digit then add one space at end
- else
- cout<<month[i][j]<<" ";
- }
- cout<<endl; //end line
- }
- //ask user to continue or stop
- cout<<"\nEnter the name of a month or stop to quit> ";
- cin>>month_name;
- //if stop then break loop
- if(month_name=="stop")
- break;
- cin.ignore();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement