PDA

View Full Version : Calendar project



codenamedave
10-30-2015, 11:07 AM
Hey I'm doing a project for an intro to programming class (c++) which involves making a calendar. I've finished it but now (for extra credit) I want to convert my code to use a graphics library so the calendar looks better. While doing research on the internet I ran into openGL and went with that because it was the library I found works easiest with Xcode. I basically found code that outputs white text on a black background in a window and have been trying to get my calendar to appear in that window. This is way out of my league but I've actually gotten somewhere with this and while I won't actually make a nice calendar I was just wondering if anyone could provide a little insight upon checking out my code.
This is the code for the calendar with the openGL:
#include <string>
#include <GLUT/glut.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <alloca.h>
#include <cstring>

using namespace std;

int get_day_of_week(int,int,int);
string get_day_of_week_name(int weekDaynum);

bool isleap (int year)
{
if ( ( !(year % 4) && (year % 100) ) || !(year % 400) )
return (true);
else
return (false);
}

int number_of_month_days(int month, int year)
{
int numberOfMonthDays;

switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numberOfMonthDays = 31;
break;
case 2:
if (isleap(year) == true) {
numberOfMonthDays = 29;
} else {
numberOfMonthDays = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
numberOfMonthDays = 30;
break;

default:
break;
}

return numberOfMonthDays;
}


int number_of_days_in_a_year(int year)
{
int numberOfDaysInAYear;

if (isleap(year)) {
numberOfDaysInAYear = 366;
} else {
numberOfDaysInAYear = 365;
}

return numberOfDaysInAYear;
}


int get_first_day_of_year (int year)
{
double dyear=(double) year;
int first_day_of_year;
first_day_of_year = ( ((year - 1) * 365) + ( (int) floor((dyear - 1) / 4) ) + ( (int) (floor(dyear - 1) / 100 )) + ( (int) floor((dyear - 1) / 400 )))%7;

return first_day_of_year;
}


int get_first_day_of_month (int month, int year)
{
int weekDayNum = get_day_of_week(month, 1, year);

return weekDayNum;
}


string get_month_name(int month)
{
string monthName;

switch (month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;

default:
break;
}

return monthName;
}


string get_day_of_week_short_name(int weekDayNum)
{
string dayOfWeekNameShort;

switch (weekDayNum) {
case 0:
dayOfWeekNameShort = "Su";
break;
case 1:
dayOfWeekNameShort = "Mo";
break;
case 2:
dayOfWeekNameShort = "Tu";
break;
case 3:
dayOfWeekNameShort = "We";
break;
case 4:
dayOfWeekNameShort = "Th";
break;
case 5:
dayOfWeekNameShort = "Fr";
break;
case 6:
dayOfWeekNameShort = "Sa";
break;

default:
break;
}
return dayOfWeekNameShort;
}


string get_day_of_week_name(int weekDaynum)
{
string dayOfWeekName;

switch (weekDaynum) {
case 0:
dayOfWeekName = "Sunday";
break;
case 1:
dayOfWeekName = "Monday";
break;
case 2:
dayOfWeekName = "Tuesday";
break;
case 3:
dayOfWeekName = "Wednesday";
break;
case 4:
dayOfWeekName = "Thursday";
break;
case 5:
dayOfWeekName = "Friday";
break;
case 6:
dayOfWeekName = "Saturday";
break;

default:
break;
}
return dayOfWeekName;
}


int get_day_of_week2(int month, int day, int year) //Find the Day of the week
{
//Uspensky and Heaslet gave the following formula, in Elementary Number Theory, 1939, for the Gregorian calendar:

// W=D+floor(2.6m-0.2)+y+floor(y/4)+floor(c/4)-2c (mod 7)

int weekDay,century;

century=year/100;
weekDay=(day+((int) floor(2.6*month-0.2)+year)+(int) floor(year/4.0)+((int) floor(century/4.0)-2*century))%7;

return weekDay;
}


int get_day_of_week(int month, int day, int year) //Find the Day of the Week
{

int weekDay;
if (year < 0) //invalid year
return 0;
else
{
if (month < 3) //Jan = 13 & Feb = 14 and year is the previous year
{
month += 12;
year -= 1;
}
weekDay = (day + 2*month + 3*(month+1)/5 + year +
year/4 - year/100 + year/400 + 1) % 7;

if (month > 12)
{ //reset Jan & Feb back to 1 &2
month -= 12;
year += 1 ;
}


}
return weekDay;
}


int day_of_year(int month, int day, int year)
{
int dayOfYear = 1;

for (int i = 1; i < month; i++) {
dayOfYear = dayOfYear + number_of_month_days(month-i, year);
}

return dayOfYear;
}


int day_of_forever(int month, int day, int year)

{
int dayOfForever = 1;

for (int i = 0; i < year; i++) {
dayOfForever = dayOfForever + number_of_days_in_a_year(i);
}
for (int i = 1; i < month; i++) {
dayOfForever = dayOfForever + number_of_month_days(month-i, year);
}

if (day>1) {
for (int i = 0; i < day - 1; i++) {
dayOfForever = dayOfForever + 1;
}
}

return dayOfForever;
}


void write_day_names_console(int weekDayNum)
{
int i = 0;
while (i < 7) {
cout << get_day_of_week_short_name(weekDayNum%7)<< " ";
i++;
weekDayNum++;
}
}


void draw_calendar_console(int month,int year, int weekDayNum)
{
cout << setfill(' ') << setw(13);
write_day_names_console(weekDayNum);
cout << endl;

int firstDayOfMonth = get_first_day_of_month(month, year);
int counter = 0;

while (firstDayOfMonth != weekDayNum) {
if (firstDayOfMonth < weekDayNum && weekDayNum == 6) {
cout << " ";
weekDayNum = 0;
counter++;
}

cout << " ";
weekDayNum++;
counter++;

if (firstDayOfMonth < weekDayNum && weekDayNum < 6) {
cout << " ";
weekDayNum++;
counter++;
}
}

cout << setfill(' ') << setw(13);
for (int i = 1; i < number_of_month_days(month, year) + 1; i++) {

if (i < 9) {
if (counter == 7) {
cout << endl;
cout << setfill(' ') << setw(13);
}
cout << i << " ";
counter ++;
} else {
if (counter % 7 == 0) {
cout << setfill(' ') << setw(13);
cout << endl;
}
cout << i << " ";
counter++;
}
}
}
/*
int find_num_sundays_in_month (int month, int weekDayNum, int year) {

int sundays = 0;

for (int i = 1; i < number_of_month_days(month, year) + 1; i++) {
if (get_day_of_week(month, i, year) == 0) {
sundays++;
}
}

return sundays;
}
*/

void *font = GLUT_BITMAP_TIMES_ROMAN_24;
void *fonts[] =
{
GLUT_BITMAP_9_BY_15,
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_BITMAP_TIMES_ROMAN_24
};
void conversion(int month, int year, int weekDayNum, char const*& message, string s);

void selectFont(int newfont);

void selectMessage(int msg);

void selectColor(int color);

void tick(void);

void output(int x, int y, char *string);

void display(void);

void reshape(int w, int h);

int main(int argc, char **argv)
{
int i, msg_submenu, color_submenu;
string s;
char const * message;
int month, year/*,day*/,weekDayNum;
bool more;
char c;
more=true;
while (more)
{
cout<<"********************Welcome to BTE320 Calendar*************************"<<endl<<endl;
cout << "Please enter the year: ";
cin >> year;
cout << endl;
cout << "Please enter the month: ";
cin >> month;
cout << endl;
cout << "Please enter which day you would like the calendar to begin with (0 = Sunday, 1 = Monday, etc.): ";
cin >> weekDayNum;
cout << endl;

/*
************ Day of the Year & Day of Forever ************

-cout << "Please enter the day: "
cin >> day;
-cout << day_of_year(month, day, year) << endl;
-cout << day_of_forever(month, day, year) << endl;
-get_first_day_of_month(month, year);
cout << endl;

******** Find how many Sundays any given year has. ********

-cout << find_num_sundays_in_month(month, weekDayNum, year);
cout << endl;
*/
conversion(month, year, weekDayNum, message, s);

glutInit(&argc, argv);
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-mono")) {
font = GLUT_BITMAP_9_BY_15;
}
}
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 200);
glutCreateWindow("Calendar");
glClearColor(0.0, 0.0, 0.0, 1.0);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(tick);
msg_submenu = glutCreateMenu(selectMessage);
glutAddMenuEntry("abc", 1);
glutAddMenuEntry("ABC", 2);
color_submenu = glutCreateMenu(selectColor);
glutAddMenuEntry("Green", 1);
glutAddMenuEntry("Red", 2);
glutAddMenuEntry("White", 3);
glutCreateMenu(selectFont);
glutAddMenuEntry("9 by 15", 0);
glutAddMenuEntry("Times Roman 10", 1);
glutAddMenuEntry("Times Roman 24", 2);
glutAddSubMenu("Messages", msg_submenu);
glutAddSubMenu("Color", color_submenu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;


cout<<"************* "<<get_month_name(month)<<" "<<year<<" *************"<<endl;
draw_calendar_console(month, year, weekDayNum);
cout<<endl<<endl;
cout<<"Would you like to view another calendar?(Enter N to end the program): ";
cin>>c;
if (c=='N' || c=='n')
{
more=false;
exit(0);
}
}
return 0; /* ANSI C requires main to return int. */
}
void conversion(int month, int year, int weekDayNum, char const*& message, string s) {
char const * s1;

s = get_month_name(month);

s1 = (char *)malloc(s.size() + 1);
memcpy(&s1, s.c_str(), s.size() + 1);
/*
char defaultMessage[s.length()];

for (int i=0; i<s.length(); i++) {
defaultMessage[i] = s.at(i);
}
s2 = defaultMessage.c_str();
message = defaultMessage;
*/
message = s1;
}

void selectFont(int newfont)
{
font = fonts[newfont];
glutPostRedisplay();
}

void selectMessage(int msg)
{
int month, year,weekDayNum;
string s;
char const * message;
conversion(month, year, weekDayNum, message, s);
switch (msg) {
case 1:
message = "abcdefghijklmnop";
break;
case 2:
message = "ABCDEFGHIJKLMNOP";
break;
}
}

void selectColor(int color)
{
switch (color) {
case 1:
glColor3f(0.0, 1.0, 0.0);
break;
case 2:
glColor3f(1.0, 0.0, 0.0);
break;
case 3:
glColor3f(1.0, 1.0, 1.0);
break;
}
glutPostRedisplay();
}

void tick(void)
{
glutPostRedisplay();
}

void output(int x, int y, char *string)
{
int len, i;

glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++) {
glutBitmapCharacter(font, string[i]);
}
}

void display(void)
{
int month, year,weekDayNum;
string s;
char const * message;
conversion(month, year, weekDayNum, message, s);
char defaultMessage [s.length()];

for (int i=0; i<s.length(); i++) {
defaultMessage[i] = s.at(i);
}
cout << defaultMessage;
glClear(GL_COLOR_BUFFER_BIT);
output(10, 24, "This is written in a GLUT bitmap font.");
output(100, 100, defaultMessage);
output(50, 145, "(positioned in pixels with upper-left origin)");

glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
}

This is my original code for my calendar (without the openGL):
#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
#include <cmath>
#include <cstdlib>

using namespace std;

int get_day_of_week(int,int,int);
string get_day_of_week_name(int weekDaynum);


bool isleap (int year)
{

if ( ( !(year % 4) && (year % 100) ) || !(year % 400) )
return (true);
else
return (false);
}


int number_of_month_days(int month, int year)
{
int numberOfMonthDays;

switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numberOfMonthDays = 31;
break;
case 2:
if (isleap(year) == true) {
numberOfMonthDays = 29;
} else {
numberOfMonthDays = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
numberOfMonthDays = 30;
break;

default:
break;
}

return numberOfMonthDays;
}


int number_of_days_in_a_year(int year)
{
int numberOfDaysInAYear;

if (isleap(year)) {
numberOfDaysInAYear = 366;
} else {
numberOfDaysInAYear = 365;
}

return numberOfDaysInAYear;
}


int get_first_day_of_year (int year)
{
double dyear=(double) year;
int first_day_of_year;
first_day_of_year = ( ((year - 1) * 365) + ( (int) floor((dyear - 1) / 4) ) + ( (int) (floor(dyear - 1) / 100 )) + ( (int) floor((dyear - 1) / 400 )))%7;

return first_day_of_year;
}


int get_first_day_of_month (int month, int year)
{
int weekDayNum = get_day_of_week(month, 1, year);

return weekDayNum;
}


string get_month_name(int month)
{
string monthName;

switch (month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;

default:
break;
}

return monthName;
}


string get_day_of_week_short_name(int weekDayNum)
{
string dayOfWeekNameShort;

switch (weekDayNum) {
case 0:
dayOfWeekNameShort = "Su";
break;
case 1:
dayOfWeekNameShort = "Mo";
break;
case 2:
dayOfWeekNameShort = "Tu";
break;
case 3:
dayOfWeekNameShort = "We";
break;
case 4:
dayOfWeekNameShort = "Th";
break;
case 5:
dayOfWeekNameShort = "Fr";
break;
case 6:
dayOfWeekNameShort = "Sa";
break;

default:
break;
}
return dayOfWeekNameShort;
}


string get_day_of_week_name(int weekDaynum)
{
string dayOfWeekName;

switch (weekDaynum) {
case 0:
dayOfWeekName = "Sunday";
break;
case 1:
dayOfWeekName = "Monday";
break;
case 2:
dayOfWeekName = "Tuesday";
break;
case 3:
dayOfWeekName = "Wednesday";
break;
case 4:
dayOfWeekName = "Thursday";
break;
case 5:
dayOfWeekName = "Friday";
break;
case 6:
dayOfWeekName = "Saturday";
break;

default:
break;
}
return dayOfWeekName;
}


int get_day_of_week2(int month, int day, int year) //Find the Day of the week
{
//Uspensky and Heaslet gave the following formula, in Elementary Number Theory, 1939, for the Gregorian calendar:

// W=D+floor(2.6m-0.2)+y+floor(y/4)+floor(c/4)-2c (mod 7)

int weekDay,century;

century=year/100;
weekDay=(day+((int) floor(2.6*month-0.2)+year)+(int) floor(year/4.0)+((int) floor(century/4.0)-2*century))%7;

return weekDay;
}


int get_day_of_week(int month, int day, int year) //Find the Day of the Week
{

int weekDay;
if (year < 0) //invalid year
return 0;
else
{
if (month < 3) //Jan = 13 & Feb = 14 and year is the previous year
{
month += 12;
year -= 1;
}
weekDay = (day + 2*month + 3*(month+1)/5 + year +
year/4 - year/100 + year/400 + 1) % 7;

if (month > 12)
{ //reset Jan & Feb back to 1 &2
month -= 12;
year += 1 ;
}


}
return weekDay;
}


int day_of_year(int month, int day, int year)
{
int dayOfYear = 1;

for (int i = 1; i < month; i++) {
dayOfYear = dayOfYear + number_of_month_days(month-i, year);
}

return dayOfYear;
}


int day_of_forever(int month, int day, int year)

{
int dayOfForever = 1;

for (int i = 0; i < year; i++) {
dayOfForever = dayOfForever + number_of_days_in_a_year(i);
}
for (int i = 1; i < month; i++) {
dayOfForever = dayOfForever + number_of_month_days(month-i, year);
}

if (day>1) {
for (int i = 0; i < day - 1; i++) {
dayOfForever = dayOfForever + 1;
}
}

return dayOfForever;
}


void write_day_names_console(int weekDayNum)
{
int i = 0;
while (i < 7) {
cout << get_day_of_week_short_name(weekDayNum%7)<< " ";
i++;
weekDayNum++;
}
}


void draw_calendar_console(int month,int year, int weekDayNum)
{
cout << setfill(' ') << setw(13);
write_day_names_console(weekDayNum);
cout << endl;

int firstDayOfMonth = get_first_day_of_month(month, year);
int counter = 0;

while (firstDayOfMonth != weekDayNum) {
if (firstDayOfMonth < weekDayNum && weekDayNum == 6) {
cout << " ";
weekDayNum = 0;
counter++;
}

cout << " ";
weekDayNum++;
counter++;

if (firstDayOfMonth < weekDayNum && weekDayNum < 6) {
cout << " ";
weekDayNum++;
counter++;
}
}

cout << setfill(' ') << setw(13);
for (int i = 1; i < number_of_month_days(month, year) + 1; i++) {

if (i < 9) {
if (counter == 7) {
cout << endl;
cout << setfill(' ') << setw(13);
}
cout << i << " ";
counter ++;
} else {
if (counter % 7 == 0) {
cout << setfill(' ') << setw(13);
cout << endl;
}
cout << i << " ";
counter++;
}
}
}
/*
int find_num_sundays_in_month (int month, int weekDayNum, int year) {

int sundays = 0;

for (int i = 1; i < number_of_month_days(month, year) + 1; i++) {
if (get_day_of_week(month, i, year) == 0) {
sundays++;
}
}

return sundays;
}
*/

int main()
{
int month, year/*,day*/,weekDayNum;
bool more;
char c;
more=true;
while (more)
{
cout<<"********************Welcome to BTE320 Calendar*************************"<<endl<<endl;
cout << "Please enter the year: ";
cin >> year;
cout << endl;
cout << "Please enter the month: ";
cin >> month;
cout << endl;
cout << "Please enter which day you would like the calendar to begin with (0 = Sunday, 1 = Monday, etc.): ";
cin >> weekDayNum;
cout << endl;

/*
************ Day of the Year & Day of Forever ************

-cout << "Please enter the day: "
cin >> day;
-cout << day_of_year(month, day, year) << endl;
-cout << day_of_forever(month, day, year) << endl;
-get_first_day_of_month(month, year);
cout << endl;

******** Find how many Sundays any given year has. ********

-cout << find_num_sundays_in_month(month, weekDayNum, year);
cout << endl;
*/

cout<<"************* "<<get_month_name(month)<<" "<<year<<" *************"<<endl;
draw_calendar_console(month, year, weekDayNum);
cout<<endl<<endl;
cout<<"Would you like to view another calendar?(Enter N to end the program): ";
cin>>c;
if (c=='N' || c=='n')
{
more=false;
exit(0);
}
}

exit(0);
}