⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pr1003.cpp

📁 practice c++, it is from the book http://www.amazon.com/Schaums-Outline-Programming-John-Hubbard
💻 CPP
字号:
//  Programming with C++, Second Edition, by John R. Hubbard
//  Copyright McGraw-Hill, 2000
//  Problem 10.3 on page 249
//  A Time class

#include <cmath>
#include <iostream>
using namespace std;

class Time
{ public:
	  Time(int h=0, int m=0, int s=0)
	      : hr(h), min(m), sec(s) { normalize(); }
	  int hours() { return hr; }	
	  int minutes() { return min; }
	  int seconds() { return sec; }
	  void advance(int =0, int =0, int =1);
	  void reset(int =0, int =0, int =0);
	  void print() { cout << hr << ":" << min << ":" << sec; }
  private:
	  int hr, min, sec;
	  void normalize();
};

int main()
{ Time t(14,58,03);
  t.print();
  cout << endl;
  t.advance();
  t.print();
  cout << endl;
  t.advance(3,5,2);
  t.print();
  cout << endl;
}

void Time::normalize()
{ min += sec/60;
  hr += min/60;
  hr %= 24;
  min %= 60;
  sec %= 60;
}

void Time::advance(int h, int m, int s)
{ hr += h;
  min += m;
  sec += s;
  normalize();
}

void Time::reset(int h, int m, int s)
{ hr = h;
  min = m;
  sec = s;
  normalize();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -