468,720 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Very strange problem with a operator "-"

I was trying to solve: acm.uva.es/p/v101/10191.html

I did this code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

bool digit(char c)
{
return isdigit(c);
}

struct appointment
{
int h1; // beginning hour of app.
int m1; // beggining minute of app.
int h2; // ending hour of app.
int m2; // ending minute of app.
};

// compare appointments by their beggining hour
bool lt(appointment a1,appointment a2)
{
return (a1.h1)<(a2.h1);
}

int main()
{
string n; // number of appointments in a day
while(getline(cin,n))
{
vector<appointment>agenda;
int app = atoi(n.c_str());
for(int times=1; times<=app; ++times)
{
string line;
getline(cin,line);
// appointments are given in format h1:m1 h2:m2 blah, blah

appointment aux;
aux.h1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':' )+1);
aux.m1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(' ')+1);
aux.h2 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':' )+1);
aux.m2 = atoi(line.c_str());
agenda.push_back(aux);
}
sort(agenda.begin(),agenda.end(),lt);
// all appointments begin at 10:00 and end at 18:00
// time elapsed between 10:00 and first appointment
cout << agenda.front().h1*60+agenda.front().m1 - 600 << endl;
for(int i=0; i<agenda.size()-1; ++i)
{
// time elapsed between i-th and i+1-th appointments
cout <<
agenda[i+1].h1*60+agenda[i+1].m1)-(agenda[i].h2*60+agenda[i].m2)
<< endl;
}
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
}
return 0;
}

My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!

What's more strange is if I do:

// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;

It prints the correct negative number!

What's happening, here?!

Thanks.

Jun 18 '06 #1
6 1501
Gaijinco wrote:

My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!


The value of 1080 - 17*60 + 45 is not the same as the value of 1080 -
(17*60 + 45).

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 18 '06 #2
[snip]

My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!
Well, if you are looking for the difference between the two times, why
don't you just subtract and take the absolute value?

diff(a, b) = |a-b| = |b-a| //math, not C++

What's more strange is if I do:

// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;

It prints the correct negative number!
This time you did the subtraction right, but you forgot to take the
absolute value.

What's happening, here?!

Thanks.


Ben
Jun 18 '06 #3

"Gaijinco" <ga******@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...
I was trying to solve: acm.uva.es/p/v101/10191.html

I did this code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;

bool digit(char c)
{
return isdigit(c);
}

struct appointment
{
int h1; // beginning hour of app.
int m1; // beggining minute of app.
int h2; // ending hour of app.
int m2; // ending minute of app.
};

// compare appointments by their beggining hour
bool lt(appointment a1,appointment a2)
{
return (a1.h1)<(a2.h1);
}

int main()
{
string n; // number of appointments in a day
while(getline(cin,n))
{
vector<appointment>agenda;
int app = atoi(n.c_str());
for(int times=1; times<=app; ++times)
{
string line;
getline(cin,line);
// appointments are given in format h1:m1 h2:m2 blah, blah

appointment aux;
aux.h1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':' )+1);
aux.m1 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(' ')+1);
aux.h2 = atoi(line.c_str());
line.erase(line.begin(),line.begin()+line.find(':' )+1);
aux.m2 = atoi(line.c_str());
agenda.push_back(aux);
}
sort(agenda.begin(),agenda.end(),lt);
// all appointments begin at 10:00 and end at 18:00
// time elapsed between 10:00 and first appointment
cout << agenda.front().h1*60+agenda.front().m1 - 600 << endl;
for(int i=0; i<agenda.size()-1; ++i)
{
// time elapsed between i-th and i+1-th appointments
cout <<
agenda[i+1].h1*60+agenda[i+1].m1)-(agenda[i].h2*60+agenda[i].m2)
<< endl;
}
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;
This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )

Is that what you wanted? If not, put parenthesis around your math to make
it evaluate in the correct order.
}
return 0;
}

My problem is with line:
// time elapsed between last appointment and 18:00
cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;

When the last input for the day is
XX:XX 17:45
It prints 105! As a matter of fact unless the last time is 18:00 it
always prints a wrong number!

What's more strange is if I do:

// time elapsed between last appointment and 18:00
cout << agenda.back().h2*60+agenda.back().m2-1080 << endl << endl;
Now this is:

((agenda.back().h2 * 60 ) + agenda.back().m2 ) - 1080

It prints the correct negative number!

What's happening, here?!

Thanks.

Jun 19 '06 #4
Jim Langston <ta*******@rocketmail.com> wrote:
"Gaijinco" <ga******@gmail.com> wrote in message

cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;


This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )


Really? I thought + and - had the same precedence and
group left to right.

I could be confused though.

Steve
Jun 19 '06 #5
"Steve Pope" <sp*****@speedymail.org> wrote in message
news:e7**********@blue.rahul.net...
Jim Langston <ta*******@rocketmail.com> wrote:
"Gaijinco" <ga******@gmail.com> wrote in message

cout << 1080-agenda.back().h2*60+agenda.back().m2<< endl << endl;


This is: 1080 - (( agenda.back().h2 * 60 ) + agenda.back().m2 )


Really? I thought + and - had the same precedence and
group left to right.

I could be confused though.

Steve


Hmm.. true, I probably have that wrong. Which goes to prove the point, if
you want to be sure, use ()'s
Jun 20 '06 #6
Jim Langston wrote:

Hmm.. true, I probably have that wrong. Which goes to prove the point, if
you want to be sure, use ()'s


If you want to be sure, pay attention to the details. Stuffing in
parentheses is a sign of laziness.

--

Pete Becker
Roundhouse Consulting, Ltd.
Jun 20 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ben | last post: by
2 posts views Thread by Jason Heyes | last post: by
4 posts views Thread by Prawit Chaivong | last post: by
7 posts views Thread by dam_fool_2003 | last post: by
6 posts views Thread by m_a_t_t | last post: by
4 posts views Thread by Harlan Messinger | last post: by
55 posts views Thread by Ennixo | last post: by
5 posts views Thread by paulo | last post: by
126 posts views Thread by jacob navia | last post: by
reply views Thread by Alexandru Palade | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.