Advertisement
dirtydevil123

assignment 3

Aug 4th, 2021
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. #include <algorithm>
  4.  
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13.                 //paralle arrays for mont names and days
  14.  
  15.                 string names[] = {"January","February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December"};
  16.  
  17.                 int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  18.  
  19.                 //parallel arrays for day name and number
  20.  
  21.                 string weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  22.  
  23.                 int week_num[] = {1,2,3,4,5,6,7};
  24.  
  25.              
  26.  
  27.                 string month_name,day; //to get month name and day
  28.  
  29.                 int year; //to store year
  30.  
  31.                 //ask user to enter month and store it in month_name
  32.  
  33.                 cout<<"\nEnter the name of a month> ";
  34.  
  35.                 cin>>month_name;
  36.  
  37.                 //loop till user enters stop
  38.  
  39.                 while(true)
  40.  
  41.                 {
  42.  
  43.                                 int month[6][7] = {0}; //initialize month array with all zeroes
  44.  
  45.                                 //ask user to enter year
  46.  
  47.                                 cout<<"\nEnter the year for "<<month_name<<"> ";
  48.  
  49.                                 cin>>year;
  50.  
  51.                                 //ask user to enter day name
  52.  
  53.                                 cout<<"\nEnter the name of the day on which this month starts> ";
  54.  
  55.                                 cin>>day;
  56.  
  57.                                 //print out month and year
  58.  
  59.                                 cout<<"\n   "<<month_name<<"   "<<year<<endl;
  60.  
  61.                                 int n, start;//n for no. of days in month and start for month starting day
  62.  
  63.                                 bool flag = false; //to cheack year is leap or not
  64.  
  65.                                 int i,j, k; //to itearate
  66.  
  67.                                 //get no. of days
  68.  
  69.                                 for(i=0;i<12;i++)
  70.  
  71.                                 {
  72.  
  73.                                                 std::transform(month_name.begin(), month_name.end(), month_name.begin(), ::tolower);
  74.  
  75.                                                 std::transform(names[i].begin(), names[i].end(), names[i].begin(), ::tolower);
  76.  
  77.                                                 //if month found
  78.  
  79.                                                 if(month_name == names[i])
  80.  
  81.                                                 {
  82.  
  83.                                                                 //if february, check for leap year
  84.  
  85.                                                                 if(month_name == "february")
  86.  
  87.                                                                 {
  88.  
  89.                                                                                 if(year%400==0)
  90.  
  91.                                                                                                 flag = true;
  92.  
  93.                                                                                 else if(year%100==0)
  94.  
  95.                                                                                                 flag = false;
  96.  
  97.                                                                                 else if(year%4==0)
  98.  
  99.                                                                                                 flag = true;
  100.  
  101.                                                                 }
  102.  
  103.                                                                 //if leap year and february then add 1 to days and assign to n
  104.  
  105.                                                                 if(flag==true)
  106.  
  107.                                                                                 n = days[i]+1;
  108.  
  109.                                                                 //if not leap year or not february then assign days to n
  110.  
  111.                                                                 else
  112.  
  113.                                                                                 n = days[i];
  114.  
  115.                                                                 break;
  116.  
  117.                                                 }
  118.  
  119.                                 }
  120.  
  121.                                 //get starting day
  122.  
  123.                                 for(i=0;i<7;i++)
  124.  
  125.                                 {
  126.  
  127.                                                 std::transform(day.begin(), day.end(), day.begin(), ::tolower);
  128.  
  129.                                                 std::transform(weeks[i].begin(), weeks[i].end(), weeks[i].begin(), ::tolower);
  130.  
  131.                                                 if(day==weeks[i])
  132.  
  133.                                                 {
  134.  
  135.                                                                 start = week_num[i];
  136.  
  137.                                                                 break;
  138.  
  139.                                                 }
  140.  
  141.                                 }
  142.  
  143.                                 //add values to month array
  144.  
  145.                                 for(i=0,j=0;(i<6&&j<(n+start));i++)
  146.  
  147.                                 {
  148.  
  149.                                                 for(k=0;k<7;k++)
  150.  
  151.                                                 {
  152.  
  153.                                                                 j++; //increment j which represents positions 0 to 41
  154.  
  155.                                                                 //if position is greater and less than end position (where last date to store)
  156.  
  157.                                                                 if(j>=start-1 && j<=(n+start-1))
  158.  
  159.                                                                 {
  160.  
  161.                                                                                 month[i][k] = j-start+1; // add day
  162.  
  163.                                                                 }
  164.  
  165.                                                 }
  166.  
  167.                                 }
  168.  
  169.                                 //print calendar
  170.  
  171.                                 for(i=0;i<6;i++)
  172.  
  173.                                 {
  174.  
  175.                                                 for(j=0;j<7;j++)
  176.  
  177.                                                 {
  178.  
  179.                                                                 //if 0 then print 3 spaces
  180.  
  181.                                                                 if(month[i][j]==0)
  182.  
  183.                                                                                 cout<<"   ";
  184.  
  185.                                                                 //if single digit add 1 space in front and at end
  186.  
  187.                                                                 else if(month[i][j]<10)
  188.  
  189.                                                                                 cout<<" "<<month[i][j]<<" ";
  190.  
  191.                                                                 //if double digit then add one space at end
  192.  
  193.                                                                 else
  194.  
  195.                                                                                 cout<<month[i][j]<<" ";
  196.  
  197.                                                 }
  198.  
  199.                                                 cout<<endl; //end line
  200.  
  201.                                 }
  202.  
  203.                                 //ask user to continue or stop
  204.  
  205.                                 cout<<"\nEnter the name of a month or stop to quit> ";
  206.  
  207.                                 cin>>month_name;
  208.  
  209.                                 //if stop then break loop
  210.  
  211.                                 if(month_name=="stop")
  212.  
  213.                                                 break;
  214.  
  215.                                 cin.ignore();
  216.  
  217.                 }
  218.  
  219.                 return 0;
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement