如何追踪和理解该计划? [英] How to trace and understand the program?

查看:43
本文介绍了如何追踪和理解该计划?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我看到一个程序时,我真的想知道它是如何工作的。例如

...

Every time I see a program I really want to know how it works. For example
the...

for(x=0; x<=2; x++)



跟踪for循环


tracing of the for loop

x=0 0<=2 TRUE
x=1 1<=2 TRUE
x=2 2<=2 TRUE
x=3 3<=2 FALSE



我发现有些程序难以追踪,即使是我很难理解我的意思是如何确定程序的流程?



这是另一个程序,这次我很难跟踪每个代码程序。

如何使用笔和纸来理解这个程序?

我如何确定编程的结果ram没有使用编译器只有笔和纸?


There are some programs that I find it difficult to trace and how can I trace it even if it is hard to trace what I mean is how to determine the flow of the program?

Here's another program this time it is hard for me to trace each code in the program.
How can I understand this program with the use of pen and paper?
How can I determine the result of the program without using the compiler only pen and paper?

#include<iostream.h>
main()
{
 int a[15]={5,10,15,20,25};
 int x,temp,size;
 size 5;

 for(x=0; x<=size/2; x++)
  {
   temp=a[x];
   a[x]=a[((size-1)-x)];
   a[((size-1)-x)]=temp;
  }
 for(x=0; x<=4; x++)
  {
   cout<<" ELEMENT "<<x<<a[x];
  }
}

推荐答案

你必须成为自己的调试器(而不是然后:记下所有变量并在执行每条指令后更新它们的值。

起初它是一个笨拙的活动,但是在你准备跟随这样一个程序之后(并且能够跳过不必要的更新步骤。)
You have to become yourself a debugger (instead of a compiler).That is: note all the variable and update their values after executing every instruction.
At first it is a clumsy activity, but after while you are going to follow mentally such a procedure (and able to skip unnecessary update steps).


我有一个不同的建议:不要花太多精力去理解别人写的程序。这是编程。尽量不要浪费你的时间。编写代码时就会理解;并且能够很好地阅读代码,至少在大多数初学者中都是如此。实际上,为什么你需要了解其他人的代码是如何工作的?学习编程?这不是最好的方法。通过阅读手册和进行简单的练习,尤其是手册和教科书中的编程练习来学习更好,但不仅仅是这些,最终你需要学习开发整个项目。



当且仅当你对写作充满信心时,你才会开始理解别人写的东西,甚至不会立即出现。对于重要的练习,请尝试了解一年前编写的自己的代码以及更多内容。如果你很难进入它(大多数开发人员遇到这种麻烦,特别是经验较少)。但如果你有这个麻烦,你还没准备好。它会来的。



当然,你可以使用调试器挖掘代码。请注意,需要深入挖掘其他人的代码并不常见,而且不应该这样。



如您所见,我假设您是初学者;你的问题似乎如此。对不起,如果你认为我弄错了。不过,希望我的建议有用。



-SA
I have a different advice: don't use so much effort to understand programs written by other people. This is programming. Try not wasting your time on that. Understanding comes when you write code; and the ability to read code well comes much later, at least in most of the beginners. Indeed, why would you need to understand how someone else's code works? To learn programming? This is not the best way. It's much better to learn by reading just the manuals and doing simple exercises, especially programming exercises from the manuals and textbooks, but not only them, eventually you need to learn to develop whole projects.

When and only when you become really confident in writing, you start understanding something other people write, and even that comes not at once. For important exercises, try to understand your own code written an year ago and more. If you have trouble getting into it (most developers have this trouble, especially with lesser experience). But if you have this trouble, you are not ready yet. It will come.

And of course, you can dig into code using the debuggers. Note that the need to dig into someone else's code does not come often, and it should not.

As you can see, I made an assumption that you are a beginner; it seems so from your question. Sorry if you think I'm mistaken. Still, hope my advice can be useful.

—SA


这篇关于如何追踪和理解该计划?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆