0
Two commented lines(with "?" mark) are confusing
int hour,min,sec; sec=3721; hour=sec/3600; min=sec%3600/60; /*......?????......*/ sec=sec%3600%60; /*........?????.....*/ cout<<hour<<":"; cout<<min<<":"<<sec;
4 Réponses
+ 2
1. When there are more operators involved in an expression, we refer their precedence (priority)
https://en.cppreference.com/w/c/language/operator_precedence
2. Multiplication, division and modulo are all left associative operators. However the assignment operation is right associative. You can see associatvity in the same page linked above.
3. If modulo was not involved it is possible. Modulo (remainder from division) in C/C++ language only works with integer family.
+ 3
Let's break it down..
sec = 3721
=====================
hour = 3721 / 3600
Yelds 1, as the hour. Remember integer division doesn't consider fractal
=====================
min = 3721 % 3600 / 60
3721 % 3600 yields 121, remainder from division of 3721 by 3600.
121 is then divided by 60 which yields 2. This is the minute.
=====================
sec = 3721 % 3600 % 60
3721 % 3600 yields 121, remainder from division of 3721 by 3600.
121 is then modulo by 60 which yields 1, remainder from division of 121 by 60. This is the second.
+ 1
I really appreciate u.Thanks
0
Three more questions:
1)if there is more operator(e.g:PEMDAS or BODMAS) when to solve modulo(%)?
2)Can't we solve the expression(sec=sec%3600%60) from right to left?
3) Can't we use float or double here?