Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This will probably not compile. Its up to you to learn how to code.
- // ~Phil
- #define JAN 1
- #define FEB 2
- #define MAR 3
- #define APR 4
- #define MAY 5
- #define JUN 6
- #define JUL 7
- #define AUG 8
- #define SEP 9
- #define OCT 10
- #define NOV 11
- #define DEC 12
- #define MIN_YEAR 1582
- bool validMonth(int month) {
- // Very verbose method
- // Creates a variable for storing the return value
- // then sets it as true or false
- // then returns the (set) variable
- bool valid;
- if (month >= JAN || month <= DEC) {
- valid = true;
- } else {
- valid = false;
- }
- return valid;
- }
- bool validYear(int year) {
- // Very direct value
- // No variable needed as the calculated value
- // is immediately returned
- return (year > MIN_YEAR);
- }
- bool leapYear(int year)
- // Note: The "%" is the mathematical "mod" function
- // Look this up to understand how this works and
- // why this works for this purpose.
- // I've also added gratuitous parentethese so its easier
- // to understand but know that they aren't all needed
- // return year % 4 == 0 && !(year % 100 == 0 && year % 400 !0 0);
- return ((year % 4 == 0) && !((year % 100 == 0) && (year % 400 != 0)));
- }
- bool validDay(int year, int month, int day) {
- // Approach:
- // Check all the "false" conditions
- // If any of these conditions trigger, immediately return false
- // If the function reaches the end, that means all
- // the "false" conditions were not met, so only then can you return true
- // Check for bad year/month
- if (!validMonth(month) || !validYear(year))
- return false;
- // Check for bad day
- switch (month) {
- // Since there's no "break" between these cases,
- // they all "fall through" and execute the same code
- case APR:
- case JUN:
- case SEP:
- case NOV:
- // 31 days
- if (day < 1 || day > 31)
- return false;
- break;
- // February is the special case
- case FEB:
- if (leapYear(year)) {
- if (day < 1 || day > 29)
- return false;
- } else {
- if (day < 1 || day > 28)
- return false;
- }
- break;
- // "default" is all the other months
- default:
- if (day < 1 || day > 30)
- return false;
- break;
- }
- // If your code has reached this point, it means the day is valid
- // This is the function you want to thoroughly check with every
- // single bad case you can think of, then some random good cases
- return true;
- }
- void getData(int& month, int& day, int& year) {
- // You figure this out, this will always be something language-specific.
- // Know though that this function takes POINTERS to variables as its arguments
- // meaning that when you manipulate the variables locally, you're actually
- // manipulating the original variables.
- // e.g.
- // int a = 1;
- // int b = 2;
- // add(&a, &b);
- // After this, a = 3, b = 2
- // BUT
- // int a = 1;
- // int b = 2;
- // add(a, b);
- // After this, a = 1, b = 2
- // This is a bad example and depends on
- // how "add" is implemented, but you get the idea - see your textbook for examples
- }
- void easterDate(int& month, int&day, int year) {
- // Exactly as the instructions say. Use "/" and "%".
- }
- int daysBetween(int month1, int day1, int month2, int day2, int year) {
- return dayOfYear(day2, month2, year) - dayOfYear(day1, month1, year);
- }
- int dayOfYear(int day, int month, int year) {
- int value;
- // Add months values
- // We can do this because we know the set values
- // Deal with leap year later
- // Note: Yes, this can be a bunch of "if" statements followed by each other
- // that step-by-step adds things up, but a single switch statement is
- // MUCH MUCH faster since it allows for compile-time calculated values
- // and has less checks
- switch(month) {
- case JAN:
- break;
- case FEB:
- value += (31);
- break;
- case MAR:
- value += (31 + 28);
- break;
- case APR:
- value += (31 + 28 + 31);
- break;
- case MAY:
- value += (31 + 28 + 31 + 30);
- break;
- case JUN:
- value += (31 + 28 + 31 + 30 + 31);
- break;
- case JUL:
- value += (31 + 28 + 31 + 30 + 31 + 30);
- break;
- case AUG:
- value += (31 + 28 + 31 + 30 + 31 + 30 + 31);
- break;
- case SEP:
- value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
- break;
- case OCT:
- value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
- break;
- case NOV:
- value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
- break;
- case DEC:
- value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
- break;
- }
- // Check leap-years
- if (leapYear(year) && month > 2) {
- value += 1;
- }
- // Add the actual day
- value += day;
- // Return calculated value
- return value;
- }
- int main() {
- bool loop = true;
- string command;
- int month1, day1, month2, day2, year;
- int dayDiff;
- // Somewhere in here you should check when the user wants to exit
- // and if found, set loop to false.
- // The easiest way would be to change getData to return a bool
- // (which is false if the user indicates he wants to exit)
- // and set loop to the returned bool
- while (loop){
- command = getCommand(); // Some prompt, this is an example
- // Here we just use a series of "if-else" because there
- // aren't many cases and each is very different
- if (strcmp(command, "exit") == 0) {
- loop = false;
- } else if (strcmp(command, "day") == 0) {
- getData(&month1, &day1, &year); // Overload getData or made a new function
- if (!validDay(year, month1, day1)) {
- // print error
- } else {
- // print dayOfYear(day1, month1, year);
- }
- } else if (strcmp(command, "easter") == 0) {
- getData(&year);
- easterDate(&month1, &day1, year);
- // print month and day
- } else if (strcmp(command, "diff") == 0) {
- getData(&month1, &day1, &month2, &day2, &year);
- dayDiff = daysBetween(month1, day1, month2, day2, year);
- if (dayDiff < 0) {
- // print error
- } else {
- // print dayDiff
- }
- }
- }
- // print exit message
- }
Add Comment
Please, Sign In to add comment