📄 pr09004.cpp
字号:
////////////////////////////////////////
// File Name: pr09004.cpp
////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <ctime>
// Date structure with a function.
struct Date
{
int month, day, year;
void display(); // A function to display the date.
};
////////////////////////////////////////
// Implementation of the Date
// structure's display() member
// function.
////////////////////////////////////////
void Date::display()
{
static char *mon[] =
{
"January","February","March","April","May","June",
"July","August","September","October","November",
"December"
};
std::cout << mon[month] << ' ' << day << ", " << year;
}
// Time structure with a function.
struct Time {
int hour, minute, second;
void display(); // A function to display the time.
};
////////////////////////////////////////
// Implementation of the Time
// structure's display() member
// function.
////////////////////////////////////////
void Time::display()
{
std::cout.fill('0');
std::cout << (hour>12?hour-12:(hour==0?12:hour)) << ':'
<< std::setw(2) << minute << ':'
<< std::setw(2) << second
<< (hour < 12 ? "am" : "pm");
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Get the current time from the OS.
std::time_t curtime = time(0);
std::tm tim = * std::localtime(&curtime);
// Define Time and Date structures.
Time now;
Date today;
// Initialize the structures.
now.hour = tim.tm_hour;
now.minute = tim.tm_min;
now.second = tim.tm_sec;
today.month = tim.tm_mon;
today.day = tim.tm_mday;
today.year = tim.tm_year+1900;
// Display the current date and time.
std::cout << "At the tone it will be ";
now.display();
std::cout << " on ";
today.display();
std::cout << '\a' << std::endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -