Código
#include <iostream> using namespace std; class Reloj{ private: int time; bool correctSum(int t); bool correctSub(int t); public: Reloj(); void subHoras(int h); void subMin(int m); void subSeg(int s); void addHoras(int h); void addMin(int m); void addSeg(int s); void printReloj(); }; Reloj::Reloj(){ time = 0; } void Reloj::addHoras(int h){ if(!correctSum(h*3600)) cout << "No se ha podido realizar esta operacion con las horas" << endl; else time += h*3600; } void Reloj::addMin(int m){ if(!correctSum(m*60)) cout << "No se ha podido realizar esta operacion con los minutos" << endl; time += m*60; } void Reloj::addSeg(int s){ if(!correctSum(s)) cout << "No se ha podido realizar esta operacion con los segundos" << endl; time += s; } void Reloj::subHoras(int h){ if(!correctSub(h*3600)) cout << "No se ha podido realizar esta operacion con las horas" << endl; else time -= h*3600; } void Reloj::subMin(int m){ if(!correctSub(m*60)) cout << "No se ha podido realizar esta operacion con los minutos" << endl; time -= m*60; } void Reloj::subSeg(int s){ if(!correctSub(s)) cout << "No se ha podido realizar esta operacion con los segundos" << endl; time -= s; } void Reloj::printReloj(){ int h = time/3600; int min = (time%3600)/60; int seg = (time%3600)%60; cout << h << ":" << min << ":" << seg << endl; } bool Reloj::correctSum(int t){ if((time + t) >= 3600*24) return false; return true; } bool Reloj::correctSub(int t){ if(time - t < 0) return false; return true; } int main(){ Reloj reloj; cout << "Introduce la hora inicial del reloj en formato HH:MM:SS" << endl; int h,m,s; char c; cin >> h >> c >> m >> c >> s; reloj.addHoras(h); reloj.addMin(m); reloj.addSeg(s); int op; cout << "1->Adelantar reloj 2->Retrasar reloj 3->Ver hora -1-> Salir" << endl; cin >> op; while(op != -1){ switch(op){ case 1: cout << "Introduce el tiempo que quieres adelantar en el siguiente formato HH:MM:SS" << endl; cin >> h >> c >> m >> c >> s; reloj.addHoras(h); reloj.addMin(m); reloj.addSeg(s); cout << "Hora:" << endl; reloj.printReloj(); break; case 2: cout << "Introduce el tiempo que quieres retrasar en el siguiente formato HH:MM:SS" << endl; cin >> h >> c >> m >> c >> s; reloj.subHoras(h); reloj.subMin(m); reloj.subSeg(s); cout << "Hora:" << endl; reloj.printReloj(); break; case 3: cout << "Hora:" << endl; reloj.printReloj(); break; default: cout << "Opcion no valida" << endl; } cin >> op; } }