Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program You want a program that can take 2 dates represented by (m1, d1, y1)

ID: 673372 • Letter: C

Question

 C++ Program   You want a program that can take 2 dates represented by (m1, d1, y1) and (m2, d2, y2) Note that m1 and m2 are month numbers (1-12) , d1 and d2 are days in the month, and y1 and y2 are year numbers.  You will create a function (deltaDays) which will compute the number of days between the 2 dates.  The function deltaDays will call some helper functions to do some of the work: isLeapYear, daysInMonth, isLessThanOrEqual.    To do this, copy the starting code in MP4ASkeleton.cpp and replace the comments describing the algorithm with real code.  MP4B You are to design and implement a Roman numeral calculator.  
 The subtractive Roman numeral notation commonly in use today was used only rarely during the time of the Roman Republic and Empire.  For ease of calculation, the Romans most frequently used a purely additive notation in which a number was simply the sum of its digits (4 equals IIII in this notation, not IV). Each number starts with as many digits of highest value as are possible and ends with only as many digits of smallest value as remain after exhausting greater values, (see example below.)  You are to use this additive notation in this program, both for input and output.  Your program inputs two Roman numbers and an arithmetic operator, printing the result of the operation, also as an additive Roman number. Values of the Roman digits are as follow:  I    1 V    5 X    10 L    50 C    100 D    500 M    1000  For example, the number MDCCCCLXXXXVI represents 1996, because 1996 really consists of:  1000 + 500 + 100 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 10 + 5 + 1.  M + D + C + C + C + C + L + X + X + X + X + V + I  Wrong ways to express this value would use too many lesser valued digits when more greater valued digits could have been used. In this example, the 1,500 is correctly represented by  MD  *not* by  DDD  The arithmetic operators your program should recognize in the input are +, -, /, *, and %. These operators specify the C++ operations of integer addition, subtraction, division, multiplication and modulo.  One way of approaching this problem is to convert the Roman numbers into integers, perform the required operation, and convert the result back to a Roman number to print.  For an input file line of MCCXXVI LXVIIII + the following might be a sample output of the program:  MCCXXVI The first number is 1226  LXVIIII The second number is 69  The desired arithmetic operation: +  The sum of MCCXXVI and LXVIIII is MCCLXXXXV (1295).  The program should check for errors in the input, such as illegal digits or invalid operators, and display an error message when these errors are found. If an error is found, do not process that line any further. Assume the input numbers are in purely additive form - this is, digits are followed only by digits of the same or lower value.   REQUIREMENTS:  This program is to be done by including the following functions.  These functions must NOT reference global variables directly (use parameter lists).  FUNCTION get_Data void get_Data(...) This function receives the input file, reads one line of data, checks for incorrect data and sends back the values read in and a boolean variable indicating whether or not there were an error. You do *not* have to check input is in "digit decreasing" order. If an error occurs, the function prints an error message, and sets the boolean variable true. Use a void function with reference parameters.  FUNCTION print_Result void print_Result(...); This void function receives the two roman numbers, the roman result and the integer result and prints messages. It does not have to return anything to the calling program.  FUNCTION convert_To_Roman string convert_to_Roman(...); This function receives an integer and returns its corresponding roman number as a string. Use a value-returning function.  FUNCTION convert_From_Roman int convert_from_Roman(...); This function receives a string and returns its corresponding integer number as an integer. Use a value-returning function.  FUNCTION calc_Romans int calc_Romans(...); This function is given two integers and a char and returns the result of the two integers based on the char (operator).   INPUT/OUTPUT: You are to make your own datafile (contents described below). Make sure to copy the data as given below INCLUDING additional data you typed in to demonstrate the robustness of your program in checking for various situations or complications.  The format of the data file:  There are two roman numbers followed by an operator per line and the program should be designed to work even if the number of lines in the datafile is changed at any time.  Numbers and operators are separated by "whitespace," i.e. space(s) or tabs.  Hint: Write this program in stages, concentrating on one function at a time. E.g. first write the get_Data() function and a simple main program to test it.  Make sure it works, reading data from the file correctly before working on other functions. Your main program will be quite short with lots of function calls doing all the work.      ********************************************************************************** Program reads input from a file named "roman.txt" containing at least the following:  MCcXxVI   mCC   -  Xvii CL +  bdD MM + XVII CL -  XVII CL * CL   XVII / CL   XVII % MMMmmDddCCCccLlXxVvIi mmm + MM BadD + MM  MM  = 

Explanation / Answer

#include <iostream>

#include <iomanip>

using namespace std;

bool isLeapYear(int y)

{

// Write code to

// return true if "y" is a leap year, otherwise return false

// LEAP YEAR DEFINITION:

//If a year is not evenly divisible by 100, then a leap year is

// any year that is evenly divisible by 4, otherwise we don't have a leap year

//If a year is evenly divisible by 100, then it is not a leap year unless

// the year is evenly divisible by 400.

if ((y % 100) != 0) {

if ((y % 4) == 0)

return true;

else

return false;

}

else {

if ((y % 400) == 0)

return true;

else

return false;

}

}

int daysInMonth(int m, int y)

{

// Write code to

// return the number of days in a month (the month number is 1-12 where 1 represents January)

// Note that April, June, September and November have 30 days

// February has 29 days if "y" is a leap year, otherwise February has 28 days

// All other months have 31 days

if (m == 4 || m == 6 || m == 9 || m == 11) {

return 30;

}

else if (m == 2) {

if (isLeapYear(y))

return 29;

else

return 28;

}

else

return 31;

}

bool isLessThanOrEqual(int m1, int d1, int y1, int m2, int d2, int y2)

{

// Write code to return a true if the date represented by

// (m1, d1, y1) is less than or equal to the date represented by (m2, d2, y2)

// HINT: if y1 != y2 then you know the answer without going any further

// if y1 == y2 then compare m1 and m2 to determine the answer

// if y1 == y2 and m1 == m2, then you will need to look at d1 and d2

if (y1 != y2) {

if (y1 < y2)

return true;

else

return false;

}

else if (m1 != m2) {

if (m1 < m2)

return true;

else

return false;

}

else {

if (d1 <= d2)

return true;

else

return false;

}

}

int deltaDays(int m1, int d1, int y1, int m2, int d2, int y2)

{

// Return the number of days to go from the date represented by

// (m1, d1, y1) to the date represented by (m2, d2, y2)

// Note this answer can be negative.

//Recommended approach:

int delta, deltaMonths;

const int MONTHS_YEAR = 12;

if (isLessThanOrEqual(m1,d1, y1, m2, d2, y2))

{

delta = d2 -d1;

if (y1 == y2) {

//isLessThanorEqual guarantees that m1 will be less than or equal to m2 when y1 == y2

while (m1 < m2) {

delta += daysInMonth(m1, y1);

m1++;

}

}

else {

//work up to the correct year

while (y1 < y2 ) {

delta += daysInMonth(m1, y1);

//rollover to the next year if needed

if (m1 == 12) {

m1 = 0;

y1++;

}

m1++;

}

//once in the right year, keep going to the correct month

while (m1 < m2) {

delta += daysInMonth(m1, y1);

m1++;

}

}

// if (m1, y1) is not equal to (m2, y2) then

// add to delta "daysInMonth(m, y)" for every

// month (m) and year(y) needed to go from

// (m1, y1) to (m2, d2) -- This will be some kind of while loop

// As an example, suppose that

// (m1, d1, y1) = (3, 10, 2012)

// (m2, d2, y2) = (5, 25, 2012)

// delta =d2 -d1 = 15;

// there are 2 months that need to be added

// for the months 3, and 4 which have 31 and 30 days respectively

// So the total answer is 76

}

else

delta = -deltaDays (m2, d2, y2, m1, d1, y1); //Note: deltaDays can call deltaDays

return delta;

}

// Leave this routine alone

void test(int m1, int d1, int y1, int m2, int d2, int y2)

{

int delta = deltaDays(m1, d1, y1, m2, d2, y2);

cout << delta << " days between " <<

m1 << "/" << d1 << "/" << y1 << " and " <<

m2 << "/" << d2 << "/" << y2 << endl;

}

// Run at least the following tests. You can add more at the end if you like

int main()

{

test(11, 20, 2001, 11, 28, 2001); // In the same month

test(11, 20, 2001, 12, 2, 2001); // different month, same year

test(11, 20, 2001, 1, 2, 2002); // different year

test(2, 4, 2001, 2, 28, 2001);

// test leap year logic

test(2 , 4, 2001, 3 , 5, 2001);

test(2 , 4, 2004, 3 , 5, 2004);

test(2 , 4, 2000, 3 , 5, 2000);

test(2 , 4, 1900, 3 , 5, 1900);

// Testing a span that is over a year

test (2, 4, 2012, 4, 4, 2013);

test (4, 4, 2013, 2, 4, 2012); // Negative answer on this one

char exit;

cin >> exit;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote