用JS寫了一個模擬串行加法器

在重溫《編碼:隱匿在計算機軟硬件背后的語言》第12章——二進制加法器時,心血來潮用JS寫了一個模擬串行加法器。

測試斷言工具TestUtils.js

function assertTrue(actual){if(!actual)throw "Error actual: " + actual + " is not true."
}function assertFalse(actual){if(actual)throw "Error actual: " + actual + " is not false."
}function assertIntEquals(expected, actual){if(typeof expected != "number")throw "Error expected: " + expected +" is not a number."if(typeof actual != "number")throw "Error actual: " + actual +" is not a number."if(expected != actual)throw "Error expected: " + expected + " and actual: " + actual +" is different."
}

十進制數與二進制數之間轉換工具utils.js

function int2LowEightBitArray(num){var result = []for(var i = 0; i < 8; i++)result.push(((num >> i) & 1) == 1)return result
}function lowEightBitArray2Int(array){var result = 0for(var i = 0; i < 8; i++){if(array[i])result += (1 << i)}return result
}function lowNineBitArray2Int(array){var result = 0for(var i = 0; i < 9; i++){if(array[i])result += (1 << i)}return result
}//用補碼表示負數
function int2EightBitArray(num){if(num < -128 || num > 127){throw "Out of boundary(-128, 127)."}var result = []for(var i = 0; i < 8; i++)result.push(((num >> i) & 1) == 1)return result
}function eightBitArray2Int(array){var result = 0for(var i = 0; i < 7; i++){if(array[i])result += (1 << i)}if(array[i])result += -128return result
}function int2SixteenBitArray(num){if(num < -(1 << 15) || num > (1 << 15) - 1){throw "Out of boundary({left}, {right}).".replace("{left}", -(1 << 15)).replace("{right}", (1 << 15) - 1)}var result = []for(var i = 0; i < 16; i++)result.push(((num >> i) & 1) == 1)return result
}function sixteentBitArray2Int(array){var result = 0for(var i = 0; i < 15; i++){if(array[i])result += (1 << i)}if(array[i])result += -(1 << 15)return result
}

加法器模型model.js

class DoubleInGate{#id#in1#in2#gateTypeName#func#out#lastOut#outReceiver#outReceiverInNameconstructor(id, gateTypeName, in1 = false, in2 = false, func){this.#id = idthis.#gateTypeName = gateTypeNamethis.#in1 = in1this.#in2 = in2this.#func = functhis.#out = this.#func(this.in1, this.in2)// this.#lastOut = this.#out}updateIn1(flag){this.#in1 = flagthis.#updateOut()}updateIn2(flag){this.#in2 = flagthis.#updateOut()}getOut(){return this.#out;}updateBothIn(in1 = false, in2 = false){this.#in1 = in1this.#in2 = in2this.#updateOut()}setOutReceiver(outReceiver, inName){this.#outReceiver = outReceiverthis.#outReceiverInName = inName}#updateOut(){this.#lastOut = this.#outthis.#out = this.#func(this.#in1, this.#in2)if(this.#out != this.#lastOut){this.#send(this.#out)}}#send(flag){if(this.#outReceiver){this.#outReceiver.receive(this.#outReceiverInName, flag)}}receive(inName, flag){if(inName == "in1"){this.updateIn1(flag)}else if(inName == "in2"){this.updateIn2(flag)}}toString(){return "{gateTypeName} id: {id}, in1: {in1}, in2: {in2}, out:{out}".replace("{gateTypeName}", this.#gateTypeName).replace("{id}", this.#id).replace("{in1}", this.#in1).replace("{in2}", this.#in2).replace("{out}", this.#out)}
}//與門
class AndGate extends DoubleInGate{constructor(id, in1 = false, in2 = false){super(id, "AndGate", in1, in2, (i1, i2) => i1 && i2)}
}//或門
class OrGate extends DoubleInGate{constructor(id, in1 = false, in2 = false){super(id, "OrGate", in1, in2, (i1, i2) => i1 || i2)}
}//異或門
class XorGate extends DoubleInGate{constructor(id, in1 = false, in2 = false){//^在js按位異或, 而在java中不是super(id, "XorGate", in1, in2, (i1, i2) => (i1 || i2) && !(i1 && i2)) }
}class SingleInGate{#id#in#out#lastOut#func#outReceiver#outReceiverInNameconstructor(id, in_, func){this.#id = idthis.#in = in_this.#func = functhis.#out = this.#func(this.#in)// this.#lastOut = this.#out}updateIn(flag){this.#in = flagthis.#updateOut()}getOut(){return this.#out}//注冊輸出端口接收者,(類觀察者模式)setOutReceiver(outReceiver, inName){this.#outReceiver = outReceiverthis.#outReceiverInName = inName}#updateOut(){this.#lastOut = this.#outthis.#out = this.#func(this.#in)if(this.#out != this.#lastOut){this.#send(this.#out)}}#send(flag){if(this.#outReceiver){this.#outReceiver.receive(this.#outReceiverInName, flag)}    }receive(inName, flag){if(inName == "in"){ //TODO 或許有更靈活的寫法this.updateIn(flag)}}
}class NullGate extends SingleInGate{constructor(id, in_= false){super(id, in_, a=>a)}
}class NotGate extends SingleInGate{constructor(id, in_= false){super(id, in_, a=>!a)}
}class Adder{#id#inA#inB#outS#outC#lastOutS#lastOutC#outSReceiver#outSReceiverInName#outCReceiver#outCReceiverInNameconstructor(id, inA=false, inB=false, outS, outC){this.#id = idthis.#inA = inAthis.#inB = inBthis.#outS = outSthis.#outC = outC}updateInA(flag, func){this.#inA = flagthis.updateOutSAndOutC(func)}updateInB(flag, func){this.#inB = flagthis.updateOutSAndOutC(func)}updateBothIn(inA, inB, func){this.#inA = inAthis.#inB = inBthis.updateOutSAndOutC(func)}getOutS(){return this.#outS}getOutC(){return this.#outC}#updateOutS(flag){this.#lastOutS = this.#outSthis.#outS = flagif(this.#outS != this.#lastOutS){this.#sendOutS(this.#outS)}}#updateOutC(flag){this.#lastOutC = this.#outCthis.#outC = flagif(this.#outC != this.#lastOutC){this.#sendOutC(this.#outC)}}updateOutSAndOutC(func){if(func){var results = func()this.#updateOutS(results[0])this.#updateOutC(results[1])}}setOutSReceiver(outSReceiver, inName){this.#outSReceiver = outSReceiverthis.#outSReceiverInName = inName}setOutCReceiver(outCReceiver, inName){this.#outCReceiver = outCReceiverthis.#outCReceiverInName = inName}receive(inName, flag){if(inName == "inA"){this.updateInA(flag)}else if(inName == "inB"){this.updateInB(flag)}}#sendOutS(flag){if(this.#outSReceiver){this.#outSReceiver.receive(this.#outSReceiverInName, flag)}}#sendOutC(flag){if(this.#outCReceiver){this.#outCReceiver.receive(this.#outCReceiverInName, flag)}}
}class HalfAdder extends Adder{#xorGate#andGateconstructor(id, inA=false, inB=false){var xorGate = new XorGate(undefined, inA, inB);var andGate = new AndGate(undefined, inA, inB);super(id, inA, inB, xorGate.getOut(), andGate.getOut())this.#xorGate = xorGatethis.#andGate = andGate }#returnOutArray(){return [this.#xorGate.getOut(), this.#andGate.getOut()]}updateInA(flag){super.updateInA(flag, ()=>{this.#xorGate.updateIn1(flag)this.#andGate.updateIn1(flag)return this.#returnOutArray()})}updateInB(flag){super.updateInB(flag, ()=>{this.#xorGate.updateIn2(flag)this.#andGate.updateIn2(flag)return this.#returnOutArray()})}updateBothIn(inA, inB){super.updateBothIn(inA, inB, ()=>{this.#xorGate.updateBothIn(inA, inB)this.#andGate.updateBothIn(inA, inB)return this.#returnOutArray()})}}class FullAdder extends Adder{#inC#halfAdder1#halfAdder2#orGateconstructor(id, inA = false, inB = false, inC = false){var halfAdder1 = new HalfAdder(undefined, inA, inB)var halfAdder2 = new HalfAdder(undefined, inC, halfAdder1.getOutS())var orGate = new OrGate(undefined, halfAdder1.getOutC(), halfAdder2.getOutC())super(id, inA, inB, halfAdder2.getOutS(), orGate.getOut())this.#inC = inCthis.#halfAdder1 = halfAdder1this.#halfAdder2 = halfAdder2this.#orGate = orGatethis.#halfAdder1.setOutSReceiver(halfAdder2, "inB")this.#halfAdder1.setOutCReceiver(orGate, "in1")this.#halfAdder2.setOutCReceiver(orGate, "in2")}#returnOutArray(){return [this.#halfAdder2.getOutS(), this.#orGate.getOut()]}updateInA(flag){super.updateInA(flag, ()=>{this.#halfAdder1.updateInA(flag)return this.#returnOutArray()})}updateInB(flag){super.updateInB(flag, ()=>{this.#halfAdder1.updateInB(flag)return this.#returnOutArray()})}updateInC(flag){this.#inC = flagthis.#halfAdder2.updateInA(flag)this.updateOutSAndOutC(()=>{return this.#returnOutArray()})}updateBothIn(inA, inB){super.updateBothIn(inA, inB, ()=>{this.#halfAdder1.updateBothIn(inA, inB)return this.#returnOutArray()})}updateThreeIn(inA, inB, inC){super.updateBothIn(inA, inB)this.#inC = inCthis.#halfAdder1.updateBothIn(inA, inB)this.#halfAdder2.updateBothIn(inC, this.#halfAdder1.getOutS())this.#orGate.updateBothIn(this.#halfAdder1.getOutC(), this.#halfAdder2.getOutC())this.updateOutSAndOutC(()=>{return this.#returnOutArray()})}receive(inName, flag){super.receive(inName, flag)if(inName == "inC"){this.updateInC(flag)}}
}class EightBitBinaryAdder{#inC#outC#lastOutC#inA#inB#outS#fullAdders#fullAdderNum#outCReceiver#outCReceiverInName#outCInOutSFlagconstructor(outCInOutSFlag = false){this.#outCInOutSFlag = outCInOutSFlagthis.#inC = falsethis.#fullAdderNum = 8this.#fullAdders = []this.#inA = []this.#inB = []for(var i = 0; i < this.#fullAdderNum; i++){this.#inA.push(false)this.#inB.push(false)}//新鍵8個全加器for(var i = 0; i < this.#fullAdderNum; i++){this.#fullAdders.push(new FullAdder("f" + i))if(i != 0){this.#fullAdders[i - 1].setOutCReceiver(this.#fullAdders[i], "inC")}}this.updateOut()}updateBothIn(arrayA, arrayB){// this.#inC = inCthis.#inA = arrayAthis.#inB = arrayBfor(var i = 0; i < this.#fullAdderNum; i++){if(i == 0){this.#fullAdders[i].updateInC(this.#inC)}this.#fullAdders[i].updateBothIn(arrayA[i], arrayB[i])}this.updateOut()}//輸出默認值updateOut(){this.#outS = []for(var i = 0; i < this.#fullAdderNum; i++){this.#outS.push(this.#fullAdders[i].getOutS())if(i == this.#fullAdderNum - 1){this.#lastOutC = this.#outCthis.#outC = this.#fullAdders[i].getOutC()if(this.#lastOutC != this.#outC)this.#sendOutC(this.#outC)if(this.#outCInOutSFlag)this.#outS.push(this.#outC)}}}updateInC(flag){this.#inC = flagthis.updateBothIn(this.#inA, this.#inB, flag)}receive(inName, flag){if(inName == "inC"){this.updateInC(flag)}}#sendOutC(flag){if(this.#outCReceiver){this.#outCReceiver.receive(this.#outCReceiverInName, flag)}}setOutCReceiver(outCReceiver, inName){this.#outCReceiver = outCReceiverthis.#outCReceiverInName = inName}getOut(){return this.#outS}
}class SixteenBitBinaryAdder{#inC#outC#lastOutC#inA#inB#outS#eightBitBinaryAdder1#eightBitBinaryAdder2#bitNum = 16#outCReceiver#outCReceiverInName#outCInOutSFlagconstructor(outCInOutSFlag){this.#outCInOutSFlag = outCInOutSFlagthis.#inC = falsethis.#eightBitBinaryAdder1 = new EightBitBinaryAdder()this.#eightBitBinaryAdder2 = new EightBitBinaryAdder()this.#inA = []this.#inB = []for(var i = 0; i < this.#bitNum; i++){this.#inA.push(false)this.#inB.push(false)}this.#eightBitBinaryAdder1.setOutCReceiver(this.#eightBitBinaryAdder2, "inC")this.updateOut()}updateBothIn(arrayA, arrayB){this.#inA = arrayAthis.#inB = arrayBthis.#eightBitBinaryAdder1.updateBothIn(arrayA.slice(0, 8), arrayB.slice(0, 8))this.#eightBitBinaryAdder2.updateBothIn(arrayA.slice(8), arrayB.slice(8))this.updateOut()}updateOut(){this.#outS = this.#eightBitBinaryAdder1.getOut().concat(this.#eightBitBinaryAdder2.getOut())//發送send(OutC)}getOut(){return this.#outS}//TODO// updateInC(flag){//     this.#inC = flag//     this.updateBothIn(this.#inA, this.#inB, flag)// }// receive(inName, flag){//     if(inName == "inC"){//         this.updateInC(flag)//     }// }#sendOutC(flag){if(this.#outCReceiver){this.#outCReceiver.receive(this.#outCReceiverInName, flag)}}setOutCReceiver(outCReceiver, inName){this.#outCReceiver = outCReceiverthis.#outCReceiverInName = inName}}

運行驗證代碼

驗證通過判據:后臺沒有打印輸出異常。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Test</title><script src="../js/test/TestUtils.js"></script><script src="../js/model.js"></script><script src="../js/utils.js"></script>
</head>
<body><script>var g1 = new NullGate("g1")var g2 = new NullGate("g2")var g3 = new NullGate("g3")var g4 = new NullGate("g4")var g5 = new NullGate("g5")var g6 = new NullGate("g6")g1.setOutReceiver(g2, "in")g2.setOutReceiver(g3, "in")g3.setOutReceiver(g4, "in")g4.setOutReceiver(g5, "in")g5.setOutReceiver(g6, "in")g1.updateIn(true)assertTrue(g6.getOut())</script><script>//與門var andGate = new AndGate("a01")assertFalse(andGate.getOut())andGate.updateIn1(true)assertFalse(andGate.getOut())andGate.updateIn2(true)assertTrue(andGate.getOut())//或門var orGate = new OrGate("o01")assertFalse(orGate.getOut())orGate.updateIn1(true)assertTrue(orGate.getOut())orGate.updateIn2(true)assertTrue(orGate.getOut())//異或門var xorGate = new XorGate("x01")assertFalse(xorGate.getOut())xorGate.updateIn1(true)assertTrue(xorGate.getOut())xorGate.updateIn2(true)assertFalse(xorGate.getOut())xorGate.updateBothIn(false, true)assertTrue(xorGate.getOut())</script><script>//半加器var ha = new HalfAdder("h01")assertFalse(ha.getOutS())assertFalse(ha.getOutC())ha.updateInB(true)assertTrue(ha.getOutS())assertFalse(ha.getOutC())ha.updateInA(true)assertFalse(ha.getOutS())assertTrue(ha.getOutC())ha.updateBothIn(true, false)assertTrue(ha.getOutS())assertFalse(ha.getOutC())</script><script>//全加器var fa = new FullAdder("fa01")assertFalse(fa.getOutC())assertFalse(fa.getOutS())function test(inA, inB, inC, expectedS, expectedC){fa.updateThreeIn(inA, inB, inC)if(expectedS)assertTrue(fa.getOutS())elseassertFalse(fa.getOutS())if(expectedC)assertTrue(fa.getOutC())elseassertFalse(fa.getOutC())}test(false, false, false, false, false)test(false, true, false, true, false)test(true, false, false, true, false)test(true, true, false, false, true)test(false, false, true, true, false)test(false, true, true, false, true)test(true, false, true, false, true)test(true, true, true, true, true)</script><script>for(var i = 0 ; i < 256; i++){assertIntEquals(i, lowEightBitArray2Int(int2LowEightBitArray(i)))}</script><script>var ebba = new EightBitBinaryAdder(true)for(var i = 0; i < 256; i++){for(var j = 0; j < 256; j++){iebba.updateBothIn(int2LowEightBitArray(i), int2LowEightBitArray(j))assertIntEquals(i + j, lowNineBitArray2Int(ebba.getOut()))}}</script><script>for(var i = -128; i <= 127; i++){assertIntEquals(i, eightBitArray2Int(int2EightBitArray(i)))}</script><script>var ebba = new EightBitBinaryAdder()for(var i = -64; i < 64; i++){for(var j = -64; j < 64; j++){ebba.updateBothIn(int2EightBitArray(i), int2EightBitArray(j))assertIntEquals(i + j, eightBitArray2Int(ebba.getOut()))}}</script><script>for(var i = -(1<<15); i <= (1<<15) - 1; i++){assertIntEquals(i, sixteentBitArray2Int(int2SixteenBitArray(i)))}</script><script>var sbba = new SixteenBitBinaryAdder()sbba.updateBothIn(int2SixteenBitArray(1156), int2SixteenBitArray(9999))assertIntEquals(9999 + 1156, sixteentBitArray2Int(sbba.getOut()))//?16384? * ?16384? = 268435456 如果這樣算會十分耗時// for(var i = -(1<<14); i < (1<<14); i++){//     for(var j = -(1<<14); j < (1<<14); j++){//         sbba.updateBothIn(int2SixteenBitArray(i), int2SixteenBitArray(j))//         assertIntEquals(i + j, sixteentBitArray2Int(sbba.getOut()))//     }// }for(var i = -100; i < 100; i++){for(var j = -100; j < 100; j++){sbba.updateBothIn(int2SixteenBitArray(i), int2SixteenBitArray(j))assertIntEquals(i + j, sixteentBitArray2Int(sbba.getOut()))}}</script>
</body>
</html>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445568.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445568.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445568.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android學習路線總結

title: Android學習路線總結&#xff0c;絕對干貨 tags: Android學習路線,Android學習資料,怎么學習android grammar_cjkRuby: true --- 一、前言 不知不覺自己已經做了幾年開發了&#xff0c;由記得剛出來工作的時候感覺自己能牛X&#xff0c;現在回想起來感覺好無知。懂的越…

雙棧

利用棧底位置相對不變的特性&#xff0c;可以讓兩個順序棧共享一個空間。 具體實現方法大概有兩種&#xff1a; 一種是奇偶棧&#xff0c;就是所有下標為奇數的是一個棧&#xff0c;偶數是另一個棧。但是這樣一個棧的最大存儲就確定了&#xff0c;并沒有起到互補空缺的作用&a…

Error when loading the SDK:解決方案

錯誤情況&#xff1a; 當打開eclipse時出現如下窗口&#xff08;內容如下&#xff09; Error when loading the SDK: Error: Error parsing \Android\adt-bundle-windows-x86_64-20140702\sdk\system-images\android-22\android-wear\armeabi-v7a\devices.xml cvc-complex-type…

單調隊列優化的背包問題

對于背包問題&#xff0c;經典的背包九講已經講的很明白了&#xff0c;本來就不打算寫這方面問題了。 但是吧。 我發現&#xff0c;那個最出名的九講竟然沒寫隊列優化的背包。。。。 那我必須寫一下咯嘿嘿&#xff0c;這么好的思想。 我們回顧一下背包問題吧。 01背包問題 …

用Python去除掃描型PDF中的水印

內容概述 含水印掃描型PDF文件&#xff0c;其中某頁如下圖所示&#xff0c;用Python去除其頁頂及頁底的水印。 處理思路&#xff1a;PDF中的每一頁的水印的相對位置基本相同&#xff0c;將PDF每一頁輸出成圖片&#xff0c;然后進行圖片編輯&#xff0c;用白色填充方形覆蓋水印…

鏈表實現棧

棧&#xff0c;是操作受限的線性表&#xff0c;只能在一端進行插入刪除。 其實就是帶尾指針的鏈表&#xff0c;尾插 #include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR 0 #define Status int #define SElemType int //只在頭部進行插入和刪除(…

二階有源濾波器

濾波器是一種使用信號通過而同時抑制無用頻率信號的電子裝置, 在信息處理、數據傳送和抑制干擾等自動控制、通信及其它電子系統中應用廣泛。濾波一般可分為有源濾波和無源濾波, 有源濾波可以使幅頻特性比較陡峭, 而無源濾波設計簡單易行, 但幅頻特性不如濾波器, 而且體積較大。…

用JS寫了一個30分鐘倒計時器

效果圖 額外功能 左鍵單擊計時器數字區&#xff0c;不顯示或顯示秒鐘區。左鍵雙擊計時器數字區&#xff0c;暫停或啟動計時器。計時完畢&#xff0c;只能刷新頁面啟動計時器。輸入框可輸入備注信息&#xff0c;輸入框失去焦點或計時完畢后&#xff0c;時間戳附帶備注信息會存入…

為什么高手離不了Linux系統?我想這就是理由!

通過本文來記錄下我在Linux系統的學習經歷&#xff0c;聊聊我為什么離不了Linux系統&#xff0c;同時也為那些想要嘗試Linux而又有所顧忌的用戶答疑解惑&#xff0c;下面將為你介紹我所喜歡的Linux系統&#xff0c;這里有一些你應該知道并為之自豪的事實。 這里你應該首先拋開W…

python學習實例(1)

# #1.2 計算機編程的基本概念 ## #1.2.2 從Python語言進入計算機語言的世界 ##<程序&#xff1a;例子1> def F(x,y):return(x*xy*y) print("F(2,2)",F(2,2)) print("F(3,2)",F(3,2))#<程序&#xff1a;例子2> def Pr():for i in range(0,10)…

用JS寫一個電影《黑客帝國》顯示屏黑底綠字雨風格的唐詩欣賞器

效果圖 放碼過來 <!DOCTYPE HTML> <html><head><meta http-equiv"Content-Type" content"text/html;charsetutf-8"/><title>Black Screen And Green Characters</title><style type"text/css">table…

python學習實例(2)

# #2.2 不同進制間的轉換 ## #2.2.1. 二進制數轉換為十進制數 ##<程序&#xff1a;2-to-10進制轉換> binput("Please enter a binary number:") d0; for i in range(0,len(b)):if b[i] 1:weight 2**(len(b)-i-1)d dweight; print(d)#<程序&#xff1a;改…

元器件封裝大全:圖解+文字詳述

先圖解如下&#xff1a; 元器件封裝類型&#xff1a; A.Axial  軸狀的封裝&#xff08;電阻的封裝&#xff09;AGP &#xff08;Accelerate raphical Port&#xff09; 加速圖形接口 AMR(Audio/MODEM Riser) 聲音/調制解調器插卡BBGA&#xff08;Ball Grid Array&#xff09;…

python學習實例(3)

# #3.4 關于Python的函數調用 ## #3.4.2 Python函數入門 ##<程序&#xff1a;計算43*22> #函數f def f(x, y):return x*y*y#主函數部分 c4f(3, 2) print (c)# #3.4.3 局部變量(Local variables)與全局變量(Global variables) ##<程序&#xff1a;打印局部變量a和全局…

用JS寫一個丐版《2048》小游戲

效果圖 放馬過來 <!DOCTYPE HTML> <html><head><meta http-equiv"Content-Type" content"text/html;charsetutf-8"/><title>2048</title><style type"text/css">.basic{height:80px;width:80px;back…

如何有效申請TI的免費樣片

轉自如何有效申請TI的免費樣片 TI公司愿意為支持中國大學的師生們的教學、實驗、創新實踐、競賽和科研項目&#xff0c;提供有限數量的免費樣片。首先需要指出的是&#xff1a;所有的樣片申請應該是誠實正當的&#xff0c;所有不恰當的申請&#xff08;包括不必要或多余的&…

python學習實例(4)

# #第四章的python程序 ## #4.1 簡潔的Python ##<程序&#xff1a;Python數組各元素加1> arr [0,1,2,3,4] for e in arr:tmpe1print (tmp)## #4.2 Python內置數據結構 ## #4.2.1 Python基本數據類型 ##<程序&#xff1a;產生10-20的隨機浮點數> import random f …

用Python批量生成字幕圖片用于視頻剪輯

說明 視頻剪輯時需要為視頻添加字幕&#xff0c;添加字幕方法之一&#xff1a;根據字幕文本文件批量生成透明底只有字幕內容的圖片文件&#xff0c;如下圖&#xff0c;然后將這些圖片文件添加到視頻剪輯軟件軌道中。 于是用pillow這Python圖片工具庫執行本次批量生成工作。 …

關于接地:數字地、模擬地、信號地、交流地、直流地、屏蔽地、浮

除了正確進行接地設計、安裝,還要正確進行各種不同信號的接地處理。控制系統中&#xff0c;大致有以下幾種地線&#xff1a; &#xff08;1&#xff09;數字地&#xff1a;也叫邏輯地&#xff0c;是各種開關量&#xff08;數字量&#xff09;信號的零電位。 &#xff08;2&…

python學習實例(5)

# #5.1 計算思維是什么 ##<程序: 找假幣的第一種方法> by Edwin Sha def findcoin_1(L):if len(L) <1:print("Error: coins are too few"); quit()i0while i<len(L):if L[i] < L[i1]: return (i)elif L[i] > L[i1]: return (i1)ii1print("All…