/* * Aresh Saharkhiz * saharkiz@gmail.com * Associate Professor / Mapua Institute of Technology / Philippines */ using System; using System.IO; // Introduction to Software Testing // Authors: Paul Ammann & Jeff Offutt // Chapter 3, section 3.4, page 132 internal class cal_class { public static int cal(int month1, int day1, int month2, int day2, int year) { //*********************************************************** // Calculate the number of Days between the two given days in // the same year. // preconditions : day1 and day2 must be in same year // 1 <= month1, month2 <= 12 // 1 <= day1, day2 <= 31 // month1 <= month2 // The range for year: 1 ... 10000 //*********************************************************** int numDays; if (month2 == month1) // in the same month { numDays = day2 - day1; } else { // Skip month 0. int[] daysIn = new int[] { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Are we in a leap year? int m4 = year % 4; int m100 = year % 100; int m400 = year % 400; if ((m4 != 0) || ((m100 == 0) && (m400 != 0))) { daysIn[2] = 28; } else { daysIn[2] = 29; } // start with days in the two months numDays = day2 + (daysIn[month1] - day1); // add the days in the intervening months for (int i = month1 + 1; i <= month2 - 1; i++) { numDays = daysIn[i] + numDays; } } return (numDays); } public static void Main(string[] argv) { // Driver program for cal int month1, day1, month2, day2, year; int T; Console.WriteLine("Enter month1: "); month1 = N; Console.WriteLine("Enter day1: "); day1 = N; Console.WriteLine("Enter month2: "); month2 = N; Console.WriteLine("Enter day2: "); day2 = N; Console.WriteLine("Enter year: "); year = N; // preconditions : day1 and day2 must be in same year // 1 <= month1, month2 <= 12 // 1 <= day1, day2 <= 31 // month1 <= month2 // The range for year: 1 ... 10000 if ((month1 < 1) || (month1 > 12)) { month1 = 1; Console.WriteLine("invalid month1, choosing 1."); } if ((month2 < 1) || (month2 > 12)) { month2 = 1; Console.WriteLine("invalid month2, choosing 1."); } if ((day1 < 1) || (day1 > 31)) { day1 = 1; Console.WriteLine("invalid day1, choosing 1."); } if ((day2 < 1) || (day2 > 31)) { day2 = 1; Console.WriteLine("invalid day2, choosing 1."); } while (month1 > month2) { Console.WriteLine("month1 must be prior or equals to month2"); Console.WriteLine("Enter month1: "); month1 = N; Console.WriteLine("Enter month2: "); month2 = N; } if ((year < 1) || (year > 10000)) { year = 1; Console.WriteLine("invalid year, choosing 1."); } T = cal(month1, day1, month2, day2, year); Console.WriteLine("Result is: " + T); } // ==================================== // Read (or choose) an integer private static int N { get { int inputInt = 1; string inStr; try { inStr = Console.ReadLine(); inputInt = Convert.ToInt32(inStr); } catch (IOException) { Console.WriteLine("Could not read input, choosing 1."); } catch (FormatException) { Console.WriteLine("Entry must be a number, choosing 1."); } return (inputInt); } } // end getN }