BadDate: c++,java,python; pointer, method, function; user input, change data type

C++

/* Program Name: BadDate.cpp
   Function: This program determines if a date entered by the user is valid.
   Input:  Interactive
   Output: Valid date is printed or user is alerted that an invalid date was entered
*/

#include <iostream>
bool validateDate(int, int, int);
void housekeeping(int* year, int* month, int* day);
void endOfJob(bool);
using namespace std;

void housekeeping(int* year, int* month, int* day)
{

    cout << "Please enter the year." << endl;
    cin >> *year;
    cout << "Please enter the month." << endl;
    cin >> *month;
    cout << "Please enter the day." << endl;
    cin >> *day;
}

bool validateDate(int year, int month, int day)
{
    bool validDate = true;
    const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
    if(year <= MIN_YEAR)  // invalid year
        validDate = false;
    else if (month < MIN_MONTH || month > MAX_MONTH)  // invalid month
        validDate = false;
    else if (day < MIN_DAY || day > MAX_DAY) // invalid day
        validDate = false;

    return validDate;
}

void endOfJob(bool validDate)
{
    if(validDate)
    {
        // Output statement
        cout << "is a valid date." << endl;
    }
    else
    {
        // Output statement
        cout << "is an invalid date." << endl;
    }
}
int main()
{
    // Declare variables

    int year;
    int month;
    int day;
    const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
    bool validDate = true;

    // This is the work of the housekeeping() method
    // Get the year, then the month, then the day

    housekeeping(&year, &month, &day);

    // This is the work of the detailLoop() method
    // Check to be sure date is valid
    validateDate(year, month, day);

    // This is the work of the endOfJob() method
    // test to see if date is valid and output date and whether it is valid or not
    endOfJob(validateDate(year, month, day));

} // end of main() function

JAVA

/* Program Name: BadDate.java
   Function: This program determines if a date entered by the user is valid.
   Input:  Interactive
   Output: Valid date is printed or user is alerted that an invalid date was entered.
*/

import java.util.Scanner;
import java.util.Map;

public class BadDate
{


    static boolean detailLoop(int year, int month, int day)
    {
        boolean validDate = true;
        final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
        // This is the work of the detailLoop() method
        // Check to be sure date is valid
        if( year <= MIN_YEAR )  // invalid year
            validDate = false;
        else if ( month < MIN_MONTH || month > MAX_MONTH )  // invalid month
            validDate = false;
        else if ( day < MIN_DAY || day > MAX_DAY ) // invalid day
            validDate = false;
        return validDate;
    }
    static void endOfJob(boolean validDate)
    {
        // This is the work of the endOfJob() method
        // Test to see if date is valid and output date and whether it is valid or not
        if(validDate)
        {
            // Output statement
            System.out.println("is a valid date.");
        }
        else
        {
            // Output statement
            System.out.println("is an invalid date.");
        }
    }

    public static void main(String args[])
    {

        // Declare variables

        String yearString;
        String monthString;
        String dayString;
        int year=1;
        int month=1;
        int day=1;
        boolean validDate = true;
        final int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;

        // This is the work of the housekeeping() method
        // This is the work of the housekeeping() method
        // Get the year, then the month, then the day
        Scanner input= new Scanner(System.in);
        // variable = input.nextLine();
        // Convert Strings to integers
        System.out.println("Enter year.");
        yearString = input.nextLine();
        year = Integer.parseInt(yearString);
        System.out.println("Enter month.");
        monthString = input.nextLine();
        month = Integer.parseInt(monthString);
        System.out.println("Enter day.");
        dayString = input.nextLine();
        day = Integer.parseInt(dayString);

        // This is the work of the detailLoop() method
        // Check to be sure date is valid



        validDate = detailLoop( year,  month,  day);
        // This is the work of the endOfJob() method
        // Test to see if date is valid and output date and whether it is valid or not
        endOfJob(validDate);

    } // end of main() method

} // end of BadDate class

Python

# Program Name: BadDate.py
# Function:     This program determines if a date entered by the user is valid.
# Input:        Interactive
# Output:       Valid date is printed or user is alerted that an invalid date was entered.
MIN_YEAR = 0
MIN_MONTH = 1
MAX_MONTH = 12
MIN_DAY = 1
MAX_DAY = 31

year = None
month = None
day = None

# Get the year, then the month, then the day
# housekeeping()
# def housekeeping(year, month, day):
year = input("Enter year.")
month = input("Enter month.")
day = input("Enter day.")


# Check to be sure date is valid
def check(year, month, day):
    validDate = True
    if int(year) <= MIN_YEAR:  # invalid year
        validDate = False
    elif int(month) < MIN_MONTH or int(month) > MAX_MONTH:  # invalid month
        validDate = False
    elif int(day) < MIN_DAY or int(day) > MAX_DAY:  # invalid day
        validDate = False
    return validDate

# Test to see if date is valid and output date and whether it is valid or not

# endOfJob()

validDate = check(year, month, day)
if validDate:
    # Output statement
    print("is a valid date.")
else:
    # Output statement
    print("is an invalid date.")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麻里很麻利

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值