隨著 Visual Studio 2017 即將正式釋出,C# 7.0 也隨之來到。身為這次的改版,將會帶來許多方便的新功能
if (int.TryParse(input, out var answer))
WriteLine(answer);
else WriteLine("Could not parse input");
一起讓我們來看看 C# 7.0 的威力吧。
A pH curve is requested for a reaction between 50 mL of 0.2 M HNO3 and 0.1 M NaOH. This involves a neutralization reaction between the acid and base that will result in the pH changing from acidic to basic as the reaction progresses. The curve will track this pH change as the moles of H+ ions are consumed by the OH- ions added from the NaOH.
This document summarizes a titration of 100 mL of 2M HCl with 1M NaOH. It calculates the volume of NaOH needed at the equivalence point as 50 mL. It then sets up the x-axis values from 0 to 100 mL NaOH added in increments of 2 mL. The y-axis values are calculated as the pH at each point using the titration equations before and after the equivalence point.
This document describes how to generate a titration curve for a strong acid-strong base titration using Python. It involves titrating 100 mL of 2 M HCl with 1 M NaOH and plotting the pH as a function of the amount of titrant added. The Python code provided calculates the pH values at different points of titrant addition, plots the curve in three segments before, at, and after the equivalence point, and displays the final titration curve graph.
This document describes an iterative bisection method to find the root of the function 2 * sin(x) + cos(2x) between an initial range. Over 17 iterations, it narrows down the range of r from [4.712389, 6.283185] to [5.908415, 5.908463] by bisection with an allowed error of 1 x 10-5.
4. 10 類別
練習 2
設計打亂區間資料類別,Random_Shuffle,元素可為整數或字元,
資料會先被打亂,然後使用 get() 取得元素,當所有元素都被取出
後,需再度打亂元素順序,以下為程式碼:
86
# foo 為 8 9 10 11 等四個數字
foo = Random_Shuffle(8,11)
# 執行 16 次,依次印出打亂後元素
for i in range( 4 * foo.len() ) :
print( foo.get() , end=" " )
print()
# bar 為 ’a’ ’b’ ’c’ ’d’ ’e’ 等五個字元
bar = Random_Shuffle(’a’,’e’)
# 執行 10 次,依次印出打亂後元素
for i in range( 2 * bar.len() ) :
print( bar.get() , end=" " )
print()
以下為程式執行的結果:
8 9 11 10 8 11 9 10 11 9 8 10 9 8 10 11
d a c e b c a d b e
提示:你可能需使用 type(x) 確認 x 的型別,使用如下:
>>> type(82)
<class ’int’>
>>> type(’cat’)
<class ’str’>
5. 10 類別
練習 3
有若干人玩紙牌遊戲,請分別設計紙牌(Cards)與玩家
(Player)兩類別,模擬紙牌分牌給玩家,最後印出每位
玩家由小到大的牌組,執行程式如下:
87
deck = Cards()
a, b, c, d = Player("Tom") , Player("Sam") , Player("Joe") ,
Player("Amy")
# 模擬分牌,輪流分五張牌給四人
for i in range(5) :
deck >> a >> b >> c >> d
print( a , b , c , d , sep="n" )
輸出:
Tom : C6 C7 D7 D8 D10
Sam : C9 C10 D3 H2 SK
Joe : CJ H5 H9 H10 S2
Amy : C8 D4 DQ H3 S3
提示:>> 要回傳物件本身
6. 10 類別
練習 4-1
參考水桶倒水程式,設計水桶類別(Bucket)用來模擬裝水、倒水的
動作,假設水桶為等底面積且無水位刻度,水桶僅能有裝滿水或倒空
水的動作,設計相關的程式使得以下的程式碼得以運作。
88
# 空水桶
a , b , c = Bucket(5,"a") , Bucket(3,"b") , Bucket(100,"c")
# fill 代表水桶一開始即是滿水位
f = Bucket(100,"f",fill=True)
# f 倒向 a 後,f 再倒向 b
f >> a >> b
print( a , b , c , foo , sep=" , " )
# b 倒向 c 後,a 再倒向 b
a >> ( b >> c )
print( a , b , c , foo , sep=" , " )
# b 倒向 c 後,a 再倒向 b
a >> ( b >> c )
print( a , b , c , foo , sep=" , " )
# f 倒向 a 後,a 再倒向 b
( a << f ) >> b
print( a , b , c , foo , sep=" , " )
21. 10 類別
練習 15-1
定義整數範圍為以下型式:
以上 [1,99] 與 [1:2] 兩者的數字範圍相同,但兩者
取出數字的機率有所差異,前者範圍內的所有數字取出機
率相同,而後者則是先決定位數,有了位數後,再由同位
數的所有數字中隨機取出數字。撰寫程式,設計
Intrange 類別,接受三種數字範圍後產生物件,隨意
產生 10 個數字,程式碼如下:
103
數字型式 數字範圍
[a,b]
[a,b]
[a]
代表數字 x 在 a 數字與 b 數字之間,a < b
代表數字 x 在 a 位數與 b 位數之間,1 ≤ a ≤ b
代表數字 x 為 a 位數,1 ≤ a