對java面向程式設計的pta 4,5,6次作業的總結

HEAT-APCR發表於2024-06-02

1.前言

第四次pta作業是判題程式的最後一次的作業練習,難度有,但一般,只需要多花一些時間總能完成任務的,第五次的pta改用物理電學題,在設計上的考慮要更高於判題程式,可以說多次練習讓我對設計有了進一步的認識和學習,哪有小孩天天哭?哪有pta次次不會做?

1)第四次作業

輸入格式:

程式輸入資訊分五種,資訊可能會打亂順序混合輸入。

1、題目資訊
題目資訊為獨行輸入,一行為一道題,多道題可分多行輸入。

格式:"#N:"+題目編號+" "+"#Q:"+題目內容+" "#A:"+標準答案
格式約束:
1、題目的輸入順序與題號不相關,不一定按題號順序從小到大輸入。
2、允許題目編號有缺失,例如:所有輸入的題號為1、2、5,缺少其中的3號題。此種情況視為正常。
樣例:#N:1 #Q:1+1= #A:2
#N:2 #Q:2+2= #A:4

2、試卷資訊

試卷資訊為獨行輸入,一行為一張試卷,多張卷可分多行輸入資料。 \

格式:"#T:"+試卷號+" "+題目編號+"-"+題目分值+" "+題目編號+"-"+題目分值+...
格式約束:
題目編號應與題目資訊中的編號對應。
一行資訊中可有多項題目編號與分值。
樣例:#T:1 3-5 4-8 5-2

3、學生資訊

學生資訊只輸入一行,一行中包括所有學生的資訊,每個學生的資訊包括學號和姓名,格式如下。

格式:"#X:"+學號+" "+姓名+"-"+學號+" "+姓名....+"-"+學號+" "+姓名
格式約束:
答案數量可以不等於試卷資訊中題目的數量,沒有答案的題目計0分,多餘的答案直接忽略,答案之間以英文空格分隔。
樣例:
#S:1 #A:5 #A:22
1是試卷號
5是1號試卷的順序第1題的題目答案
4、答卷資訊

答卷資訊按行輸入,每一行為一張答卷的答案,每組答案包含某個試卷資訊中的題目的解題答案,答案的順序號與試 卷資訊中的題目順序相對應。答卷中:

格式:"#S:"+試卷號+" "+學號+" "+"#A:"+試卷題目的順序號+"-"+答案內容+...
格式約束:
答案數量可以不等於試卷資訊中題目的數量,沒有答案的題目計0分,多餘的答案直接忽略,答案之間以英文空格分隔。
答案內容可以為空,即””。
答案內容中如果首尾有多餘的空格,應去除後再進行判斷。
答卷資訊中僅包含試卷號、學號,而沒有後續內容的,視為一張空白卷,為有效資訊,不做格式錯誤處理。
樣例:
#T:1 1-5 3-2 2-5 6-9 4-10 7-3
#S:1 20201103 #A:2-5 #A:6-4
1是試卷號
20201103是學號
2-5中的2是試卷中順序號,5是試卷第2題的答案,即T中3-2的答案
6-4中的6是試卷中順序號,4是試卷第6題的答案,即T中7-3的答案
注意:不要混淆順序號與題號

5、刪除題目資訊

刪除題目資訊為獨行輸入,每一行為一條刪除資訊,多條刪除資訊可分多行輸入。該資訊用於刪除一道題目資訊,題目被刪除之後,引用該題目的試卷依然有效,但被刪除的題目將以0分計,同時在輸出答案時,題目內容與答案改為一條失效提示,例如:”the question 2 invalid~0”

格式:"#D:N-"+題目號
格式約束:
題目號與第一項”題目資訊”中的題號相對應,不是試卷中的題目順序號。
本題暫不考慮刪除的題號不存在的情況。
樣例:

N:1 #Q:1+1= #A:2

N:2 #Q:2+2= #A:4

T:1 1-5 2-8

X:20201103 Tom-20201104 Jack

S:1 20201103 #A:1-5 #A:2-4

D:N-2

end

輸出:
alert: full score of test paper1 is not 100 points
1+1=5false
the question 2 invalid~0
20201103 Tom: 0 0~0
答題資訊以一行"end"標記結束,"end"之後的資訊忽略。

輸出格式:

1、試卷總分警示

該部分僅當一張試卷的總分分值不等於100分時作提示之用,試卷依然屬於正常試卷,可用於後面的答題。如果總分等於100 分,該部分忽略,不輸出。
格式:"alert: full score of test paper"+試卷號+" is not 100 points"
約束:有多張試卷時,按輸入資訊的先後順序輸出警示。

樣例:alert: full score of test paper2 is not 100 points

2、答卷資訊

一行為一道題的答題資訊,根據試卷的題目的數量輸出多行資料。

格式:題目內容+""+答案++""+判題結果(true/false)

約束:如果輸入的答案資訊少於試卷的題目數量,每一個缺失答案的題目都要輸出"answer is null" 。

樣例:

 answer is null
 3+2=~5~true
 4+6=~22~false.
 answer is null

3、判分資訊

判分資訊為一行資料,是一條答題記錄所對應試卷的每道小題的計分以及總分,計分輸出的先後順序與題目題號相對應。

格式:學號+" "+姓名+": "+題目得分+" "+....+題目得分+"~"+總分
格式約束:
1、沒有輸入答案的題目、被刪除的題目、答案錯誤的題目計0分
2、判題資訊的順序與輸入答題資訊中的順序相同
樣例:20201103 Tom: 0 0~0
根據輸入的答卷的數量以上2、3項答卷資訊與判分資訊將重複輸出。

4、被刪除的題目提示資訊

當某題目被試卷引用,同時被刪除時,答案中輸出提示資訊。樣例見第5種輸入資訊“刪除題目資訊”。

5、題目引用錯誤提示資訊

試卷錯誤地引用了一道不存在題號的試題,在輸出學生答案時,提示”non-existent question~”加答案。例如:

輸入:

N:1 #Q:1+1= #A:2

T:1 3-8

X:20201103 Tom-20201104 Jack-20201105 Www

S:1 20201103 #A:1-4

end
輸出:
alert: full score of test paper1 is not 100 points
non-existent question~0
20201103 Tom: 0~0
如果答案輸出時,一道題目同時出現答案不存在、引用錯誤題號、題目被刪除,只提示一種資訊,答案不存在的優先順序最高,例如:

輸入:

N:1 #Q:1+1= #A:2

T:1 3-8

X:20201103 Tom-20201104 Jack-20201105 Www

S:1 20201103

end
輸出:
alert: full score of test paper1 is not 100 points
answer is null
20201103 Tom: 0~0
6、格式錯誤提示資訊

輸入資訊只要不符合格式要求,均輸出”wrong format:”+資訊內容。

  例如:wrong format:2 #Q:2+2= #4

7、試卷號引用錯誤提示輸出

如果答卷資訊中試卷的編號找不到,則輸出”the test paper number does not exist”,答卷中的答案不用輸出,參見樣例8。

8、學號引用錯誤提示資訊

如果答卷中的學號資訊不在學生列表中,答案照常輸出,判分時提示錯誤。參見樣例9。

本次作業新增內容:

1、輸入選擇題題目資訊

題目資訊為獨行輸入,一行為一道題,多道題可分多行輸入。

格式:"#Z:"+題目編號+" "+"#Q:"+題目內容+" "#A:"+標準答案
格式基本的約束與一般的題目輸入資訊一致。

新增約束:標準答案中如果包含多個正確答案(多選題),正確答案之間用英文空格分隔。
例如:

Z:2 #Q:宋代書法有蘇黃米蔡四家,分別是: #A:蘇軾 黃庭堅 米芾 蔡襄

多選題輸出:

輸出格式與一般答卷題目的輸出一致,判斷結果除了true、false,增加一項”partially correct”表示部分正確。

多選題給分方式:

答案包含所有正確答案且不含錯誤答案給滿分;包含一個錯誤答案或完全沒有答案給0分;包含部分正確答案且不含錯誤答案給一半分,如果一半分值為小數,按截尾規則只保留整數部分。
例如:

N:1 #Q:1+1= #A:2

Z:2 #Q:黨十八大報告提出要加強()建設。A 政務誠信 B 商務誠信 C社會誠信 D司法公信 #A:A B C D

T:1 1-5 2-9

X:20201103 Tom

S:1 20201103 #A:1-5 #A:2-A C

end
輸出:
alert: full score of test paper1 is not 100 points
1+1=5false
黨十八大報告提出要加強()建設。A 政務誠信 B 商務誠信 C社會誠信 D司法公信~A C~partially correct
20201103 Tom: 0 4~4

2、輸入填空題題目資訊

題目資訊為獨行輸入,一行為一道題,多道題可分多行輸入。

格式:"#K:"+題目編號+" "+"#Q:"+題目內容+" "#A:"+標準答案
格式基本的約束與一般的題目輸入資訊一致。
例如:#K:2 #Q:古琴在古代被稱為: #A:瑤琴或七絃琴
填空題輸出:

輸出格式與一般答卷題目的輸出一致,判斷結果除了true、false,增加一項”partially correct”表示部分正確。

填空題給分方式:

答案與標準答案內容完全匹配給滿分,包含一個錯誤字元或完全沒有答案給0分,包含部分正確答案且不含錯誤字元給一半分,如果一半分值為小數,按截尾規則只保留整數部分。

例如:

N:1 #Q:1+1= #A:2

K:2 #Q:古琴在古代被稱為: #A:瑤琴或七絃琴

T:1 1-5 2-10

X:20201103 Tom

S:1 20201103 #A:1-5 #A:2-瑤琴

end
輸出:
alert: full score of test paper1 is not 100 points
1+1=5false
古琴在古代被稱為:瑤琴partially correct
20201103 Tom: 0 5~5

3、輸出順序變化

只要是正確格式的資訊,可以以任意的先後順序輸入各類不同的資訊。比如試卷可以出現在題目之前,刪除題目的資訊可以出現在題目之前等。

例如:

T:1 1-5 2-10

N:1 #Q:1+1= #A:2

K:2 #Q:古琴在古代被稱為: #A:瑤琴或七絃琴

X:20201103 Tom

S:1 20201103 #A:1-5 #A:2-古箏

end
輸出:
alert: full score of test paper1 is not 100 points
1+1=5false
古琴在古代被稱為:古箏false
20201103 Tom: 0 0~0

4、多張試卷資訊

本題考慮多個同學有多張不同試卷的答卷的情況。輸出順序優先順序為學號、試卷號,按從小到大的順序先按學號排序,再按試卷號。

例如:

T:1 1-5 2-10

T:2 1-8 2-21

N:1 #Q:1+1= #A:2

S:2 20201103 #A:1-2 #A:2-古箏

S:1 20201103 #A:1-5 #A:2-瑤琴或七絃琴

S:1 20201104 #A:1-2 #A:2-瑟

S:2 20201104 #A:1-5 #A:2-七絃琴

X:20201103 Tom-20201104 Jack

K:2 #Q:古琴在古代被稱為: #A:瑤琴或七絃琴

end
輸出:
alert: full score of test paper1 is not 100 points
alert: full score of test paper2 is not 100 points
1+1=5false
古琴在古代被稱為:瑤琴或七絃琴true
20201103 Tom: 0 10~10
1+1=2true
古琴在古代被稱為:古箏false
20201103 Tom: 8 0~8
1+1=2true
古琴在古代被稱為:false
20201104 Jack: 5 0~5
1+1=5false
古琴在古代被稱為:七絃琴partially correct
20201104 Jack: 0 10~10

這道題增加了多選題,還要對答案的內容判斷是否正確或半對,相對前面的單選題要考慮的東西更多,還有新增的填空題就比較簡單,總體來講新增的內容複雜度不高,細細研究就能夠寫出來
對有合理類設計的同學會更簡單,不像他們,我只會hardcoding

2)第五次作業

智慧家居是在當下家庭中越來越流行的一種配置方案,它透過物聯網技術將家中的各種裝置(如音影片裝置、照明系統、窗簾控制、空調控制、安防系統、數字影院系統、影音伺服器、影櫃系統、網路家電等)連線到一起,提供家電控制、照明控制、電話遠端控制、室內外遙控、防盜報警、環境監測、暖通控制、紅外轉發以及可程式設計定時控制等多種功能和手段。與普通家居相比,智慧家居不僅具有傳統的居住功能,兼備建築、網路通訊、資訊家電、裝置自動化,提供全方位的資訊互動功能。請根據如下要去設計一個智慧家居強電電路模擬系統。

1、控制裝置模擬

本題模擬的控制裝置包括:開關、分檔調速器、連續調速器。

開關:包括0和1兩種狀態。

開關有兩個引腳,任意一個引腳都可以是輸入引腳,而另一個則是輸出引腳。開關狀態為0時,無論輸入電位是多少,輸出引腳電位為0。當開關狀態為1時,輸出引腳電位等於輸入電位。
分檔調速器

按檔位調整,常見的有3檔、4檔、5檔調速器,檔位值從0檔-2(3/4)檔變化。本次迭代模擬4檔調速器,每個檔位的輸出電位分別為0、0.3、0.6、0.9倍的輸入電壓。
連續調速器

沒有固定檔位,按位置比例得到檔位引數,數值範圍在[0.00-1.00]之間,含兩位小數。輸出電位為檔位引數乘以輸入電壓。
所有調速器都有兩個引腳,一個固定的輸入(引腳編號為1)、一個輸出引腳(引腳編號為2)。當輸入電位為0時,輸出引腳輸出的電位固定為0,不受各類開關調節的影響。

所有控制裝置的初始狀態/檔位為0。

控制裝置的輸入引腳編號為1,輸出引腳編號為2。

2、受控裝置模擬

本題模擬的受控裝置包括:燈、風扇。兩種裝置都有兩根引腳,透過兩根引腳電壓的電壓差驅動裝置工作。

燈有兩種工作狀態:亮、滅。在亮的狀態下,有的燈會因引腳電位差的不同亮度會有區別。
風扇在接電後有兩種工作狀態:停止、轉動。風扇的轉速會因引腳的電位差的不同而有區別。
本次迭代模擬兩種燈具。

白熾燈:

亮度在0~200lux(流明)之間。
電位差為0-9V時亮度為0,其他電位差按比例,電位差10V對應50ux,220V對應200lux,其他電位差與對應亮度值成正比。白熾燈超過220V。
日光燈:

亮度為180lux。
只有兩種狀態,電位差為0時,亮度為0,電位差不為0,亮度為180。
本次迭代模擬一種吊扇。

工作電壓區間為80V-150V,對應轉速區間為80-360轉/分鐘。80V對應轉速為80轉/分鐘,150V對應轉速為360轉/分鐘,超過150V轉速為360轉/分鐘(本次迭代暫不考慮電壓超標的異常情況)。其他電壓值與轉速成正比,輸入輸出電位差小於80V時轉速為0。
輸入資訊:

1、裝置資訊

分別用裝置識別符號K、F、L、B、R、D分別表示開關、分檔調速器、連續調速器、白熾燈、日光燈、吊扇。

裝置標識用識別符號+編號表示,如K1、F3、L2等。
引腳格式:裝置標識-引腳編號,例如:K1-1標識編號為1的開關的輸入引腳。

三種控制開關的輸入引腳編號為1,輸出引腳編號為2。
受控裝置的兩個引腳編號分別為1、2。
約束條件:

不同裝置的編號可以相同。
同種裝置的編號可以不連續。
裝置資訊不單獨輸入,包含在連線資訊中。

2、連線資訊

一條連線資訊佔一行,用[]表示一組連線在一起的裝置引腳,引腳與引腳之間用英文空格" "分隔。

格式:"["+引腳號+" "+...+" "+引腳號+"]"
例如:[K1-1 K3-2 D5-1]表示K1的輸入引腳,K3的輸出引腳,D5的1號引腳連線在一起。
約束條件:

本次迭代不考慮兩個輸出引腳短接的情況
考慮調速器輸出串聯到其他控制裝置(開關)的情況
不考慮調速器串聯到其他調速器的情況。
不考慮各類控制裝置的並聯接入或反饋接入。例如,K1的輸出接到L2的輸入,L2的輸出再接其他裝置屬於串聯接線。K1的輸出接到L2的輸出,同時K1的輸入接到L2的輸入,這種情況屬於並聯。K1的輸出接到L2的輸入,K1的輸入接到L2的輸出,屬於反饋接線。
3、控制裝置調節資訊

開關調節資訊格式:

+裝置標識K+裝置編號,例如:#K2,代表切換K2開關的狀態。

分檔調速器的調節資訊格式:

+裝置標識F+裝置編號+"+" 代表加一檔,例如:#F3+,代表F3輸出加一檔。

+裝置標識F+裝置編號+"-" 代表減一檔,例如:#F1-,代表F1輸出減一檔。

連續調速器的調節資訊格式:

+裝置標識L+裝置編號+":" +數值 代表將連續調速器的檔位設定到對應數值,例如:#L3:0.6,代表L3輸出檔位引數0.6。

4、電源接地標識:VCC,電壓220V,GND,電壓0V。沒有接線的引腳預設接地,電壓為0V。

輸入資訊以end為結束標誌,忽略end之後的輸入資訊。

輸出資訊:

按開關、分檔調速器、連續調速器、白熾燈、日光燈、吊扇的順序依次輸出所有裝置的狀態或引數。每個裝置一行。同類裝置按編號順序從小到大輸出。

輸出格式:@裝置標識+裝置編號+":" +裝置引數值(控制開關的檔位或狀態、燈的亮度、風扇的轉速,只輸出值,不輸出單位)
連續調速器的檔位資訊保留兩位小數,即使小數為0,依然顯示兩位小數.00。
開關狀態為0(開啟)時顯示turned on,狀態為1(合上)時顯示closed
如:
@K1:turned on
@B1:190
@L1:0.60
本題不考慮輸入電壓或電壓差超過220V的情況。

本題只考慮串聯的形式,所以所有測試用例的所有連線資訊都只包含兩個引腳

本題電路中除了開關可能出現多個,其他電路裝置均只出現一次。
電源VCC一定是第一個連線的第一項,接地GND一定是最後一個連線的後一項。

家居電路模擬系列所有題目的預設規則:

1、當計算電壓值等數值的過程中,最終結果出現小數時,用截尾規則去掉小數部分,只保留整數部分。為避免精度的誤差,所有有可能出現小數的數值用double型別儲存並計算,不要作下轉型資料型別轉換,例如電壓、轉速、亮度等,只有在最後輸出時再把計算結果按截尾規則,捨棄尾數,保留整數輸出。

2、所有連線資訊按電路從電源到接地的順序依次輸入,不會出現錯位的情況。

3、連線資訊如果只包含兩個引腳,靠電源端的引腳在前,靠接地端的在後。

4、對於調速器,其輸入端只會直連VCC,不會接其他裝置。整個電路中最多隻有一個調速器,且連線在電源上。

家居電路模擬系列1-4題目後續迭代設計:

1、電路結構變化:

迭代1:只有一條線路,所有元件串聯
迭代2:線路中包含一個並聯電路
迭代3:線路中包含多個串聯起來的並聯電路
迭代4:並聯電路之間可能出現包含關係

電路結構變化示意圖見圖1。

2、輸入資訊的變化

串聯線路資訊:用於記錄一段串聯電路的元件與連線資訊。

例如: #T1:[IN K1-1] [K1-2 D2-1] [D2-2 OUT]
#T1:[IN K1-1] [K1-2 M1-IN][M1-OUT D2-1] [D2-2 GND]
並聯線路資訊:用於記錄一段並聯電路所包含的所有串聯電路資訊。

例如:#M1:[T1 T2 T3]
以上格式僅做參考,格式細節可能會調整,以具體釋出的為準。

3、計算方式的變化

迭代1只包含1個受控元件,不用計算電流,之後的電路計算要包含電流、電阻等電路引數。

4、電路元件的變化

每次迭代會增加1-2個新的電路元件。

3)第六次作業

智慧家居是在當下家庭中越來越流行的一種配置方案,它透過物聯網技術將家中的各種裝置(如音影片裝置、照明系統、窗簾控制、空調控制、安防系統、數字影院系統、影音伺服器、影櫃系統、網路家電等)連線到一起,提供家電控制、照明控制、電話遠端控制、室內外遙控、防盜報警、環境監測、暖通控制、紅外轉發以及可程式設計定時控制等多種功能和手段。與普通家居相比,智慧家居不僅具有傳統的居住功能,兼備建築、網路通訊、資訊家電、裝置自動化,提供全方位的資訊互動功能。請根據如下要去設計一個智慧家居強電電路模擬系統。以下題目介紹中加粗的部分為本次迭代在“家居強電電路模擬程式-1”的基礎上增加的功能要求。

1、控制裝置

本題模擬的控制裝置包括:開關、分檔調速器、連續調速器。

開關:包括0和1兩種狀態。

開關有兩個引腳,任意一個引腳都可以是輸入引腳,而另一個則是輸出引腳。開關狀態為0時,無論輸入電位是多少,輸出引腳電位為0。當開關狀態為1時,輸出引腳電位等於輸入電位。
分檔調速器

按檔位調整,常見的有3檔、4檔、5檔調速器,檔位值從0檔-2(3/4)檔變化。本次迭代模擬4檔調速器,每個檔位的輸出電位分別為0、0.3、0.6、0.9倍的輸入電壓。
連續調速器

沒有固定檔位,按位置比例得到檔位引數,數值範圍在[0.00-1.00]之間,含兩位小數。輸出電位為檔位引數乘以輸入電壓。
所有調速器都有兩個引腳,一個固定的輸入(引腳編號為1)、一個輸出引腳(引腳編號為2)。當輸入電位為0時,輸出引腳輸出的電位固定為0,不受各類開關調節的影響。

所有控制裝置的初始狀態/檔位為0。

控制裝置的輸入引腳編號為1,輸出引腳編號為2。
所有開關的電阻為 0。

2、受控裝置

本題模擬的受控裝置包括:燈、風扇。兩種裝置都有兩根引腳,透過兩根引腳電壓的電壓差驅動裝置工作。

燈有兩種工作狀態:亮、滅。在亮的狀態下,有的燈會因引腳電位差的不同亮度會有區別。
風扇在接電後有兩種工作狀態:停止、轉動。風扇的轉速會因引腳間電位差的不同而有區別。
本次迭代模擬兩種燈具。

白熾燈:

亮度在0~200lux(流明)之間。
電位差為0-9V時亮度為0,其他電位差按比例,電位差10V對應50ux,220V對應200lux,其他電位差與對應亮度值成正比。白熾燈超過220V。
日光燈:

亮度為180lux。
只有兩種狀態,電位差為0時,亮度為0,電位差不為0,亮度為180。
本次迭代模擬一種吊扇。

工作電壓區間為80V-150V,對應轉速區間為80-360轉/分鐘。80V對應轉速為80轉/分鐘,150V對應轉速為360轉/分鐘,超過150V轉速為360轉/分鐘(本次迭代暫不考慮電壓超標的異常情況)。其他電壓值與轉速成正比,輸入輸出電位差小於80V時轉速為0。
本次迭代模擬一種落地扇。

工作電壓區間為 [80V,150V],對應轉速區間為 80-360 轉/分鐘。電壓在[80,100)V 區間對應轉速為 80 轉/分 鍾,[100-120)V 區間對應轉速為 160 轉/分鐘,[120-140)V 區間對應轉速為 260 轉/分鐘,超過 140V 轉速 為 360 轉/分鐘(本次迭代暫不考慮電壓超標的異常情況)輸入資訊:
本次迭代考慮電阻:白熾燈的電阻為 10,日光燈的電阻為 5,吊扇的電阻為 20,落 地扇的電阻為 20

3、輸入資訊

1)輸入裝置資訊

分別用裝置識別符號K、F、L、B、R、D、A分別表示開關、分檔調速器、連續調速器、白熾燈、日光燈、吊扇、落地扇。

裝置標識用識別符號+編號表示,如K1、F3、L2等。
引腳格式:裝置標識-引腳編號,例如:K1-1標識編號為1的開關的輸入引腳。

三種控制開關的輸入引腳編號為1,輸出引腳編號為2。
受控裝置的兩個引腳編號分別為1、2。
約束條件:

不同裝置的編號可以相同。
同種裝置的編號可以不連續。
裝置資訊不單獨輸入,包含在連線資訊中。

2)輸入連線資訊

一條連線資訊佔一行,用[]表示一組連線在一起的裝置引腳,引腳與引腳之間用英文空格" "分隔。

格式:"["+引腳號+" "+...+" "+引腳號+"]"
例如:[K1-1 K3-2 D5-1]表示K1的輸入引腳,K3的輸出引腳,D5的1號引腳連線在一起。
約束條件:

不考慮調速器串聯到其他調速器的情況。
不考慮調速器串聯到其他調速器的情況。
考慮各類裝置的並聯接入。例如,K1 的輸出接到 L2 的輸入,L2 的輸出再接其他裝置屬於串聯接線。K1 的輸出接到 L2 的輸出,同時 K1 的輸入接到 L2 的輸入,這種情況屬於並聯。
本次迭代的連線資訊不單獨輸入,包含線上路資訊中。

3)輸入控制裝置調節資訊

開關調節資訊格式:

+裝置標識K+裝置編號,例如:#K2,代表切換K2開關的狀態。

分檔調速器的調節資訊格式:

+裝置標識F+裝置編號+"+" 代表加一檔,例如:#F3+,代表F3輸出加一檔。

+裝置標識F+裝置編號+"-" 代表減一檔,例如:#F1-,代表F1輸出減一檔。

連續調速器的調節資訊格式:

+裝置標識L+裝置編號+":" +數值 代表將連續調速器的檔位設定到對應數值,例如:#L3:0.6,代表L3輸出檔位引數0.6。

4)電源接地標識:

VCC,電壓220V,GND,電壓0V。沒有接線的引腳預設接地,電壓為0V。

5)輸入串聯電路資訊

一條串聯電路佔一行,串聯電路由按從靠電源端到接地端順序依次輸入的 n 個連線 資訊組成,連線資訊之間用英文空格" "分隔。

串聯電路資訊格式:

"#T"+電路編號+":"+連線資訊+" "+連線資訊+...+" "+連線資訊
例如:#T1:[IN K1-1] [K1-2 D2-1] [D2-2 OUT] 一個串聯電路的第一個引腳是 IN,代表起始端,靠電源。最後一個引腳是 OUT,代表結尾端, 靠接地。
約束條件:

不同的串聯電路資訊編號不同。
輸入的最後一條電路資訊必定是總電路資訊,總電路資訊的起始引腳是 VCC,結束引腳是 GND。
連線資訊中的引腳可能是一條串聯或並聯電路的 IN 或者 OUT。例如:

T1:[IN K1-1] [K1-2 T2-IN] [T2-OUT OUT]

T1:[IN K1-1] [K1-2 T2-IN] [T2-OUT M2-IN] [M2-OUT OUT]

6)輸入並聯電路資訊

一條並聯電路佔一行,並聯電路由其包含的幾條串聯電路組成,串聯電路標識之間用英文空格" "分隔。

格式:

"#M"+電路編號+":"+”[”+串聯電路資訊+" "+....+" "+串聯電路資訊+”]”
例如:#M1:[T1 T2 T3]
該例宣告瞭一個並聯電路,由 T1、T2、T3 三條串聯電路並聯而成,三條串聯電路的 IN 短 接在一起構成 M1 的 IN,三條串聯電路的 OUT 短接在一起構成 M1 的 OUT。
約束條件:

本次迭代不考慮並聯電路中包含並聯電路的情況,也不考慮多個並聯電路串聯的情況。
本題不考慮輸入電壓或電壓差超過220V的情況。

輸入資訊以end為結束標誌,忽略end之後的輸入資訊。

本題中的並聯資訊所包含的串聯電路的資訊都在並聯資訊之前輸入,不考慮亂序輸入的情況。
只要不造成短路而產生無窮大的電流燒壞電路,都是合理情況,在測試點的考慮範圍之內。本次大作業不考慮短路的情況,測試點中不包括短路的電路。

本題不考慮一條串聯電路中包含其他串聯電路的情況。例如:

T3:[VCC K1-1] [K1-2 T2-IN] [T2-OUT K2-1] [K2-2 T1-IN] [T1-OUT GND]

本例中T1\T2兩條串聯電路實際是T3的一個部分,本題不考慮這種型別的輸入,而是當將T1\T2的所有連線資訊直接包含在T3中定義。
下次迭代中需要考慮這種型別的輸入。
4、輸出資訊:

按開關、分檔調速器、連續調速器、白熾燈、日光燈、吊扇、落地扇的順序依次輸出所有裝置的狀態或引數。每個裝置一行。同類裝置按編號順序從小到大輸出。

輸出格式:@裝置標識+裝置編號+":" +裝置引數值(控制開關的檔位或狀態、燈的亮度、風扇的轉速,只輸出值,不輸出單位)
連續調速器的檔位資訊保留兩位小數,即使小數為0,依然顯示兩位小數.00。
開關狀態為0(開啟)時顯示turned on,狀態為1(合上)時顯示closed
如:
@K1:turned on
@B1:190
@L1:0.60
5、家居電路模擬系列所有題目的預設規則:

1)當計算電壓值等數值的過程中,最終結果出現小數時,用截尾規則去掉小數部分,只保留整數部分。為避免精度的誤差,所有有可能出現小數的數值用double型別儲存並計算,不要作下轉型資料型別轉換,例如電壓、轉速、亮度等,只有在最後輸出時再把計算結果按截尾規則,捨棄尾數,保留整數輸出。

2)所有連線資訊按電路從電源到接地的順序依次輸入,不會出現錯位的情況。電源VCC一定是第一個連線的第一項,接地GND一定是最後一個連線的後一項。

3)連線資訊如果只包含兩個引腳,靠電源端的引腳在前,靠接地端的在後。

4)調速器的輸入端只會直連VCC,不會接其他裝置。整個電路最多隻有連線在電源上的一個調速器,且不包含在並聯單路中。

6、家居電路模擬系列1-4題目後續迭代設計:

1)電路結構變化:

迭代1:只有一條線路,所有元件串聯
迭代2:線路中包含一個並聯電路
迭代3:線路中包含多個串聯起來的並聯電路
迭代4:並聯電路之間可能出現包含關係

電路結構變化示意圖見圖1。

2)計算方式的變化

迭代1只包含1個受控元件,不用計算電流,之後的電路計算要包含電流、電阻等電路引數。

3)電路元件的變化

每次迭代會增加1-2個新的電路元件。

2.設計與分析
1)對輸入資料進行分析:
第四題:
i.新增多選題類MoreAnswerQuestion用來儲存多選題的資訊
ii.新增單選題類SummaryQuestion用來儲存單選題的資訊

第五題:
i.需要各種類的陣列來儲存每個元件的物件(好處:每次改變某個元件資訊時,可以直接對其儲存物件更改)例如:Switch[] Switchs=new Switch[100]
ii.需要容器儲存主幹的元件(使用連結串列allDevices)

第六題:
i.增加hashMap來儲存並聯資訊(優點:hashMap可以無序儲存所需資訊,在這裡採用String-ArrayList鍵值儲存)
ii.增加連結串列resistance來儲存並聯電路電阻用於之後求電阻分壓

注意:
i.一定要注意程式碼的邏輯性,一次性寫複雜的程式碼偶爾會手忙腳亂
ii.必要的註釋要寫,不然迭代的時候連程式碼都看不清楚邏輯性
2)分析題目
1.分析第四題
i.相對第三題增加多選題類和填空題類
ii.增加對多選題類和填空題類匹配和分割的方法體
iii.增加輸出的方式,輸出多選題的全對和半對
樣例:
輸入:

輸出:

輸入:

輸出:

2.分析第五題
i.按題目意思,設計控制類,電器類,包括繼承和多型
ii.採用分割的方式,分割儲存輸入資料,儲存方式採用類的陣列
iii.要注意輸出的時候要按順序輸出,每種元件內部也要按順序從小到大輸出,需要排序類
iiii.給定電壓值,每次透過元件就更新電壓值
樣例:
輸入:

輸出:

輸入:

輸出:

3.分析第六題
i.增加落地扇類,增加該類陣列儲存
ii.利用hashMap儲存並聯資訊T,利用hashMap儲存並聯節點
iii.排序方法增加落地扇的儲存和排序
iiii.增加連結串列儲存並聯和主幹的電阻分佈,便於計算阻值
樣例:
輸入:

輸出:

輸入:

輸出:

3.我的程式碼
1)第四題程式碼:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.LinkedList;

class QuestionMessage{
    private int num;
    private String message;
    private String standardAnswer;
    public QuestionMessage(){

    }
    public void setNum(int num){
        this.num=num;
    }
    public void setMessage(String message){
        this.message=message;
    }
    public void setStandardAnswer(String standardAnswer){
        this.standardAnswer=standardAnswer;
    }
    public int getNum(){
        return num;
    }
    public String getMessage(){
        return message;
    }
    public String getStandardAnswer(){
        return standardAnswer;
    }
    public boolean judge(String answer){
        if(this.standardAnswer==answer)
            return true;
        else
            return false;
    }
    public QuestionMessage(int num,String message,String standardAnswer){
        this.num=num;
        this.message=message;
        this.standardAnswer=standardAnswer;
    }
}//一道題目的內容
class TextPaper{
    public QuestionMessage[] quest=new QuestionMessage[100];
    public MoreAnswerQuestion[] moreAnswerQuestions=new MoreAnswerQuestion[100];
    public SummaryQuestion[] summaryQuestions=new SummaryQuestion[100];
    private int[] questionState=new int[100];
    public int[] getQuestionState() {
        return questionState;
    }
    public void setQuestionState(int[] questionState) {
        this.questionState = questionState;
    }
    public void textPaper(String questions){
        if(questions.startsWith("#N:")) {
            questions = questions.replaceAll("#N|#Q|#A", "");
            String[] question = questions.split(":");
            String trim1 = null;
            String trim2 = null;
            String trim3 = null;
                int stand = 0;
                trim1 = question[1].trim();
                trim2 = question[2].trim();
                if (question.length < 4) {
                    trim3 = "";
                } else {
                    trim3 = question[3].trim();
                }
                quest[Integer.valueOf(trim1)] = new QuestionMessage(Integer.valueOf(trim1), trim2, trim3);
            questionState[Integer.valueOf(trim1)]=1;
        }
        if(questions.startsWith("#Z:")) {
            questions = questions.replaceAll("#Z|#Q|#A", "");
            String[] question = questions.split(":");
            String trim1 = null;
            String trim2 = null;
            String trim3 = null;
                int stand = 0;
                trim1 = question[1].trim();
                trim2 = question[2].trim();
                if (question.length < 4) {
                    trim3 = "";
                } else {
                    trim3 = question[3].trim();
                }
                moreAnswerQuestions[Integer.valueOf(trim1)] = new MoreAnswerQuestion(Integer.valueOf(trim1), trim2, trim3);
            questionState[Integer.valueOf(trim1)]=2;
        }
        if(questions.startsWith("#K:")) {
            questions = questions.replaceAll("#K|#Q|#A", "");
            String[] question = questions.split(":");
            String trim1 = null;
            String trim2 = null;
            String trim3 = null;
                trim1 = question[1].trim();
                trim2 = question[2].trim();
                if (question.length < 4) {
                    trim3 = "";
                } else {
                    trim3 = question[3].trim();
                }
                summaryQuestions[Integer.valueOf(trim1)] = new SummaryQuestion(Integer.valueOf(trim1), trim2, trim3);
            questionState[Integer.valueOf(trim1)]=3;
        }
    }
    public boolean checkQuestionN(int number){
        if(questionState[number]==1){
            return true;
        }
        return false;
    }
    public boolean checkQuestionZ(int number){
        if(questionState[number]==2){
            return true;
        }
        return false;
    }
    public boolean checkQuestionK(int number){
        if(questionState[number]==3){
            return true;
        }
        return false;
    }
    public int judgeAnswer(String answer1,int number1){
        if(checkQuestionN(number1)){
            if(answer1.equals(quest[number1].getStandardAnswer())){
                return 1;
            }
            else{
                return 0;
            }
        }
        if(checkQuestionZ(number1)){
            if(answer1.equals(moreAnswerQuestions[number1].getStandardAnswer())){
                return 2;
            }
            else{
                String[] answer=moreAnswerQuestions[number1].getStandardAnswer().split("\\s+");
                String[] Answer=answer1.split("\\s+");
                int num=0;
                for(int a=0;a<Answer.length;a++){
                    int right=0;
                    for(int b=0;b<answer.length;b++){
                        if(Answer[a].equals(answer[b])){
                            right=1;
                            num++;
                            break;
                        }
                    }
                    if(right==0){
                        return -1;
                    }
                    if(num==answer.length&&a==Answer.length-1){
                        return 2;
                    }
                    if(num<answer.length&&a==Answer.length-1&&num>0){
                        return 3;
                    }

                }
            }
        }
        if(checkQuestionK(number1)){
            if(answer1.trim().equals(summaryQuestions[number1].getStandardAnswer())){
                return 4;
            }
            if(summaryQuestions[number1].getStandardAnswer().contains(answer1.trim())){
                return  5;
            }
            else{
                return -2;
            }
        }
        return 0;
    }
}
class AnswerPaper{
    private int sum=0;
    private int[] score=new int[100];
    public TextPaper text=new TextPaper();
    public int[] judgements=new int[100];
    public AnswerQuestion[] Answers=new AnswerQuestion[100];
    public AnswerPaper(){

    }
    public void addQuestion(int num){
        Answers[num]=new AnswerQuestion();
    }
    public void setAnswers(String answers, int num,int num1){
        Answers[num1].setAnswers(answers,num);
    }
    public String getAnswers(int num4,int num){
        return Answers[num].getAnswers(num4);
    }
    public void setStudentID(int studentID,int num){
        Answers[num].setStudentID(studentID);
    }
    public void setScore(int num,int sorce){
        this.score[num]=sorce;
    }
    public int getScore(int num){
        return score[num];
    }
    public void addSum(int sum){
        this.sum+=sum;
    }
    public void setSum(int score){
        this.sum=score;
    }
    public int getSum(){
        return sum;
    }
    public int getAllScore(){
        return sum;
    }
    public boolean checkQuestion(LinkedList<String> questions,int q){
        String trim1;
        for(int a=0;a<questions.size();a++){
            String question=questions.get(a);
            String quest=question.replaceAll("#N|#Z|#K|#A|#Q","");
            String[] quests=quest.split(":");
            trim1=quests[1].trim();
            if(Integer.valueOf(trim1)==q){
                return true;
            }
        }
        return false;
    }
    public void ifRight(int num1,int num2,int num3){
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==1){
            this.judgements[num2]=1;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==0){
            this.judgements[num2]=0;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==2){
            this.judgements[num2]=2;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==3){
            this.judgements[num2]=3;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==4){
            this.judgements[num2]=4;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==5){
            this.judgements[num2]=5;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==-1){
            this.judgements[num2]=-1;
        }
        if(text.judgeAnswer(Answers[num3].getAnswers(num2),num1)==-2){
            this.judgements[num2]=-2;
        }
    }
}
class AnswerQuestion{
    private int studentID;
    private String[] answers=new String[100];
    public AnswerQuestion(){

    }
    public void setAnswers(String answers, int num){
        this.answers[num]=answers;
    }
    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }
    public void setAnswers(String[] answers) {
        this.answers = answers;
    }
    public String getAnswers(int num4){
        return answers[num4];
    }

}
class Students{
    private String ID;
    private String name;
    public Students(String ID,String name){
        this.ID=ID;
        this.name=name;
    }
    public String getID() {
        return ID;
    }
    public String getName(){
        return name;
    }
    public void setID(String ID){
        this.ID=ID;
    }
    public void setName(String name){
        this.name=name;
    }
}
class Process{
    public int num=0;
    private LinkedList<String> T=new LinkedList<String>();
    private LinkedList<String> S=new LinkedList<String>();
    private LinkedList<String> student=new LinkedList<String>();
    private LinkedList<String> D=new LinkedList<String>();
    private LinkedList<String> question=new LinkedList<String>();
    public Process(){

    }
    public LinkedList<String> getQuestion(){
        return question;
    }
    public LinkedList<String> getT(){
        for(int a=0;a<T.size();a++){
            T.set(a,T.get(a).replaceAll("#T:(\\s*)",""));
        }
        return T;
    }
    public LinkedList<String> getS(){
        for(int a=0;a<S.size();a++){
            for(int b=0;b<S.size()-1;b++){
                String summary=S.get(b);
                String summary1=S.get(b+1);
                String summary2=summary.replaceAll("#S:","");
                String summary12=summary1.replaceAll("#S:","");
                String[] Summary=summary2.split("\\s+");
                String[] Summary1=summary12.split("\\s+");
                if(Integer.valueOf(Summary[0])>Integer.valueOf(Summary1[0])){
                    S.set(b,summary1);
                    S.set(b+1,summary);
                }
            }
        }
        for(int a=0;a<S.size();a++){
            for(int b=0;b<S.size()-1;b++){
                String summary=S.get(b);
                String summary1=S.get(b+1);
                String summary2=summary.replaceAll("#S:","");
                String summary12=summary1.replaceAll("#S:","");
                String[] Summary=summary2.split("\\s+");
                String[] Summary1=summary12.split("\\s+");
                if(Integer.valueOf(Summary[1])>Integer.valueOf(Summary1[1])){
                    S.set(b,summary1);
                    S.set(b+1,summary);
                }
            }
        }
        return S;
    }
    public LinkedList<String> getStudent(){
        for(int a=0;a<student.size();a++){
            student.set(a,student.get(a).replaceAll("#X:(\\s*)",""));
        }
        return student;
    }
    public LinkedList<String> getD(){
        for(int a=0;a<D.size();a++){
            D.set(a,D.get(a).replaceAll("#D:(\\s*)N(\\s*)-",""));
        }
        return D;
    }
    public void sortS(LinkedList<String> s){
        for(int a=0;a<=s.size();a++){
            for(int b=0;b<s.size();b++){
                String summary=s.get(b);
                String summary1=s.get(b+1);
                String[] Summary=summary.split("\\s+");
                String[] Summary1=summary1.split("\\s+");
                if(Integer.valueOf(Summary[1])>Integer.valueOf(Summary1[1])){
                    s.set(b,summary1);
                    s.set(b+1,summary);
                }
            }
        }
        for(int a=0;a<=s.size();a++){
            for(int b=0;b<s.size();b++){
                String summary=s.get(b);
                String summary1=s.get(b+1);
                summary=summary.replaceAll("#S:","");
                summary1=summary1.replaceAll("#S:","");
                String[] Summary=summary.split("\\s+");
                String[] Summary1=summary1.split("\\s+");
                if(Integer.valueOf(Summary[0])>Integer.valueOf(Summary1[0])){
                    s.set(b,summary1);
                    s.set(b+1,summary);
                }
            }
        }
    }
    public void formatJudge(String message){
        int check=0;
        Pattern pattern=Pattern.compile("(^#N:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*?)\\s*($|\\n))");
        Pattern pattern1=Pattern.compile("^#T:\\s*(\\d+)(\\s*\\d+\\s*\\-\\s*\\d+\\s*)*$");
        Pattern pattern2=Pattern.compile("^#X:(\\s*(\\d+)\\s*((\\w+)|[\u4e00-\u9fa5]+)\\s*(\\-)*\\s*)*");
        Pattern pattern3=Pattern.compile("^#S:(\\d+)\\s*(\\d+)\\s*(#A:\\s*(\\d+)\\s*\\-\\s*(.*?)\\s*(?=#A:|\\n|$))*");
        Pattern pattern4=Pattern.compile("^#D:\\s*N\\s*\\-\\s*(\\d+)$");
        Pattern pattern5=Pattern.compile("(^#Z:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*?)\\s*($|\\n))");
        Pattern pattern6=Pattern.compile("(^#K:\\s*(\\d+)\\s*#Q:\\s*(.*?)\\s*#A:\\s*(.*?)\\s*($|\\n))");
        Matcher matcher5=pattern5.matcher(message);
        while(matcher5.find()){
            question.add(message);
            num++;
            check=1;
        }
        Matcher matcher6=pattern6.matcher(message);
        while(matcher6.find()){
            question.add(message);
            num++;
            check=1;
        }
        Matcher matcher=pattern.matcher(message);
        while(matcher.find()){
            question.add(message);
            num++;
            check=1;
        }
        Matcher matcher1=pattern1.matcher(message);
        while(matcher1.find()){
            T.add(message);
            check=1;
        }
        Matcher matcher2=pattern3.matcher(message);
        while(matcher2.find()){
            S.add(message);
            check=1;
        }
        Matcher matcher3=pattern2.matcher(message);
        while(matcher3.find()){
            student.add(message);
            check=1;
        }
        Matcher matcher4=pattern4.matcher(message);
        while(matcher4.find()){
            D.add(message);
            check=1;
        }
        if(check==0){
            System.out.println("wrong format:"+message);
        }
    }
}
class MoreAnswerQuestion extends QuestionMessage{
    public MoreAnswerQuestion(int num,String question,String standerAnswer){
        this.setNum(num);
        this.setMessage(question);
        this.setStandardAnswer(standerAnswer);
    }
}
class SummaryQuestion extends QuestionMessage{
    public SummaryQuestion(int num,String question,String standerAnswer){
        this.setNum(num);
        this.setMessage(question);
        this.setStandardAnswer(standerAnswer);
    }
}
public class Main {
    public static void main(String[]args){
        Scanner s=new Scanner(System.in);
        Process process=new Process();
        for(int a=0;;a++){
            String message=s.nextLine();
            if(message.equals("end")){
                break;
            }
            process.formatJudge(message);
        }

        String[] S1=new String[100];
        String[] T1=new String[100];
        AnswerPaper[] ans=new AnswerPaper[100];
        for(int a=0;a<process.getT().size();a++){
            T1=process.getT().get(a).split("\\s+");
            ans[Integer.valueOf(T1[0])]=new AnswerPaper();
            for(int d=0;d<process.getQuestion().size();d++){
                ans[Integer.valueOf(T1[0])].text.textPaper(process.getQuestion().get(d));
            }
            for(int b=1;b<T1.length;b++){
                String[] T2=T1[b].split("-");
                int check=0;
                for(int c=0;c<process.getD().size();c++){
                    if(Integer.valueOf(process.getD().get(c).trim())==Integer.valueOf(T2[0])){
                        check=1;
                    }
                }
                if (check == 1&&ans[Integer.valueOf(T1[0])].checkQuestion(process.getQuestion(),Integer.valueOf(T2[0]))) {
                    ans[Integer.valueOf(T1[0])].setScore(b,0);
                    ans[Integer.valueOf(T1[0])].addSum(0);
                }
                else if(check == 0&&ans[Integer.valueOf(T1[0])].checkQuestion(process.getQuestion(),Integer.valueOf(T2[0]))){
                    ans[Integer.valueOf(T1[0])].setScore(b,Integer.valueOf(T2[1]));
                    ans[Integer.valueOf(T1[0])].addSum(Integer.valueOf(T2[1]));
                }
            }
        }
        String[] S3=new String[100];
        String[] S4=new String[100];
        for(int e=0;e<process.getT().size();e++){
            String[] T=process.getT().get(e).split("\\s+");
            for(int c=0;c<process.getS().size();c++){
                String question=process.getS().get(c).replaceAll("#S:|#A:","");
                String[] Question=question.split("\\s+");
                if(T[0].equals(Question[0])){
                    ans[Integer.valueOf(Question[0])].addQuestion(c);
                }
            }
            int[] answerNum=new int[100];
            for(int a=0;a<process.getS().size();a++) {
                String question=process.getS().get(a).replaceAll("#S:|#A:","");
                String[] Question=question.split("\\s+");
                if(T[0].equals(Question[0])){
                    S1 = process.getS().get(a).split("#");
                    for (int b = 0; b < S1.length; b++) {
                        if(S1[b].startsWith("S:")){
                            S1[b] = S1[b].replaceAll("S:(\\s*)", "");
                            S3=S1[b].split(" ");
                        }
                        if(S1[b].startsWith("A:")){
                            S1[b] = S1[b].replaceAll("A:(\\s*)", "");
                            if(S1[b].length()>0){
                                S4=S1[b].split("-");
                                answerNum[Integer.valueOf(S4[0])]=Integer.valueOf(S4[0]);
                                if(S4.length<2){
                                    ans[Integer.valueOf(S3[0])].setAnswers("",Integer.valueOf(S4[0]),a);
                                }
                                else{
                                    S4[1]=S4[1].replaceAll("(\\s*)$","");
                                    ans[Integer.valueOf(S3[0])].setAnswers(S4[1],Integer.valueOf(S4[0]),a);
                                }
                            }
                        }
                    }
                }

            }
        }

        int studentNum=0;
        Students[] students=new Students[100];
        for(int a=0;a<process.getStudent().size();a++){
            process.getStudent().set(a,process.getStudent().get(a).replaceAll("#X:",""));
            String[] student1=process.getStudent().get(a).split("-");
            for(int b=0;b<student1.length;b++){
                String[] student2=student1[b].split("\\s+");
                students[b]=new Students(student2[0],student2[1]);
                studentNum++;
            }
        }
        int[] check = new int[100];
        int[] number2=new int[100];
        //System.out.println(process.getT().size());
        if(process.getT().size()!=0){
            int Max=Integer.valueOf(T1[0]);
            for (int b = 0; b < process.getT().size(); b++){
                T1=process.getT().get(b).split("\\s+");
                if (ans[Integer.valueOf(T1[0])].getAllScore() != 100) {
                    number2[Integer.valueOf(T1[0])]=1;
                    if(Max<Integer.valueOf(T1[0])){
                        Max=Integer.valueOf(T1[0]);
                    }
                }
            }
            for (int b = 1; b <= Max; b++){
                if(number2[b]==1){
                    System.out.println("alert: full score of test paper" + b + " is not 100 points");
                }
            }
        }
        for (int a = 0; a < process.getS().size(); a++) {
                S1 = process.getS().get(a).split("(\\s*)#");
                for(int d=0;d<S1.length;d++) {
                    if (S1[d].startsWith("S:")) {
                        S1[d] = S1[d].replaceAll("S:(\\s*)", "");
                        S3 = S1[d].split(" ");
                    }
                    if (S1[d].startsWith("A:")) {
                        S1[d] = S1[d].replaceAll("A:(\\s*)", "");
                        S4 = S1[d].split("-");
                    }
                }
                for(int b=0;b<process.getT().size();b++){
                    T1=process.getT().get(b).split("\\s+");
                    if(Integer.valueOf(S3[0])==Integer.valueOf(T1[0])){
                        break;
                    }
                }
                String[] T4=new String[100];
                check[a] = Integer.valueOf(S3[0]);
                    int check1 = 0;
                    if(process.getT().size()!=0){
                        if (Integer.valueOf(S3[0]) == Integer.valueOf(T1[0])) {
                            check1 = 1;
                        }
                    }
                    if (check1 == 0) {
                        System.out.println("The test paper number does not exist");
                        continue;
                    }
                    if (a > 1) {
                        check[a] = check[a - 1];
                    }
                for(int d=0;d<process.getT().size();d++) {
                    if (Integer.valueOf(T1[0]) == Integer.valueOf(S3[0])) {
                        break;
                    }
                }
                for(int b=0;b<T1.length-1;b++){
                    String[] T2=T1[b+1].split("-");
                    T4[b]=T2[0];
                }
                int num=1;
                for (int b=1;b<T1.length;b++) {
                    S1 = process.getS().get(a).split("(\\s*)#");
                    String[] T3 = T1[b].split("-");
                    for(int c=0;c<S1.length;c++){
                        if(S1[c].startsWith("S:")){
                            S1[c] = S1[c].replaceAll("S:(\\s*)", "");
                            S3=S1[c].split(" ");
                        }
                        if(S1[c].startsWith("A:")) {
                            S1[c] = S1[c].replaceAll("A:(\\s*)", "");
                                if(S1[c].length()>0){
                                    S4 = S1[c].split("-");
                                    if(b==Integer.valueOf(S4[0])){
                                        if (ans[Integer.valueOf(S3[0])].checkQuestion(process.getQuestion(), Integer.valueOf(T3[0]))) {
                                            ans[Integer.valueOf(S3[0])].ifRight(Integer.valueOf(T4[b-1]), Integer.valueOf(S4[0]),a);
                                        }
                                    }
                                }
                        }
                    }
                    String question=process.getS().get(a).replaceAll("#S:|#A:","");
                    String[] Question=question.split("\\s+");

                        if(ans[Integer.valueOf(S3[0])].getAnswers(b,a)==null){
                            System.out.println("answer is null");
                        }
                        else{
                            if(ans[Integer.valueOf(S3[0])].checkQuestion(process.getQuestion(),Integer.valueOf(T3[0]))){
                                if(ans[Integer.valueOf(S3[0])].getScore(Integer.valueOf(b))==0){
                                    System.out.println("the question "+T3[0]+" invalid~0");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == 1) {
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.quest[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "true");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == 2){
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.moreAnswerQuestions[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "true");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == 3){
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.moreAnswerQuestions[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "partially correct");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == 4){
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.summaryQuestions[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "true");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == 5){
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.summaryQuestions[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "partially correct");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == -1){
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.moreAnswerQuestions[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "false");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == -2){
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.summaryQuestions[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "false");
                                }
                                else if (ans[Integer.valueOf(S3[0])].judgements[b] == 0) {
                                    System.out.println(ans[Integer.valueOf(S3[0])].text.quest[Integer.valueOf(T3[0])].getMessage() + "~" + ans[Integer.valueOf(S3[0])].getAnswers(b,a) + "~" + "false");
                                }
                            }
                            else{
                                System.out.println("non-existent question~0");
                            }
                        }


                }
                int sum = 0;
                int check2=0;
                for (int b = 0; b < studentNum; b++){
                    if(students[b].getID().equals(S3[1])){
                        check2=1;
                        break;
                    }
                }
                if(check2==1){
                    int number1=0;
                    for(int c=0;c<students.length;c++){
                        if(students[c].getID().equals(S3[1])){
                            number1=c;
                            break;
                        }
                    }
                    System.out.printf("%s %s: ",students[number1].getID(),students[number1].getName());
                    for (int b = 1; b < T1.length; b++) {
                        String[] T3 = T1[b].split("-");
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 1 && b + 1 != T1.length) {
                            System.out.printf("%d ", ans[Integer.valueOf(T1[0])].getScore(b));
                            sum += ans[Integer.valueOf(T1[0])].getScore(b);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 0 && b + 1 != T1.length) {
                            System.out.printf("0 ");
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 2 && b + 1 != T1.length) {
                            System.out.printf("%d ", ans[Integer.valueOf(T1[0])].getScore(b));
                            sum += ans[Integer.valueOf(T1[0])].getScore(b);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 3 && b + 1 != T1.length) {
                            System.out.printf("%d ", ans[Integer.valueOf(T1[0])].getScore(b)/2);
                            sum += ans[Integer.valueOf(T1[0])].getScore(b)/2;
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == -1 && b + 1 != T1.length) {
                            System.out.printf("0 ");
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 4 && b + 1 != T1.length) {
                            System.out.printf("%d ", ans[Integer.valueOf(T1[0])].getScore(b));
                            sum += ans[Integer.valueOf(T1[0])].getScore(b);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 5 && b + 1 != T1.length) {
                            System.out.printf("%d ", ans[Integer.valueOf(T1[0])].getScore(b)/2);
                            sum += ans[Integer.valueOf(T1[0])].getScore(b)/2;
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == -2 && b + 1 != T1.length) {
                            System.out.printf("0 ");
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 1 && b + 1 == T1.length) {
                            sum += ans[Integer.valueOf(T1[0])].getScore(b);
                            System.out.printf("%d~%d\n", ans[Integer.valueOf(T1[0])].getScore(b), sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 0 && b + 1 == T1.length) {
                            System.out.printf("0~%d\n", sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 2 && b + 1 == T1.length) {
                            sum += ans[Integer.valueOf(T1[0])].getScore(b);
                            System.out.printf("%d~%d\n", ans[Integer.valueOf(T1[0])].getScore(b), sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 3 && b + 1 == T1.length) {
                            sum += ans[Integer.valueOf(T1[0])].getScore(b)/2;
                            System.out.printf("%d~%d\n", ans[Integer.valueOf(T1[0])].getScore(b)/2, sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == -1 && b + 1 == T1.length) {
                            System.out.printf("0~%d\n", sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 4 && b + 1 == T1.length) {
                            sum += ans[Integer.valueOf(T1[0])].getScore(b);
                            System.out.printf("%d~%d\n", ans[Integer.valueOf(T1[0])].getScore(b), sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == 5 && b + 1 == T1.length) {
                            sum += ans[Integer.valueOf(T1[0])].getScore(b)/2;
                            System.out.printf("%d~%d\n", ans[Integer.valueOf(T1[0])].getScore(b)/2, sum);
                        }
                        if (ans[Integer.valueOf(T1[0])].judgements[b] == -2 && b + 1 == T1.length) {
                            System.out.printf("0~%d\n", sum);
                        }
                    }
                }
                else{
                    System.out.println(S3[1]+" not found");
                }


        }
    }
}

2)第五次程式碼:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.LinkedList;
abstract class ControlDevice{
    protected double Pin1=0;
    protected double Pin2=0;
    public double getInPin1() {
        return Pin1;
    }
    public void setPin1(double Pin1) {
        this.Pin1 = Pin1;
    }
    public double getPin2() {
        return Pin2;
    }
    public void setPin2(double Pin2) {
        this.Pin2 = Pin2;
    }
}
class Switch extends ControlDevice{
    private int state=0;
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public double getPin2(){
        if(state==0){
            return 0;
        }
        else {
            return Pin1;
        }
    }
    public void exchange(){
        if(state==0){
            state=1;
        }
        else {
            state=0;
        }
    }
}
class Governor extends ControlDevice{
    protected double inputPin=0;
    protected double outputPin=0;
}
class ConstantGovernor extends Governor{
    private double parameter=0;
    public double getParameter() {
        return parameter;
    }
    public void setParameter(double parameter) {
        this.parameter = parameter;
    }
    public double getInputPin() {
        return inputPin;
    }
    public void setInputPin(double inputPin) {
        this.inputPin = inputPin;
    }
    public double getOutputPin() {
        return inputPin*parameter;
    }

}
class GradGovernor extends Governor{
    private int gear=0;
    public int getGear() {
        return gear;
    }
    public void setGear(int gear) {
        this.gear = gear;
    }
    public double getInputPin() {
        return inputPin;
    }
    public void setInputPin(double inputPin) {
        this.inputPin = inputPin;
    }
    public double getOutputPin() {
        if(this.gear==0){
            return 0;
        }
        else if(this.gear==1){
            return inputPin*0.3;
        }
        else if(this.gear==2){
            return inputPin*0.6;
        }
        else{
            return inputPin*0.9;
        }
    }
    public void upGear(){
        if(gear<3){
            gear++;
        }
    }
    public void downGear(){
        if(gear>0){
            gear--;
        }
    }
}
abstract class ControlledDevice{
    protected double Pin1=0;
    protected double Pin2=0;
    public double getPin1() {
        return Pin1;
    }
    public void setPin1(double pin1) {
        Pin1 = pin1;
    }
    public double getPin2() {
        return Pin2;
    }
    public void setPin2(double pin2) {
        Pin2 = pin2;
    }
}
class Light extends ControlledDevice{
    protected int brightness=0;
}
class IncandescentLamp extends Light{
    public int getBrightness(){
        if(Math.abs(Pin1-Pin2)>=0&&Math.abs(Pin1-Pin2)<9){
            return 0;
        }
        else if(Math.abs(Pin1-Pin2)==220){
            return 200;
        }
        else if(Math.abs(Pin1-Pin2)==10){
            return 50;
        }
        else{
            return (int)((Math.abs(Pin1-Pin2)-10)*(150.0/210.0)+50);
        }
    }
}
class FluorescentLamp extends ControlledDevice{
    public int getBrightness(){
        if(Math.abs(Pin1-Pin2)==0){
            return 0;
        }
        else{
            return 180;
        }
    }
}
class Fan extends ControlledDevice{
    private int speed;
    public int getSpeed() {
        if(Math.abs(Pin1-Pin2)<80){
            return 0;
        }
        else if(Math.abs(Pin1-Pin2)==80){
            return 80;
        }
        else if(Math.abs(Pin1-Pin2)==150){
            return 360;
        }
        else if(Math.abs(Pin1-Pin2)>150){
            return 360;
        }
        else {
            return (int)((Math.abs(Pin1-Pin2)-80)*(280/70)+80);
        }
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}
class Power{
    private int power;
    public double getPower(){
        return 220;
    }
}
class Agent{
    private Switch[] Switchs=new Switch[100];
    private GradGovernor[] gradGovernors=new GradGovernor[100];
    private ConstantGovernor[] constantGovernors=new ConstantGovernor[100];
    private IncandescentLamp[] incandescentLamps=new IncandescentLamp[100];
    private FluorescentLamp[] fluorescentLamps=new FluorescentLamp[100];
    private LinkedList<String> allDevices=new LinkedList<>();
    private Fan[] fans=new Fan[100];
    private Power power=new Power();
    public void process(String s){
        if(s.startsWith("[")){
            s=s.replaceAll("[\\[\\]]","");
            String[] S=s.split("\\s+");
            for(int a=0;a<2;a++){
                S[a]=S[a].replaceAll("-\\d+","");
                String[] number=S[a].split("[KFLBRD]");
                if(S[a].equals("VCC")){
                    allDevices.add(S[a]);
                }
                else if(S[a].startsWith("K")){
                    Switchs[Integer.valueOf(number[1])]=new Switch();
                    allDevices.add(S[a]);
                }
                else if(S[a].startsWith("F")){
                    gradGovernors[Integer.valueOf(number[1])]=new GradGovernor();
                    allDevices.add(S[a]);
                }
                else if(S[a].startsWith("L")){
                    constantGovernors[Integer.valueOf(number[1])]=new ConstantGovernor();
                    allDevices.add(S[a]);
                }
                else if(S[a].startsWith("B")){
                    incandescentLamps[Integer.valueOf(number[1])]=new IncandescentLamp();
                    allDevices.add(S[a]);
                }
                else if(S[a].startsWith("R")){
                    fluorescentLamps[Integer.valueOf(number[1])]=new FluorescentLamp();
                    allDevices.add(S[a]);
                }
                else if(S[a].startsWith("D")){
                    fans[Integer.valueOf(number[1])]=new Fan();
                    allDevices.add(S[a]);
                }
            }
        }
        else if(s.startsWith("#")){
            String[] number=s.split("[KFLBRD]");
            if(s.startsWith("#K")){
                Switchs[Integer.parseInt(number[1])].exchange();
            }
            if(s.startsWith("#F")){
                String[] upDown=number[1].split("\\d+");
                String num=number[1].replaceAll("[+\\-]","");
                if(Objects.equals(upDown[1], "+")){
                    gradGovernors[Integer.parseInt(num)].upGear();
                }
                else {
                    gradGovernors[Integer.parseInt(num)].downGear();
                }
            }
            if(s.startsWith("#L")){
                String[] apart=number[1].split(":");
                constantGovernors[Integer.parseInt(apart[0])].setParameter(Double.valueOf(apart[1]));
            }
        }
    }
    public void outPut(){
        LinkedList<String> K=new LinkedList<>();
        LinkedList<String> F=new LinkedList<>();
        LinkedList<String> L=new LinkedList<>();
        LinkedList<String> B=new LinkedList<>();
        LinkedList<String> R=new LinkedList<>();
        LinkedList<String> D=new LinkedList<>();
        double outPower=power.getPower();
        for(int a=0;a<allDevices.size();a++){
            String[] number=allDevices.get(a).split("[KFLBRD]");
            if(allDevices.get(a).startsWith("K")&&Switchs[Integer.valueOf(number[1])].getState()==0){
                outPower=0;
                break;
            }
        }
        for(int a=1;a<allDevices.size();a+=2){
            String[] number=allDevices.get(a).split("[KFLBRD]");
            if(allDevices.get(a).startsWith("K")){
                Switchs[Integer.valueOf(number[1])].setPin1(outPower);
                outPower=Switchs[Integer.valueOf(number[1])].getPin2();
                K.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("F")){
                gradGovernors[Integer.valueOf(number[1])].setInputPin(outPower);
                outPower=gradGovernors[Integer.valueOf(number[1])].getOutputPin();
                F.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("L")){
                constantGovernors[Integer.valueOf(number[1])].setInputPin(outPower);
                outPower=constantGovernors[Integer.valueOf(number[1])].getOutputPin();
                L.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("B")){
                incandescentLamps[Integer.valueOf(number[1])].setPin1(outPower);
                outPower=incandescentLamps[Integer.valueOf(number[1])].getPin2();
                B.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("R")){
                fluorescentLamps[Integer.valueOf(number[1])].setPin1(outPower);
                outPower=fluorescentLamps[Integer.valueOf(number[1])].getPin2();
                R.add(allDevices.get(a));
            }
            else{
                fans[Integer.valueOf(number[1])].setPin1(outPower);
                outPower=fans[Integer.valueOf(number[1])].getPin2();
                D.add(allDevices.get(a));
            }
        }
        Sort(K);
        Sort(F);
        Sort(L);
        Sort(B);
        Sort(R);
        Sort(D);
        if(!K.isEmpty()){
            for(int a=0;a<K.size();a++){
                String[] number=K.get(a).split("[KFLBRD]");
                if(Switchs[Integer.valueOf(number[1])].getState()==0){
                    System.out.println("@"+K.get(a)+":turned on");
                }
                else{
                    System.out.println("@"+K.get(a)+":closed");
                }
            }
        }
        if(!F.isEmpty()){
            for(int a=0;a<F.size();a++){
                String[] number=F.get(a).split("[KFLBRD]");
                System.out.println("@"+F.get(a)+":"+gradGovernors[Integer.valueOf(number[1])].getGear());
            }
        }
        if(!L.isEmpty()){
            for(int a=0;a<L.size();a++){
                String[] number=L.get(a).split("[KFLBRD]");
                System.out.printf("@%s:%.2f\n",L.get(a),constantGovernors[Integer.valueOf(number[1])].getParameter());
            }
        }
        if(!B.isEmpty()){
            for(int a=0;a<B.size();a++){
                String[] number=B.get(a).split("[KFLBRD]");
                System.out.printf("@%s:%d\n",B.get(a),incandescentLamps[Integer.valueOf(number[1])].getBrightness());
            }
        }
        if(!R.isEmpty()){
            for(int a=0;a<R.size();a++){
                String[] number=R.get(a).split("[KFLBRD]");
                System.out.printf("@%s:%d\n",R.get(a),fluorescentLamps[Integer.valueOf(number[1])].getBrightness());
            }
        }
        if(!D.isEmpty()){
            for(int a=0;a<D.size();a++){
                String[] number=D.get(a).split("[KFLBRD]");
                System.out.printf("@%s:%d\n",D.get(a),fans[Integer.valueOf(number[1])].getSpeed());
            }
        }
    }
    public void Sort(LinkedList<String> n){
        for(int a=0;a<n.size();a++){
            for(int b=0;b<n.size()-1;b++){
                String first=n.get(b);
                String late=n.get(b+1);
                if(n.get(b).compareTo(n.get(b+1))==1){
                    n.set(b,late);
                    n.set(b+1,first);
                }
            }
        }
    }
}
public class Main {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        Agent agent=new Agent();
        for(int a=0;;a++){
            String message=input.nextLine();
            if(message.equals("end")){
                break;
            }
            agent.process(message);
        }
        agent.outPut();
    }
}

3)第六次程式碼:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.LinkedList;
abstract class ControlDevice{
    protected double Pin1=0;
    protected double Pin2=0;
    public double getInPin1() {
        return Pin1;
    }
    public void setPin1(double Pin1) {
        this.Pin1 = Pin1;
    }
    public double getPin2() {
        return Pin2;
    }
    public void setPin2(double Pin2) {
        this.Pin2 = Pin2;
    }
}
class Switch extends ControlDevice{
    private int state=0;
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public double getPin2(){
        if(state==0){
            return 0;
        }
        else {
            return Pin1;
        }
    }
    public void exchange(){
        if(state==0){
            state=1;
        }
        else {
            state=0;
        }
    }
}
class Governor extends ControlDevice{
    protected double inputPin=0;
    protected double outputPin=0;
}
class ConstantGovernor extends Governor{
    private double parameter=0;
    public double getParameter() {
        return parameter;
    }
    public void setParameter(double parameter) {
        this.parameter = parameter;
    }
    public double getInputPin() {
        return inputPin;
    }
    public void setInputPin(double inputPin) {
        this.inputPin = inputPin;
    }
    public double getOutputPin() {
        return inputPin*parameter;
    }

}
class GradGovernor extends Governor{
    private int gear=0;
    public int getGear() {
        return gear;
    }
    public void setGear(int gear) {
        this.gear = gear;
    }
    public double getInputPin() {
        return inputPin;
    }
    public void setInputPin(double inputPin) {
        this.inputPin = inputPin;
    }
    public double getOutputPin() {
        if(this.gear==0){
            return 0;
        }
        else if(this.gear==1){
            return inputPin*0.3;
        }
        else if(this.gear==2){
            return inputPin*0.6;
        }
        else{
            return inputPin*0.9;
        }
    }
    public void upGear(){
        if(gear<3){
            gear++;
        }
    }
    public void downGear(){
        if(gear>0){
            gear--;
        }
    }
}
abstract class ControlledDevice{
    protected int resistance;
    protected double Pin1=0;
    protected double Pin2=0;
    public double getPin1() {
        return Pin1;
    }
    public void setPin1(double pin1) {
        Pin1 = pin1;
    }
    public double getPin2() {
        return Pin2;
    }
    public void setPin2(double pin2) {
        Pin2 = pin2;
    }
}
class Light extends ControlledDevice{
    protected int brightness=0;
}
class IncandescentLamp extends Light{
    public IncandescentLamp(){
        super.resistance=10;
    }
    public int getBrightness(){
        if(Math.abs(Pin1-Pin2)>=0&&Math.abs(Pin1-Pin2)<9){
            return 0;
        }
        else if(Math.abs(Pin1-Pin2)==220){
            return 200;
        }
        else if(Math.abs(Pin1-Pin2)==10){
            return 50;
        }
        else{
            return (int)((Math.abs(Pin1-Pin2)-10)*(150.0/210.0)+50);
        }
    }
}
class FluorescentLamp extends ControlledDevice{
    public FluorescentLamp(){
        super.resistance=5;
    }
    public int getBrightness(){
        if(Math.abs(Pin1-Pin2)==0){
            return 0;
        }
        else{
            return 180;
        }
    }
}
class Fan extends ControlledDevice{
    private int speed;
    public Fan(){
        super.resistance=20;
    }
    public int getSpeed() {
        if(Math.abs(Pin1-Pin2)<80){
            return 0;
        }
        else if(Math.abs(Pin1-Pin2)==80){
            return 80;
        }
        else if(Math.abs(Pin1-Pin2)==150){
            return 360;
        }
        else if(Math.abs(Pin1-Pin2)>150){
            return 360;
        }
        else {
            return (int)((Math.abs(Pin1-Pin2)-80)*(280/70)+80);
        }
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}
class FloorFan extends ControlledDevice{
    private int speed;
    public FloorFan(){
        super.resistance=20;
    }
    public int getSpeed() {
        if(Math.abs(Pin1-Pin2)<80){
            return 0;
        }
        else if(Math.abs(Pin1-Pin2)>=80&&Math.abs(Pin1-Pin2)<100){
            return 80;
        }
        else if(Math.abs(Pin1-Pin2)>=100&&Math.abs(Pin1-Pin2)<120){
            return 160;
        }
        else if(Math.abs(Pin1-Pin2)>=120&&Math.abs(Pin1-Pin2)<140){
            return 260;
        }
        else {
            return 360;
        }
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}
class Power{
    private int power;
    public double getPower() {
        return 220;
    }
}
class Agent{
    private Map<String,LinkedList<String>> T=new HashMap<>();
    private Map<String,ArrayList<String>> Map=new HashMap<>();
    LinkedList<String> allDevices=new LinkedList<>();
    private Switch[] Switchs=new Switch[100];
    private GradGovernor[] gradGovernors=new GradGovernor[100];
    private ConstantGovernor[] constantGovernors=new ConstantGovernor[100];
    private IncandescentLamp[] incandescentLamps=new IncandescentLamp[100];
    private FluorescentLamp[] fluorescentLamps=new FluorescentLamp[100];
    private Fan[] fans=new Fan[100];
    private FloorFan[] floorFans=new FloorFan[100];
    private Power power=new Power();
    public void process(String s){
        if(s.startsWith("#T")){
            s=s.replaceAll("#","");
            String[] S=s.split(":");
            S[1]=S[1].replaceAll("[\\[\\]]","");
            String[] components=S[1].split("\\s+");
            if(S[1].startsWith("IN")){
                LinkedList<String> allDevices=new LinkedList<>();
                for(int a=0;a<components.length;a++){
                    String det=components[a].replaceAll("-\\d+","");
                    String[] number=det.split("[KFLBRDA]");
                    if(components[a].equals("IN")){
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("K")){
                        Switchs[Integer.valueOf(number[1])]=new Switch();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("F")){
                        gradGovernors[Integer.valueOf(number[1])]=new GradGovernor();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("L")){
                        constantGovernors[Integer.valueOf(number[1])]=new ConstantGovernor();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("B")){
                        incandescentLamps[Integer.valueOf(number[1])]=new IncandescentLamp();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("R")){
                        fluorescentLamps[Integer.valueOf(number[1])]=new FluorescentLamp();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("D")){
                        fans[Integer.valueOf(number[1])]=new Fan();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("A")){
                        floorFans[Integer.valueOf(number[1])]=new FloorFan();
                        allDevices.add(det);
                    }
                }
                T.put(S[0],allDevices);
            }
            if(S[1].startsWith("VCC")){
                for(int a=0;a<components.length;a++){
                    String det=components[a].replaceAll("-\\d+|-IN|-OUT","");
                    String[] number=det.split("[KFLBRDA]");
                    if(components[a].equals("VCC")){
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("K")){
                        Switchs[Integer.valueOf(number[1])]=new Switch();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("F")){
                        gradGovernors[Integer.valueOf(number[1])]=new GradGovernor();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("L")){
                        constantGovernors[Integer.valueOf(number[1])]=new ConstantGovernor();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("B")){
                        incandescentLamps[Integer.valueOf(number[1])]=new IncandescentLamp();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("R")){
                        fluorescentLamps[Integer.valueOf(number[1])]=new FluorescentLamp();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("D")){
                        fans[Integer.valueOf(number[1])]=new Fan();
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("M")){
                        allDevices.add(det);
                    }
                    else if(components[a].startsWith("A")){
                        floorFans[Integer.valueOf(number[1])]=new FloorFan();
                        allDevices.add(det);
                    }
                }
            }
        }
        if(s.startsWith("#M")){
            ArrayList<String> M=new ArrayList<>();
            s=s.replaceAll("#","");
            String[] S=s.split(":");
            S[1]=S[1].replaceAll("[\\[\\]]","");
            String[] components=S[1].split("\\s+");
            for(int a=0;a<components.length;a++){
                M.add(components[a]);
            }
            Map.put(S[0],M);
        }
        else{
            String[] number=s.split("[KFLBRDA]");
            if(s.startsWith("#K")){
                Switchs[Integer.parseInt(number[1])].exchange();
            }
            if(s.startsWith("#F")){
                String[] upDown=number[1].split("\\d+");
                String num=number[1].replaceAll("[+\\-]","");
                if(Objects.equals(upDown[1], "+")){
                    gradGovernors[Integer.parseInt(num)].upGear();
                }
                else {
                    gradGovernors[Integer.parseInt(num)].downGear();
                }
            }
            if(s.startsWith("#L")){
                String[] apart=number[1].split(":");
                constantGovernors[Integer.parseInt(apart[0])].setParameter(Double.valueOf(apart[1]));
            }
        }
    }
    public void outPut(){
        LinkedList<String> K=new LinkedList<>();
        LinkedList<String> F=new LinkedList<>();
        LinkedList<String> L=new LinkedList<>();
        LinkedList<String> B=new LinkedList<>();
        LinkedList<String> R=new LinkedList<>();
        LinkedList<String> D=new LinkedList<>();
        LinkedList<String> A=new LinkedList<>();
        LinkedList<Double> resistance=new LinkedList<>();
        Map<String,ArrayList<Double>> branchResistance=new HashMap<>();
        int position=0;
        double allResistance=0;
        double outPower=power.getPower();
        for(int a=1;a<allDevices.size();a+=2) {
            String[] number = allDevices.get(a).split("[KFLBRDA]");
            if(allDevices.get(a).startsWith("B")){
                resistance.add(10.0);
            }
            if(allDevices.get(a).startsWith("R")){
                resistance.add(5.0);
            }
            if(allDevices.get(a).startsWith("D")||allDevices.get(a).startsWith("A")){
                resistance.add(20.0);
            }
            if (allDevices.get(a).startsWith("K") && Switchs[Integer.valueOf(number[1])].getState() == 0) {
                outPower = 0;
            }
            if (allDevices.get(a).startsWith("M")) {
                int KDepart = 0;
                double allOuMu=0;
                double allOuMu1=0;
                for (int c = 0; c < Map.get(allDevices.get(a)).size(); c++) {
                    ArrayList<Double> OuMu=new ArrayList<>();
                    boolean switchOK=true;
                    for(int d=1;d<T.get(Map.get(allDevices.get(a)).get(c)).size();d+=2){
                        String[] number2 = T.get(Map.get(allDevices.get(a)).get(c)).get(d).split("[KFLBRDA]");
                        if(T.get(Map.get(allDevices.get(a)).get(c)).get(d).startsWith("K")&& Switchs[Integer.valueOf(number2[1])].getState() == 0){
                            switchOK=false;
                            break;
                        }
                    }
                    if(switchOK){
                        for (int b = 1; b < T.get(Map.get(allDevices.get(a)).get(c)).size(); b+=2) {
                            String[] number1 = T.get(Map.get(allDevices.get(a)).get(c)).get(b).split("[KFLBRDA]");
                            if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("B")){
                                OuMu.add(10.0);
                                allOuMu+=10;
                            }
                            if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("R")){
                                OuMu.add(5.0);
                                allOuMu+=5;
                            }
                            if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("D")||T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("A")){
                                OuMu.add(20.0);
                                allOuMu+=20;
                            }
                            if (T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("K") && Switchs[Integer.valueOf(number1[1])].getState() == 0) {
                                KDepart++;
                            }
                        }
                        branchResistance.put(Map.get(allDevices.get(a)).get(c),OuMu);
                        allOuMu1+=Math.pow(allOuMu,-1);
                        allOuMu=0;
                    }
                    else {
                        for (int b = 1; b < T.get(Map.get(allDevices.get(a)).get(c)).size(); b+=2) {
                            if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("B")){
                                OuMu.add(10.0);
                            }
                            if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("R")){
                                OuMu.add(5.0);
                            }
                            if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("D")||T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("A")){
                                OuMu.add(20.0);
                            }
                        }
                        branchResistance.put(Map.get(allDevices.get(a)).get(c),OuMu);
                    }
                }
                allOuMu1=Math.pow(allOuMu1,-1);
                resistance.add(allOuMu1);
                if (KDepart == Map.get(allDevices.get(a)).size()) {
                    outPower = 0;
                }
            }
        }
        for(int a=0;a<resistance.size();a++){
            allResistance+=resistance.get(a);
        }
        for(int a=1;a<allDevices.size();a+=2){
            String[] number=allDevices.get(a).split("[KFLBRDA]");
            if(allDevices.get(a).startsWith("F")){
                gradGovernors[Integer.valueOf(number[1])].setInputPin(outPower);
                outPower=gradGovernors[Integer.valueOf(number[1])].getOutputPin();
                F.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("L")){
                constantGovernors[Integer.valueOf(number[1])].setInputPin(outPower);
                outPower=constantGovernors[Integer.valueOf(number[1])].getOutputPin();
                L.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("K")){
                Switchs[Integer.valueOf(number[1])].setPin1(outPower);
                outPower=Switchs[Integer.valueOf(number[1])].getPin2();
                K.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("B")){
                incandescentLamps[Integer.valueOf(number[1])].setPin1(outPower*(resistance.get(position)/allResistance));
                position++;
                B.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("R")){
                fluorescentLamps[Integer.valueOf(number[1])].setPin1(outPower*(resistance.get(position)/allResistance));
                position++;
                R.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("A")){
                floorFans[Integer.valueOf(number[1])].setPin1(outPower*(resistance.get(position)/allResistance));
                position++;
                A.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("D")){
                fans[Integer.valueOf(number[1])].setPin1(outPower*(resistance.get(position)/allResistance));
                position++;
                D.add(allDevices.get(a));
            }
            else if(allDevices.get(a).startsWith("M")){
                for (int c = 0; c < Map.get(allDevices.get(a)).size(); c++){
                    double outPower1=outPower*(resistance.get(position)/allResistance);
                    double allResistance1=0;
                    int location=0;
                    for(int d=0;d<branchResistance.get(Map.get(allDevices.get(a)).get(c)).size();d++){
                        allResistance1+=branchResistance.get(Map.get(allDevices.get(a)).get(c)).get(d);
                    }
                    for(int b=1;b<T.get(Map.get(allDevices.get(a)).get(c)).size();b+=2){
                        String[] number1 = T.get(Map.get(allDevices.get(a)).get(c)).get(b).split("[KFLBRDA]");
                        if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("K")&&Switchs[Integer.valueOf(number1[1])].getState() == 0){
                            outPower1=0;
                        }
                    }
                    for(int b=1;b<T.get(Map.get(allDevices.get(a)).get(c)).size();b+=2){
                        String[] number1 = T.get(Map.get(allDevices.get(a)).get(c)).get(b).split("[KFLBRDA]");
                        if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("K")){
                            Switchs[Integer.valueOf(number1[1])].setPin1(outPower1);
                            outPower1=Switchs[Integer.valueOf(number1[1])].getPin2();
                            K.add(T.get(Map.get(allDevices.get(a)).get(c)).get(b));
                        }
                        else if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("B")){
                            incandescentLamps[Integer.valueOf(number1[1])].setPin1(outPower1*(branchResistance.get(Map.get(allDevices.get(a)).get(c)).get(location)/allResistance1));
                            location++;
                            B.add(T.get(Map.get(allDevices.get(a)).get(c)).get(b));
                        }
                        else if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("R")){
                            fluorescentLamps[Integer.valueOf(number1[1])].setPin1(outPower1*(branchResistance.get(Map.get(allDevices.get(a)).get(c)).get(location)/allResistance1));
                            location++;
                            R.add(T.get(Map.get(allDevices.get(a)).get(c)).get(b));
                        }
                        else if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("A")){
                            floorFans[Integer.valueOf(number1[1])].setPin1(outPower1*(branchResistance.get(Map.get(allDevices.get(a)).get(c)).get(location)/allResistance1));
                            location++;
                            A.add(T.get(Map.get(allDevices.get(a)).get(c)).get(b));
                        }
                        else if(T.get(Map.get(allDevices.get(a)).get(c)).get(b).startsWith("D")){
                            fans[Integer.valueOf(number1[1])].setPin1(outPower1*(branchResistance.get(Map.get(allDevices.get(a)).get(c)).get(location)/allResistance1));
                            location++;
                            D.add(T.get(Map.get(allDevices.get(a)).get(c)).get(b));
                        }
                    }
                }
                position++;
            }
        }
        Sort(K);
        Sort(F);
        Sort(L);
        Sort(B);
        Sort(R);
        Sort(D);
        Sort(A);
        if(!K.isEmpty()){
            for(int a=0;a<K.size();a++){
                String[] number=K.get(a).split("[KFLBRDA]");
                if(Switchs[Integer.valueOf(number[1])].getState()==0){
                    System.out.println("@"+K.get(a)+":turned on");
                }
                else{
                    System.out.println("@"+K.get(a)+":closed");
                }
            }
        }
        if(!F.isEmpty()){
            for(int a=0;a<F.size();a++){
                String[] number=F.get(a).split("[KFLBRDA]");
                System.out.println("@"+F.get(a)+":"+gradGovernors[Integer.valueOf(number[1])].getGear());
            }
        }
        if(!L.isEmpty()){
            for(int a=0;a<L.size();a++){
                String[] number=L.get(a).split("[KFLBRDA]");
                System.out.printf("@%s:%.2f\n",L.get(a),constantGovernors[Integer.valueOf(number[1])].getParameter());
            }
        }
        if(!B.isEmpty()){
            for(int a=0;a<B.size();a++){
                String[] number=B.get(a).split("[KFLBRDA]");
                System.out.printf("@%s:%d\n",B.get(a),incandescentLamps[Integer.valueOf(number[1])].getBrightness());
            }
        }
        if(!R.isEmpty()){
            for(int a=0;a<R.size();a++){
                String[] number=R.get(a).split("[KFLBRDA]");
                System.out.printf("@%s:%d\n",R.get(a),fluorescentLamps[Integer.valueOf(number[1])].getBrightness());
            }
        }
        if(!D.isEmpty()){
            for(int a=0;a<D.size();a++){
                String[] number=D.get(a).split("[KFLBRDA]");
                System.out.printf("@%s:%d\n",D.get(a),fans[Integer.valueOf(number[1])].getSpeed());
            }
        }
        if(!A.isEmpty()){
            for(int a=0;a<A.size();a++){
                String[] number=A.get(a).split("[KFLBRDA]");
                System.out.printf("@%s:%d\n",A.get(a),floorFans[Integer.valueOf(number[1])].getSpeed());
            }
        }
    }
    public void Sort(LinkedList<String> n){
        for(int a=0;a<n.size();a++){
            for(int b=0;b<n.size()-1;b++){
                String[] number=n.get(b).split("[KFLBRDA]");
                String[] number1=n.get(b+1).split("[KFLBRDA]");
                int first=Integer.valueOf(number[1]);
                int late=Integer.valueOf(number1[1]);
                String first1=n.get(b);
                String late1=n.get(b+1);
                if(first>late){
                    n.set(b,late1);
                    n.set(b+1,first1);
                }
            }
        }
    }
}
public class Main {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        Agent agent=new Agent();
        for(int a=0;;a++){
            String message=input.nextLine();
            if(message.equals("end")){
                break;
            }
            agent.process(message);
        }
        agent.outPut();
    }
}

4.改進建議
新的pta相比上一次的進步是肯定的,類的設計在一定程度上要高於演算法帶來的便利,對以後的程式碼改進提供了方便
建議:
i.還是老毛病了,第五次作業不明顯,寫hardcoding還是改不了的毛病
ii.建議將Agent類裡面的方法在拆開來,分成更細的方法,這樣就可以重複使用某一個方法,提高複用性
5.總結
i.在這三次pta作業中可以明顯感覺到類設計的重要性
ii.我進一步學習到了繼承和多型
iii.進一步學習瞭如何處理類間關係

學習永無止境,期待未來的我們

相關文章