I am writing a program display the calander using C++.First,the program prompts the user to enter year and first day of the year.
I have finished my code but i confirm my code not a true source code although i can get the result.
Is that anyone got any idea?How to shortcut my source code?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int year;
cout<<"Year: ";
cin>>year;
int First_day;
cout<<"The first day of the year: ";
cin>>First_day;
cout<<endl;
cout<<setw(20)<<"January "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
int count=0;
const int day_per_line=7;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
int number;
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
if((year%4==0&&year%100!=0)||(year%400==0))
{First_day=(First_day+number)%7;
cout<<setw(20)<<"Febuary "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=29;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
}
else
{First_day=(First_day+number)%7;
cout<<setw(20)<<"Febuary "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=28;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"March "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"April "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=30;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"May "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"June "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=30;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"July "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"August "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"September "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=30;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"October "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"November "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=30;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
cout<<endl;
First_day=(First_day+number)%7;
cout<<setw(20)<<"December "<<year<<endl;
cout<<"_______________________________________"<<endl;
cout<<setw(5)<<"Sun"<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thu"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<endl;
count=0;
for(int i=0;i<First_day;i++)
{ count++;
cout<<setw(5)<<" ";
}
for(int j=1;j<=31;j++)
{ count++;
if(count%day_per_line==0)
{cout<<setw(5)<<j<<endl; }
else
{cout<<setw(5)<<j; }
number=j;
}
cout<<endl;
system("pause");
return 0;
}

New Topic/Question
Reply



MultiQuote








|