peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Hàm isValidDate() trong C/C++ - Freetuts
CÁC HÀM C / C++
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Hàm isValidDate() trong C/C++

Hàm isValidDate() trong C/C++cho phép kiểm tra xem một ngày tháng cụ thể có hợp lệ hay không, dựa trên các tiêu chí như định dạng ngày tháng, năm nhuận, v.v.

Trong bài viết này, mình sẽ đi vào chi tiết về cú pháp, cách sử dụng và các ví dụ minh họa về hàm isValidDate(), cũng như những lưu ý quan trọng khi sử dụng nó trong các ứng dụng thực tế. Hãy cùng freetuts.net tìm hiểu chi tiết nhé!

Hàm isValidDate() trong C/C++

Cú pháp của hàm isValidDate()

bool isValidDate(int day, int month, int year);

Tham số của hàm isValidDate()

  • day: Số nguyên biểu diễn ngày trong tháng (từ 1 đến 31).
  • month: Số nguyên biểu diễn tháng trong năm (từ 1 đến 12).
  • year: Số nguyên biểu diễn năm (ví dụ: 2022, 2023, ...).

Giá trị trả về của hàm isValidDate()

  • true: Nếu ngày tháng năm được cung cấp là hợp lệ.
  • false: Nếu ngày tháng năm không hợp lệ.

Cách sử dụng hàm isValidDate() trong C/C++

Sử dụng hàm isValidDate() để kiểm tra tính hợp lệ của ngày tháng:

//Bài viết này được đăng tại freetuts.net
if (isValidDate(day, month, year)) {
    printf("Ngay thang nam %d/%d/%d hop le.\n", day, month, year);
} else {
    printf("Ngay thang nam %d/%d/%d khong hop le.\n", day, month, year);
}

Xử lý các trường hợp đặc biệt như định dạng ngày tháng, năm nhuận, v.v.

  • Kiểm tra tháng có phải là số từ 1 đến 12.
  • Kiểm tra ngày hợp lệ của từng tháng (ví dụ: tháng 4 có tối đa 30 ngày).
  • Kiểm tra năm nhuận (năm chia hết cho 4, ngoại trừ những năm chia hết cho 100 nhưng không chia hết cho 400).

Ví dụ về cách sử dụng hàm isValidDate()

#include <stdio.h>
#include <stdbool.h>
//Bài viết này được đăng tại freetuts.net
bool isValidDate(int day, int month, int year) {
    if (year < 0) return false;
    if (month < 1 || month > 12) return false;
    if (day < 1) return false;
    
    int daysInMonth;
    if (month == 2) {
        // Check for leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            daysInMonth = 29;
        } else {
            daysInMonth = 28;
        }
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        daysInMonth = 30;
    } else {
        daysInMonth = 31;
    }
    
    return day <= daysInMonth;
}
//Bài viết này được đăng tại freetuts.net
int main() {
    int day, month, year;
    printf("Nhap ngay thang nam (dd/mm/yyyy): ");
    scanf("%d/%d/%d", &day, &month, &year);
    
    if (isValidDate(day, month, year)) {
        printf("Ngay thang nam %d/%d/%d hop le.\n", day, month, year);
    } else {
        printf("Ngay thang nam %d/%d/%d khong hop le.\n", day, month, year);
    }
    
    return 0;
}

Output:

Nhap ngay thang nam (dd/mm/yyyy): 
20/11/2023
Ngay thang nam 20/11/2023 hop le.

Lưu ý khi sử dụng hàm isValidDate() trong C/C++

Các trường hợp đặc biệt cần chú ý khi kiểm tra tính hợp lệ của ngày tháng

  • Kiểm tra ngày tháng nằm trong khoảng giá trị hợp lệ.
  • Xác định ngày cuối cùng của mỗi tháng, đặc biệt là tháng 2 với năm nhuận và không nhuận.
  • Xử lý định dạng ngày tháng đầu vào.

Hạn chế của hàm isValidDate():

  • Hàm này chỉ kiểm tra tính hợp lệ của ngày tháng, không kiểm tra múi giờ, định dạng hoặc các yếu tố khác liên quan đến ngày tháng như dữ liệu thời gian thực.

Ví dụ về việc sử dụng hàm isValidDate() trong C/C++

Kiểm tra tính hợp lệ của ngày tháng trong một chương trình đơn giản

#include <iostream>
#include <ctime>
//Bài viết này được đăng tại freetuts.net
// Hàm kiểm tra tính hợp lệ của ngày tháng
bool isValidDate(int day, int month, int year) {
    if (year < 0) return false; // Năm không thể là số âm

    if (month < 1 || month > 12) return false; // Tháng phải trong khoảng từ 1 đến 12

    int maxDay; // Số ngày tối đa của mỗi tháng
    switch (month) {
        case 2:
            maxDay = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28; // Tháng 2 của năm nhuận có 29 ngày, không nhuận có 28 ngày
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            maxDay = 30; // Tháng 4, 6, 9, 11 có 30 ngày
            break;
        default:
            maxDay = 31; // Các tháng còn lại có 31 ngày
    }
//Bài viết này được đăng tại freetuts.net
    return (day >= 1 && day <= maxDay); // Kiểm tra ngày có nằm trong khoảng hợp lệ của tháng không
}

int main() {
    int day, month, year;

    // Nhập ngày tháng từ người dùng
    std::cout << "Nhap ngay: ";
    std::cin >> day;
    std::cout << "Nhap thang: ";
    std::cin >> month;
    std::cout << "Nhap nam: ";
    std::cin >> year;
   std::cout << std::endl;
   std::cout << "freetuts.net";
    std::cout << std::endl;

    // Kiểm tra tính hợp lệ của ngày tháng
    if (isValidDate(day, month, year)) {
        std::cout << "Ngay thang nhap hop le.\n";
    } else {
        std::cout << "Ngay thang nhap khong hop le.\n";
    }

    return 0;
}

Output

Screenshot 202024 04 19 20160211 png Screenshot 202024 04 19 20160242 png

Kết quả kiểm tra tính hợp lệ và không hợp lệ của ngày tháng

Kết bài

Trong bài viết này, mình đã tìm hiểu về hàm isValidDate() trong C/C++, một công cụ hữu ích để kiểm tra tính hợp lệ của ngày tháng. Mình cũng đã tìm hiểu về cú pháp, tham số và giá trị trả về của hàm, cũng như cách sử dụng và các lưu ý khi áp dụng hàm này.

Việc kiểm tra tính hợp lệ của ngày tháng là rất quan trọng trong nhiều ứng dụng, từ việc nhập liệu đến tính toán thời gian. Bằng cách sử dụng hàm isValidDate(), ta có thể đảm bảo rằng ngày tháng được nhập vào là đúng và hợp lệ.

Nếu bạn có bất kỳ câu hỏi hoặc ý kiến đóng góp nào, đừng ngần ngại để lại cho freetuts.net biết. Cảm ơn bạn đã đọc!

test php

Bài giải

-------------------- ######## --------------------

Câu hỏi thường gặp liên quan:

Cùng chuyên mục:

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý mảng đa chiều (array.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số thực (float.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý số nguyên lớn (bigint.h) trong C/C++

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý thời gian (time.h) trong C

Các hàm xử lý chuỗi (string.h) trong C/C++

Các hàm xử lý chuỗi (string.h) trong C/C++

Thread Pools và Parallel Algorithms trong C++

Thread Pools và Parallel Algorithms trong C++

Tạo và quản lý các Multithreading trong C++

Tạo và quản lý các Multithreading trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Xử lý ngoại lệ khi làm việc với Memory Allocation trong C++

Try, Catch, và Throw của Exception Handling trong C++

Try, Catch, và Throw của Exception Handling trong C++

Cách sử dụng Lambda Expressions trong C++

Cách sử dụng Lambda Expressions trong C++

Sử dụng weak_ptr trong C++

Sử dụng weak_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng shared_ptr trong C++

Sử dụng unique_ptr trong C++

Sử dụng unique_ptr trong C++

Tổng quan về Smart Pointers trong C++

Tổng quan về Smart Pointers trong C++

Sử dụng Iterators trong STL của C++

Sử dụng Iterators trong STL của C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng Vector trong C++

[Iterator] Sử dụng trong List trong C++

[Iterator] Sử dụng trong List trong C++

[STL] Sử dụng Vector trong C++

[STL] Sử dụng Vector trong C++

Tổng quan về Iterators trong C++

Tổng quan về Iterators trong C++

[STL] Các hàm thường dùng của lớp Vector trong C++

[STL] Các hàm thường dùng của lớp Vector trong C++

Top