关于 c :如何添加模数? (时:分:秒) | 珊瑚贝

How do I add modulus to this? (hours:minutes:seconds)


这是我第一次访问这个网站,因此,这个问题的格式在某些方面可能是错误的。话虽如此,这是我正在努力的练习。
“编写一个 C 程序,提示用户输入事件的经过时间(以秒为单位)。然后程序以小时、分钟和秒为单位输出经过的时间。(例如,如果经过的时间是 9630 秒,则输出是 2:40:30。)”
这是我到目前为止在 Code::Blocks-

中编写的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
int seconds;
int hours;
int minutes;
int seconds1;
cout <<“How long did the event take in seconds?” << endl;
cin >> seconds;

hours= seconds/3600;
minutes= %(seconds/3600);
seconds1= %(seconds/minutes);
cout <<“The event took”<<hours<<“:”<<minutes<<“:”<<seconds1<<“.” << endl;
cout << endl;

return 0;

}

我的主要问题是如何将模运算添加到该程序中。我知道我需要包含它,因为变量后面有一个明确的余数:小时和分钟。

另外,这个程序通过编译器运行时会出现两个错误代码:

Line 16) error: expected primary-expression before ‘%’ token

Line 17) error: expected primary-expression before ‘%’ token


1
minutes= %(seconds/3600);

mod, % 运算符在赋值语句中的语法是

1
minutes = value1 % value2;

你的表达式中没有左手参数,这里是 value1。

seconds1 部分很简单,您只需将 seconds mod 60。

1
seconds1 = seconds % 60;

分钟部分你需要考虑一下。你可以通过两种方式做到这一点。

  • 您需要减去已经由小时值计算的秒数,然后除以 60。不需要 mod,或者更确切地说是手动 mod。
  • 使用模组。获取小时计算未考虑的剩余小时数,% 3600 然后计算分钟数。
    • 十分感谢你的帮助!


    来源:https://www.codenong.com/36000627/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9581.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?