Jump to content
xisto Community
Sign in to follow this  
jackson_cn

C++basics === calendar

Recommended Posts

#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;}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 !!!)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

#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;}

Share this post


Link to post
Share on other sites

any explanations?

 

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

<{POST_SNAPBACK}>


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.

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.

Share this post


Link to post
Share on other sites

*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..

<{POST_SNAPBACK}>


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.