OK, so how can I code this up? Well, you might think, let's just jump in and write some code, I don't want to quite do that though, because I want to show you another tool that's valuable for thinking about how to structure the code, and that is a something called a flow chart. Now.
好,那么我怎么把这个以代码实现呢,你可能会想,让我们进去直接写代码把,但是我不想这么做,因为我想教给大家另外一项很有价值的,用来组织代码的工具,叫做流程图,现在,像Guttag教授,和我这么大岁数的人。
Suppose I want to do little piece of code that's going to do planar geometry, points in the plane.
好,因此我需要一些,把这些东西收集到,一起的一些方式。
And notice because I have just one line of code inside of each of these branches, what have I clearly omitted?
请注意因为我在每个分支中只有一行代码,显然地我省略了什么?
And the most important thing in the Southern code of honor, I think, safe to say, was reputation.
南方人所谓的荣誉的核心部分,我以为,可以肯定的说,是声誉
I'm not going to do it for you, but what would I think about doing when I change the code?
要改变代码应该做什么呢?,我不会帮你们做这些,但是我在改编代码的时候会想什么呢?
Right now, I have to take that piece of code and replicate it everywhere I want in my larger file.
现在我要把这段代码,复制到,大文件中的各个部分。
I will be posting code that you can play with, and I suggest you go through exactly this kind of exercise.
我给你一些你用的上的代码,我建议你们,多进行一些这种实验。
But there's a key line of code that I lost over a moment ago and that was this one here.
但是我刚才漏了一行关键的代码,就是这里的这一行。
Having now used this pseudo code to line things up, I can start putting some additional structure on this.
用伪代码把这些事情串起来后,我可以开始进行,一些结构方面的优化了。
Because I might have written some code in which I'm expecting that integer particular variable to have an integer value.
因为我可能写了一些代码,来把一些,变量的类型,指定为。
That is if I can write code that would solve all instances of smaller size strings, then what I just described isPalindrome will solve the larger size one.
然后我会描述如何处理大字符串,事实上这正是我所做的,我希望你能看看这边的这段代码,就是这个。
I want the function, which is a little tool, a line of code I can include in my own programs and generally those tools live in sections two or three of the manual.
我想要一个函数,那是一个小工具,在我程序中可以包含的一行代码,通常这些工具在手册的第二节,或第三节。
Up on the screen, I've got-- I commented it out, but I've got a piece of code that you've seen before, right up here. OK?
我现在在大屏幕上标出来的,是你们以前看过的一段代码,就在这儿?
I want to show you how easy it is to change the code. But, notice, once I've added this I actually have a problem. This is now an under-constrained problem. I have more unknowns than I have equations.
我加上这个条件后,我马上面临一个问题了,这就是现在是一个非约束性问题了,我的未知数比方程数多,你从代数中可以知道。
And for reference, incidentally, in case you ever forget what the point of some exercise was, I'll almost always comment the code up top with a quick sentence or two that reminds you what this program does.
顺便作为参考,为了避免我们可能忘记一些代码的意思,我通常会用一两个简单的句子,在上面作出注释,那样会提醒我们这个程序是干嘛的。
If somewhere later on in the code it shifts to string, I might not be manipulating that and getting actual values out, but not what I wanted, and it's going to be really hard for me to chase it back.
然后后面的一些代码,把变量类型变为了字符串,我可能不会通过实际操作,去取得实际的变量的类型,但是这并不是我想要的类型,而且对我来说把类型,变回去也很困难。
All right so-- yeah, how do I get to the code for that? That's interesting.
噢,我是怎么写出这个的代码,这件事儿挺有意思的。
All right. The fourth thing I'm going to do, is I'm going to then construct the block of code.
好,我要做的第四件事情就是,我会去写这块循环代码。
But if I did have two lines of code or more, I would in fact need to put those back.
但是如果我的确有两行或更多的代码,我其实应该把花括号加上。
And then what I get to do inside the curly braces is literally line by line enumerate the cases that I want to apply to the following code.
然后我们在花括号中要做的是,逐行地列举接下来我想要,应用的代码。
And we hope that at the end of the class, every time you're confronted with some technical problem, one of your first instincts is going to be, "How do I write the piece of code that's going to help me solve that?"
并且我们希望到了期末,每当你要面对一些,技术问题的时候,你们的本能之一将会是:,“我怎么才能编写一个代码,来帮我解决这问题?“
I'm thinking about a problem, I'm going to show you an example in a second, first thing I'm going to do is say, what is the thing that's going to change every time I run through the same set of code?
我要解决一个问题,我马上会让大家看一个例子,第一件我要做的事情就是,弄明白每次运行同样的指令集时,每次都会改变的东西是什么?
What does that say? It says if in the code up here I get an exception of that sort, I'm going to go to this place to handle it.
这意味着如果在代码中,我得到一个这样的异常,我能到这里来处理它。
And I want to remind you, that block of code is a set of instructions, the same set of instructions that are going to be done each time through the loop.
我想提醒大家,这块代码是一个指令集,一个每次循环都会被执行的相同指令集,会变化的仅仅是变量的值。
Which is a way of reminding you that I need to think as I write the code about what are my expectations from the input, and how might I enforce them.
通过这个我在提醒大家,在写代码的时候,要搞清楚对输入的期望,并思考下如何执行这些期望。
The only thing I've done in this part of the program, now I'm going to go and read the code, is I've gotten the user to input a bunch of data.
现在我去读读这里的代码,是我会让用户输入一些数据,然后根据用户输入的数据。
Now you have a set of parentheses as we'll see int i=0 in actual code and then I have int I equal 0.
你将会在实际的代码中看到一对圆括号,比如,我们让。
Important lesson, so that there would be some predictability in the results and I would have some, if you will, smoke test for knowing whether or not I was getting, my code seemed to be working.
这很重要,因为这样我就可以预测,结果会是多少了,如果你愿意,我们可以做烟气试验,来看我的代码是否工作正常。
And that then let's me get to, basically, base this code. Having done that, height I simply call base with get float, I call height with get float, and do the rest of the work.
现在让我们看看这段代码,其实已经写好了,我简单的调用下,用来得到一个浮点数,再调用下,来得到另外一个浮点数,然后做剩下的工作就可以了。
If I come into this code, I'm going to check this branch first, if it's not true, ah, there's a return at the end of that branch. If it is true, I do that, and then I've got a second test. If it's true, return I return, otherwise a return.
但是如果我们看看这里,如果我看这段程序,我会先检查这一部分,如果它是假的,啊,这部分分支结尾有一个。
应用推荐