/*Time.h 头文件*/
#include <iostream>
struct Time
{
unsigned hour,
Minute;
char AMorPM;
unsigned milTime;
};
void set(Time &t,unsigned hours,unsigned minutes,char AMPM);
void display(const Time &t,ostream &out);
void advance(Time & t,unsigned hours,unsigned minutes);
bool lessThan(const Time &t1,const Time &t2);
/* Time.cpp */
// Time.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
#include "Time.h"
int toMilitary(unsigned hours,unsigned minutes,char AMPM);
void toStandard(unsigned military,unsigned &hours,unsigned &minutes,char &M);
void set(Time &t,unsigned &hours,unsigned &minutes,char AMPM)
{
if(hours>=1&&hours<=12&&minutes>=0&&minutes<=59&(AMPM=='A'||AMPM=='p'))
{
t.hours=hours;
t.minutes=minutes;
t.AMorPM=AMPM;
t.milTime=toMilitary(hours,minutes,AMPM);
}
else
cerr<<" ***Can't set time with these values***\n"
}
void display(const Time &t,ostream &out)
{
cout<<t.hour<<':'
<<(t,minutes<10 ?""<<t.minute
<<' '<<t.AMorPM<<'.M.'
<<t.milTime<<" mil.Time";
}
void advance(Time &t,unsigned &hours,unsigned minutes)
{
t.milTime+=100*hours+minutes;
unsigned milHours=t.milTimes/100;
unsigned milMins=t.milTime%100;
milHours+=milMins/60;
milMins%=60;
milHours%=24;
t.milTime=100*milHours+milMins;
toStandard(t.milTime,t.hour,t.minute,t.AMorPM);
}
bool lessThan(const Time &t1,const Time &t2)
{
return (t1.milTime<t2.milTime);
}
int toMilitary(unsigned hours,unsigned minutes,char AMPM)
{
if(hours==12)
hours=0;
return hours*100+minutes+(AMPM=='P'?1200:0);
}
void toStandard(unsigned military,unsigned &hours,unsigned &minutes,char &M)
{
hours=(military/100)%12;
if(hours==0)
hours=12;
minutes=military%100;
AMPM=(military/100)<12?'a':'p';
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
生成时的错误提示:
错误 1 error C2065: “Time”: 未声明的标识符 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 11
错误 2 error C2065: “t”: 未声明的标识符 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 11
错误 3 error C2062: 意外的类型“unsigned int” c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 11
错误 4 error C2143: 语法错误 : 缺少“;”(在“{”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 12
错误 5 error C2447: “{”: 缺少函数标题(是否是老式的形式表?) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 12
错误 6 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 24
错误 7 error C2143: 语法错误 : 缺少“,”(在“&”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 24
错误 8 error C2228: “.hour”的左边必须有类/结构/联合 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 26
错误 9 error C2065: “minutes”: 未声明的标识符 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 27
错误 10 error C2228: “.minute”的左边必须有类/结构/联合 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 28
错误 11 error C2228: “.AMorPM”的左边必须有类/结构/联合 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 28
错误 12 error C2228: “.milTime”的左边必须有类/结构/联合 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 29
错误 13 error C2143: 语法错误 : 缺少“,”(在“;”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 29
错误 14 error C2143: 语法错误 : 缺少“)”(在“}”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 30
错误 15 error C2143: 语法错误 : 缺少“;”(在“)”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 30
错误 16 error C2062: 意外的类型“unsigned int” c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 31
错误 17 error C2143: 语法错误 : 缺少“;”(在“{”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 32
错误 18 error C2447: “{”: 缺少函数标题(是否是老式的形式表?) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 32
错误 19 error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 43
错误 20 error C2143: 语法错误 : 缺少“,”(在“&”的前面) c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 43
错误 21 error C2065: “t1”: 未声明的标识符 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 45
错误 22 error C2228: “.milTime”的左边必须有类/结构/联合 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 45
错误 23 error C2065: “t2”: 未声明的标识符 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 45
错误 24 error C2228: “.milTime”的左边必须有类/结构/联合 c:\users\a\documents\visual studio 2005\projects\adts-119\time\time.cpp 45
测试Time的驱动程序:
// adts-119.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
using namespace std;
#include "Time.h"
int _tmain(int argc, _TCHAR* argv[])
{
Time mealTime,
goToWorkTime;
set(mealTime,5,30,'P');
cout<<"We'll be eating at ";
display(mealTime,cout);
cout<<endl;
set(goToWorkTime,5,30,'P');
cout<<"You leave for work at ";
display(goToWorkTime,cout);
cout<<endl;
if(lessThan(mealTime,goToWorkTime))
cout<<"If you hurry ,you can eat first.\n";
else
cout<<"Sorry you can't eat with us.\n";
advance(goToWorkTime,0,30);
cout<<"You boss called.You go in later at ";
display(goToWorkTime,cout);
cout<<endl;
if(lessThan(mealTime,goToWorkTime))
cout<<"If you hurry ,you can eat first.\n";
else
cout<<"Sorry you can't eat with us .\n";
cout<<endl;
return 0;
}