📄 fig06_01.cpp
字号:
// Fig. 6.1: fig06_01.cpp
// Create a structure, set its members, and print it.
#include <iostream>
using std::cout;
using std::endl;
struct Time { // structure definition
int hour; // 0-23
int minute; // 0-59
int second; // 0-59
};
void printMilitary( const Time & ); // prototype
void printStandard( const Time & ); // prototype
int main()
{
Time dinnerTime; // variable of new type Time
// set members to valid values
dinnerTime.hour = 18;
dinnerTime.minute = 30;
dinnerTime.second = 0;
cout << "Dinner will be held at ";
printMilitary( dinnerTime );
cout << " military time,\nwhich is ";
printStandard( dinnerTime );
cout << " standard time.\n";
// set members to invalid values
dinnerTime.hour = 29;
dinnerTime.minute = 73;
cout << "\nTime with invalid values: ";
printMilitary( dinnerTime );
cout << endl;
return 0;
}
// Print the time in military format
void printMilitary( const Time &t )
{
cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":"
<< ( t.minute < 10 ? "0" : "" ) << t.minute;
}
// Print the time in standard format
void printStandard( const Time &t )
{
cout << ( ( t.hour == 0 || t.hour == 12 ) ?
12 : t.hour % 12 )
<< ":" << ( t.minute < 10 ? "0" : "" ) << t.minute
<< ":" << ( t.second < 10 ? "0" : "" ) << t.second
<< ( t.hour < 12 ? " AM" : " PM" );
}
/**************************************************************************
* (C) Copyright 2000 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -