In my programming class, we have tests and quizzes based on code samples that we must walk through and determine the final output. Usually they are tricky pieces of code and by the time I realize, I'm stuck in some random function and have no idea what I'm doing.
在我的编程类中,我们有基于代码示例的测试和测验,我们必须遍历这些测试并确定最终的输出。通常情况下,它们都是复杂的代码片段,当我意识到这一点时,我陷入了一些随机的函数中,不知道自己在做什么。
How do you properly run through code on paper? Keeping track of loops, variables, functions, everything, it's confusing to me.
如何在纸上正确地运行代码?跟踪循环,变量,函数,所有东西,这让我很困惑。
For example, here is a past quiz we had, which I got 100% on but it took me forever and was very messy:
例如,这是我们以前做的一个测验,我100%通过了,但是我花了很长时间,结果非常糟糕:
#include <iostream>
#include <cstring>
using namespace std;
class foo {
char word[20];
int qty;
public:
foo( ) { set(3, 5); }
foo( int m, const char * s) { set(m, m+1);
strcpy(word, s); }
foo( const foo& a ) { cout << "... hahaha.1" << endl;
qty = 3 + a.qty;
strcpy( word, a.word );
strcat( word, ".5.6.7" );
cout << "... hahah.2" << endl; }
~foo( ) { cout << qty << "," << word << "!!!" << endl; }
void set(int a, int b){ qty = a + b;
strcpy( word, "summer" ); }
void wow();
void output(){ cout << word << "," << qty << endl; }
};
void hello( foo& );
void greet( foo );
int main() {
foo x, y(100, "QUIZ");
greet( y );
cout << "a.b.c.d.e." << endl;
hello( x );
x.output();
y.output();
cout << "...the end" << endl;
return 0;
}
void foo::wow() { strcat(word,".1.2.3");
qty += 4; }
void greet( foo g ) { cout << "...HI.1\n";
g.wow();
g.output();
cout << "...HI.2\n"; }
void hello(foo & h) { cout << "...hello.1" << endl;
foo e;
e = h;
h.wow();
h.output();
e.output();
cout << "...hello.2\n"; }
2 个解决方案
#1
0
Practise is the best way to understand code. When i'm trying to do exercise like this, i'm not trying to understand all the functions at the beginning. I'm starting from main and just go like debbuger line by line watching all the variables. If sth is confusing you, just write all variables on paper and mark their every change. However there is no way to learn reading and understanding code better and faster than training.
练习是理解代码的最好方法。当我试着做这样的练习时,我并不是试图在一开始就理解所有的函数。我从main开始,像debbuger一样,逐行观察所有变量。如果某事物让你感到困惑,那就把所有的变量写在纸上,标出它们的每一个变化。然而,没有办法比训练更好更快地学习阅读和理解代码。
#2
0
"Think like a computer"
“像电脑一样思考”
You have a program and data the program is using.
你有一个程序和数据程序正在使用。
You need to know where abouts in the program you are up to. This really needs to be a stack since when you call a method you have to know where to come back to when the method ends.
你需要知道你在程序中的位置。这确实需要一个堆栈,因为当您调用一个方法时,您必须知道在方法结束时返回到哪里。
You need to keep track of each variable - of course variables can be local to a method call, so these are really a stack too, and that is where you have to be really careful to make sure you are changing the right instance of a local variable. Global variables are easy since there is only one copy, so you can put them in your stack, but just down the bottom.
您需要跟踪每个变量——当然变量可以是方法调用的本地变量,因此这些也是一个堆栈,因此您必须非常小心地确保更改了本地变量的正确实例。全局变量很容易,因为只有一个副本,所以您可以将它们放在堆栈中,但只能放在底部。
To work on paper, it's easiest to put the bottom of the stack at the top of the page and grow the stack downwards. Remember that when a method returns, it's local variables no longer exist, so rub them out or mark the top of the stack in some way.
要在纸上工作,最简单的方法是将堆栈的底部放在页面的顶部,并向下扩展堆栈。请记住,当一个方法返回时,它的局部变量将不再存在,所以以某种方式擦掉它们或标记堆栈的顶部。
Things get even more fun if you are doing any multi threading...
如果你做任何多线程,事情会变得更有趣……
#1
0
Practise is the best way to understand code. When i'm trying to do exercise like this, i'm not trying to understand all the functions at the beginning. I'm starting from main and just go like debbuger line by line watching all the variables. If sth is confusing you, just write all variables on paper and mark their every change. However there is no way to learn reading and understanding code better and faster than training.
练习是理解代码的最好方法。当我试着做这样的练习时,我并不是试图在一开始就理解所有的函数。我从main开始,像debbuger一样,逐行观察所有变量。如果某事物让你感到困惑,那就把所有的变量写在纸上,标出它们的每一个变化。然而,没有办法比训练更好更快地学习阅读和理解代码。
#2
0
"Think like a computer"
“像电脑一样思考”
You have a program and data the program is using.
你有一个程序和数据程序正在使用。
You need to know where abouts in the program you are up to. This really needs to be a stack since when you call a method you have to know where to come back to when the method ends.
你需要知道你在程序中的位置。这确实需要一个堆栈,因为当您调用一个方法时,您必须知道在方法结束时返回到哪里。
You need to keep track of each variable - of course variables can be local to a method call, so these are really a stack too, and that is where you have to be really careful to make sure you are changing the right instance of a local variable. Global variables are easy since there is only one copy, so you can put them in your stack, but just down the bottom.
您需要跟踪每个变量——当然变量可以是方法调用的本地变量,因此这些也是一个堆栈,因此您必须非常小心地确保更改了本地变量的正确实例。全局变量很容易,因为只有一个副本,所以您可以将它们放在堆栈中,但只能放在底部。
To work on paper, it's easiest to put the bottom of the stack at the top of the page and grow the stack downwards. Remember that when a method returns, it's local variables no longer exist, so rub them out or mark the top of the stack in some way.
要在纸上工作,最简单的方法是将堆栈的底部放在页面的顶部,并向下扩展堆栈。请记住,当一个方法返回时,它的局部变量将不再存在,所以以某种方式擦掉它们或标记堆栈的顶部。
Things get even more fun if you are doing any multi threading...
如果你做任何多线程,事情会变得更有趣……