5 Replies - 402 Views - Last Post: 06 July 2013 - 04:04 AM Rate Topic: -----

#1 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

How to multiply and divide times of the day like 10:00 and 5:00?

Posted 02 July 2013 - 03:37 PM

I need to multiply and divide times of the day that the user inputs e.g. 10:00 and 5:00. I have tried everything I can think of, what am I supposed to put in the MyTime.cc file in the overloaded * and / operators to get it to work correct? As you can see I have already converted the first time to all minutes but I don't know what to do or write next.

//main.cc File
#include <iostream>
#include "MyTime.h"

int main()
{
   MyTime t1, t2;
   int counter = 0;

   while(counter < 2){
   std::cout << "Enter a time:  ";
   std::cin >> t1;

   std::cout << "Enter another time:  ";
   std::cin >> t2;

   if(t1 == t2)
     std::cout << t1 << " is equal to " << t2 << std::endl;

   if(t1 < t2)
     std::cout << t1 << " is less than " << t2 << std::endl;

   if(t1 <= t2)
     std::cout << t1 << " is less than or equal to " << t2 << std::endl;

   std::cout << t1 << " + " << t2 << " = " << t1 + t2 << std::endl;
   std::cout << t1 << " - " << t2 << " = " << t1 - t2 << std::endl;
   std::cout << t1 << " * " << t2 << " = " << t1 * t2 << std::endl;
   std::cout << t1 << " / " << t2 << " = " << t1 / t2 << std::endl;

   counter++;
   }

   return 0;
}


---------------------------------------------------------------------------------------------
//MyTime.h File
#include <iostream>

class MyTime
{
  public:

    MyTime(int h = 0, int m = 0);

    void Reset(int h, int m);

    void input();

    void output() const;

    MyTime operator + (const MyTime& t1) const;    

    MyTime operator - (const MyTime& t1) const;

    MyTime operator * (const MyTime& t1);

    MyTime operator / (const MyTime& t1);

    bool operator == (const MyTime& t1) const;

    bool operator < (const MyTime& t1) const;

    bool operator <= (const MyTime& t1) const;

    int get_hours() const{return hours;}
    int get_minutes() const{return minutes;}

  private:
      void simplify();
      int hours;      // hours can be > 24
      int minutes;   // 0 <= minutes <= 59
 };

std::istream& operator >>(std::istream& fin, MyTime& t);

std::ostream& operator <<(std::ostream& fout, const MyTime& t);


-------------------------------------------------------------------------------------------
//MyTime.cc File
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

// Constructor

MyTime::MyTime(int h, int m){
        hours = h;
        minutes = m;
}

void MyTime::Reset(int h, int m){
        hours = h;
        minutes = m;
}

void MyTime::simplify(){
        hours += minutes/60;
        minutes = minutes%60;
}

void MyTime::input(){
        char junk;
        cin >> hours;
        cin.get(junk);
        cin >> minutes;
        simplify();
}

void MyTime::output() const{
        cout << hours << ':' << setw(2) << setfill('0') << minutes;
}

MyTime MyTime::operator +(const MyTime& t1) const{
        MyTime tmp;
        tmp.hours = t1.hours + hours;
        tmp.minutes = t1.minutes + minutes;
        tmp.simplify();
        return tmp;
}

MyTime MyTime::operator -(const MyTime& t1) const{
        MyTime tmp;
        tmp.minutes = abs((t1.hours*60+t1.minutes) -
                                        (hours*60+minutes));
        tmp.simplify();
        return tmp;
}

MyTime MyTime::operator /(const MyTime& t1){
        MyTime tmp;
        tmp.minutes = t1.hours * 60 + t1.minutes;
        tmp.simplify();
        return tmp;
}

MyTime MyTime::operator *(const MyTime& t1){
        MyTime tmp;
        tmp.minutes = t1.hours * 60 + t1.minutes;
        tmp.simplify();
        return tmp;
}

bool MyTime::operator == (const MyTime& t1) const{
        return t1.hours == hours && t1.minutes == minutes;
}

bool MyTime::operator < (const MyTime& t1) const{
        return (t1.hours * 60 + t1.minutes) < (hours * 60 + minutes);
}

bool MyTime::operator <=(const MyTime& t1) const{
        return (t1 == (hours * 60 + minutes)) || (t1 < (hours * 60 + minutes));
}

ostream& operator <<(ostream& fout, const MyTime& t){
        t.output();
        return fout;
}

istream& operator >>(istream& fin, MyTime& t){
        t.input();
        return fin;
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to multiply and divide times of the day like 10:00 and 5:00?

#2 jordannn15  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 215
  • Joined: 06-July 12

Re: How to multiply and divide times of the day like 10:00 and 5:00?

Posted 02 July 2013 - 05:52 PM

Help please.
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: How to multiply and divide times of the day like 10:00 and 5:00?

Posted 02 July 2013 - 07:25 PM

You can find a date and time that is so many minutes after (or before) a certain date and time ...

but perhaps what you wish to do ... ?

is to multiply (or divide) the decremented/incremented (minutes) by a constant?

You may like to see some of the examples here ...

some date and time examples

This post has been edited by David W: 02 July 2013 - 07:27 PM

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,473
  • Joined: 05-May 12

Re: How to multiply and divide times of the day like 10:00 and 5:00?

Posted 02 July 2013 - 08:23 PM

What does it mean to multiply a two times of day together? I'm having a hard time visualizing it. For example, if I multiplied 1:00 AM by 1:00 AM, am I still supposed to get 1:00 AM, or some other time? Or for that matter, what happens when 1:00 AM is multiplied by 1:00 PM, am I supposed to now get 1:00 PM?
Was This Post Helpful? 1
  • +
  • -

#5 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: How to multiply and divide times of the day like 10:00 and 5:00?

Posted 06 July 2013 - 12:11 AM

I think whoever set you this task is trying to get you to manipulate hours and minutes. For example what is the sum of 5 hr 37 min and 3 hr 43 min or what is 4 hr 47 min multiplied by 4. The multiply operator (operator*())and the mod operator(operator%())and the sum operator (operator+())
will be needed. The first and last of these could be the actual function names.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: How to multiply and divide times of the day like 10:00 and 5:00?

Posted 06 July 2013 - 04:04 AM

Forget void simplify(). It is, ironically, making your life less simple. Instead, port the logic over to your gets.

While multiply is a rather bizarre operation for two times, having a single value should make the implementation as obvious at the rest of them. e.g.
class MyTime {
public:
	MyTime(int h = 0, int m = 0);
	MyTime(int m = 0);
	//...
	int get_hours() const { return minutes/60; }
	int get_minutes() const { return minutes%60; }

  private:
	int minutes;
 };



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1