#include // Many calendar systems designate a particular year as // year 1. For example, in the traditional numbering of // years in the Gregorian calendar, the sequence around // the beginning of the Common Era is // // 3 BCE, 2 BCE, 1 BCE, 1 CE, 2 CE, 3 CE, ... // // Notice that there is no year 0. // // We will represent CE years using positive integers and // BCE years using negative integers. Thus, // // -3 represents 3 BCE, // -1 represents 1 BCE, // 1 represents 1 CE, and // 3 represents 3 CE. // // Consequently, 0 is not a valid year. // // Write a program that asks the user for two years. The // second year must be the same as or later than the first. // // If either year is 0, print an appropriate error message // and terminate the program by returning 0 from main(). // // If the second year is earlier than the first, print an // appropriate error message and terminate the program. // // Calculate the number of years from the first year through // the second year, including both given years. // // For example, the years from 1970 through 1980 are // // 1970, 1971, ..., 1979, 1980, // // so there are 11 years. // // Similarly, the years from -5 through -2 are // // -5, -4, -3, -2, // // so there are 4 years. // // There is one important special case. If the first year is // negative and the second year is positive, the interval // crosses from BCE to CE. Since there is no year 0, the // usual calculation must be adjusted. // // Determine the appropriate formulas and implement them. // Function declarations int main(); // Function definitions int main() { int year1{}; std::cout << "Enter the first year: "; std::cin >> year1; int year2{}; std::cout << "Enter the second year: "; std::cin >> year2; std::cout << (year2 - year1) << std::endl; return 0; } // Hint: // // First consider two years that are both positive: // // 1970 through 1980. // // The difference // // 1980 - 1970 // // is 10, but there are 11 years when both endpoints are // included. Thus, in this case, you must add 1. // // The same calculation works if both years are negative. // // Now consider // // -2 through 2. // // The actual years are // // -2, -1, 1, 2. // // There are 4 years, not 5, because there is no year 0. // // Compare this with // // 2 - (-2). // // What formula should therefore be used when the first // year is negative and the second year is positive? // Here is one solution: // // Notice that the conditional determines the value of // 'number_of_years', but the result is printed only once. /* int main() { int year1{}; std::cout << "Enter the first year: "; std::cin >> year1; if ( year1 == 0 ) { std::cout << "0 is not a valid year." << std::endl; return 0; } int year2{}; std::cout << "Enter the second year: "; std::cin >> year2; if ( year2 == 0 ) { std::cout << "0 is not a valid year." << std::endl; return 0; } if ( year1 > year2 ) { std::cout << "The second year must be the same as or " << "later than the first year." << std::endl; return 0; } int number_of_years{}; if ( (year1 < 0) && (year2 > 0) ) { // Normally we would add 1 because both endpoints // are included. However, there is no year 0, so // these two adjustments cancel each other. number_of_years = year2 - year1; } else { // Both endpoints are included. number_of_years = year2 - year1 + 1; } std::cout << "There are " << number_of_years << " years from " << year1 << " through " << year2 << ", inclusive." << std::endl; return 0; } */ // Compare the solution above with the following approach: // // if ( (year1 < 0) && (year2 > 0) ) { // std::cout << "There are " // << (year2 - year1) // << " years from " // << year1 << " through " << year2 // << ", inclusive." // << std::endl; // } else { // std::cout << "There are " // << (year2 - year1 + 1) // << " years from " // << year1 << " through " << year2 // << ", inclusive." // << std::endl; // } // // Both approaches calculate the correct answer. // // What are some disadvantages of the second approach? // // In particular, consider: // // 1. How much code is duplicated? // // 2. If you wanted to change the wording of the output, // how many places would you have to change? // // 3. Which version more clearly separates calculating // the answer from displaying the answer? // // 4. If the calculated number of years were needed again // later in the program, which approach would make that // easier?