Nov 22, 2009

C++basics === calendar

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

C++basics === calendar

jackson_cn
#include<iostream>
#include<iomanip>

using namespace std;
int FirstDayofYear (int y);
int DaysofMonth(int m);
void printMonth(int m);
void printHead(int m);
bool LeapYear(int y);
int WeekDay,year;

main()
{
cerr<<"please enter the year(>1):";
cin>>year;
WeekDay=FirstDayofYear(year);
cout<<"\n\n"<<year<<"Year\n";
cout<<"==……==";
for (int a=1;a<13;a++)
printMonth(a);
cout<<"\n";
system("pause");
getchar();
}

void printMonth(int m)
{
printHead(m);
int day=DaysofMonth(m);
for(int i=1;i<=day;i++)
{
cout<<setw(5)<<i;
WeekDay=(WeekDay+day)%7;
if(WeekDay==0)
{
cout<<endl;
cout<<setw(5)<<" ";
}
}
}
void printHead(int m)
{
cout<<"\n\n"<<setw(2)<<m;
cout<<"Month"<<setw(5)<<" Sunday"<<setw(5)<<"Monday"<<setw(5)<<"Tuesday"<<setw(5)<<"Wednesday"<<setw(5)<<"Thursday"\
<<setw(5)<<"Friday"<<setw(5)<<"Saturday\n";
cout<<setw(5)<<" ";
for(int i=0;i<WeekDay;i++)
cout<<setw(5)<<" ";
}
int DaysofMonth(int m)
{
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(LeapYear(year))
return 29;
else
return 28;
}
return 0;
}
bool LeapYear(int y)
{
if((y%4==0) && (y%100 !=0) || (y%400==0))
return TRUE;
else
return FALSE;
}
int FirstDayofYear(int y)
{
long m;
m=y*365;
for(int i=1;i<y;i++)
m+=LeapYear(i);
return m%=7;
}

 

 

 


Comment/Reply (w/o sign-up)

overture
Perhaps some explanation would be good for your tutorial, maybe to show that you know what it all means, but most importantly letting others now what each part or line does so they themselves can learn.

Comment/Reply (w/o sign-up)

qwijibow
i dont mean to be rude. but you have obviously never programmed before.
your code is a complete mess, anthough the algorithms are impressive.

i got rid of the global variables, and made them priivate to the functions they are used in.
i removed the parameter names from the prototyles.
i replaced that ugly switch case statement with a much smaller more easy to understand if then else statement.

and generally just Fixed your code...

this code will Print out a Yearly Calender with correct days of the week.

(sorry, this forum seems a but crap at hanlding indents !!!)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CODE
#include<iostream>
#include<iomanip>

using namespace std;
int FirstDayofYear (int);
int DaysofMonth(int,int);
void printMonth(int,int);
void printHead(int,int);
bool LeapYear(int y);

// main shouud be type integer !
int main() {

    int year; // i made year non global !

    cerr<<"please enter the year(>1):";
    cin>>year;

    cout<<"\n\n"<<year<<"Year\n";
    cout<<"==……==";
    for (int a=1;a<13;a++)
    printMonth(a,year); // added year as a parameter to printmonth
    cout<<"\n";
    return 0;
}

void printMonth(int m,int year) {
    int WeekDay = FirstDayofYear(year); // made WeekDay private
    printHead(m,WeekDay);
    int day=DaysofMonth(m,year); // pass year as parameter
    for(int i=1;i<=day;i++) {
 cout<<setw(5)<<i;
 WeekDay=(WeekDay+day)%7;
 if(WeekDay==0) {
     cout<<endl;
     cout<<setw(5)<<" ";
 }
    }
}

void printHead(int m,int WeekDay) {
    cout<<"\n\n"<<setw(2)<<m;
    cout<<"Month"<<setw(5)<<" \
    Sunday"<<setw(5)<<"Monday"<<setw(5)<<"Tuesday"<<setw(5)<<"Wednesday"<<setw(5)<<"Thursday"\
    <<setw(5)<<"Friday"<<setw(5)<<"Saturday\n";
    cout<<setw(5)<<" ";
    for(int i=0;i<WeekDay;i++) { cout<<setw(5)<<" "; }
}

int DaysofMonth(int m,int year) {

      // got rid of that AWFULL case statement

    if (m==2) {
 return 28 + LeapYear(year); // 28 + true == 29 !
    }

    if (m==4 || m==6 || m==9 || m==11) {
 return 30;
    }
       return 31;
}

bool LeapYear(int y) {
    if((y%4==0) && (y%100 !=0) || (y%400==0)) {
 return true;
    }
    else {
 return false;
    }
}

int FirstDayofYear(int y) {
    long m;
    m=y*365;
    for(int i=1;i<y;i++) {
 m+=LeapYear(i);
    }
    return m%=7;
}

 

 

 


Comment/Reply (w/o sign-up)

r3d
any explanations?

and about the "int main()" my friend use "void main(void)" what's the diff?

Comment/Reply (w/o sign-up)

qwijibow
QUOTE (r3d @ Sep 14 2004, 08:08 PM)
any explanations?

and about the "int main()" my friend use "void main(void)" what's the diff?
*


The difference is that In C++ the main function must be of type int and return an integer, otherwise the orgram will not compile !

its True that the Microsoft Compilers willl accept void.... but this will break compatability with proper c++ compilers... the windows standards are serverly broken.

also.... the parameter of main should be an integer and a pointer to a pointer to a character, or nothing at all.
CODE
int main(int args, char *arguments[]) {
 
  cout << "there were " << args << " arguments passed to this command" << endl;
  cout << "this program's executable name is " << arguments[ 0 ] << end;

  for (int x=1; x<=args; x++) {
      cout << "argument #" << x << " = " << arguments[ x ] << endl;
  }

   return 0;
}


its always important to follow standards, or the behavior of the code in alien envoronments will be unpredictable.

Comment/Reply (w/o sign-up)

kraizii88z
*I think the second explanation is much tidier than the first & it seems that this thread could still use a little explanation considering it is a tutorial..

Comment/Reply (w/o sign-up)

qwijibow
QUOTE (kraizii88z @ Sep 17 2004, 08:04 PM)
*I think the second explanation is much tidier than the first & it seems that this thread could still use a little explanation considering it is a tutorial..
*


to be honest i think this was just a shamless atempts at free hosting rather than actual tutorial...
i just tried to salvage it by actually converting the messs into real C++ code.

maybe it will now be slightly usefull to some1 ?
as for an explaanation.... well...

it just prints out a year calender...
if naything its just a creative program that gives examples on how to use simple if structures.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Cbasics Calendar

  1. C++ Console/dos Based Calendar - Working Calendar for Linux/DOS (0)



Looking for c, basics, calendar

See Also,

*SIMILAR VIDEOS*
Searching Video's for c, basics, calendar
advertisement



C++basics === calendar

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com