Placeholder Image

字幕列表 影片播放

  • >> DAVID J MALAN: All right, so this is CS50.

    >> 大衛·馬蘭:所有 沒錯,所以這是CS50。

  • And this is clearly a Friday.

    而這顯然是一個星期五。

  • And this is the end of week one.

    而這是一個星期的末端。

  • So you may recall that we left off, last time, with a cliffhanger of sorts.

    所以,你可能還記得,我們​​不放過, 最後一次,有各種各樣的懸念。

  • Whereby we expose this lie, that no matter what you've been taught growing

    我們藉以揭露這個謊言,沒有 不管你被教導成長

  • up, 1 divided by 10 is not, in fact, 0.1 or 0.100 or 0.10000.

    向上,1除以10不是在 事實上,0.1或0.100或0.10000。

  • In fact, if we compile this program, as we did yesterday, with,

    事實上,如果我們編譯這個 計劃,因為我們沒有昨天,有,

  • make imprecision, and then do dot, slash, imprecision,

    使不精確性,然後 做點,斜線,不精確,

  • we learned that this is what's 1 divided by 10 actually is.

    我們了解到,這是什麼 1除以10實際上是。

  • So that's not really quite the case.

    所以這不是真的相當的情況下。

  • But this does hint at some fundamental limitation of computers.

    但是,這確實暗示了一些 電腦的基本限制。

  • And indeed, among the things we're going to do

    事實上,中 我們將要做的事情

  • today is take a look at why this has happened, what implications this has,

    今天就來看看為什麼這有 事情發生了,有什麼意義這有,

  • how humanity has failed to grasp this in some very

    人類如何失敗 掌握這一些很

  • serious situations-- the result of the which

    嚴重situations-- 在其結果

  • has been quite tragic and expensive-- and also

    已經相當慘烈 和expensive--也

  • take a look at how we can actually defend

    看看我們如何 實際上可以捍衛

  • against these kinds of limitations.

    針對這些種類的限制。

  • So intuitively, perhaps, why is 1 divided by 10,

    所以直覺,也許, 為什麼是1除以10,

  • according to my computer here, not just 1/10, 0.10?

    根據我的電腦 這裡,不只是的1/10,0.10?

  • Yeah.

    是啊。

  • What do you think?

    你怎麼看?

  • >> AUDIENCE MEMBER: The radix is different?

    >> 觀眾:基數是不同的?

  • >> DAVID J MALAN: The what is?

    >> 大衛·馬蘭:該是什麼?

  • Oh, the radix is different?

    呵呵,基數是不同的?

  • So not quite.

    因此,不完全是。

  • It's actually more fundamental to the hardware.

    它實際上更多 基本的硬件。

  • Other thoughts?

    其他的想法?

  • Yeah.

    是啊。

  • AUDIENCE MEMBER: They represent numbers differently?

    觀眾:他們 表示數字不同?

  • DAVID J MALAN: So they-- Right.

    大衛·馬蘭:所以they--權。

  • They represent numbers differently.

    他們代表的數字不同。

  • Inaccurately, apparently.

    不準確,很明顯。

  • So that is-- Well, differently from what?

    這樣is--好, 不同於什麼?

  • Or from whom?

    或者從誰?

  • From us?

    從我們呢?

  • AUDIENCE MEMBER: Yeah.

    觀眾:是啊。

  • They don't use the decimal system to [INAUDIBLE].

    它們不使用小數 系統[聽不清]。

  • DAVID J MALAN: OK.

    大衛·馬蘭:OK。

  • So in a sense, they don't use the decimal system.

    因此,從某種意義上說,他們不 使用十進制系統。

  • Underneath the hood, everything is, indeed, binary,

    在系統底層, 一切,的確,二進制,

  • and that's related, in fact.

    這就是相關的,其實。

  • But it's even a simpler explanation than that.

    但是,它甚至更簡單 解釋比。

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: They only have so many bits.

    >> 觀眾:他們 只有這麼多的位。

  • They can only store up to a certain extent, like with the decimals.

    他們最多只能存儲一定 某種程度上,像小數。

  • >> DAVID J MALAN: Yeah.

    >> 大衛·馬蘭:是的。

  • This is what's really getting at it.

    這是什麼真正得到它。

  • It turns out that computers typically, will always only

    事實證明,計算機 通常情況下,將永遠只有

  • use a finite amount of information to represent something.

    使用的信息的有限數量 代表什麼。

  • Right?

    對嗎?

  • After all, we only have a fixed amount of hard disk space.

    畢竟,我們只有一個固定的 量的硬盤空間。

  • We only have a fixed amount of RAM, or computer memory.

    我們只有一個固定量 的RAM,或計算機的內存。

  • And if you only have a fixed amount of something,

    如果你只有一個 固定金額的東西,

  • surely you can't actually count up to infinity or any number you want.

    當然你不能真正計數 到無窮大或任何你想要的數字。

  • You, kind of, have to pick and choose what range

    你有種,有 挑選什麼範圍

  • of values you're going to support.

    值你要支持。

  • >> And so for instance, a week or two ago, when we talked about ASCII,

    >> 因此,例如,一兩個星期 以前,當我們談到ASCII,

  • and we talked about 8 bits, or a byte, so to speak,

    我們談了8位, 或字節,可以這麼說,

  • the biggest number we could represent with 8 bits was what?

    最大數我們可以 有8位表示是什麼呢?

  • 255.

    255。

  • And we can represent 256 total values but not if we spend one of them

    我們可以表示256個總價值 但如果我們把其中的一個

  • on 0-- Well, but if we spend one of them on 0,

    在0--好了,但如果我們 花其中的一個0,

  • then the biggest number is, of course, 255.

    那麼最大的數 是,當然,255。

  • >> So it turns out, that this is the case too, in this context.

    >> 所以,事實證明,這是 的情況下也一樣,在這種情況下。

  • We started talking, last time, about floating-point numbers, which

    我們開始談,最後一次, 有關浮點數字,

  • are different from integers, in that they have a decimal point

    來自整數不同,在 他們有一個小數點

  • and, hopefully, some numbers after that, but they are too.

    並希望一些數字 在這之後,但是它們太。

  • A computer is only typically going to use 32 bits, maybe 64 bits

    一台計算機通常只打算 使用32位,也許64位

  • to represent a floating point value.

    來表示一個浮點值。

  • So even though we've grown up learning mathematics and knowing

    因此,即使我們已經長大了 學習數學和了解

  • that you can absolutely have an infinite number of numbers

    你絕對可以有 數的無限數量

  • after the decimal point, not if you have finite memory.

    小數點後不 如果你有有限的內存。

  • You, kind of, have to round, or pick and choose which

    你有種,有圓形, 或者挑選哪些

  • numbers you're going to represent.

    你會數來表示。

  • And so you can think of, in a sense, this being the computer's closest

    所以,你能想到的,從某種意義上說, 這是計算機的最接近

  • approximation to the value 1/10 that it can get, with just 32 or so bits.

    近似​​的值的1/10 它可以得到,只有32左右位。

  • And it's not just 1/10.

    而且它不只是1/10。

  • >> For instance, if I change this to 1/3, which we think is even simpler.

    >> 舉例來說,如果我改變這 1/3,我們認為這是更簡單。

  • So 1 divided by 3.

    因此,1除以3。

  • Let me save the file.

    讓我保存文件。

  • Let me go ahead and recompile it.

    讓我繼續前進,重新編譯它。

  • And let me re-run it.

    讓我重新運行它。

  • Here too, apparently, 1/3 is not 0.3 and then an infinite number of 3's

    在這裡,顯然,三分之一沒有0.3 然後3的無限數量

  • thereafter.

    其後。

  • You have this imprecision at the end.

    你有這樣的不精確性在最後。

  • So we humans are correct, and what you learned is, in fact, correct,

    所以,我們人類是正確的,什麼 你了解,其實,正確的,

  • but we're bumping up against some limitations.

    但是我們撞了 針對一些限制。

  • >> And what I thought we'd do today, is begin

    >> 而我的想法 我們今天會做,是開始

  • by looking at, frankly, the tragic consequences of this

    通過看,坦率地說,在 這種悲慘的後果

  • sometimes, when mankind does not quite implement

    有時,人類的時候 並不完全實現

  • for this reality and these limitations.

    針對這一現實,這些限制。

  • And we'll see a series of vignettes from the History Channel that takes

    而且我們會看到一系列的小插圖 從歷史頻道的需要

  • a look at how things have gone wrong.

    一起來看看如何事情已經出了問題。

  • It's about 8 minutes long, and we'll come back after this

    這是約8分鐘之久, 我們將在這之後回來

  • and take a look at exactly what else can go wrong.

    看一看在完全相同 還有什麼可能出錯。

  • If we could dim the lights--

    如果我們能昏暗的lights--

  • >> [VIDEO PLAYBACK]

    >> [視頻回放]

  • >> -Computers, we've all come to accept the often frustrating problems that

    >> -Computers,我們都逐漸接受 經常令人沮喪的問題,

  • go with them.

    和他們一起去。

  • Bugs, viruses, and software glitches are small prices

    錯誤,病毒,和軟件 故障都是小的價格

  • to pay for the convenience.

    支付的便利。

  • But in high-tech and high-speed military and space program applications,

    但是,在高科技和高速 軍事和航天計劃的應用,

  • the smallest problem can be magnified into disaster.

    可以最小的問題 被放大成災難。

  • On June 4, 1996, scientists prepared to launch an unmanned Ariane 5 rocket.

    1996年6月4日,科學家準備 啟動一個無人阿麗亞娜5型火箭。

  • It was carrying scientific satellites designed

    它攜帶的科學 衛星設計

  • to establish precisely how the Earth's magnetic field interacts

    建立精確如何 地球的磁場相互作用

  • with solar winds.

    與太陽風。

  • The rocket was built for the European Space Agency

    火箭是專為 歐洲航天局

  • and lifted off from its facility on the coast of French Guiana.

    並從其工廠升空 在法屬圭亞那的海岸。

  • >> -And about 30 seconds into the flight, they first

    >> - 和大約30秒後進入 飛行中,他們首先

  • noticed something was going wrong.

    發現了一些打算錯了。

  • That the nozzles were swiveling in a way they really shouldn't.

    使噴嘴被旋轉 在某種程度上,他們真的不應該。

  • Around 40 seconds into the flight, clearly the vehicle was in trouble,

    約40秒後進入飛行, 明確了車輛遇到了麻煩,

  • and that's when they made the decision to destroy it.

    而當他們提出的 決定摧毀它。

  • A Range Safety Officer with tremendous guts pressed the button,

    隨著一系列安全主任 巨大的勇氣按下按鈕,

  • blew up the rocket before it could become a hazard to public safety.

    炸毀了火箭之前,它可能 成為危害公眾安全。

  • >> -This was the maiden voyage of the Ariane 5,

    >> - 這是處女 阿麗亞娜5的航程,

  • and its destruction took place because of a flaw

    它破壞了 地方,因為一個缺陷

  • embedded in the rocket's software.

    嵌在火箭的軟件。

  • -The problem on the Ariane was that there

    在-The問題 阿麗亞娜是,有

  • was a number that required 64 bits to express,

    是一個數字,需要 64位表示,

  • and he wanted to convert it to a 16 bit number.

    他想轉換 它以一個16位的數。

  • They assumed that the number was never going to be very big,

    他們假定數 從來沒有將是非常大的,

  • that most of those digits in the 64 bit number were 0's.

    大多數的這些數字中 64位數字是0。

  • They were wrong.

    但他們錯了。

  • >> -The inability of one software program to accept

    >> 一-The無力 軟件程序接受

  • the kind of number generated by another was at the root of the failure.

    所產生的那種數 另一種是在失敗的根源。

  • Software development had become a very costly part of new technology.

    軟件開發已經成為一個 的新技術非常昂貴的部分。

  • The Ariane 4 rocket had been very successful, so much of the software

    阿麗亞娜-4火箭一直非常 成功的,那麼多的軟件

  • created for it was also used on the Ariane 5.

    創造這也是 用在阿麗亞娜5。

  • >> -The basic problem was that the Ariane 5 was faster, accelerated faster.

    >> -The基本的問題是,阿麗亞娜 5增快,加速快。

  • And the software hadn't accounted for that.

    而且軟件早已不是 佔了這一點。

  • >> -The destruction of the rocket was a huge financial disaster,

    >> 火箭-The破壞 是一個巨大的金融災難,

  • all due to a minute software error.

    全因一分鐘,軟件錯誤。

  • But this wasn't the first time data conversion problems

    但是,這是不是第一次 實時數據轉換問題

  • had plagued modern rocket technology.

    一直困擾現代火箭技術。

  • >> -In 1991, with the start of the First Gulf War,

    >> -in 1991,在開始 第一次海灣戰爭中,

  • the Patriot missile experienced a similar kind

    愛國者導彈 經歷了類似的一種

  • of number conversion problem.

    數轉換的問題。

  • As a result, 28 people, 28 American soldiers

    其結果是,28人, 28美國士兵

  • were killed and about 100 others wounded,

    被打死,約 100人受傷,

  • when the Patriot, which was supposed to protect against incoming scuds,

    當愛國者,這是應該 以防止傳入的飛毛腿導彈,

  • failed to fire a missile.

    沒有發射導彈。

  • >> -When Iraq invaded Kuwait and America launched Desert Storm in early 1991,

    >> - 當伊拉克入侵科威特和美國 在1991年年初推出沙漠風暴,

  • Patriot missile batteries were deployed to protect Saudi Arabia and Israel

    愛國者導彈部署 保護沙特阿拉伯和以色列

  • from Iraqi Scud missile attacks.

    從伊拉克飛毛腿導彈攻擊。

  • The Patriot is a US, medium-range surface-to-air system

    愛國者是美國,中程 表面對空氣系統

  • manufactured by the Raytheon company.

    由雷神公司製造。

  • >> -The size of the Patriot interceptor, itself, is about, roughly 20 feet long.

    >> 愛國者攔截-The大小, 本身就是約,約20英尺長。

  • And it weighs about 2000 pounds.

    而且它的重量約2000磅。

  • And it carries a warhead of about-- I think it's roughly 150 pounds.

    同時還可以進行about--的彈頭 我認為這是約150磅。

  • And the warhead, itself, is a high explosive,

    和彈頭本身, 是一種高爆炸藥,

  • which has fragments around it.

    它有它周圍的碎片。

  • The casing of the warhead is designed to act like buckshot.

    彈頭的外殼是 設計像鹿彈。

  • >> -The missiles are carried, 4 per container,

    >> -The導彈 進行,4%的集裝箱,

  • and are transported by a semi-trailer.

    並通過半拖車被運送。

  • >> -The Patriot anti missile system goes back at least 20 years now.

    >> -The愛國者反導彈系統 追溯到現在至少需要20年。

  • It was originally designed as an air defense missile

    它最初的設計 作為防空導彈

  • to shoot down enemy airplanes.

    擊落敵人的飛機。

  • In the First Gulf War, when that war came along,

    在第一次海灣戰爭, 當戰爭來了,

  • the Army wanted to use it to shoot down scuds, not airplanes.

    陸軍希望用它來 擊落飛毛腿導彈,而不是飛機。

  • The Iraqi Force was not so much of a problem,

    伊拉克隊還是沒有 這麼多的問題,

  • but the Army was worried about scuds.

    但陸軍擔心飛毛腿導彈。

  • And so they tried to upgrade the Patriot.

    因此,他們試圖 升級愛國者。

  • >> -Intercepting an enemy missile traveling at mach five

    >> -Intercepting敵人 導彈行駛在馬赫5

  • was going to be challenging enough.

    將要進行足夠的挑戰。

  • But when the Patriot was rushed into service,

    但是愛國者的時候 被衝進服務,

  • the Army was not aware of an Iraqi modification that made

    陸軍不知道的 ,使得伊拉克修改

  • their scuds nearly impossible to hit.

    他們的飛毛腿導彈幾乎是不可能的打擊。

  • >> -What happened, is the scuds that were coming in, were unstable,

    >> - 什麼事,是飛毛腿的 要來的,是不穩定的,

  • they were wobbling.

    他們搖晃。

  • The reason for this, was the Iraqis, in order

    這樣做的原因, 是伊拉克人,為了

  • to get 600 kilometers out of a 300 kilometer range missile,

    讓600公里出 300公里的射程導彈,

  • took weight out of the front warhead and made the warhead lighter.

    把重出前彈頭 並提出彈頭更輕。

  • So now, the Patriot's trying to come at the scud, and most of the time,

    所以,現在,愛國者的苦思 在飛毛腿,並且大多數時候,

  • the overwhelming majority of the time, it would just fly by the scud.

    絕大多數的 時間,它只是由飛毛腿飛。

  • >> -Once the Patriot system operators realized the Patriot missed its target,

    >> - 一旦愛國者系統運營商 實現了愛國者偏離目標,

  • they detonated the Patriot's warhead, to avoid possible casualties if it

    他們引爆了愛國者彈頭, 為了避免可能的人員傷亡,如果它

  • was allowed to fall to the ground.

    被允許倒在地上。

  • >> -That was what most people saw, those big fireballs in the sky,

    >> - 那是大多數人所看到的, 那些大火球在天空中,

  • and misunderstood as intercepts of Scud warheads.

    和誤解為 飛毛腿彈頭的攔截。

  • Although in the night skies, Patriots appeared

    雖然在夜間 天空,愛國者亮相

  • to be successfully destroying scuds, at Dhahran,

    要成功地 摧毀飛毛腿導彈,在達蘭,

  • there could be no mistake about its performance.

    有可能是沒有錯 關於它的性能。

  • There, the Patriot's radar system lost track of an incoming Scud,

    在那裡,愛國者的雷達系統 失去了進入的飛毛腿的軌道,

  • and never launched, due to a software flaw.

    永不推出, 由於軟件缺陷。

  • It was the Israelis who first discovered that the longer the system was on,

    這是誰首先發現了以色列人 該系統不再是上,

  • the greater the time discrepancy became, due to a clock embedded

    越大時間差異 成為,由於一個時鐘的嵌入式

  • in the system's computer.

    在系統的計算機。

  • >> -About 2 weeks before the tragedy in Dhahran,

    >> - 關於前2週 悲劇發生在宰赫蘭,

  • the Israelis reported to the Defense Department,

    以色列人報 國防部,

  • that the system was losing time, that after about 8 hours of running,

    該系統是浪費時間, 該後約8小時的運行,

  • they noticed that the system was becoming noticeably less accurate.

    他們注意到,該系統 變得明顯不太準確。

  • The Defense Department responded by telling all of the Patriot batteries

    國防部回應 告訴所有的愛國者電池

  • to not leave the systems on for a long time.

    不離開系統 上很長一段時間。

  • They never said what a long time was.

    他們從來不說什麼很長一段時間了。

  • 8 hours?

    8小時?

  • 10 hours?

    10小時?

  • 1000 hours?

    千小時?

  • Nobody knew.

    沒有人知道。

  • -The Patriot battery stationed at the barracks

    -The愛國者電池 駐紮在軍營

  • at Dhahran, and its flawed internal clock, had been on over 100 hours

    在宰赫蘭的,其有缺陷的內部 時鐘,已經在超過100小時

  • on the night of February 25.

    2月25日的晚上。

  • >> -It tracked time to an accuracy of about 1/10 of a second.

    >> - 它跟踪時間精度 約一秒的1/10。

  • Now 1/10 of a second is an interesting number

    現在1/10秒是 一個有趣的數字

  • because it can't be expressed in binary, exactly.

    因為它不能 按二進制,沒錯。

  • Which means, it can't be expressed, exactly,

    這意味著,它不能 表示,準確地說,

  • in any modern digital computer.

    在任何現代數字計算機。

  • It's hard to believe, but use this as an example.

    很難相信,但 使用這個作為一個例子。

  • Let's take the number, 1/3.

    讓我們來數的1/3。

  • 1/3 cannot be expressed in decimal, exactly.

    三分之一不能表示 十進制,沒錯。

  • 1/3 is 0.333 going on for infinity.

    1/3是0.333持續了無窮大。

  • There's no way to do that, with absolute accuracy, in decimal.

    有沒有辦法做到這一點,有 絕對精度,十進制。

  • That's exactly the same kind of problem that happened in the Patriot.

    這正是相同種類的 問題發生在愛國者。

  • The longer the system ran, the worse the time error became.

    該系統運行更長的 更糟糕的時間誤差成了。

  • >> -After 100 hours of operation, the error in time was only about 1/3 of a second.

    >> -After運行100小時,誤差 在時間只有一秒鐘的約1/3。

  • But in terms of targeting a missile traveling at mach 5,

    但是,在針對一個方面 導彈5馬赫旅行,

  • it resulted in a tracking error of over 600 meters.

    它導致了跟踪 對600多萬米的錯誤。

  • It would be a fatal error for the soldiers at Dhahran.

    這將是一個致命的錯誤 對於戰士在宰赫蘭。

  • >> -What happened, is a Scud launch was detected by early-warning satellites,

    >> - 什麼事,是飛毛腿發射是 通過預警衛星探測,

  • and they new that the Scud was coming in their general direction.

    他們新的飛毛腿是 即將在他們的大方向。

  • They didn't know where it was coming.

    他們不知道有人來了。

  • >> -It was now up to the radar component of the Patriot system,

    >> 現在 - 它要由雷達 愛國者系統的組成部分,

  • defending Dhahran, to locate and keep track of the incoming enemy missile.

    保衛達蘭,定位和保持 跟踪來襲的敵方導彈。

  • >> -The radar was very smart.

    >> -The雷達是非常聰明的。

  • It would actually track the position of the Scud

    它實際上跟踪 飛毛腿的位置

  • and then predict where it probably would be,

    然後預測哪裡 它很可能是,

  • the next time, the radar sent a pulse out.

    接下來的時間,該 雷達發送的脈衝輸出。

  • That was called the range gate.

    這被稱為距離門。

  • >> -Then, once the Patriot decides enough time has

    >> - 然後,一旦愛國者 決定時間已夠

  • passed to go back and check the next location for this detected object,

    傳遞回去檢查下 具體地址為檢測對象,

  • it goes back.

    它可以追溯到。

  • So when it went back to the wrong place, it then sees no object

    因此,當它回到了錯 地方,然後看到沒有對象

  • and it decides that there was no object, it was a false detection,

    和它決定,沒有 對象,這是一個錯誤的檢測,

  • and drops the track.

    和下降的軌道。

  • The incoming Scud disappeared from the radar screen,

    傳入飛毛腿消失 從雷達屏幕上,

  • and seconds later, it slammed into the barracks.

    幾秒鐘後,它 撞上了軍營。

  • The Scud killed 28 and was the last one fired during the First Gulf War.

    飛毛腿殺害28是最後一個 第一次海灣戰爭期間,人開火。

  • Tragically, the updated software arrived at Dhahran the following day.

    可悲的是,更新的軟件 抵達宰赫蘭的第二天。

  • The software flaw had been fixed, closing

    軟件缺陷有 得到修復,閉合

  • one chapter in the troubled history of the Patriot missile.

    在陷入困境的一章 愛國者導彈的歷史。

  • >> [END PLAYBACK]

    >> [結束播放]

  • >> DAVID J MALAN: So we'll take a look at some similar limitations in just a bit.

    >> 大衛·馬蘭:所以我們就來看看 一些類似的限制,只是有點。

  • But first, let's transition to a few FYI's.

    但首先,讓我們來 過渡到一些僅供參考的。

  • So one-- this weekend, there will be super sections,

    因此,埃德蒙頓這個週末,有 將超級部分,

  • which are meant to supplant regularly scheduled sections, which

    這意味著可以​​取代 定期的部分,這

  • will start up a week hence.

    將一個星期,因此啟動。

  • Take a look at the CS50's website for more information on those.

    看看在CS50的網站 對於這些詳細信息。

  • They will also be filmed and streamed live for those unable to attend.

    他們也將被拍攝下來,流 住那些無法參加。

  • Problem set 1 is on the course's website already,

    問題1是關於 當然,本已網站,

  • and we'll take a look at that in just a little bit.

    我們將看看 在只是一點點。

  • And office hours too, will take place this Monday through Thursday.

    而辦公時間也將採取 至週四把這個星期一。

  • >> So this was the most canonical program we looked at last time.

    >> 因此,這是最典型 節目中,我們看了最後一次。

  • It's like the simplest program you can write in C,

    這就像最簡單 程序,你可以用C寫的,

  • and even that's a bit of a bold claim.

    即使這是一個有點大膽索賠。

  • Right?

    對嗎?

  • Because there's a lot of seeming complexity to this.

    因為有很多的 似乎複雜此。

  • So let's take a quick look at what some of these elements

    所以,讓我們快速瀏覽一下 什麼一些這些元件中的

  • were and then try to provide a mental model for how

    分別為,然後嘗試提供 如何的心理模型

  • these simplest of programs work, and then we'll start

    這些簡單的程序 工作,然後我們將開始

  • looking at things ever more complex.

    看事情越來越複雜。

  • So this line here, highlighted now, in yellow, what did we say, last time,

    因此,這條線在這裡,現在強調的, 在黃色的,沒有大家說什麼,最後一次,

  • that this does for us?

    這為我們做?

  • What's the purpose it serves?

    什麼是它服務的目的是什麼?

  • Anyone from farther back?

    任何人從更遠了?

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: [INAUDIBLE]

    >> 觀眾:[聽不清]

  • DAVID J MALAN: Good.

    大衛·馬蘭:好。

  • So it gives you access to commands, or let's

    所以,它給你的訪問 為命令,或讓我們

  • call them functions, that someone else wrote, that are declared,

    給他們打電話的功能,有人 別人寫的,被聲明,

  • so to speak, in some other file.

    可以這麼說,在一些其他的文件。

  • So we'll see exactly what a .h file is versus a .c file, eventually.

    所以我們會看到什麼.h文件 是對.c文件,最終。

  • But for now, just know that printf, for instance,

    但現在,只知道 該printf的,例如,

  • is among the functions that have been declared in some other file, somewhere

    是其中已被功能 在其他文件中聲明,在某處

  • else on the cloud's hard drive that allows us to access printf and use it

    否則雲計算的硬盤​​驅動器上 允許我們訪問printf和使用

  • without having to reinvent that wheel ourselves.

    而不必重新發明 那個輪子自己。

  • Meanwhile, main.

    同時,主要的。

  • What was the analog of main, last week?

    什麼是主要的,上週的模擬?

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: Green flag is clicked.

    >> 觀眾:綠旗點擊。

  • >> DAVID J MALAN: Yeah.

    >> 大衛·馬蘭:是的。

  • Scratch's, when green flag clicked.

    划痕的,當綠旗點擊。

  • It's like the puzzle piece that kicks things off.

    這就像一塊拼圖 該踢東西了。

  • And so similarly, did the world decided some years ago that in C,

    所以同樣,做世界 幾年前的決定C,

  • and a bunch of other languages, if you want to write a program,

    和一堆其他語言, 如果你想編寫一個程序,

  • your first function has to be called, main.

    你的第一個功能 已被調用,主。

  • And it has to look like this.

    而且它有看起來像這樣。

  • But we'll come back, another time, to what, int,

    但是,我們會回來的, 另一次,是什麼,INT,

  • and, void, mean in that context.

    並且,無效的,意味著在這方面。

  • For now, the curly braces are kind of like Scratch's puzzle piece shape

    就目前而言,花括號樣 像划痕的拼圖形狀

  • that encapsulates some number of lines.

    它封裝線的一些數字。

  • And among on the lines here, is this one here.

    而就在電線之間 在這裡,這是一個在這裡。

  • printf is a function whose purpose in life is to print a formatted string.

    printf的是,其目的在一個函數 生活是打印的格式化字符串。

  • And by formatted, I mean you can plug in placeholder values,

    而到格式化的,我的意思是你 可以在佔位符值堵塞,

  • and you can specify how many decimal points, how many numbers to print

    你可以指定多個小數 點,有多少數字打印

  • after a decimal point and the like.

    後一個小數點等。

  • And printf, of course, takes one or more arguments or parameters,

    和printf,當然,需要一個 以上參數或參數,

  • otherwise known, more simply, as inputs.

    否則已知的,更簡單地說,作為輸入。

  • >> So printf, like a lot of functions, takes inputs.

    >> 所以printf的,想了很多 函數的輸入參數。

  • And those inputs are embraced by the two parentheses here.

    而這些輸入被接受 由兩個括號這裡。

  • And inside of those is one input.

    而那些裡面是一個輸入。

  • It's a string, as we've called it, which is just a sequence of characters,

    這是一個字符串,因為我們已經把它稱為, 這是一個字符的只是一個序列,

  • like a word, or a phrase, or a whole essay even, in between double quotes.

    像一個字,或短語,或整個 文章甚至在雙引號之間。

  • And that's what's going to influence the behavior of printf because, of course,

    這就是發生了什麼事情來影響 當然,printf的行為,因為,

  • it's just a generic print function.

    它只是一個普通的打印功能。

  • It's not going to know what to print, unless you tell it.

    它不會知道是什麼 打印,除非你告訴它。

  • And then, some minutiae.

    然後,一些細枝末節。

  • What did we say this weird sequence of symbols is?

    那麼,我們能說這個奇怪 的符號序列是?

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: New line.

    >> 觀眾:新行。

  • >> DAVID J MALAN: New line.

    >> 大衛·馬蘭:新行。

  • So it turns out, you can't just hit, enter, when you're writing the program.

    所以,事實證明,你不能只打, 進入,當你寫的程序。

  • Generally, the compiler is going to get a little confused as to what you mean.

    通常,編譯器是會得到 有點困惑,你的意思。

  • Rather, you have to literally say, give me a new line here.

    相反,你必須從字面上 說,給我一個新的生產線在這裡。

  • And so /n is what we generally call an escape character.

    所以/ N是我們一般 調用一個轉義字符。

  • So n, for new line.

    因此n,新線路。

  • And the compiler knows that when it sees /n,

    而編譯器知道 當它看到/ N,

  • it should actually induce the computer, ultimately, or printf, in this case,

    它實際上應該誘導計算機, 最終,或printf,在這種情況下,

  • to print out an actual new line, like hitting the, enter,

    打印出一個新的實際 行,像打了,進入,

  • key on your keyboard.

    鍵盤上的鍵。

  • And lastly, what did we say this piece of syntax is for?

    最後,做什麼我們說 這一塊的語法是什麼?

  • What does it represent?

    這是什麼代表什麼呢?

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: [INAUDIBLE]

    >> 觀眾:[聽不清]

  • >> DAVID J MALAN: It's just the end of the line.

    >> 大衛·馬蘭:這只是 該行的末尾。

  • It's the end of the statement.

    它是語句的末尾。

  • And realize that we don't put them everywhere.

    並認識到我們不 把它們無處不在。

  • We certainly don't put them at the ends of every line.

    我們當然不會把他們 在每行的末端。

  • For instance, there's none on the first line, there's none on the line

    舉例來說,有沒有對 第一行,有沒有上線

  • with, main, there's none after the curly braces,

    有,主,有沒有 花括號後,

  • but you'll start to see and get familiar with where it's called for.

    但你會開始看到並獲得 熟悉的地方,它被稱為為。

  • And it's almost always after a function call or a statement, some action

    它是一個功能之後幾乎總是 致電或發表聲明,一些行動

  • that you're actually taking.

    你的確服用。

  • >> And know now, especially if among those less comfortable,

    >> 而現在知道,特別是如果 在那些不太舒服,

  • these are the kinds of stupid things that you'll end up accidentally banging

    這些都是種愚蠢的事情 你會最終不小心撞

  • your head against the wall over.

    你的頭撞牆了。

  • Because you'll be logically confident in some problem

    因為你會在邏輯上 相信在某些問題

  • you've solved for a problem set, and the damn thing just

    你已經解決了問題 集,該死的東西只是

  • won't compile or even run.

    將無法編譯,甚至運行。

  • And so often, early on, it's going to be because you missed a parenthesis,

    所以通常情況下,在初期,它會 是因為你錯過了一個括號,

  • or you missed a semicolon.

    或者你錯過了一個分號。

  • And so just be mindful of these kinds of things,

    所以只是留心 中,這樣的東西

  • and try not to get frustrated by them because very quickly does this

    並盡量不要被沮喪 因為他們很快做到這一點

  • become old hat.

    成為舊帽子。

  • But it's very easy to get frustrated early on, as a result.

    但它很容易得到 受挫早上,作為一個結果。

  • >> So now, let's take a look at how this line is actually working

    >> 所以,現在,讓我們來看看如何 這條線是實際工作

  • and then look at a slightly more complicated one.

    再看看一個稍微 更複雜的之一。

  • So we have over here, the ability to draw on this screen.

    所以我們在這裡,在 能夠得出這樣的屏幕上。

  • And let's suppose that this is my computer screen,

    讓我們假設 這是我的電腦屏幕,

  • but I am writing the, hello program, and I have not implemented, printf.

    但我寫的,你好程序, 而我還沒有實現,printf的。

  • Someone else has implemented, printf.

    別人已經實施,printf的。

  • Who would like to claim to have implemented, printf?

    誰願意自稱 已經實施了,printf的?

  • If we may?

    如果我們可能嗎?

  • All right, what's your name?

    好吧,你叫什麼名字?

  • >> STUDENT 1: [? Copal. ?]

    >> 學生1:[?柯巴。 ?]

  • >> DAVID J MALAN: Copal, come on up.

    >> 大衛·馬蘭:Copal的,拜託了。

  • Come on up.

    上來吧。

  • All right.

    好的。

  • So we have, here, some name tags, since we'll make a little game of this.

    因此,我們有,在這裡,一些名稱標籤, 因為我們會做出這樣的小遊戲。

  • And we will call you, printf.

    我們會打電話給你,printf的。

  • And if you want to come over here, what I've just drawn on the screen,

    如果你想要到這裡來, 我剛剛在屏幕上繪製,

  • it's quite simply, there's me, this.

    這是相當簡單的,還有我,這。

  • >> All right, so, hello my name is, printf, if you'd like to put that on.

    >> 所有的權利,所以,你好我的名字是, printf的,如果你想要把那。

  • All right.

    好的。

  • And if you can go stand by the computer screen

    如果你可以去展台 通過在計算機屏幕

  • as though you are the function that came with this computer system.

    就好像你是功能 來到這個計算機系統。

  • And your purpose in life is to actually print something.

    而你的人生目標是 實際打印的東西。

  • But much like the program we just had on the screen, here,

    但是,就像節目我們 剛剛在屏幕上,在這裡,

  • we're going to have to actually give you some input.

    我們將不得不 實際上給你一些意見。

  • >> And so, if my input, here, is apparently, what is passed to printf,

    >> 所以,如果我的輸入,在這裡, 顯然,什麼是傳遞給printf,

  • let's kind of mock it up like this.

    讓我們種的譏笑它是這樣的。

  • I'm going to literally write on a piece of paper, "hello, world,"

    我將字面上寫上 一張紙,“你好,世界”

  • backslash N. And to be clear, what I've just drawn on this piece of paper,

    反斜線N.而且要明確,什麼樣 我剛剛畫在這張紙上,

  • looks like this.

    看起來是這樣的。

  • So when I run this program, and this yellow line of code gets executed,

    所以,當我運行這個程序,而這 代碼黃線被執行,

  • it's as though I, the hello program, am handing some input off to a function

    就好像我,hello程序, 我交給一些輸入開了一個功能

  • that someone else wrote.

    別人寫的。

  • >> And if you, with your finger, could actually, with your finger,

    >> 如果你與你的手指, 居然可以,用你的手指,

  • draw on the screen whatever it is you have been handed,

    在屏幕上繪製什麼 這是你一直在流傳,

  • the effect, ultimately, is to see exactly that, on the screen.

    的效果,最終,是 看到正是這樣,在屏幕上。

  • And a Little corner case here.

    和一個小角落的情況下在這裡。

  • And good, we shouldn't see the, new line, at this point.

    而良好的,我們不應該看到 的,新線,在這一點上。

  • It would be incorrect for you to explicitly draw the newline.

    這將是不正確的你 明確地得出新行。

  • But if we kept writing words on the screen, they would end up below that.

    但是,如果我們不停地寫上的字 屏幕上,他們最終會低於。

  • >> So thank you very much, but stick around here for just one moment.

    >> 所以,非常感謝你,但堅持 這裡只是一個瞬間各地。

  • We now need one other volunteer, if we could,

    我們現在需要一個其他 志願者,如果我們能,

  • that's going to need to play the role of-- It's only people in the orchestra

    那將需要發揮的作用 of--這只是人們在樂團

  • right now.

    現在。

  • How about-- OK.

    如何about--確定。

  • Right here.

    就在這裡。

  • Come on up.

    上來吧。

  • What's your name?

    你叫什麼名字?

  • >> STUDENT 2: [? Ivay. ?]

    >> 學生2:[? Ivay。 ?]

  • DAVID J MALAN: Sorry?

    大衛·馬蘭:對不起?

  • STUDENT 2: [? Ivay. ?]

    學生2:[? Ivay。 ?]

  • DAVID J MALAN: Ethan, come on up.

    大衛·馬蘭:阮經天,上來吧。

  • No?

    沒有?

  • Did I get that wrong, even after you said it twice?

    難道我得到的錯誤,甚至 你說了兩遍之後?

  • Come on up.

    上來吧。

  • It's hard to hear up here.

    這是很難聽到了這裡。

  • OK.

    確定。

  • And I'm sorry, what's your name?

    我很抱歉,你叫什麼名字?

  • >> STUDENT 2: [? Ivay. ?]

    >> 學生2:[? Ivay。 ?]

  • >> [? DAVID J MALAN: Ivay. ?] OK.

    >> [?大衛·馬蘭:Ivay。 ?]確定。

  • For now, if you don't mind, you are GetString.

    現在,如果你不 心,你的GetString。

  • >> STUDENT 2: OK.

    >> 學生2:確定。

  • Cool.

    酷。

  • DAVID J MALAN: So if you would like to stand here for just a moment,

    大衛·馬蘭:所以,如果你想 站在這裡只是一瞬間,

  • let's take a look at a slightly more complex program, that

    讓我們來看看一個稍微 更複雜的程序,即

  • now has three lines of code.

    現在有三行代碼。

  • So we have, one, state your name using printf; two, a call to GetString,

    因此,我們有,其一,說明用你的名字 printf的;二,一個電話給GetString,

  • followed by an assignment to a variable called, string s, or called s;

    隨後分配到一個 變量調用,字符串s,或稱為S;

  • and then another call to, printf, but this time with two inputs.

    然後再次調用,printf的, 但是這次使用兩個輸入。

  • >> So we've already done state your name, or rather, we've

    >> 因此,我們已經做狀態 你的名字,或者更確切地說,我們已經

  • already done a printf call.

    已經做了printf的電話。

  • So I'm going to write, State your name.

    所以我會寫,說出你的名字。

  • And so, what I'm going to pass, printf, in just a moment,

    所以,有什麼我要去 通,printf的,在短短的時刻,

  • is quite simply, this.

    是很簡單,這一點。

  • So if you want to go ahead and draw this on the screen, that's your input now.

    所以,如果你想繼續前進,得出這樣的 在屏幕上,這是現在你的輸入。

  • All right.

    好的。

  • And forget string, we now have our own line of code here.

    而忘記字符串,我們現在有 我們自己的行代碼在這裡。

  • So in, GetString, we need to actually call, GetString.

    所以,GetString的,我們需要 實際調用,GetString的。

  • So your purpose in life is to just walk out into the orchestra, if you could,

    所以,你的人生目標是只走 外面的樂團,如果你能,

  • and get someone's name.

    並得到某人的名字。

  • But let's give you something to put it on.

    但是,讓我們給你 東西把它。

  • If you want to, go ahead and get a string,

    如果你想,去 進取,得到一個字符串,

  • get someone's name on that piece of paper, if you could.

    得到某人的名字上 一張紙,如果你能。

  • >> All right.

    >> 好的。

  • And we'll see, in just a moment, whose name we're getting.

    我們將看到,在短短的時刻, 他的名字我們得到。

  • Meanwhile, what I'm going to have ready, is a blank piece

    同時,我所要 有現成的,是一張空白

  • of paper, in which, I'm going to store whatever value it

    紙,其中,我要去 存儲任何值它

  • is that GetString is returning to me, I, being a string variable called, s.

    是,GetString的是返回給我, 我,作為一個所謂的字符串變量,S。

  • All right.

    好的。

  • So what do you have here?

    所以,你有什麼嗎?

  • Nik.

    聶。

  • All right.

    好的。

  • So we have Nik's name here.

    因此,我們有聶的名字在這裡。

  • So this is what literally has been returned

    因此,這是字面上 已返回

  • to me, so to speak, by GetString.

    對我來說,可以這麼說,通過GetString的。

  • >> I, now, am going to execute the left-hand side

    >> 我,現在,我準備 執行左手側

  • of that expression, where I simply copy down, for today's purposes, Nik.

    那表情,在這裡我簡單的 抄下來,今天的目的,聶。

  • So now, I have a variable called, s, storing Nik's name.

    所以,現在,我有一個變量 所謂,S,存儲聶的名字。

  • I've already handed to printf, a previous argument.

    我已經交給 printf的,以前的說法。

  • But in our third and final line of code, I actually have to hand printf

    但是,在我們的第三個,也是最後的行 代碼,我實際上是手的printf

  • something a little different-- "hello, %s," backslash n.

    東西有點different-- “你好,%S,”反斜線ñ。

  • >> And so the last line I'm going to send-- the last thing I'm

    >> 這樣一來,最後一行我要去 到send--的最後一件事,我

  • going to write down now, is this.

    要現在寫下來,是這樣的。

  • So the two lines of code, or rather the last line of code,

    所以兩行代碼,或 而代碼的最後一行,

  • calls for two inputs-- one, this, and two, this.

    要求兩inputs-- 之一,這和兩個,這一點。

  • So if our printf function can now take these as input,

    所以,如果我們的printf函數 現在可以把這些作為輸入,

  • let me clear the screen for you.

    讓我清除屏幕為您服務。

  • Actually no, you can go ahead.

    其實也沒什麼,你可以繼續。

  • We'll leave it up, since it's on the same program.

    我們會離開它,因為 它是在相同的程序。

  • We should see, hello, Nik.

    我們應該看到,你好,聶。

  • All right.

    好的。

  • So this was quite a few hoops to jump through, just

    因此,這是相當多的 箍跳通過,只

  • to write, state your name, and, hello, Nik.

    寫,說明你的 名稱和,你好,聶。

  • But this simple idea of message passing, of input passing and output receiving,

    但消息傳遞的這個簡單的想法, 輸入的傳球和輸出接收的,

  • is exactly the model we're going to have for even the most complex functions.

    正是我們將不得不模型 即使是最複雜的功能。

  • So thank you so much to you both.

    所以,謝謝你這麼多,你倆。

  • We have a lovely stress ball here for you.

    我們有一個可愛的壓力 球在這裡為你。

  • And thank you to our GetString and printf volunteers alike.

    謝謝你對我們的GetString 和printf志願者的一致好評。

  • Thank you.

    謝謝。

  • All right.

    好的。

  • Thank you to you both.

    謝謝你們。

  • >> So we've been talking about thus far, mostly about strings.

    >> 因此,我們一直在談論 迄今為止,大部分是關於串。

  • And it turns out that C can actually understand a few different data types.

    而事實證明,C能實際 了解幾個不同的數據類型。

  • In fact, let's take a look at these here.

    事實上,讓我們 一起來看看這些在這裡。

  • So C, and a lot of languages, understand things called chars.

    因此,C,和很多語言, 認識事物稱為字符。

  • A char is generally a single byte, or eight bits.

    一個char通常一個是 單字節或8位。

  • And it represents a single character, like the letter A, or the capital

    它代表一個字符, 像字母A或資本

  • letter A, or the lowercase letter A, or an exclamation point,

    字母A,或小寫字母 A,或感嘆號,

  • or any character that you can type on your keyboard, and sometimes even more.

    或任何字符,你可以鍵入 你的鍵盤,有時甚至更多。

  • We also happen to see floats.

    我們也碰巧看到彩車。

  • A float is, generally, a 32-bit value, or four bytes

    浮子是,一般情況下, 32位值,或四個字節

  • because again, one byte is eight bits.

    因為再次,一個字節是8比特。

  • >> So a float is a floating point value, something with a decimal point.

    >> 所以一個float是一個浮點值, 大一些的小數點。

  • And indeed, that's what the movie was talking

    事實上,這就是 這部電影是說

  • about when they spoke about floating point values, some fixed number of bits

    ,當他們談到浮動 點值,一些固定位的數目

  • being used to represent a real number.

    被用於表示一個實數。

  • But there's also things called doubles.

    但也有東西叫做雙打。

  • These exist in Java, if you've taken APCS,

    這些存在於Java中, 如果你已經採取了APCS,

  • and a double, as the name thankfully suggests, is twice as big as a float.

    和雙,正如其名字令人欣慰 顧名思義,是兩次大如浮動。

  • It's still a real number, it just has more bits

    它仍然是一個實數, 它只是有更多的位

  • with which to be ever more precise, or to store even larger numbers.

    與要更加精確, 或存儲更大的數字。

  • int is easy.

    int是容易的。

  • We talked about that last time.

    我們談到了這最後的時間。

  • It's just an integer.

    這只是一個整數。

  • And it's generally 32 bits, or four bytes.

    而且它是一般32 位,或四個字節。

  • >> And so, if you have, let's see now, 32 bits-- and we did this in week 0,

    >> 所以,如果你有,我們現在看到的, 32 bits--我們0週這樣做,

  • ever so briefly-- if you have 32 bits, what's the biggest

    曾經如此briefly--,如果您有 32位,什麼是最大的

  • number you can represent as an integer?

    數可以表示為一個整數?

  • Give or take?

    給予或採取?

  • It's like 4 billion, and that's only if we're

    這樣4十億, 而這只有當我們

  • representing positive numbers only.

    僅代表正數。

  • If you have 32 bits, and you want to represent negative numbers as well,

    如果你有32位,並且要 表示負數為好,

  • your range is, essentially, negative 2 billion to positive 2 billion.

    你的範圍是,從本質上講,負 2十億轉正2十億。

  • But generally, we'll start at 0 go up to 4 billion.

    但是總體來說,我們將開始 在0升至4十億。

  • >> You don't have to know precisely, but we can see this, in fact, if I just

    >> 你不必準確地知道,但 我們可以看到這一點,事實上,如果我只是

  • open up a little calculator, here.

    開了一個小計算器,點擊這裡。

  • I can do 2 the 32, and that's exactly how big,

    我可以做2的32,和 這正是有多大,

  • how many values you can represent, with 32 bits.

    有多少價值,你可以 表示,具有32位。

  • And it's roughly 4 billion.

    而且它的約4十億。

  • So we'll keep seeing that number in a few different places.

    因此,我們會繼續看到這個數字 在幾個不同的地方。

  • But if you need longer numbers than that,

    但是如果你需要更長的 數比,

  • it turns out there's something called a long long.

    原來有 一些所謂的很長很長。

  • And a long long is generally 64 bits, which

    而一個很長很長的 通常64位,這

  • means it's an order of magnitude even bigger than an int.

    意味著它是一個數量級 比int更大。

  • >> So I can't even pronounce the biggest number that you can represent,

    >> 所以,我甚至不能發音最大 你可以代表數字,

  • but it's markedly bigger.

    但它明顯更大。

  • Now as an aside, historically, if an int is 32 bits,

    現在,順便說一句,從歷史上看, 如果一個int是32位,

  • and a long long is 64 bits, how big is a long, not a long long?

    和很長很長為64位,怎麼樣 大是一個長期的,而不是一個很長很長?

  • You'd think it's longer than an int but maybe less long than a long long,

    你會認為這是比int長 但也許少長不是很長很長,

  • but it actually depends.

    但它實際上依賴。

  • >> And so it turns out one of the frustrations too,

    >> 所以它變成一顆顆 過的挫折,

  • with writing code on certain systems, is that not all of these data types

    對某些系統編寫代碼, 是,並不是所有的這些數據類型的

  • have pre-determined values.

    有預先確定的值。

  • Sometimes it's this many bits.

    有時候,這多少位。

  • Sometimes it's that many bits.

    有時候,很多位。

  • So you actually have to know, sometimes, what hardware

    所以,你居然要知道, 有時,哪些硬件

  • you're running your software on.

    您正在運行的軟件。

  • Thankfully, other languages and other data types that now exist,

    值得慶幸的是,其他語言和 現在還存在著其他的數據類型,

  • allow you to be more precise.

    讓您更精確。

  • Well, we saw string, and we saw bool, too,

    好了,我們看到的字符串, 而我們看到的布爾也

  • but it turns out those come only with the CS50 library.

    但事實證明,這些都 只與CS50庫。

  • So those are not built into C. Those instead,

    因此,那些不建 到C.這些替代,

  • come in that file called CS50.h, that we'll eventually

    進來叫該文件 CS50.h,我們最終會

  • peel back the layers of.

    剝開的層。

  • But for now, they're just additional data types.

    但現在,他們只是 附加數據類型。

  • A bool is a true or false, and a string is a sequence of characters,

    一個布爾值是真還是假,和 串是字符序列,

  • like a word.

    就像一個字。

  • Now printf, we've seen, has placeholders, %s is one.

    現在printf的,我們已經看到,有 佔位符,%S就是其中之一。

  • And you might be able, to now, infer from these other examples, how

    你也許可以,到現在為止, 從這些其它實例中,如何推斷

  • you could have a placeholder for different data types.

    你可以有一個佔位符 對於不同的數據類型。

  • For instance, take a guess, if you wanted to print out a single char using

    例如,以此來猜測,如果你 想使用打印出單個字符

  • printf, the placeholder is probably %c.

    printf的,佔位符大概%C。

  • And if you want to print out an integer with the placeholder, %i.

    如果你想打印出 整數的佔位符,%我。

  • %lld is a long long decimal value, but long long, so that maps to that.

    %LLD是一個很長很長的十進制值, 但很長很長,所以映射到這一點。

  • And then %f for floating point value or for a double,

    然後%f浮點 點值或雙,

  • so sometimes they're re-used in different contexts.

    所以有時候他們重新使用 在不同的上下文。

  • So we'll see and use some of those over time.

    所以我們會看到和使用 其中一些隨著時間的推移。

  • And printf and other functions also support others' escape sequences,

    和printf等功能也 支持別人的轉義序列,

  • and sometimes, these are necessary.

    有時,這些都是必要的。

  • So backslash n is a new line.

    所以反斜杠n是一個新的生產線。

  • Backslash t, does anyone want to take a stab?

    反斜杠T,沒有任何人 要採取刺?

  • AUDIENCE MEMBER: Tab.

    觀眾:標籤。

  • DAVID J MALAN: Tab.

    大衛·馬蘭:標籤。

  • So if you actually want to print out a tab, not

    所以,如果你真的想 打印出標籤,而不是

  • a fixed number of spaces but an actual tab character, you don't hit your,

    的空間的固定號碼,但實際的 製表符,你不打你,

  • tab, key on the keyboard, generally, you actually do backslash t.

    標籤上,在鍵盤上鍵,通常, 你其實反斜杠噸。

  • Backslash double quote, why would I ever want that?

    反斜杠雙引號, 為什麼我會永遠想的?

  • Right?

    對嗎?

  • Why can't I just type a double quote on my keyboard?

    為什麼我不能只是輸入 我的鍵盤上雙引號?

  • AUDIENCE MEMBER: Because, otherwise, it's

    觀眾: 因為,否則,它是

  • going to think it's the end of like your print [? text. ?]

    會認為這是結束 對喜歡您的打印[?文本。 ?]

  • >> DAVID J MALAN: Exactly.

    >> 大衛·馬蘭:沒錯。

  • Remember, with our printf examples, when we

    請記住,我們的 printf的例子,當我們

  • were passing to printf in input, on the left of that input string

    被傳遞的輸入對printf, 在該輸入字符串的左

  • and on the right of that input spring, of course, was a double quote.

    並在該輸入的右 春天,當然,是一個雙引號。

  • If your own input has a double quote in the middle of that,

    如果你自己輸入有雙重 報價在中間的那個,

  • the computer might potentially get confused, as to,

    計算機可能潛在地 感到困惑,因為到,

  • does this double quote belong in the middle?

    這是否雙引號 屬於中間?

  • Does it belong with the left one?

    它是否與左邊的歸屬?

  • Does it belong with the right one?

    是否有一個正確的歸屬?

  • And so, if you want to make it super clear, you do backslash double quote,

    所以,如果你想使超 明確的,你做的反斜線雙引號,

  • so that it's escaped, so to speak, and it's not conflated for something else.

    因此,它的逃脫了,可以這麼說,和 它不是混為一談別的東西。

  • And there's a few others here, backslash r, single quotes, 0,

    而且還有其他幾個人在這裡, 反斜線R,單引號,0,

  • that we may see over time, as well.

    我們可以看到隨著時間的推移,以及。

  • And now, what about functions?

    而現在,怎麼樣的功能呢?

  • >> So actions that we can take thus far in this language, C, well, we've seen,

    >> 這樣我們就可以採取迄今的行動 在這種語言,C,好了,我們已經看到,

  • printf, of course, and all of the others on the screen,

    printf的,當然,所有的 屏幕上的其他人的,

  • here, that we'll use for the course's first few weeks only,

    在這裡,我們將使用的 當然第一個只有少數幾個星期,

  • come when the CS50 library.

    來當CS50庫。

  • And they make it much easier, in C, to actually get user input.

    他們能夠更容易,在 C,實際獲取用戶輸入。

  • It turns out that in C, and frankly in a few languages,

    事實證明,在C和 坦率地說在幾種語言,

  • it's a real pain in the neck to do something simple, like prompt the user

    它在脖子上做一個真正的痛苦 簡單的東西,比如提示用戶

  • for keyboard, for his or her input.

    為鍵盤,為他或她的輸入。

  • And so these functions make it easier.

    所以,這些功能更容易。

  • >> And it also has error checking throughout,

    >> 它也有錯誤 在整個檢查,

  • so that, when you recall on Wednesday, we

    這樣一來,當你 記得上週三,我們

  • saw the, retry, warning, when I didn't cooperate,

    看到了,重試,警告, 當我不配合,

  • and I typed a word, instead of a number?

    而我輸入一個字,而不是數字?

  • We've done the heavy lifting early on to make sure the user cooperates.

    我們早做了繁重 在確保用戶合作。

  • But these are just training wheels that we will eventually,

    但這些都只是培訓 車輪,我們將最終,

  • and quickly, take off.

    並迅速,起飛。

  • >> So to recap, then, let's take a quick look,

    >> 因此,要回顧一下,然後, 讓我們快速瀏覽一下,

  • much like we did with Scratch, at some canonical constructs in C.

    就像我們有划痕,在沒有 C.在一些典型結構

  • This is meant to, sort of, be a whirlwind tour,

    這是為了,排序 的,是一個旋風之旅,

  • just so that you have a reference and that you've seen things at first.

    只是讓你有一個參考 那你首先看到的東西。

  • But then we'll look at actual coding use some of these building blocks.

    但是,我們來看看實際的編碼 使用其中的一些積木。

  • So much like in Scratch, when we had statements like,

    所以,就像在刮, 當我們已經報表喜歡,

  • say or, wait, in C, we do have functions as well, like printf.

    或者說,等待,在C,我們確實有 功能以及,如printf。

  • >> If we want to express a condition in C, it's similar, in spirit,

    >> 如果我們想表達的條件 在C,它是類似的,在精神上,

  • to that puzzle piece that looked like this in Scratch.

    該拼圖的 看起來是這樣的划痕。

  • But instead, we literally just write, if.

    但是相反,我們 從字面上只寫,如果。

  • And then, in parentheses, we put a condition,

    然後,在括號 我們把一個條件,

  • where that condition is what we'll call, again, a boolean expression.

    其中,該條件是我們將 打電話,再次,一個布爾表達式。

  • And again, this is sort of pseudocode.

    再次,這是有點偽的。

  • And, in fact, the // is a comment.

    而且,事實上,//是註釋。

  • It's just English words to myself.

    這只是英語單詞我自己。

  • But this is the general structure of an, if, condition.

    但是,這是一般 的一個,如果,條件結構。

  • But we'll see concrete examples in just a moment.

    但是,我們會看到具體的 例子在短短的時刻。

  • >> If you want to have a two-way fork in the road,

    >> 如果你想有一個 兩路的岔路口,

  • much like we did with our volunteer on Wednesday, you can have an, else if.

    就像我們與我們的志願者在做 週三,你可以有一個,否則,如果。

  • And if you want to have a third and final condition, or default situation,

    如果你想有一個第三和 最後一個條件,或者默認的情況下,

  • you can have just an else block, there.

    你可以只是一個其他塊,還有。

  • And similarly, with boolean expressions, you can, and, them together.

    同樣,用布爾表達式, 你可以,而且,它們在一起。

  • And we saw on Wednesday, that it's not a single ampersand,

    而且我們看到在週三,即 它不是一個單一的符號,

  • it's two, for lower-level reasons that we'll eventually see and play with.

    它的2,為低級別的原因 我們最終會看到和發揮。

  • Or-ing things together is 2 vertical bars.

    的OR-ing的東西放在一起 是2豎條。

  • On a US keyboard, this is generally a key

    根據美國的鍵盤,這 通常是一個關鍵

  • with the Shift key above your Enter key or Return key.

    與上面的Shift鍵 您的Enter鍵或Return鍵。

  • >> Then there's these things that we'll use maybe once or twice.

    >> 此外,還有這些東西 我們將使用也許一次或兩次。

  • They are functionally equivalent to what you can do with an, if, else if,

    它們是功能上等同於 你可以用做的,如果,否則,如果,

  • else if else, construct, but they're called a switch.

    否則,如果其他人,建設, 但他們稱為交換機。

  • They look very different, but we'll see in some of our distribution code,

    他們看起來非常不同,但我們會 看到我們的一些分配代碼,

  • for a future problem set, most likely, that it's sometimes just a prettier

    對於未來的習題集,最有可能的, 它有時只是一個漂亮

  • way of expressing a whole bunch of conditions,

    表達一個整體的方式 一堆條件,

  • without having a lot of curly braces and a lot of parentheses and indentation.

    無需大量的花括號和 很多括號和縮進的。

  • But they give us no more power than we have already.

    但他們給我們沒有更多的 功率比我們已經。

  • And now loops.

    現在循環。

  • And this one, we'll look at a little more slowly.

    而這其中,我們來看看 在慢一點。

  • But then, we'll start to use these, especially for those already familiar.

    但隨後,我們將開始使用這些, 特別是對於那些已經熟悉。

  • This is the canonical way, if incredibly arcane way, to write a loop in C.

    這是規範的方式,如果難以置信 神秘的方式,用C編寫一個循環

  • >> Now a loop in Scratch was pretty straightforward.

    >> 現在,在划痕一個循環是 很簡單。

  • You have a, forever, block.

    你有一個,永遠塊。

  • You have a, repeat, block with just a number you have to type in.

    你有一個,重複,塊與 只是一個數字,你必須輸入內容。

  • And with the, for loop, you can implement both of those ideas,

    而伴隨著,for循環,可以 同時實現這些想法,

  • but it's a little more technical.

    但它是一個多一點的技術。

  • But frankly, it's also relatively simple.

    但坦率地說,這也是 相對簡單。

  • Once you know the order of operations, you literally

    一旦你知道的順序 操作,你從字面上

  • are just going to plug in values and tell the computer what to do.

    只是要在價值觀堵塞 並告訴計算機做什麼。

  • So here's an example.

    因此,這裡有一個例子。

  • This is a loop that, quite simply, counts from one number up

    這是一個循環的是,相當 簡單地說,從一個人數多達計算

  • through another.

    通過另一個。

  • And just by glancing at it, even if you have

    而就掃一眼 它,即使你有

  • no prior experience with this language, what number

    沒有以往的經驗 這種語言,什麼號碼

  • does it probably start counting at?

    它可能開始計數?

  • OK, 0.

    OK,0。

  • And I'm guessing that's because you see that there's an int and an i,

    我猜那是因為你 看到有一個int和我,

  • which is a variable.

    這是一個變量。

  • It's initialized to 0.

    它初始化為0。

  • >> And then later, it looks like we're passing printf, a value.

    >> 再後來,它看起來像 我們傳遞的printf,一個值。

  • And, in fact, I made a little typo here.

    而且,事實上,我在這裡做一個小錯字。

  • But that's easily fixed.

    但是,這容易固定。

  • Let me add in ,i here.

    我要補充的,我在這裡。

  • We now have printf being passed that placeholder value.

    我們現在有printf的存在 通過該佔位符值。

  • And what's going to count up through?

    這有什麼經歷來算呢?

  • AUDIENCE: 50.

    聽眾:50。

  • 50.

    50。

  • 49.

    49。

  • DAVID J MALAN: 49.

    大衛·馬蘭:49。

  • So 50, at first glance, seems right.

    所以50,乍一看,似乎是正確的。

  • But that, it turns out, is going to be our condition that we keep checking.

    但是,事實證明,將是 我們的條件,我們繼續檢查。

  • And we're going to stop once i is no longer less than 50.

    而且我們要停止一次 i是不再小於50。

  • So this loop, conversely, should execute, so long as i is less than 50.

    所以這個循環中,反之,應 執行,只要i小於50。

  • But as soon as it becomes 50, or 51, or worse, it should stop automatically.

    但只要它變為50,或51, 或者更糟,它應該自動停止。

  • All right.

    好的。

  • >> So what actually happens here?

    >> 那麼究竟是什麼發生在這裡?

  • So this is the order of operations for a loop.

    因此,這是秩序 操作為一個循環。

  • One, you have the so-called initialization.

    一,你有 所謂初始化。

  • This yellow highlighted chunk of code is executed first,

    這種黃色高亮塊 代碼首先執行,

  • and it has the effect that, per Wednesday, you probably imagine.

    並且它有一個,每效果 週三,你可能想像。

  • It creates a variable called i and it stores in that variable the value 0.

    它創建了一個名為i和它的變量 在變量存儲值0。

  • So i is 0 at this point in the story.

    所以i為0,在這一點上的故事。

  • >> The next thing that happens in this construct,

    >> 接下來的事情, 發生在這個結構,

  • is that the condition gets checked.

    是該條件被檢查。

  • So I check immediately, is i less than 50?

    於是我立即檢查, 為i小於50?

  • And of course, the answer for now is, surely, yes.

    當然,答案 現在來看,毫無疑問,是的。

  • Yes, because i is 0, and that's surely less than 50.

    是的,因為我是0, 這是肯定小於50。

  • Then what happens, is that this line of code gets executed.

    然後發生了什麼,是這 行代碼被執行。

  • And in fact, if there's multiple lines of code in those curly braces,

    而事實上,如果有多個 的代碼中的那些大括號線,

  • they all get executed one after another.

    他們都得到執行此起彼伏。

  • >> And in the effect here, is apparently, to print out

    >> 並且在這裡的效果,是 顯然,打印出

  • the number i, which is going to be 0, and then 1, and then 2.

    的編號i,這是會 為0,然後1,然後2。

  • But why?

    但是,為什麼?

  • Why does it get incremented?

    它為什麼會得到增加?

  • Well, the fourth thing that happens is that this syntax

    那麼,第四件事, 碰巧的是,這個語法

  • gets executed, after the semicolon.

    被執行,分號之後。

  • i++ is a shorthand way of saying; take the value of i, and add 1 to it;

    我++是一個說速記的方式; 取的i的值,並把它加1;

  • and then the next time around, add 1 to it; and the next time around,

    再下一次,加 1到它;而下一次,

  • add 1 to it.

    其加1。

  • >> So if we keep going, what's going to happen

    >> 因此,如果我們繼續下去, 什麼會發生

  • next is I'm not going to initialize i ever again.

    接下來就是我不打算 以後再初始化我。

  • If I kept initializing i to 0, this example

    如果我一直在初始化 i到0,本實施例

  • would never end because i would be stuck at 0.

    永遠不會結束,因為 我會停留在0。

  • But what is going to happen is that the condition will be checked,

    但什麼事情發生是 該條件將被檢查,

  • the line of code will get executed, the i will be incremented,

    該行代碼會被執行, 在我將增加,

  • condition will be checked, code will get executed.

    條件將被檢查, 代碼會被執行。

  • And it kept cycling again, and again, and again,

    它再次保持騎自行車, 又一次,又一次,

  • until i++ induces a value of 50.

    直到我++誘導值50。

  • >> The condition then says, is 50 less than 50?

    >> 條件接著說, 比50 50少了呢?

  • The answer, of course, is no, and so the whole code stops executing.

    當然,答案是否定的, 所以整個代碼停止執行。

  • And if you have more code on the screen down below, that's what happens next.

    如果你有在屏幕上更多的代碼 向下跌破,那接下來會發生什麼。

  • It pops out of these curly braces and continues to print more after that.

    它彈出來,這些花括號和 繼續打印後更多。

  • >> So a joke now, from FoxTrot, that you'll perhaps now understand.

    >> 所以,現在開個玩笑,從狐步舞,那 你也許現在明白了。

  • It's always funny.

    它總是有趣的。

  • It's like the chuckles kind of percolate,

    這就像笑 樣的過濾,

  • and then you realize you shouldn't be laughing at humor like this.

    然後你意識到你不應該 是笑的幽默是這樣。

  • But there is some takeaway here, pedagogically, too.

    但是,有一些外賣 在這裡,教學法,太。

  • So it turns out that I'm missing a piece of syntax,

    所以,事實證明,我 缺了一塊的語法,

  • or FoxTrot's missing a piece of syntax here.

    或狐步舞的失踪 一塊語法在這裡。

  • What's missing, that we've had in every other example, thus far?

    現在缺少的,我們已經有 在每一個其他實施例,從而多遠?

  • Return, is there, so that's something else,

    返回,在那裡,所以 這是別的東西,

  • that we'll come back to before long.

    我們再回過頭來不久。

  • What's missing, though?

    缺少了什麼有關係嗎?

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: The initialization of [INAUDIBLE].

    >> 觀眾:該 初始化[聽不清]。

  • >> DAVID J MALAN: OK.

    >> 大衛·馬蘭:OK。

  • So yeah.

    所以呀。

  • So actually, this is-- Well, and count is initialized

    所以實際上,這個is--嘛, 和計數初始化

  • up above, or rather, declared up above.

    上述起來,或者說,上述宣布了起來。

  • So this is another way of doing it.

    因此,這是做的另一種方式。

  • But not in one line it's valid though.

    但不是在一條線是有效的,但。

  • How about here?

    怎麼在這裡?

  • >> AUDIENCE MEMBER: Is it the curly braces?

    >> 觀眾:是不是大括號?

  • >> DAVID J MALAN: Yeah.

    >> 大衛·馬蘭:是的。

  • So the curly braces are missing.

    因此,大括號丟失。

  • But this code, I mean, to their credit, is actually, syntactically valid.

    但是這個代碼,我的意思是,他們的信用, 實際上,語法上有效。

  • It turns out, you don't need the curly braces

    事實證明,你不 需要花括號

  • if you only have one line of code that you want to execute inside of the loop.

    如果你只有一行代碼 要在循環內執行。

  • Now we always, in class and in all of our distribution code,

    現在我們總是在課堂上和 我們所有的分銷代碼,

  • include the curly braces anyway, just for clarity's sake.

    包括大括號 無論如何,僅僅是為了清楚起見。

  • But realize that in textbooks and online examples,

    但要意識到,在教科書 和網上的例子,

  • you may very well see curly braces missing sometime.

    你很可能看到大 牙套有時失踪。

  • And that's OK, if what you have indented and intended

    這沒關係,如果你 已經縮進和預期

  • is just a single line of code, and not multiple lines of code, potentially.

    是代碼只是一個單一的線,而不是 多行代碼,潛在的。

  • All right.

    好的。

  • So related to the issue of imprecision, is an issue of overflow,

    所以相關的問題 不精確,溢出的問題,

  • in the sense that, similarly, do integers have limits to them

    在這個意義上,類似地, 確實整數必須限制他們

  • just like floating point values?

    就像浮點值?

  • In the world a floating point values, we can only be so precise,

    在世界上的浮點 價值觀,我們只能是如此精確,

  • after which, sometimes bad things can happen,

    在此之後,有時 不好的事情都可能發生,

  • and our programs can be buggy and error.

    而我們的方案可以是越野車和錯誤。

  • Now even with integers, you can run into problems.

    現在,即使是整數, 你會遇到的問題。

  • Now an integer doesn't have a decimal point or numbers after it.

    現在的整數不具有 小數點或之後的數字。

  • It's just a natural number, typically.

    它只是一個自然數,一般。

  • So what could go wrong with an int?

    那麼,什麼可以去錯了一個int?

  • If I'm using an int to count, what could go wrong?

    如果我用一個int來 計數,有什麼可以去錯了嗎?

  • Seems a lot simpler.

    似乎簡單了很多。

  • Yeah.

    是啊。

  • >> AUDIENCE MEMBER: If the number gets [INAUDIBLE]

    >> 觀眾:如果 數字變[聽不清]

  • >> DAVID J MALAN: Yeah.

    >> 大衛·馬蘭:是的。

  • What if you count so high, that you can't express that really big number.

    如果算上如此之高,你有什麼 無法用語言表達的真正的大數目。

  • Right?

    對嗎?

  • At some point, you're going to exceed the boundaries

    在某些時候,你會 超過界限

  • of a 32-bit value or a 64-bit value.

    的一個32位值或64位的值。

  • Now again, I'm not sure how to pronounce a 64-bit value,

    現在,再次,我不知道怎麼樣 發音一個64位的值,

  • but I know with a 32-bit integer, the biggest value it can be,

    但我知道,有一個32位整數, 它可以是最大的值,

  • if it's only positive values, is roughly 4 billion.

    如果它是唯一積極的 值,是約4十億。

  • So if I try to count to 5 billion, something's going to happen.

    所以,如果我嘗試數到5十億, 事情將要發生。

  • But let's see what, in fact, can happen.

    但是,讓我們看看,其實都有可能發生。

  • In the world of integer overflow, where you, in a sense,

    在整數世界 溢出,在那裡你,從某種意義上說,

  • overflow the capacity of an integer, what might happen?

    溢出的能力 整數,會發生什麼事情?

  • >> So here's a binary number, it's a throwback to 0.

    >> 所以這裡有一個二進制數, 這是一個倒退到0。

  • It's all 1's, and the placeholders there, are powers of 2,

    這是全1,和佔位符 在那裡,有2的冪,

  • so this is binary.

    所以這是二進制的。

  • So these are 8, 1 bits on the screen.

    因此,這些都是8,1位在屏幕上。

  • And if you recall or you quickly do the math,

    如果你還記得或 你快做數學題,

  • what value is being represented here with these eight 1 bits?

    什麼樣的價值正在被代表 這裡這八個1位?

  • 255.

    255。

  • And even if you weren't quite sure of the math, you could do it out.

    即使你是不是很確定 數學的,你可以做出來。

  • Or you could just kind of reason through it,

    或者你可以只種 原因通過它,

  • wait a minute, if I'm representing an 8 bit value,

    等一下,如果我 較8位值,

  • and I've got 256 possible values, but the first of which is 0,

    我已經得到了256個可能的值, 但其中第一是0,

  • I just know that the biggest is going to be 255,

    我只知道, 最大的將是255,

  • and that's what this one might be.

    而這正是這一個可能。

  • >> So suppose I try to add 1 to this value.

    >> 因此,假設我嘗試添加1到這個值。

  • What would you do in grade school when adding a 1, and it

    你會在年級做 加入學校的時候1,而且

  • doesn't really fit because you have to carry the 1?

    並不真正適合,因為 你必須隨身攜帶的1?

  • What is this number going to become when you add 1?

    這是什麼數將 當你加1變成什麼?

  • It's going to become 0.

    這將變為0。

  • Right?

    對嗎?

  • Because if you had more bits-- and I'll try to, kind of, type it out here--

    因為如果你有更多的bits--,我會 嘗試,種,它這裡 - 打出來

  • if we had more bits, what we could do here is add the 1,

    如果我們有更多的位,我們 可以在這裡做的是添加1,

  • and then we'd get this.

    然後我們會得到這一點。

  • Whoops.

    哎呦。

  • We'd have a 1 bit all the way over here.

    我們希望有一個1位一路看過來。

  • >> But if this is a finite value, it's only 8 bits,

    >> 但是,如果這是一個有限 值,這是只有8位,

  • and that's pre-determined by the computer,

    這就是預先確定 由計算機,

  • that 1 is effectively not there.

    這1實際上是不存在的。

  • It just kind of falls off a cliff.

    正中下懷它掉下懸崖。

  • And so if you add 1 to 255, what value do you apparently get?

    所以,如果你添加1到255,有什麼 值你顯然得到什麼呢?

  • 0.

    0。

  • And so numbers accidentally, and perhaps, unintentionally,

    所以意外的數字, 或許,無意間,

  • end up wrapping around like this.

    最終披著這樣。

  • So what can be the implication of this?

    那麼什麼可以是這意味著什麼呢?

  • Well, there's a few different things.

    嗯,有一些不同的東西。

  • So one, it does end up looking like 0, unintentionally,

    所以之一,它最終看起來 如0,無意間,

  • but you can kind of see, even in the real world,

    但你可以種看, 即使在現實世界中,

  • for better or for worse, manifestations of this idea of a limit.

    是好還是壞, 表現這種思想的限制。

  • For instance, any of you who have ever played Lego Star Wars,

    例如,任何你誰 玩過樂高星球大戰,

  • does anyone happen to know the maximum number of coins

    沒有人知道發生 硬幣的最大數目

  • you can collect in Lego Star Wars?

    您可以在樂高星球大戰收集?

  • Take a guess based on today's leading questions.

    以基於猜測 當今領先的問題。

  • It's bigger than 256 or 255.

    這是大於256或255。

  • It's 4 billion.

    它的4間十億。

  • So it turns out, and there's some people--

    所以,事實證明,和 有一些people--

  • some googling confirmed last night-- have gotten 4 billion gold coins,

    一些谷歌上搜索確認最後night-- 已經得到了4個十億金幣,

  • or whatnot, in Lego Star Wars.

    或者諸如此類的東西,在樂高星球大戰。

  • Though apparently, there's a way to trick the game,

    雖然很明顯,有 一種方法來欺騙遊戲,

  • there's a bug or feature, that lets you just accrue lots and lots of points.

    有一個錯誤或功能,可以讓你 只是累積很多很多的點。

  • But the largest possible value, according

    但最大的可能 值,根據

  • to this screen shot of someone I found online, is indeed, 4 billion.

    這個屏幕截圖的人,我 網上找到的,確實是,4個十億。

  • >> Now why is that?

    >> 現在,這是為什麼?

  • It's precisely 4 billion, probably because someone decided,

    這正是4個十億, 可能是因為有人決定,

  • who was writing this game, that they could do 4 billion,

    誰在寫本場比賽, 他們可以做4組十億,

  • something, something, something, like the value I put up

    什麼,什麼,什麼, 像值我忍了

  • with the calculator earlier, but it's just a little cleaner for humans

    用計算器較早,但 它只是一個少許清潔劑對人類

  • to say the maximum number of coins-- or studs,

    說的最大數量 的coins--或螺栓,

  • as they call them-- that you can collect is 4 billion.

    他們稱之為them--你 可以收集4十億。

  • And so, why is this?

    所以,這是為什麼?

  • How is the LEGO game implementing the counter

    如何為樂高遊戲 實施反

  • that's keeping track of the number of coins you have?

    那就是維持軌道的 硬幣有多少?

  • They're using what?

    他們使用的是什麼?

  • AUDIENCE MEMBER: It stops counting after 4 billion.

    觀眾:它停止 4個十億後計數。

  • DAVID J MALAN: It stops counting after 4 billion,

    大衛·馬蘭:它停止 4個十億計數後,

  • which means you can infer, as a programmer,

    這意味著你可以 推斷,作為一個程序員,

  • that they're probably using a 32 bit integer.

    他們很可能 使用32位的整數。

  • Like the programmer literally just typed, int, in his or her code,

    就像程序員真的只是 類型化,INT,在他或她的代碼,

  • and that's the type of variable that they're using to store someone's code.

    這就是可變的類型 他們使用存儲別人的代碼。

  • So there's other manifestations of these kinds of limits.

    因此,有其他表現 這些類型的限制。

  • >> So I have not played this game-- and I was reading up on the history

    >> 所以,我沒有玩過這個game-- 而我讀了歷史

  • to confirm as much-- but in the original version of Civilization, where

    確認作為muc​​h--但在 文明,那裡的原始版本

  • you apparently interact with each other and can wage war or have peace,

    你顯然與每個互動 另外,可以發動戰爭或和平,

  • Gandhi was supposed to be one of the most peaceful characters, as I

    甘地被認為是一個 最和平的角色,因為我

  • understand it, in the first version of Civilization.

    理解,在第一 版本文明。

  • And in fact, on a scale of 1 to 10, his aggressiveness was just a 1.

    而事實上,在範圍為1至10, 他的侵略性只是一個1。

  • So ever so mildly aggressive, apparently.

    所以永遠是那麼溫和 侵略性,顯然。

  • >> But at some point, you can apparently install democracy in your geography.

    >> 但是,在某些時候,你可以很明顯 在地理上安裝民主。

  • And if you install democracy into your version of the game,

    如果你安裝的民主 到你的遊戲版本,

  • then your aggression level goes down.

    那麼你的攻擊水平下降。

  • It's a good thing.

    這是一個很好的事情。

  • People are more tranquil apparently, in that situation.

    人們更安靜 顯然,在這種情況下。

  • But apparently, someone did not have an, if, condition

    但很顯然,有人做 沒有一個,如果,條件

  • in the original version of the code.

    在代碼中的原始版本。

  • >> So Gandhi's aggression level went from positive 1, minus 2, to negative 1,

    >> 所以,甘地的侵略水平由去 正1,負2,負1,

  • but the game doesn't understand negative numbers.

    但遊戲不 了解負數。

  • So what happened was Gandhi's aggression level went from 1, to 0, to negative 1,

    所以,事情的經過是甘地的侵略 從1級去了,到0,負1,

  • which had the effect of wrapping around to being

    這具有的效果 折回是

  • the most aggressive character in the game, at a value of 255,

    最積極的角色 在比賽中,以255的值,

  • on a scale of 1 to 10.

    上的比例為1至10。

  • And since then, there's been more incarnations of this game,

    從那時起,還有的是 更化身這場比賽,

  • and they've kept it as a sort of Easter egg,

    他們已經把它作為 一種復活節彩蛋的,

  • that Gandhi is apparently so terribly aggressive.

    甘地顯然是 這麼可怕的攻擊性。

  • But it was the result of a very simple programming error

    但它是一個結果 很簡單的編程錯誤

  • in that very early version of the game.

    在遊戲中的該非常早期版本。

  • >> Now more disturbingly, more recently, the Boeing 787

    >> 現在更令人不安的是,多 最近,波音787

  • was documented as having a bug, not the kind of device

    被記錄為具有 錯誤,不是那種設備

  • you particularly want to have a bug.

    你特別想有一個bug。

  • And the symptoms, that I'll read here, from an article online,

    且症狀,我會讀 在這裡,從網上的文章,

  • was this, a model 787 airplane that has been powered continuously

    是這樣的,一個模型787飛機 已連續供電

  • for 248 days can lose all alternating current, AC,

    248天就失去了所有 交變電流,交流,

  • electrical power, due to the generator control units,

    電功率,由於 發電機控制單元,

  • GCUs, simultaneously going into fail-safe mode.

    GCUs,同時 進入故障安全模式。

  • So this was a warning issued when this problem was discovered.

    因此,這是發出警告 當這個問題被發現。

  • This condition is caused by a software counter,

    這種情況是由於 通過軟件計數器,

  • internal to the GCUs, so an integer or a variable,

    內部的GCUs,所以 整數或一個變量,

  • that will overflow after 248 days of continuous power.

    248之後溢出 連續功率天。

  • Boeing is in the process of developing a GCU software upgrade that

    波音是在過程中 開發GCU軟件升級

  • will remedy the unsafe condition.

    將彌補的不安全狀態。

  • >> So much like the missile scenario, wherein

    >> 那麼像導彈 方案中,其中,

  • they had some kind of variable that was counting, and counting,

    他們有一些類型的變量 被計數,計數,

  • and counting, but gradually overflowing the boundaries of its capability,

    和計數,但漸漸溢滿 其能力的界限,

  • similarly did the damn airplane have a variable overflow

    同樣做了該死的飛機 具有可變的溢出

  • after enough time of running.

    經過足夠的時間運行。

  • And so the tongue-in-cheek way of working around this issue

    這樣一來,舌頭在臉頰的方式 解決此問題的工作

  • is to truly, reboot, your plane every 247 days, so that the memory gets wiped

    是真正的,重新啟動,你的飛機每 247個天,使得存儲器被擦拭

  • and the variable goes back to 0.

    和可變回到0。

  • But realize, this is a very large incarnation of software,

    但要意識到,這是一個非常 大型化身軟件,

  • but especially as we hear about Apple's operating systems going into cars,

    但特別是當我們聽到蘋果 操作系統進入汽車,

  • and self-driving cars from Google, and any number of incarnations of software

    並從谷歌自駕車車, 任意數量的軟件的化身

  • in our daily lives, TVs and watches, and more,

    在我們的日常生活中,電視 和手錶等,

  • realize were surrounded by software, all of which is written by us humans.

    實現由軟件所包圍, 所有這一切都寫的是我們人類。

  • >> And as we'll all soon discover, it's very easy and very typical

    >> 正如我們都會很快就會發現, 它很容易和非常典型

  • to make mistakes when writing software.

    編寫軟件時犯錯誤。

  • And if you don't catch them, some bad things can happen.

    如果你不抓住他們, 一些不好的事情都可能發生。

  • Now sometimes, some funny things can happen,

    現在有時,一些 有趣的事情都可能發生,

  • or at least sometimes we know to expect some badness.

    或者至少有時候我們 知道會發生一些不良。

  • So 0 divided by 0, from grade school, is generally a bad thing.

    因此,0除以0,從等級 學校,一般是一件壞事。

  • It's undefined.

    這是不確定的。

  • And it turns out-- and let's see if my mic can pick this up-- that Apple

    而事實證明out--,讓如果我們看 我的麥克風可以選擇此up--蘋果

  • had some fun with this recently.

    曾與這一些有趣的最近。

  • So I have an iPhone here.

    所以,我有一個iPhone在這裡。

  • I'm going to talk to Siri and ask her to give me the answer to 0 divided by 0.

    我要談的Siri,問她 給我的答案0除以0。

  • What is 0 divided by 0?

    什麼是0除以0?

  • >> SIRI: Imagine that you have 0 cookies, and you split them evenly

    >> SIRI:假設您有0 餅乾,和你平分他們

  • among 0 friends.

    在0朋友。

  • How many cookies does each person get?

    多少餅乾確實每個人得到什麼呢?

  • See?

    看到了嗎?

  • It doesn't make sense.

    它沒有意義。

  • And Cookie Monster is sad that there are no cookies.

    而且餅乾怪獸是悲哀 有沒有餅乾。

  • And you are sad that you have no friends.

    而你傷心, 你有沒有朋友。

  • DAVID J MALAN: It's kind of obnoxious.

    大衛·馬蘭:這是一種令人厭惡的。

  • So this is what was just said there.

    所以這就是剛才說有。

  • It's indeterminate, it's not defined, and indeed, many programming languages

    這是不確定的,它沒有定義, 而事實上,許多編程語言

  • or, really, compilers will detect when you, in a program,

    或者,真的,編譯器會 當您,檢測程序,

  • try to divide 0 by 0.

    試圖除以0 0。

  • More fun than this though, is that apparently, Cookie Monster

    更多樂趣比這雖然是 ,顯然,餅乾怪獸

  • is on Twitter these days.

    是在Twitter上的這些日子。

  • And he replied to this, with this, which is absolutely adorable.

    他回答了,這一點, 這是絕對可愛。

  • But let's take a quick look at a couple of other constructs,

    但是,讓我們快速瀏覽一下 一對夫婦的其他構造,

  • and then put some of this code to use, in good ways.

    然後把一些這方面的 代碼中使用,以良好的方式。

  • So it turns out, besides for loops, there's

    所以,事實證明,除了 對循環,有

  • something called a while loop, that looks different and is implemented

    一種叫做while循環,即 看起來不同的,是實現

  • a little differently-- and we'll eventually see examples--

    一個小differently--和 我們最終會看到examples--

  • but in some sense, it's simpler because it

    但在某種意義上, 它的簡單,因為它

  • doesn't allow you to initialize and update within the boundary of the loop.

    不允許初始化和 環路的邊界之內更新。

  • You can still implement it.

    你仍然可以實現它。

  • So you can do the exact same things with a while loop, as with a for loop,

    所以,你可以做同樣的事情 與while循環,與一個for循環,

  • but your syntax, ultimately-- as we'll eventually

    但你的語法,ultimately-- 因為我們最終會

  • see-- is going to be different.

    see--將是不同的。

  • There's even a do while loop, which is actually a little different,

    甚至還有一個do while循環,這 其實是有一點不同,

  • in that whereas a for loop and a while loop always

    在而一個for循環 和while循環總是

  • check their condition first, if you read this thing top to bottom,

    首先檢查自己的條件,如果 你看過這個東西從上到下,

  • it kind of looks like it's going to check it's condition last because it's

    那種看起來它會 檢查它的情況最後,因為它是

  • truly the last line of code.

    真正的最後一行代碼。

  • And indeed, that's going to be useful in certain programs that we write,

    事實上,這將是有益的 在我們編寫某些程序,

  • if you want to just blindly do something and eventually check the condition.

    如果你想只是盲目地做一些事情 最終檢查的條件。

  • That's not necessarily a bad thing.

    這不一定是壞事。

  • If we want variables, we can do it in a couple of different ways.

    如果我們想要的變量,我們可以做 它在幾個不同的方式。

  • And we saw in the FoxTrot cartoon, one way of doing it,

    而我們的狐步舞看見 卡通,做的一種方法,

  • where you declare your variable, like int counter semicolon,

    在這裡你聲明的變量, 如int計數器分號,

  • and then later-- maybe the next line, maybe 10 lines later--

    然後later--說不定下 行,也許10線later--

  • you actually initialize it.

    你居然初始化。

  • So these 2 lines of code declare a variable of type int

    因此,這2行代碼 聲明一個int類型的變量

  • and call it counter, so it gives me enough bits to hold an int.

    並稱之為反,所以它給 我足夠的​​位來容納一個int。

  • And then eventually, it puts the value 0 into that variable.

    然後最終,它把 值0到該變量。

  • It arranges the 0's and 1's in a pattern, that we know from last week,

    它排列0和1的在一個 模式,我們知道上週,

  • represents the number we know as 0.

    代表我們所知道的0號。

  • Or frankly, you can do this much more succinctly, just like this.

    或者坦白地說,你可以做這麼多 更簡潔地說,就這樣。

  • >> Now we also have the ability to call functions.

    >> 現在,我們也有 能夠調用函數。

  • And in fact, here's a 2 line program, or an excerpt thereof,

    而事實上,這裡有一個2線 程序,或它們的摘錄,

  • that allows us to actually write some code that

    這使我們能夠真正 編寫一些代碼,

  • gets a string from the user-- much like our volunteer a moment ago--

    得到來自user--字符串多 就像我們的志願者一時ago--

  • storing the result in a variable called name,

    結果存儲到 一個名為名稱的變量,

  • and then-- much like with our volunteer with printf-- prints out those values

    和then--很像我們的志願者 與printf--打印出這些值

  • by passing in two arguments, the string, followed by the variable

    通過傳遞兩個參數, 串,其次是可變

  • called, name, itself.

    叫,名稱本身。

  • So let's take a look, before we come back to Mario there,

    因此,讓我們來看看,前 我們再回過頭來馬里奧那裡,

  • at a couple of now, examples of this.

    在一對夫婦的這個現在的例子。

  • >> I'm going to go ahead and open up, let's say, function-0.c.

    >> 我要繼續前進,開 了,讓我們說,功能0.c。

  • And as always, this code is available on the course's website,

    和往常一樣,這個代碼是 可在球場上的網站,

  • so you can play along at home and look at it later.

    這樣你就可以在一起玩 回家看它以後。

  • But here's the program in it's essence, from line 17 to 22.

    但這裡有一個程序在它的 本質上,從線17至22。

  • The main program is where the program is always going to start.

    主程序所在 計劃總是會啟動。

  • This program, apparently, is going to print out your name, colon.

    這一方案,顯然,是怎麼回事 打印出你的名字,冒號。

  • It's then going to call GetString, just like we did with our volunteers.

    它隨後將打電話的GetString, 就像我們做了與我們的志願者。

  • And then, this is interesting, it's going to call PrintName.

    然後,這是有趣的, 它會調用PrintName。

  • It turns out, all this time, there seems to be a function called PrintName.

    事實證明,這一切的時候,有 似乎是被稱為PrintName函數。

  • That prints someone's name.

    可打印某人的名字。

  • We didn't need to use printf from yesteryear, there's PrintName.

    我們並不需要用printf從 昔日,有PrintName。

  • >> But that's misleading because PrintName does not

    >> 但是,這是誤導 因為PrintName不

  • come with C. People did not invent it some 40 or 50 years ago,

    配備了C.人們不 一些40年或50年前發明了它,

  • I did, instead.

    我這樣做,而不是。

  • And in fact, if I scroll down further, notice

    而事實上,如果我滾動 進一步下降,通知

  • how I can write my own functions in C. We'll eventually

    我怎麼能寫我自己 在C函數我們最終會

  • explain why we keep saying, void, in a few places,

    解釋為什麼我們一直在說, 無效,在少數地方,

  • but for today, let's just look at the name.

    但今天,讓我們 光看這個名字。

  • >> On line 24, if you want to create your own function,

    >> 在第24行,如果你想 創建自己的功能,

  • you literally write the name of the function.

    你從字面上寫的 功能的命名。

  • I chose PrintName.

    我選擇了PrintName。

  • In parentheses, you then specify what kinds

    在括號,你 然後指定哪幾種

  • of inputs, and how many you want this function to take.

    投入,有多少你 希望此功能可走。

  • In this case, I want to take 1 variable called, name,

    在這種情況下,我想帶 1變量調用,名稱,

  • and it's going to be of type, string, so it's going

    而這將是 型,字符串,因此它會

  • to be some sequence of characters.

    是一串字符。

  • And then, this program-- much like in Scratch,

    然後,這個program-- 就像在刮,

  • you can have custom puzzle pieces-- is going to have this custom behavior.

    你可以定制拼圖pieces-- 將有這種自定義行為。

  • It's going to call printf passing in, hello, placeholder,

    這將調用printf 傳遞,你好,佔位符,

  • and then it's going to plug in whatever the user called.

    然後它會堵塞 在任何用戶調用。

  • >> So this is an example of what a computer scientist would

    >> 所以這是一個例子是什麼 計算機科學家會

  • call abstraction or functional decomposition, which are just

    調用抽象或功能 分解,這只是

  • fancy ways of saying-- Is if you like this high level idea,

    花哨的方式saying--是,如果 你喜歡這個高層次的想法,

  • like I want functionality that prints someone's name, absolutely you

    像我想功能, 打印某人的名字,絕對是你

  • can literally write printf and then pass in the arguments you want,

    可以從字面上寫的printf然後 通過在你想要的參數,

  • and the program will work, as it has since Wednesday.

    並計劃將工作, 因為它有自週三。

  • But you can start to abstract away the notion of printing a name.

    但是,你可以開始抽象掉 打印名的概念。

  • You can give it a name, like PrintName, and this

    你可以給它一個名字, 像PrintName,和這

  • is this idea of layering from week 0.

    這種想法從0週分層的。

  • >> Henceforth, I and you don't have to know or care how PrintName is implemented.

    >> 從今往後,我和你不必知道 或護理PrintName是如何實現的。

  • Yes it uses printf, maybe it doesn't, who knows what uses?

    是的,它使用printf的,也許它 不,誰知道什麼用?

  • Who cares?

    誰在乎呢?

  • Now I'm talking up here, instead of down here.

    現在,我說在這裡, 而不是到這裡的。

  • And indeed, as our programs get more advanced and sophisticated,

    事實上,作為我們的計劃獲得 更加先進和複雜,

  • we're going to keep taking for granted that lower level puzzle pieces exist.

    我們將繼續採取理所當然 較低級別的拼圖存在。

  • Because we wrote them or someone else did, so

    因為我們寫了他們 或者別人沒有,所以

  • that we can then build on top of them.

    我們可以再建在它們上面。

  • Let's take a look at this variant, function one.

    讓我們來看看這個 變型,功能之一。

  • >> So this one's a little more advanced, but it turns out

    >> 所以這一塊是一個有點多 先進的,但事實證明,

  • that in CS50's library, there's only a GetInt function.

    在CS50的圖書館, 這裡只有一個調用getInt功能。

  • We didn't think, years ago, to implement a GetPositiveInt function.

    我們沒有想到,幾年前,到 實現GetPositiveInt功能。

  • And that's a little annoying because if you guys are writing a program wherein

    這是一個有點惱人,因為如果 你們正在編寫一個程序,其中

  • you want to get a positive integer from the user,

    你想獲得一個積極的 從用戶的整數,

  • you can absolutely use GetInt.

    你完全可以使用調用getInt。

  • And you can absolutely check with the condition and maybe a loop

    你完全可以用檢查 條件,也許一個循環

  • if that int is greater than 0 and yell at the user

    如果int是更大 比0和罵的用戶

  • if he or she doesn't give you a positive number.

    如果他或她不給 你正數。

  • But let's build this building block ourselves, a,

    但是,讓我們建立這個 建築擋住自己,一,

  • custom Scratch piece, if you will.

    定制刮片,如果你願意。

  • I'm going to have a program here that ultimately, I

    我將有一個程序 在這裡,最終,我

  • want to be able to call GetPositiveInt, and I want to be

    希望能夠調用 GetPositiveInt,我想成為

  • able to print out whatever that int is.

    能夠打印出什麼那int是。

  • But this is abstracted away now.

    但是,這是現在的抽象化了。

  • It's just been given a high level name that says what it does,

    它只是被賦予了很高的水平 名稱它做什麼,

  • which is wonderful because it's very intuitive now to read.

    這是美妙的,因為它是 非常直觀,現在讀。

  • And if I do care what's underneath the hood, let me scroll down.

    如果我做不在乎什麼是底下 引擎蓋,讓我向下滾動。

  • And it's a little intimidating at first, especially

    這是一個有點嚇人 在第一,尤其

  • if this is your first program, but let's take a look.

    如果這是你的第一個程序, 但讓​​我們一起來看看。

  • >> I'm no longer saying, void, because it turns out functions,

    >> 我不再說的,無效的, 因為事實證明功能,

  • much like GetString, can return a value to me.

    就像GetString的,可 返回一個值給我。

  • They don't just have to print to the screen,

    他們不只是有 打印到屏幕上,

  • they can actually hand me something back.

    他們實際上可以手 我的東西回來。

  • And whereas before PrintName, I didn't need anything back.

    和而PrintName之前, 我沒回來需要什麼。

  • I needed the side effect of something showing up on the screen,

    我需要的副作用 東西顯示在屏幕上,

  • but I didn't need a human to hand me something back.

    但我並不需要一個人 遞給我的東西回來。

  • Here, with GetPositiveInt, like with GetInt,

    這裡,與GetPositiveInt, 像調用getInt,

  • I want to be handed something back.

    我想交到東西回來。

  • So I'm saying not, void, on line 23, but int,

    所以我說不是的,無效的, 在線路23,但整型,

  • which says, this function that I am writing,

    它說,該功能 我正在寫,

  • called GetPositiveInt is going to hand me back an integer, not nothing,

    稱為GetPositiveInt是要 遞給我回一個整數,沒有什麼,

  • not void.

    不作廢。

  • >> Meanwhile, it's going to take no inputs, so I've, kind of, reversed it.

    >> 同時,要採取不 投入,所以我,善良,推翻它。

  • I'm not giving GetPositiveInt any input, I want it to give me its output.

    我不給GetPositiveInt任何 輸入時,我希望它給我的輸出。

  • And then what happens now?

    然後現在會發生什麼?

  • So here's how I can declare a variable.

    因此,這裡就是我可以聲明一個變量。

  • I've done it outside of the loop, for reasons we'll eventually see,

    我一直在循環之外做了, 對於原因,我們最終會看到,

  • but this just gives me 32 bits called, n,

    但是這只是給 我的32位調用,N,

  • and I've pre-determining them to store integer.

    我已經預先確定 他們存儲的整數。

  • And here's that, do while construct, and this is why it's useful.

    而下面這一點,做while結構, 這就是為什麼它是非常有用的。

  • Literally do this, while n is less than 1.

    從字面上做到這一點, 而n小於1。

  • So let's see what happens.

    因此,讓我們看看會發生什麼。

  • I print out, please give me a positive int.

    我打印出來,請 給我一個積極的INT。

  • I then get an int, using CS50's function and stored in n.

    然後我得到一個int,使用CS50的 功能與存儲在n個。

  • >> And then, what line of code probably gets executed next, logically?

    >> 然後,什麼樣的代碼行可能 下一個被執行,邏輯?

  • Which line number?

    哪條線是多少?

  • Yeah, so 31.

    是啊,所以31。

  • You wouldn't know this until you've been told or sort of infer it,

    你不會知道,直到你 被告知或排序的推斷是,

  • but that's true.

    但是這是真的。

  • It goes top to bottom and then keeps repeating.

    不言而喻從上到下 然後不斷重複。

  • So if I have typed in say, the number negative 1, is n less than negative 1?

    所以,如果我已經輸入中說,數 負1,為n比1負少?

  • Yeah.

    是啊。

  • Because negative 1 is less than 1.

    因為負1小於1。

  • So what should happen?

    那麼會發生什麼情況?

  • I'm going to do this while n is less than 1,

    我要做到這一點 而n是小於1,

  • so I'm going to go back to line 28.

    所以我打算回去線28。

  • >> And each time-- and let's run this-- make function 1 to compile it,

    >> 而每一個時間 - 讓我們運行this-- 讓功能1編譯它,

  • and now dot slash function 1.

    現在點斜線功能1。

  • If I type negative 1, it's going to keep yelling at me

    如果我型負1,這是 要保持吼我

  • until I cooperate because each of my inputs is less than 1

    直到我合作,因為每個 我的輸入小於1

  • and if it's while less than 1, I'm going to keep doing this.

    如果它而小於1, 我將繼續這樣做。

  • If I finally give it a number like 50, thankfully, it says, thanks for the 50.

    如果我終於給它一些像50, 值得慶幸的是,它說,感謝50。

  • Why?

    為什麼呢?

  • Because as soon as n is not less than 1, I stop getting stuck in this loop,

    因為只要n是不小於 1,我停止陷在這個循環中,

  • and this new keyword today, return, literally does that.

    而這種新的關鍵字的今天, 返回時,從字面上做到這一點。

  • So I've just implemented, in a sense, the equivalent of GetString,

    所以,我剛剛實施,在 感,GetString的相當於,

  • where I'm handing back to whoever is using me, some value.

    在那裡我遞回 不管是誰用我的,一定的價值。

  • It doesn't have to be a string, it's an int.

    它並不必須是 一個字符串,它是一個int。

  • So a simple, quick example, but we'll soon

    因此,一個簡單,快捷 舉例來說,但我們會很快

  • see some more sophisticated versions still.

    看到一些更複雜 版本還在。

  • In fact, let's take a look at a numeric one, which is called return.c.

    事實上,讓我們來看看一個 數字1,這是所​​謂return.c。

  • And this one's actually a little simpler.

    而這其中實際上 簡單一點。

  • So this program's purpose in life-- let's compile and run it,

    因此,在這個方案的目的 life--讓我們編譯並運行它,

  • so make return, dot slash, return-- notice

    所以要回報,點 斜線,return--通知

  • the program simply cubes the value 2.

    程序簡單立方體的價值2。

  • It's pretty stupid, it's hard code, it doesn't take any inputs,

    它是非常愚蠢的,這是很難 代碼,它沒有採取任何投入,

  • but it does demonstrate another function that I've written myself.

    但它確實說明另一個 我寫我自己的功能。

  • So here, I've declared a variable, called x, of type int,

    所以在這裡,我宣布一個 可變的,所謂的類型為int x,,

  • equal to the number 2, completely arbitrary.

    等於數字2, 完全是任意的。

  • This is just some fluffy printing.

    這僅僅是一些蓬鬆打印。

  • It says x is now, such and such, cubing dot dot dot.

    它說,x是現在,這樣的和 這樣,多維數據點點點。

  • And the magic is apparently in line 21.

    而神奇的是顯然的第21行。

  • I'm calling a function called, cube, I'm handing it a sheet of paper

    我打電話調用的函數,立方體, 我交給它的一張紙

  • with the number 2 written on it, and what value, mathematically,

    用數字2寫在上面, 和什麼樣的價值,數學,

  • do I want to get out of it?

    我想出來的嗎?

  • Just as a sanity check?

    正如完整性檢查?

  • 8.

    8。

  • Right?

    對嗎?

  • I want 2 cubed back, 2 to the power of 3, so 8 back.

    我想2立方回,2〜 3的力量,所以8回。

  • >> So where is cube implemented?

    >> 那麼,是立方體實現?

  • Well, notice it's implemented down here.

    好吧,發現它在這裡實現了。

  • And just like before, logically, even though the syntax is probably

    就如同之前,從邏輯上講, 即使語法可能是

  • very new to many of you, I want this function

    很新,你們中許多人, 我想這個功能

  • to hand me back a sheet of paper with an int on it.

    遞給我回來的片 紙,上面有一個int。

  • So I have an int, the name is arbitrarily,

    所以,我有一個int,該 名字是隨意,

  • but conveniently called cube.

    可方便地稱為立方體。

  • The input to it, is n of type integer, so that's

    輸入到它,是n 整型,所以這是

  • how I can pass in the number 2 on a sheet of paper.

    我怎麼可以通過在數 2上一張紙。

  • And then it turns out C supports math, so you don't have x's for times,

    然後事實證明C支持 數學,所以你不用X的的時間,

  • you just use the asterisk for multiplication.

    你只需要使用星號 乘法。

  • And this returns n times n times n, which is simply a cubed value.

    而這個返回n次n次 N,這是一個簡單的立方值。

  • >> So where are we going with all of this?

    >> 那麼,我們要與這一切?

  • This is definitely a whirlwind tour, rest assured,

    這絕對是一個 旋風之旅,放心,

  • that in the super sections and in problem set 1,

    在超級節 而在問題設置1,

  • you'll be walked through all this all the more.

    你可以通過走 這一切全愈。

  • And in problem set 1, we'll transition from the graphical world of Scratch

    而在問題設置1,我們將過渡 從頭開始的圖形世界

  • to something more command line in C. But we'll draw inspiration

    到更多的東西命令行 在C.但我們會汲取靈感

  • from this here game from yesteryear, wherein

    從此這裡的遊戲 從昔日的,其中,

  • using C and the standard edition of the p set you'll implement Mario's pyramid.

    使用C和的標準版 p將您將實現馬里奧的金字塔。

  • And in the hacker edition of the p set, if you so choose to elect,

    而在的黑客版 p將,如果你選擇選舉,

  • you will implement a bit more challenging pyramid with two peaks.

    您將實現多一點 有兩個高峰挑戰金字塔。

  • You'll also implement an algorithm, a greedy algorithm.

    您還可以實施 算法,貪婪算法。

  • It turns out there's some interesting logic

    原來有 一些有趣的邏輯

  • behind the process of running a cashier's station

    過程背後 運行出納站

  • and actually handing someone back change.

    實際上交給 有人背的變化。

  • There is an algorithm that's fairly straightforward, that you might even

    有一種算法,是相當 簡單的,你甚至可能

  • grasp intuitively when you first read it-- realizing that's what I've always

    直觀地把握,當你第一次讀到 它 - 意識到這就是我一直

  • done any time I've given someone some money back-- that allows you to always

    做我已經給了一些人的任何時間 錢back--,讓你永遠

  • minimize the number of paper notes or metal coins

    最小化的數量 紙幣或金屬幣

  • that you're handing back to the user.

    您正在移交給用戶。

  • And this, of course, is compelling because if you go to CVS or whatnot,

    而這,當然,是引人注目 因為如果你去CVS或諸如此類的東西,

  • you don't want to be handed a whole bunch of ones

    你不想成為手 一大堆的人

  • or a whole bunch of pennies.

    或者一大堆便士。

  • You want the fewest coins, probably, possible.

    你想最少 硬幣,大概,可能。

  • >> Finally, you'll also be challenged to dabble in the world of water

    >> 最後,你也可以挑戰 在水世界玩水

  • and actually get an appreciation for a mapping between rates of flow,

    而實際上得到升值 對於流動速率之間的映射,

  • of like, water in a shower, just how much water is used.

    都喜歡,水淋浴, 到底有多少水被使用。

  • And the allusion therein, will be this clip here,

    而其中的典故, 將這個片段在這裡,

  • which we'll end on for just 60 seconds, that

    我們將結束對 短短60秒,即

  • paints a picture of low-flow shower heads.

    描繪了一幅圖片 低流量的淋浴噴頭。

  • >> [VIDEO PLAYBACK]

    >> [視頻回放]

  • -All right.

    - 所有權利。

  • I got everything here.

    我在這裡得到的一切。

  • I got the Cyclone F Series, Hydra, Jetflow, Stockholm

    我得到了旋風F系列, 水潤,Jetflow,斯德哥爾摩

  • Supersteam, you name it.

    Supersteam,你的名字。

  • >> -And what do you recommend?

    >> - 和你有什麼建議?

  • >> -What are you looking for?

    >> - 什麼是你找誰?

  • >> -Power man.

    >> -Power的人。

  • Power.

    電源。

  • -Like Silkwood.

    樣絲克伍事件。

  • -That's for radiation.

    - 這就是輻射。

  • -That's right.

    - 那是正確的。

  • -Now what is this?

    - 現在這是什麼?

  • -That's the Commando 450.

    - 這就是特種兵450。

  • I don't sell that one.

    我不賣這個。

  • >> -But that's what we want.

    >> - 但是,這就是我們想要的。

  • It's a commando 450.

    這是一個突擊隊450。

  • >> -No believe me.

    >> - 不相信我。

  • It's only used in the circus.

    它僅用於馬戲團。

  • It's for elephants.

    這是大象。

  • >> -I'll pay anything.

    >> -I'll支付任何費用。

  • >> -What about Jerry?

    >> - 什麼對傑里?

  • >> -He couldn't handle that.

    >> - 他不能處理。

  • He's delicate.

    他細膩。

  • Oh yeah.

    哦,是的。

  • [END PLAYBACK]

    [結束播放]

  • DAVID J MALAN: All right.

    大衛·馬蘭:好的。

  • That's if for CS50.

    那如果CS50。

  • We'll see you next week.

    我們會看到你下週。

  • >> SPEAKER 1: [? Scully ?], [? Ian, ?] as far as this outro project,

    >> 揚聲器1:[?斯卡利?],[?伊恩?] 只要這個尾奏項目,

  • what have you guys come up with?

    你有什麼傢伙想出了?

  • >> SPEAKER 2: Well, we've given it a variety of thought,

    >> 揚聲器2:好了,我們已經給 它的各種思想,

  • and we think that the best way to--

    我們認為,最好的辦法用於:

  • >> SPEAKER 3: May I?

    >> 揚聲器3:我?

  • >> SPEAKER 2: Yeah.

    >> 揚聲器2:是的。

  • By all means, actually.

    通過一切手段,其實。

  • >> SPEAKER 3: So I think we can sum up our idea

    >> 揚聲器3:所以我覺得 我們可以總結一下我們的想法

  • for the outros with one word-- nothing.

    對於一個word--什麼的片尾。

  • >> DAVID J MALAN: Nothing?

    >> 大衛·馬蘭:沒事?

  • SPEAKER 3: Nothing.

    揚聲器3:沒有。

  • DAVID J MALAN: What does that mean?

    大衛·馬蘭:這是什麼意思?

  • SPEAKER 3: The outros are about nothing.

    SPEAKER 3:片尾是關於什麼。

  • SPEAKER 2: Well, I mean, in philosophy, I mean, nothing is always something.

    揚聲器2:嗯,我的意思是,在哲學, 我的意思是,沒有什麼是永遠的東西。

  • SPEAKER 1: So what's-- what's the premise?

    揚聲器1:所以what's-- 有什麼前提?

  • SPEAKER 3: So it's like life.

    揚聲器3:所以它就像生活。

  • OK.

    確定。

  • What did you do today?

    今天你做了什麼?

  • >> DAVID J MALAN: I got up, had breakfast, and came to work.

    >> 大衛·馬蘭:我站起來,有 早餐,來到工作。

  • >> SPEAKER 3: That's an outro.

    >> 揚聲器3:這是一個片尾曲。

  • SPEAKER 2: But, I mean, shouldn't something happen to him on the--

    揚聲器2:但是,我的意思是,不應該 事情發生在他身上的the--

  • SPEAKER 3: No, no, no, no.

    揚聲器3:不,不,不,不。

  • Nothing happens.

    什麼也沒有發生。

  • >> SPEAKER 1: So why are we watching?

    >> 揚聲器1:那麼,為什麼我們看什麼?

  • >> SPEAKER 3: Because it's an outro for CS50.

    >> 揚聲器3:因為 這是一個尾奏的CS50。

  • >> DAVID J MALAN: Not yet.

    >> 大衛·馬蘭:還沒有。

>> DAVID J MALAN: All right, so this is CS50.

>> 大衛·馬蘭:所有 沒錯,所以這是CS50。

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋