你能解释下划线代码中发生了什么吗

你能解释下划线代码中发生了什么吗

本文介绍了你能解释下划线代码中发生了什么吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,用于将给定秒数转换为[HH:MM:SS]格式。你可以解释下划线代码中发生了什么吗?



我尝试过:



I have a code for converting given seconds to the [HH:MM:SS] format. Could you Please explain what is happening in the underlined code?

What I have tried:

#include <iostream>
using namespace std;

int  main()
{

	int t,h, m, s;


	cout << "Enter a time in seconds: ";
	cin >> t;
	h = t/3600;
	t = t%3600;
	m = t/60;
	t = t%60;
	s = t;

	cout<<endl;cout<<endl;cout<<endl;

	cout<<"The time in HH:MM:SS format is: "<<h<<" hours, "
		<<m<<" minutes, and "<<s<<" seconds!";
cout<<endl;cout<<endl;cout<<endl;
	return 0;
}

推荐答案

Take a simple arithmetic problem: what's left over when you divide 11 by 3? The answer is easy to compute: divide 11 by 3 and take the remainder: 2. But how would you compute this in a programming language like C or C++? It's not hard to come up with a formula, but the language provides a built-in mechanism, the modulus operator ('%'), that computes the remainder that results from performing integer division.



假设t = 3602,



h = t / 3600; // h = 1因为整数



t = t%3600; // t = 3602 - 3600 = 2;



等...


Assuming t = 3602,

h = t/3600; // h = 1 because of integer

t = t%3600; // t = 3602 - 3600 = 2;

and so on ...



Quote:

我有一个代码,用于将给定秒数转换为[HH:MM:SS]格式。你可以解释下划线代码中发生了什么吗?

I have a code for converting given seconds to the [HH:MM:SS] format. Could you Please explain what is happening in the underlined code?



看看用调试器执行的代码怎么样?

读它不会太早语言文档也是。



你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与之比较它应该做什么。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向你展示什么是帮助你继续当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

]

[]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


What about watching the code execute with the debugger ?
It is never too soon to read the language documentation too.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于你能解释下划线代码中发生了什么吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:39