Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Программирование и базы данных (http://forum.oszone.net/forumdisplay.php?f=21)
-   -   Конвертация времени (http://forum.oszone.net/showthread.php?t=249170)

SAU-trade 14-12-2012 23:34 2045520

Конвертация времени
 
Здравствуйте!
Задание:
Создайте класс Время, в котором реализованы операции сложения, вычитания, сравнения, ввода и вывод на экран. Возможность конвертации времени из американского формата am (pm): 10:00 pm = 22:00, 12:00 pm =00:00
Все сделал кроме конвертации... Помогите реализовать данную функцию..


Код:

//main
#include <iostream>
#include "time.h"

using namespace std;
 
int main(){
       
        Time A (13, 50);
        cout <<A;
        Time B (12, 45);
        cout << B;
        Time C = A + B;
        cout << C;
        C = A - B;
        cout << C;
        C = B - A;
        cout << C;
        cout << (A == B);
        cin >> C;
        cout << C;
}

Код:

//time.h

#include <iostream>

using namespace std;

class Time{
private:
        int hour;
        int minute;

public:
        Time(int, int);
        Time operator + (Time);
        Time operator - (Time);
        bool operator == (Time);
        friend istream& operator >> (istream&, Time&);
        friend ostream& operator << (ostream&, const Time&);
        void Convert();

};

Код:

//time.cpp

#include <iostream>
#include "time.h"

Time::Time(int h, int m){
       
        hour = h;
        minute = m;
}

Time Time::operator+(Time tm){
       
        Time t(this->hour, this->minute);
        t.hour += tm.hour;
        t.minute += tm.minute;
        if (t.minute >= 60){
               
                do{
                        t.minute -= 60;
                        t.hour += 1;
                }while(t.minute >= 60);
        }
        if(t.hour >= 24){
               
                t.hour = t.hour%24;
        }
        return t;
}

Time Time::operator-(Time tm){
       
        Time t(this->hour, this->minute);
        t.hour -= tm.hour;
        t.minute -= tm.minute;
        if(t.minute < 0){
               
                t.minute = t.minute + 60;
                t.hour -= 1;
        }
        if(t.hour < 0){
               
                t.hour = 0 - t.hour;
        }
        return t;
}

bool Time::operator==(Time t){
       
        if (this->hour == t.hour && this->minute == t.minute)
                return true;
        else
                return false;
}

istream& operator >> (istream &input, Time &t){

        cout << "Enter hour: ";
        input >> t.hour;
        cout << "Enter minute: ";
        input >> t.minute;
        return input;
}

ostream& operator << (ostream &output, const Time &t){

        output.width(2);
        output.fill('0');
        output << t.hour;
        output << ":";
        output.width(2);
        output.fill('0');
        output << t.minute;
        output << endl;
        return output;
}

void Time::Convert(){

}


Delirium 15-12-2012 02:24 2045566

А в чем сложность? Если PM - прибавляем 12, если AM - оставляем как есть. Также проверяем, если PM+12=24, то выводим 00:00. И вся конвертация.


Время: 17:43.

Время: 17:43.
© OSzone.net 2001-