# C# HOMEWORK --- [TOC] ## 一上 ## Assignment6 ### Assignment6_1 ![](https://i.imgur.com/plyAQsD.png) #### solution1 ```csharp= double a, d, n, sum = 0; Console.Write("pls enter the value of a: "); a = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter the value of d: "); d = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter the value og N: "); n = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(" i sum"); Console.WriteLine("--- -----"); for (int i = 1; i<=n;i++) { sum = sum + a + (i - 1) * d; Console.WriteLine("{0,3} {1}", i, sum); } ``` #### solution2 ```csharp= double a, d, n, sum = 0, i = 1; Console.Write("pls enter the value of a: "); a = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter the value of d: "); d = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter the value og N: "); n = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(" i sum"); Console.WriteLine("--- -----"); while(i<=n) { sum += + a + (i - 1) * d; Console.WriteLine("{0,3} {1}", i, sum); i++; } ``` #### solution3 ```csharp= double a, d, n, sum, i; Console.Write("Please enter the value of a: "); a = Convert.ToDouble(Console.ReadLine()); Console.Write("Please enter the value of d: "); d = Convert.ToDouble(Console.ReadLine()); Console.Write("Please enter the value of N: "); n = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(" i sum"); Console.WriteLine("--- -----"); sum = 0; for (i = 1; i<= n ; i++) { sum = sum + a + (i - 1) * d; Console.WriteLine("{0,3} {1,-5}",i,sum); } ``` ### Assignment6_2 ![](https://i.imgur.com/1D7gC1Q.png) #### solution1 ```csharp= double a = Convert.ToDouble(textBox1.Text); double d = Convert.ToDouble(textBox2.Text); double n = Convert.ToDouble(textBox3.Text); double sum = 0; textBox4.Text = " i sum \r\n--- ----- \r\n"; for (int i = 1; i <= n; i++) { sum = sum + a + (i - 1) * d; textBox4.Text += string.Format("{0} {1}\r\n", i, sum); } ``` #### solution2 ```csharp= double a = Convert.ToDouble(textBox1.Text); double d = Convert.ToDouble(textBox2.Text); double n = Convert.ToDouble(textBox3.Text); double sum = 0; int i = 1; textBox4.Text = " i sum \r\n--- ----- \r\n"; while (i <= n) { sum = sum + a + (i - 1) * d; textBox4.Text += string.Format("{0} {1}\r\n", i, sum); i++; } ``` #### solution3 ```csharp= double a, d, n, i, sum; a = Convert.ToDouble(textBox1.Text); d = Convert.ToDouble(textBox2.Text); n = Convert.ToDouble(textBox3.Text); i = 1; sum = 0; textBox4.Text = " i sum \r\n--- ----- \r\n"; /* textBox4.Text += " i sum\r\n"; textBox4.Text += "--- -----\r\n"; */ for (;i<=n;i++) { sum += a + (i - 1) * d; textBox4.Text += string.Format("{0} {1}\r\n", i, sum); /* or textBox4.Text += Convert.ToString(i) + " "; textBox4.Text += Convert.ToString(sum)+"\r\n"; */ } ``` ### Assignment6_3 ![](https://i.imgur.com/R2wbILo.png) #### solution1 ```csharp= double n, m, up = 1, down = 1; Console.Write("pls enter the value of n: "); n = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter the value of m: "); m = Convert.ToDouble(Console.ReadLine()); for(int i =1; i<=m;i++) { up = up * (n - (i-1)); down = down * (i); } Console.WriteLine("The combination is: " + up / down); ``` #### solution2 ```csharp= double n, m, up = 1, down = 1, i =0; Console.Write("pls enter the value of n: "); n = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter the value of m: "); m = Convert.ToDouble(Console.ReadLine()); while(i<m) { up = up * (n - i); down = down * (m - i); i=i+1; } Console.WriteLine("The combination is: " + up / down); ``` #### solution3 ```csharp= double n, m, n1, m1, i; Console.Write("Please enter the value of n: "); n = Convert.ToDouble(Console.ReadLine()); Console.Write("Please enter the value of m: "); m = Convert.ToDouble(Console.ReadLine()); n1 = 1; m1 = 1; for (i = 0;i < m;i++) { n1 *= (n-i); m1 *= (m-i); } Console.WriteLine("The combination is: {0}", n1 /m1); ``` #### solution4 ```csharp= Console.Write("Please enter the value of n: "); string s_n = Console.ReadLine(); int n = Convert.ToInt32(s_n); Console.Write("Please enter the value of m: "); string s_m = Console.ReadLine(); int m = Convert.ToInt32(s_m); double a=1, b=1, c=1; for (int i = 1; i <= n; i++) a = a * i; for (int i = 1; i <= m; i++) b *= i; for (int i = 1; i <= (n - m); i++) c *= i; double result = a / (b * c); Console.WriteLine("The combination is: {0}", result); ``` ### Assignment6_4 ![](https://i.imgur.com/fmKNpll.png) #### solution1 ```csharp= double num, i , a = 0, b = 0, c = 0, score; Console.Write("pls enter student's number: "); num = Convert.ToDouble(Console.ReadLine()); for (i = 1; i <= num; i += 1) { Console.Write("pls enter {0} student's score: ", i); score = Convert.ToDouble(Console.ReadLine()); if (score >= 80) a++; else if (score >= 60) b += 1; else c++; } Console.WriteLine("======================="); Console.WriteLine("get a: " + a); Console.WriteLine("get b: " + b); Console.WriteLine("get c: " + c); ``` #### solution2 ```csharp= double num, i = 1, a = 0, b = 0, c = 0, score; Console.Write("pls enter student's number: "); num = Convert.ToDouble(Console.ReadLine()); while(i<=num) { Console.Write("pls enter {0} student's score: ", i); score = Convert.ToDouble(Console.ReadLine()); if (score >= 80) a++; else if (score >= 60) b += 1; else c++; i++; } Console.WriteLine("======================="); Console.WriteLine("get a: " + a); Console.WriteLine("get b: " + b); Console.WriteLine("get c: " + c); ``` #### solution3 ```csharp= int a, b, c; a = 0; b = 0; c = 0; Console.Write("請輸入學生人數: "); double num = Convert.ToDouble(Console.ReadLine()); for (int i = 1; i<=num;i++) { Console.Write("請輸入第{0}個同學的成績: ", i); double score = Convert.ToDouble(Console.ReadLine()); if (score >= 80) a += 1; else if (score >= 60) b += 1; else c += 1; } Console.WriteLine("=========成績統計========="); Console.WriteLine("得A個數: {0} \n得B個數: {1} \n得C個數: {2}", a, b, c); ``` ### Assignment6_5 ![](https://i.imgur.com/pRGs6S2.png) #### solution1 ```csharp= double num, data, ave = 0, i1, i2, a; Console.Write("pls enter num: "); num = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter data: "); data = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(""); for (i1=1;i1<=num;i1++) { Console.WriteLine("worker " + i1 + " how"); for(i2=1;i2<=data;i2++) { Console.Write("pls enter{0} data", i2); a = Convert.ToDouble(Console.ReadLine()); ave = ave + a; } Console.WriteLine("ave:" + ave / data); ave = 0; Console.WriteLine(""); } ``` #### solution2 ```csharp= double num, data, ave = 0, i1, i2, a; Console.Write("pls enter num: "); num = Convert.ToDouble(Console.ReadLine()); Console.Write("pls enter data: "); data = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(""); i1 = 1; i2 = 1; while(i1<=num) { Console.WriteLine("worker " + i1 + " how"); while(i2<=data) { Console.Write("pls enter{0} data", i2); a = Convert.ToDouble(Console.ReadLine()); ave = ave + a; i2++; } Console.WriteLine("ave:" + ave / data); ave = 0; Console.WriteLine(""); i1++; i2 = 1; } ``` #### solution3 ```csharp= double num1, num2,i1,i2,ave,ave1; Console.Write("請輸入操作人員數: "); num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("請輸入每個操作人員的資料數: "); num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(""); for (i1 = 1; i1 <= num1; i1++) { ave = 0; Console.WriteLine("操作員{0}號", i1); for (i2 = 1; i2 <= num2; i2++) { Console.Write("請輸入第{0}筆資料: ", i2); ave1 = Convert.ToDouble(Console.ReadLine()); ave += ave1; } Console.WriteLine("數據平均值為: {0}", ave / num2); Console.WriteLine(""); ``` ### Assignment6_6 ![](https://i.imgur.com/2Hr3mm7.png) #### solution1 ```csharp= double num, sum, max, ave,ave2; max = 0; sum = 0; ave = 0; while(true) { Console.Write("請輸入一個整數(-1代表結束): "); num = Convert.ToDouble(Console.ReadLine()); if (num == -1) break; else { if (num >= max) max = num; sum++; ave = ave + num; } } Console.WriteLine("============================="); Console.WriteLine("最大的數字為: " + max); Console.WriteLine("輸入數字的總數: " + sum); Console.WriteLine("輸入數字的平均值為: " + ave/sum); ``` #### solution2 ```csharp= for(; ; ) { Console.Write("請輸入一個整數(-1代表結束): "); num = Convert.ToDouble(Console.ReadLine()); if (num == -1) break; else { if (num >= max) max = num; sum++; ave = ave + num; } } Console.WriteLine("============================="); Console.WriteLine("最大的數字為: " + max); Console.WriteLine("輸入數字的總數: " + sum); Console.WriteLine("輸入數字的平均值為: " + ave / sum); ``` #### solution3 ```csharp= double max = 0, amount = 0, num = 0, f = 0; while (true) { Console.Write("請輸入一個整數(-1代表結束): "); num = Convert.ToDouble(Console.ReadLine()); if (num != -1) { max = max > num ? max : num; amount += num; f += 1; } else break; } Console.WriteLine("================================="); Console.WriteLine("最大的數字為: {0} \n輸入數字的總數為: {1} \n輸入數字的平均值為: {2}", max, f, amount/f); ``` ### Assignment6_7 ![](https://i.imgur.com/GmBsVUz.png) #### solution1 ```csharp= int num = Convert.ToInt32(textBox1.Text); int amount = 0; for (int i = 1;i>0;i++) { if (num / 7 == i) { amount += num + i; if (amount / 7 >i) { amount++; break; } else break; } } textBox2.Text = "你購買的數量: " + Convert.ToString(num) + "瓶\r\n" + "你可喝的數量: " + Convert.ToString(amount) + "瓶"; ``` #### solution2 ```csharp= int num = Convert.ToInt32(textBox1.Text); int num0 = num; int c = num / 7; num = num + c; int b = num / 7; if (b > c) num = num + b - c; textBox2.Text = string.Format("buy: {0} \r\ndrink: {1}", num0, num); ``` #### solution3 ```csharp= int original = Convert.ToInt32(textBox1.Text); int current = original; int total = original; while (current >= 7) { current = current - 7 + 1; total = total + 1; } //while (current >= 7) //{ // total = total + current / 7; // current = current % 7 + current / 7; // current % 7 => 目前喝剩下的瓶數; current / 7 => 換來的瓶數 //} textBox2.Text = "你購買的數量: " + original + " 瓶" + "\r\n"; textBox2.Text += "你可喝的數量: " + total + " 瓶"; ``` ## Assignment7 ### Assignment7_1 ![](https://i.imgur.com/WvdUUyO.png) #### solution1 ```csharp= int [] a = new int [10]; int max = -10000000, e = 0 ; for (int i = 0;i<a.Length;i++) { Console.Write("Enter the number {0}: ",i+1); a[i] = Convert.ToInt32(Console.ReadLine()); if (a[i] >= max) { max = a[i]; e = i; } } Console.WriteLine(""); Console.WriteLine("Largest number is: {0}", max); Console.WriteLine("It's element number<s> is: {0}", e+1); ``` #### solution2 ```csharp= int [] a = new int [10]; int max = -10000000, e = 0 ; for (int i = 0;i<10;i++) { Console.Write("Enter the number"+(i+1)+": "); a[i] = Convert.ToInt32(Console.ReadLine()); if (a[i] >= max) { max = a[i]; e = i; } } Console.Write("\n"); Console.WriteLine("Largest number is: "+ max); Console.WriteLine("It's element number<s> is: " + e+1); ``` #### solution3 ```csharp= int max = 0, en = 0; int[] array1 = new int[10]; for (int i = 0;i<10;i++) { Console.Write("Enter the number {0}: ", i+1); array1[i] = Convert.ToInt32(Console.ReadLine()); if (i == 0) max = array1[i]; else max = max >= array1[i] ? max : array1[i]; if (max == array1[i]) en = i+1; } Console.WriteLine(""); Console.WriteLine("Largest number is: " + max); Console.WriteLine("It's element number<s> is: " + en); ``` ### Assignment7_2 ![](https://i.imgur.com/HBHbwYT.png) #### solution1 ```csharp= int[] a = new int[10]; int max = -10000000; for (int i = 0; i < a.Length; i++) { Console.Write("Enter the number {0}: ", i + 1); a[i] = Convert.ToInt32(Console.ReadLine()); if (a[i] >= max) max = a[i]; } Console.WriteLine(""); Console.WriteLine("Largest number is: {0}", max); Console.Write("It's element number<s> is: "); for (int i = 0; i<10;i++) { if (max == a[i]) Console.Write(i + 1 + " "); } Console.WriteLine(""); ``` #### solution2 ```csharp= int [] a = new int [10]; int max = -10000000 ; for (int i = 0;i<10;i++) { Console.Write("Enter the number"+(i+1)+": "); a[i] = Convert.ToInt32(Console.ReadLine()); if (a[i] >= max) max = a[i]; } Console.Write("\n"); Console.WriteLine("Largest number is: "+ max); Console.Write("It's element number<s> is: "); for (int i = 0; i<10;i++) { if (max == a[i]) Console.Write(i + 1 + " "); } Console.Write("\n"); ``` #### solution3 ```csharp= int max = 0; int[] array1 = new int[10]; for (int i = 0; i < 10; i++) { Console.Write("Enter the number {0}: ", i + 1); array1[i] = Convert.ToInt32(Console.ReadLine()); if (i == 0) max = array1[i]; else max = max >= array1[i] ? max : array1[i]; } Console.WriteLine(""); Console.WriteLine("Largest number is: " + max); Console.Write("It's element number<s> is: "); for (int i1 = 0; i1 < 10; i1++) { if (max == array1[i1]) Console.Write(i1+1+" "); } Console.WriteLine(""); ``` ### assignment7_3 ![](https://i.imgur.com/8spGMaD.png) #### solution1 ```csharp= int[] a = new int[10]; int max = -10000000; Console.Write("pls enter the your num: "); int b = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(""); for (int i = 0; i < b; i++) { Console.Write("Enter the number {0}: ", i + 1); a[i] = Convert.ToInt32(Console.ReadLine()); if (a[i] >= max) max = a[i]; } Console.WriteLine(""); Console.WriteLine("Largest number is: {0}", max); Console.Write("It's element number<s> is: "); for (int i = 0;i<b;i++) { if (max == a[i]) Console.Write(i + 1 + " "); } Console.WriteLine(""); ``` #### solution2 ```csharp= int max = -10000000; Console.Write("pls enter the your num: "); int b = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(""); int[] a = new int[b]; for (int i = 0; i < a.Length; i++) { Console.Write("Enter the number {0}: ", i + 1); a[i] = Convert.ToInt32(Console.ReadLine()); if (a[i] >= max) max = a[i]; } Console.WriteLine(""); Console.WriteLine("Largest number is: {0}", max); Console.Write("It's element number<s> is: "); for (int i = 0; i<a.Length;i++) { if (max == a[i]) Console.Write(i + 1 + " "); } Console.WriteLine(""); ``` ##### solution3 ```csharp= Console.Write("Please enter the amount of your numbers: "); int num = Convert.ToInt32(Console.ReadLine()); int[] array = new int[num]; int max = 0; Console.WriteLine(""); for(int i = 0;i<array.Length;i++) { Console.Write("Enter the number {0}: ", i + 1); array[i] = Convert.ToInt32(Console.ReadLine()); if (i == 0) max = array[i]; else max = max > array[i] ? max : array[i]; } Console.WriteLine(""); Console.WriteLine("Largest number is: " + max); Console.Write("It's element number<s> is: "); for (int i2 = 0; i2 < array.Length; i2++) { if (max == array[i2]) Console.Write(i2 + 1 + " "); } Console.WriteLine(""); ``` ### Assignment7_4 ![](https://i.imgur.com/AvBSaQE.png) #### solution1 ```csharp= int max = -10000000; Console.Write("pls enter the your num: "); string b = Console.ReadLine(); Console.WriteLine(""); string[] a = b.Split(','); for (int i = 0; i < a.Length; i++) { int c = Convert.ToInt32(a[i]); Console.Write("Enter the number {0}: ", i + 1); Console.WriteLine(c); if (c >= max) max = c; } Console.WriteLine(""); Console.WriteLine("Largest number is: {0}", max); Console.Write("It's element number<s> is: "); for (int i = 0; i<a.Length;i++) { int c = Convert.ToInt32(a[i]); if (max ==c) Console.Write(i + 1 + " "); } Console.WriteLine(""); ``` #### solution2 ```csharp= ``` #### solution3 ```csharp= Console.Write("Enter a set of numbers: "); string num = Console.ReadLine(); string[] array = num.Split(','); // #單引號(字元) Console.WriteLine(""); int max = 0; for (int i = 0; i < array.Length; i++) { Console.WriteLine("The number {0}: {1}",i+1,array[i] ); int ass = Convert.ToInt32(array[i]); if (i == 0) max = ass; else max = max > ass ? max : ass; } Console.WriteLine(""); Console.WriteLine("Largest number is: " + max); Console.Write("It's element number<s> is: "); for(int i2 = 0;i2<array.Length;i2++) { int bnn = Convert.ToInt32(array[i2]); if (max == bnn) Console.Write(i2+1 + " "); } Console.WriteLine(""); ``` ### Assignment7_5 ![](https://i.imgur.com/AGpor08.png) #### solution1 ```csharp= string[] Name = { "王一 ", "黃二 ", "張三 ", "李四 ", "陳五 ", "丁六 ", "鄭七 " }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; Console.Write("成績分割點<只能輸入0~100的整數>: "); int a = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(""); Console.WriteLine("國文成績大於及等於 {0} 者:", a); Console.WriteLine("姓名 成績"); for (int i = 0;i<Name.Length;i++) { if (Chinese[i] >= a) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("國文成績小於 {0} 者:", a); Console.WriteLine("姓名 成績"); for (int i = 0; i < Name.Length; i++) { if (Chinese[i] < a) Console.WriteLine(Name[i] + " " + Chinese[i]); } ``` #### solution2 ```csharp= ``` #### solution3 ```csharp= string[] Name = { "王一 ", "黃二 ", "張三 ", "李四 ", "陳五 ", "丁六 ", "鄭七 " }; int[] Chinese = { 80, 45, 60, 90, 20, 50,70}; Console.Write("成績分割點<只能輸入0~100的整數>: "); int num = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(""); Console.WriteLine("國文成績大於及等於 {0} 者:", num); Console.WriteLine("姓名 成績"); for(int i = 0;i<Name.Length;i++) { if (Chinese[i] >= num) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine("\n"); Console.WriteLine("國文成績小於 {0} 者:", num); Console.WriteLine("姓名 成績"); for (int i = 0; i < Name.Length; i++) { if (Chinese[i] < num) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine(""); ``` ### Assignment7_6 ![](https://i.imgur.com/ue57pfT.png) #### solution1 ```csharp= int num = Convert.ToInt32(textBox1.Text); string[] Name = { "王一 ", "黃二 ", "張三 ", "李四 ", "陳五 ", "丁六 ", "鄭七 " }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; label2.Text = string.Format("國文成績大於及等於{0}者:",num); label3.Text = string.Format("國文成績小於{0}者:",num); textBox2.Text = "姓名 成績\r\n"; for (int i = 0;i<Name.Length;i++) { if(Chinese[i]>=num) textBox2.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } if (textBox2.Text == "姓名 成績\r\n") textBox2.Text = "無此學生"; textBox3.Text = "姓名 成績\r\n"; for (int i = 0; i < Name.Length; i++) { if (Chinese[i] < num) textBox3.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } if (textBox3.Text == "姓名 成績\r\n") textBox3.Text = "無此學生"; ``` #### solution2 ```csharp= ``` #### solution3 ```csharp= int num = Convert.ToInt32(textBox1.Text); string[] Name = { "王一 ", "黃二 ", "張三 ", "李四 ", "陳五 ", "丁六 ", "鄭七 " }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; label2.Text = string.Format("國文成績大於及等於{0}者:",num); label3.Text = string.Format("國文成績小於{0}者:",num); int ass = 0; int bnn = 0; for (int i = 0; i < Name.Length; i++) { if (Chinese[i] >= num) ass++; else bnn++; } textBox2.Text = "姓名 成績\r\n"; for (int i = 0;i<Name.Length;i++) { if (bnn == Name.Length) textBox2.Text = "無此學生"; else if(Chinese[i]>=num) textBox2.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } textBox3.Text = "姓名 成績\r\n"; for (int i = 0; i < Name.Length; i++) { if (ass == Name.Length) textBox3.Text = "無此學生"; else { if (Chinese[i] < num) textBox3.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } } ``` ### Assignment7_7 ![](https://i.imgur.com/TWL39z9.png) ![](https://i.imgur.com/eupfMEz.png) #### solution1 ```csharp= string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; string[] Department = { "工管系", "機械系", "化材系", "化材系", "工管系", "機械系", "工管系" }; string[] Gender = { "男 ", "男 ", "女", "男", "女", "女", "男" }; int num = Convert.ToInt32(textBox1.Text); label2.Text = "國文成績大於等於及等於" + num + "者:"; label3.Text = "國文成績小於" + num + "者:"; textBox2.Text = ""; textBox3.Text = ""; for (int i = 0; i < Chinese.Length; i++) { if (Chinese[i] >= num) { if (checkBox1.Checked == true && checkBox2.Checked == true) textBox2.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + " " + Gender[i] + "\r\n"); else if (checkBox1.Checked == false && checkBox2.Checked == true) textBox2.Text += (Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"); else if (checkBox1.Checked == true && checkBox2.Checked == false) textBox2.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + "\r\n"); else textBox2.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } if (Chinese[i] < num) { if (checkBox1.Checked == true && checkBox2.Checked == true) textBox3.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + " " + Gender[i] + "\r\n"); else if (checkBox1.Checked == true && checkBox2.Checked == false) textBox3.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + "\r\n"); else if (checkBox1.Checked == false && checkBox2.Checked == true) textBox3.Text += (Name[i] + " " + Chinese[i] + " " + Gender[i] + " " + "\r\n"); else textBox3.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } } if (textBox2.Text == "") textBox2.Text = "無此學生"; if (textBox3.Text == "") textBox3.Text = "無此學生"; ``` #### solution2 ```csharp= ``` #### solution3 ```csharp= string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; string[] Department = { "工管系", "機械系", "化材系", "化材系", "工管系", "機械系", "工管系" }; string[] Gender = { "男 ", "男 ", "女", "男", "女", "女", "男" }; int num = Convert.ToInt32(textBox1.Text); int ass = 0, bnn = 0; label2.Text = "國文成績大於等於及等於" + num + "者:"; label3.Text = "國文成績小於" + num + "者:"; for (int i = 0;i<Chinese.Length;i++) { if (Chinese[i] >= num) ass++; else bnn++; } textBox2.Text = ""; textBox3.Text = ""; for (int i = 0; i < Chinese.Length; i++) { if (ass == 0) textBox2.Text = "無此學生"; else if (Chinese[i] >= num) { if (checkBox1.Checked == true && checkBox2.Checked == true) textBox2.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + " " + Gender[i] + "\r\n"); else if (checkBox1.Checked == true && checkBox2.Checked == false) textBox2.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + "\r\n"); else if (checkBox1.Checked == false && checkBox2.Checked == true) textBox2.Text += (Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"); else textBox2.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } if (bnn == 0) textBox3.Text = "無此學生"; else if (Chinese[i] < num) { if (checkBox1.Checked == true && checkBox2.Checked == true) textBox3.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + " " + Gender[i] + "\r\n"); else if (checkBox1.Checked == true && checkBox2.Checked == false) textBox3.Text += (Name[i] + " " + Chinese[i] + " " + Department[i] + "\r\n"); else if (checkBox1.Checked == false && checkBox2.Checked == true) textBox3.Text += (Name[i] + " " + Chinese[i] + " " + Gender[i] + " " + "\r\n"); else textBox3.Text += (Name[i] + " " + Chinese[i] + "\r\n"); } } ``` ### Assignment7_8 ![](https://i.imgur.com/vPpAbvo.png) ![](https://i.imgur.com/czu5xfj.png) #### solution1 ```csharp= int num = Convert.ToInt32(textBox1.Text); label2.Text = "國文成績大於及等於" + num + "者:"; label3.Text = "國文成績小於" + num + "者:"; string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; string[] Gender = { "男", "男", "女", "男", "女", "女", "男" }; textBox2.Text = ""; textBox3.Text = ""; for (int i = 0;i<Chinese.Length;i++) { if (Chinese[i] < num) { if (radioButton1.Checked == true) { if (Gender[i] == "男") textBox3.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } if (radioButton2.Checked == true) { if (Gender[i] == "女") textBox3.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } } if (Chinese[i] >= num) { if (radioButton1.Checked == true) { if (Gender[i] == "男") textBox2.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } if (radioButton2.Checked == true) { if (Gender[i] == "女") textBox2.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } } } if (textBox2.Text == "") textBox2.Text = "無此學生"; if (textBox3.Text == "") textBox3.Text = "無此學生"; } ``` #### solution2 ```csharp= ``` #### solution3 ```csharp= int num = Convert.ToInt32(textBox1.Text); label2.Text = "國文成績大於及等於" + num + "者:"; label3.Text = "國文成績小於" + num + "者:"; string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; string[] Gender = { "男", "男", "女", "男", "女", "女", "男" }; int ass = 0, bnn = 0; for(int i = 0;i<Chinese.Length;i++) { if (Chinese[i] >= num) ass++; else bnn++; } textBox2.Text = ""; textBox3.Text = ""; for (int i = 0;i<Chinese.Length;i++) { if (ass == Chinese.Length) textBox3.Text = "無此學生"; else if (Chinese[i] < num) { if (radioButton1.Checked == true) { if (Gender[i] == "男") textBox3.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } if (radioButton2.Checked == true) { if (Gender[i] == "女") textBox3.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } } if (bnn == Chinese.Length) textBox2.Text = "無此學生"; else if (Chinese[i] >= num) { if (radioButton1.Checked == true) { if (Gender[i] == "男") textBox2.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } if (radioButton2.Checked == true) { if (Gender[i] == "女") textBox2.Text += Name[i] + " " + Chinese[i] + " " + Gender[i] + "\r\n"; } } } if (textBox2.Text == "") textBox2.Text = "無此學生"; if (textBox3.Text == "") textBox3.Text = "無此學生"; ``` --- ## 一下 ## Assignment1 ### Assignment1_1 ![](https://i.imgur.com/W2pTNux.png) #### solution1 ```csharp= class Program { static void Main(string[] args) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; Console.Write("成績分割點 <只能輸入0~100的整數>: "); int point = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(""); Console.WriteLine("國文成績大於及等於 {0} 者:", point); Console.WriteLine("姓名 成績"); for (int i = 0; i < Name.Length; i++) { if (Chinese[i] >= point) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine("\n"); Console.WriteLine("國文成績小於 {0} 者:", point); Console.WriteLine("姓名 成績"); for(int i = 0;i<Name.Length;i++) { if (Chinese[i] < point) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine(""); } } ``` #### solution2 ```csharp= class Program { static void Main(string[] args) { Console.Write("成績分割點 <只能輸入0~100的整數>: "); int point = Convert.ToInt32(Console.ReadLine()); score Score = new score(); Score.get(point); } } class score { public void get(int point) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; Console.WriteLine(""); Console.WriteLine("國文成績大於及等於 {0} 者:", point); Console.WriteLine("姓名 成績"); for (int i = 0; i < Name.Length; i++) { if (Chinese[i] >= point) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine("\n"); Console.WriteLine("國文成績小於 {0} 者:", point); Console.WriteLine("姓名 成績"); for (int i = 0; i < Name.Length; i++) { if (Chinese[i] < point) Console.WriteLine(Name[i] + " " + Chinese[i]); } Console.WriteLine(""); } } ``` ### Assignment1_2 ![](https://i.imgur.com/E68Ma0c.png) #### solution1 ```csharp= class Program { static void Main(string[] args) { string[] ToDoList = { "領錢", "小工盃比賽", "看電影", "準備統計小考", "買文具", "睡覺", "吃飯" }; int[] Priority = { 3, 2, 7, 1, 6, 4, 5 }; Console.Write("查看待辦事項數目: "); int num = Convert.ToInt32(Console.ReadLine()); for(int i = 1;i<=num;i++) { for(int i2 = 0;i2<Priority.Length;i2++) { if (Priority[i2] == i) Console.WriteLine("優先順序{0}: {1}", i, ToDoList[i2]); } } } } ``` #### solution2 ```csharp= class Program { static void Main(string[] args) { string[] ToDoList = { "領錢", "小工盃比賽", "看電影", "準備統計小考", "買文具", "睡覺", "吃飯" }; int[] Priority = { 3, 2, 7, 1, 6, 4, 5 }; Array.Sort(Priority,ToDoList); //Array.Sort(int[],string[]); Console.Write("查看待辦事項數目: "); int num = Convert.ToInt32(Console.ReadLine()); for(int i = 0; i < num; i++) { Console.WriteLine("優先順序" + (i+1) + ": " + ToDoList[i]); } } } ``` ### Assignment1_3 ![](https://i.imgur.com/md5Kr0T.png) #### solution1 ```csharp= class Program { static void Main(string[] args) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; for(int i = 0; i < Chinese.Length - 1; i++) { for(int i2 = i+1;i2<Chinese.Length;i2++) { if (Chinese[i] < Chinese[i2]) { int temp_chinese = Chinese[i2]; Chinese[i2] = Chinese[i]; Chinese[i] = temp_chinese; string temp_name = Name[i2]; Name[i2] = Name[i]; Name[i] = temp_name; } } } for(int i = 0; i < Name.Length; i++) { Console.WriteLine(Name[i]+": "+Chinese[i]); } } } ``` #### solution2 ```csharp= class Program { static void Main(string[] args) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; int[] Chinese = { 80, 45, 60, 90, 20, 50, 70 }; for (int i = 0; i < Chinese.Length; i++) { for (int i2 = 0; i2 < Chinese.Length-1-i; i2++) { if (Chinese[i2] < Chinese[i2+1]) { int temp_chinese = Chinese[i2]; Chinese[i2] = Chinese[i2+1]; Chinese[i2+1] = temp_chinese; string temp_name = Name[i2]; Name[i2] = Name[i2+1]; Name[i2+1] = temp_name; } } } for (int i = 0; i < Name.Length; i++) { Console.WriteLine(Name[i] + ": " + Chinese[i]); } } } ``` ### Assignment1_4 ![](https://i.imgur.com/l8lqHSL.png) #### solution1 ```csharp= class Program { static void Main(string[] args) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; Random randint = new Random(); Console.Write("請輸入人數: "); int num = Convert.ToInt32(Console.ReadLine()); int[] random_array = new int[Name.Length]; for (int i = 0; i < Name.Length; i++) { random_array[i] = randint.Next(1, Name.Length + 1); for (int i2 = 0; i2 < i; i2++) { while (random_array[i2] == random_array[i]) { i2 = 0; random_array[i] =randint.Next(1, Name.Length + 1); } } } for (int i3 = 1; i3 <= num; i3++) { for (int i4 = 0; i4 < Name.Length; i4++) { if (random_array[i4] == i3) { Console.WriteLine(Name[i4]); } } } } } ``` #### solution2 ```csharp = class Program { static void Main(string[] args) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; Random randint = new Random(); // Console.Write("請輸入人數: "); //int num = Convert.ToInt32(Console.ReadLine()); int[] random_array = new int[Name.Length]; for (int i = 0; i < Name.Length; i++) { random_array[i] = randint.Next(1, Name.Length + 1); for (int i2 = 0; i2 < i;i2++ ) { if (random_array[i2] == random_array[i]) { i2 = -1; random_array[i] = randint.Next(1, Name.Length + 1); } } } for(int i = 0; i < Name.Length; i++) { Console.WriteLine(random_array[i]); } for (int i3 = 1; i3 <= num; i3++) { for (int i4 = 0; i4 < Name.Length; i4++) { if (random_array[i4] == i3) { Console.WriteLine(Name[i4]); } } } } } ``` #### solution3 ```csharp= class Program { static void Main(string[] args) { string[] Name = { "王一", "黃二", "張三", "李四", "陳五", "丁六", "鄭七" }; Random rnd = new Random(); Console.Write("請輸入人數: "); int num = Convert.ToInt32(Console.ReadLine()); int[] arr = new int[num]; for(int i = 0; i < num; i++) { arr[i] = rnd.Next(0, Name.Length); for(int i2 = 0; i2 < i; i2++) { while(arr[i2] == arr[i]) { i2 = 0; arr[i] = rnd.Next(0, Name.Length); } } } for (int i3 = 0; i3 < num; i3++) { Console.WriteLine(Name[arr[i3]]); } } } ``` ## Assignment2 ### Assignment2_1 ![](https://i.imgur.com/GkkAKSl.png) ![](https://i.imgur.com/ZLMoXwP.png) #### solution1 ```csharp= class Program { static void Main(string[] args) { double[,] price = { { 0, 140, 480, 950 }, { 130, 0, 550, 880 }, { 520, 430, 0, 520 }, { 980, 870, 500, 0 } }; string[] station = { "台北", "桃園", "台中", "高雄" }; Console.Write("最便宜票價為: "); double ticket_price = Convert.ToDouble(Console.ReadLine()); int starting_point = 0, end_point = 0, b = 0; if (ticket_price != 0) { for (int i = 0; i < price.GetLength(0); i++) { end_point = 0; for (int i2 = 0; i2 < price.GetLength(1); i2++) { if (price[i, i2] == ticket_price) { b = 1; break; } end_point++; } if (b == 1) break; starting_point++; } Console.WriteLine("起始站為: {0} \n終點站為: {1}", station[starting_point], station[end_point]); } else Console.Write(""); } } ``` #### solution2 ```csharp= double[,] price = { { 0, 140, 480, 950 }, { 130, 0, 550, 880 }, { 520, 430, 0, 520 }, { 980, 870, 500, 0 } }; string[] station = { "台北", "桃園", "台中", "高雄" }; Console.Write("最便宜票價為: "); double ticket_price = Convert.ToDouble(Console.ReadLine()); if (ticket_price != 0) { for (int i = 0; i < price.GetLength(0); i++) { for (int i2 = 0; i2 < price.GetLength(1); i2++) { if (price[i, i2] == ticket_price) { Console.WriteLine("起始站為: {0} \n終點站為: {1}", station[i], station[i2]); } } } } else Console.Write(""); ``` #### solution1_change ```csharp= class Program { static void Main(string[] args) { double[,] price = { { 0, 140, 480, 950 }, { 130, 0, 550, 880 }, { 520, 430, 0, 520 }, { 980, 870, 500, 0 } }; string[] station = { "台北", "桃園", "台中", "高雄" }; double min = price[0, 1]; string result = ""; for(int i =0;i<price.GetLength(0);i++) { for(int i2 = 0;i2<price.GetLength(1);i2++) { if(i!=i2) { if (min > price[i, i2]) { min = price[i, i2]; result = String.Format("最便宜票價為: {0}\n起始站為: {1}\n終點站為: {2}",min,station[i],station[i2]); } } } } Console.WriteLine(result); Console.ReadKey(); } } ``` #### solution2_change ```csharp= class Program { static void Main(string[]args) { double[,] price = { { 0, 140, 480, 950 }, { 130, 0, 550, 880 }, { 520, 430, 0, 520 }, { 980, 870, 500, 0 } }; string[] station = { "台北", "桃園", "台中", "高雄" }; double min = 1; int num1 = 0, num2 = 0; for(int i = 0;i<price.GetLength(0);i++) { for(int i2 = 0;i2<price.GetLength(1);i2++) { if(i!=i2) { if (min == 1) min = price[i, i2]; if (min > price[i, i2]) { min = price[i, i2]; num1 = i; num2 = i2; } } } } Console.WriteLine("最便宜票價為: " + min); Console.WriteLine("起始站為: "+station[num1]); Console.WriteLine("終點站為: "+ station[num2]); Console.ReadKey(); } } ``` ### Assignment2_2 ![](https://i.imgur.com/0TPxSJI.png) #### solution1 ```csharp= public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label4_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { string[] station = { "台北", "桃園", "台中", "高雄" }; for(int i = 0;i<station.Length;i++) { comboBox1.Items.Add(station[i]); //Add 方法:items屬性的方法之一 comboBox2.Items.Add(station[i]); } //comboBox1.Text = station[0]; 預選值 //or comboBox1.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { double[,] price = { { 0, 140, 480, 950 }, { 130, 0, 550, 880 }, { 520, 430, 0, 520 }, { 980, 870, 500, 0 } }; int starting_station = comboBox1.SelectedIndex; //SelectedIndex 屬性返回一個表示與當前選定列表項的索引的整數值,可以程式設計更改它 int end_station = comboBox2.SelectedIndex; label4.Text = Convert.ToString(price[starting_station, end_station]); //SelectedItem 屬性與 SelectedIndex 屬性類似,但是SelectedItem 屬性返回的是項 //SelectedText 屬性:表示組合框中當前選定文字的字串 //BeginUpdate 方法和 EndUpdate 方法:當使用Add 方法一次新增一個項時,則可以使用 BeginUpdate 方法,以防止每次向列表新增項時控制元件都重新繪製 ComboBox。完成向列表新增項的任務後,呼叫 EndUpdate 方法來啟用 ComboBox 進行重新繪製。當向列表新增大量的項時,使用這種方法新增項可以防止繪製 ComboBox 時閃爍 } } ``` #### solution1 ```csharp= private void Form1_Load(object sender, EventArgs e) { string[] station = { "台北", "桃園", "台中", "高雄" }; foreach (string text in station) { comboBox1.Items.Add(text); comboBox2.Items.Add(text); } comboBox1.SelectedIndex = 0; comboBox2.SelectedIndex = 0; } private void button1_Click_1(object sender, EventArgs e) { double[,] price = { { 0, 140, 480, 950 }, { 130, 0, 550, 880 }, { 520, 430, 0, 520 }, { 980, 870, 500, 0 } }; label4.Text = Convert.ToString(price[comboBox1.SelectedIndex, comboBox2.SelectedIndex]); } ``` ### Assignment2_3 ![](https://i.imgur.com/0Mgs4qx.png) #### solution1 ```csharp= public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string[,] student = { { "王一", "80", "55" }, { "黃二", "45", "50" }, { "張三", "60", "40" }, { "李四", "90", "80" }, { "陳五", "20", "70" } }; double chinese_score = Convert.ToDouble(textBox1.Text); double english_score = Convert.ToDouble(textBox2.Text); textBox3.Text = "姓名 國文 英文\r\n"; int student_chinese, student_english; for(int i = 0;i<student.GetLength(0);i++) { student_chinese = Convert.ToInt32(student[i, 1]); student_english = Convert.ToInt32(student[i, 2]); if (student_chinese >= chinese_score && student_english>=english_score) textBox3.Text += string.Format("{0} {1} {2} \r\n", student[i, 0], student[i, 1], student[i, 2]); } } } ``` #### solution2 ```csharp= private void button1_Click_1(object sender, EventArgs e) { int c = Convert.ToInt32(textBox1.Text); int i = Convert.ToInt32(textBox1.Text); int a, b; string[,] student = { { "王一", "80", "55" }, { "黃二", "45", "50" }, { "張三", "60", "40" }, { "李四", "90", "80" }, { "陳五", "20", "70" } }; textBox3.Text = "name chinese english\r\n"; for(int i2 = 0;i2<student.GetLength(0);i2++) { a = Convert.ToInt32(student[i2, 1]); b = Convert.ToInt32(student[i2, 2]); if (c <= a && i <= b) textBox3.Text += student[i2, 0] + " " + student[i2, 1] + " " + student[i2, 2] + "\r\n"; } } ``` ### Assignment2_4 ![](https://i.imgur.com/PUzY7qD.png) #### solution1 ```csharp= public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable table = new DataTable(); private void Form1_Load(object sender, EventArgs e) { table.Columns.Add("姓名"); table.Columns.Add("國文"); table.Columns.Add("英文"); dataGridView1.DataSource = table; } private void button1_Click(object sender, EventArgs e) { string[,] student = { { "王一", "80", "55" }, { "黃二", "45", "50" }, { "張三", "60", "40" }, { "李四", "90", "80" }, { "陳五", "20", "70" } }; table.Rows.Clear(); int chinese_score = Convert.ToInt32(textBox1.Text); int english_score = Convert.ToInt32(textBox2.Text); int student_chinese, student_english; string[] name; for (int i = 0;i<student.GetLength(0);i++) { student_chinese = Convert.ToInt32(student[i, 1]); student_english = Convert.ToInt32(student[i, 2]); if (student_chinese >= chinese_score && student_english >= english_score) { name = new string[student.GetLength(1)]; for(int i2 = 0;i2<student.GetLength(1);i2++) { name[i2] = student[i,i2]; } table.Rows.Add(name); } } } } ``` #### solution2 ```csharp= private void Form1_Load(object sender, EventArgs e) { dt.Columns.Add("name"); dt.Columns.Add("chinese"); dt.Columns.Add("english"); dataGridView1.DataSource = dt; } private void button1_Click_1(object sender, EventArgs e) { string[,] student = { { "王一", "80", "55" }, { "黃二", "45", "50" }, { "張三", "60", "40" }, { "李四", "90", "80" }, { "陳五", "20", "70" } }; dt.Rows.Clear(); int chinese_score = Convert.ToInt32(textBox1.Text); int english_score = Convert.ToInt32(textBox2.Text); int student_chinese, student_english; for (int i = 0; i < student.GetLength(0); i++) { student_chinese = Convert.ToInt32(student[i, 1]); student_english = Convert.ToInt32(student[i, 2]); if (student_chinese >= chinese_score && student_english >= english_score) dt.Rows.Add(student[i,0],student[i,1],student[i,2]); } } ``` ## Assignment3 ### Assignment3_1 ![](https://i.imgur.com/FpTeZbY.png) ![](https://i.imgur.com/MTylvbl.png) #### solution1 ```csharp= public DataTable dt = new DataTable(); private void Form1_Load(object sender, EventArgs e) { dt.Columns.Add("Name"); dt.Columns.Add("Min"); dt.Columns.Add("Max"); dt.Columns.Add("Mean"); dt.Columns.Add("SD"); dataGridView1.DataSource = dt; } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "text files(*.txt)|*.txt|All files(*.*)|*.*"; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "開啟"; openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) textBox1.Text = openFileDialog1.FileName; } private void button2_Click(object sender, EventArgs e) { dt.Rows.Clear(); string path = textBox1.Text; FileInfo fi = new FileInfo(path); StreamReader sr = fi.OpenText(); string temp; int column = 0,row = 0; while((temp = sr.ReadLine())!=null) { if (row == 0) column = temp.Split(',').Length; row++; } sr.Close(); sr = fi.OpenText(); string[,] data = new string[row, column]; int index = 0; while((temp = sr.ReadLine())!= null) { for (int i = 0; i < column; i++) data[index, i] = temp.Split(',')[i]; index++; } sr.Close(); double min, max, sum, tempnum, mean,sd1,sd2; for(int i = 1;i<row;i++) { min = Convert.ToDouble(data[i, 1]); max = Convert.ToDouble(data[i, 1]); sum = 0; sd1 = 0; for(int i2 = 1;i2<column;i2++) { tempnum = Convert.ToDouble(data[i, i2]); if (min > tempnum) min = tempnum; if (max < tempnum) max = tempnum; sum += tempnum; sd1 += tempnum * tempnum; } mean = sum / (column-1); sd2 = Math.Sqrt(sd1 / (column-1) - mean * mean); dt.Rows.Add(data[i, 0], min, max, mean, sd2); } } ``` ### Assignment3_2 ![](https://i.imgur.com/6KdoJUK.png) #### solution1 ```csharp= public class scores { public string name { get; set; } public string department { get; set; } public int exam1 { get; set; } public int exam2 { get; set; } public int exam3 { get; set; } public int exam4 { get; set; } public int exam5 { get; set; } public int exam6 { get; set; } public int exam7 { get; set; } } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "json files(*.json)|*.json|All files(*.*)|*.*"; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "開啟"; openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) textBox1.Text = openFileDialog1.FileName; } private void button2_Click(object sender, EventArgs e) { string path = textBox1.Text; string json = File.ReadAllText(path); var jd = JsonDocument.Parse(json); var rootelement = jd.RootElement; var scrpro = rootelement.GetProperty("scores"); int len = 0; foreach (JsonElement element in scrpro.EnumerateArray()) len++; double[,] exam = new double[len,7]; int index = 0; foreach(JsonElement element in scrpro.EnumerateArray()) { var obj = element.Deserialize<scores>(); exam[index, 0] = obj.exam1; exam[index, 1] = obj.exam2; exam[index, 2] = obj.exam3; exam[index, 3] = obj.exam4; exam[index, 4] = obj.exam5; exam[index, 5] = obj.exam6; exam[index, 6] = obj.exam7; index++; } textBox2.Text = "Exam Mean Stdand Deviation\r\n"; double sum, mean, sd1, sd2; for(int i =0;i<exam.GetLength(1);i++) { sum = 0; sd1 = 0; for(int i2 = 0;i2<exam.GetLength(0);i2++) { sum += exam[i2, i]; sd1 += exam[i2, i] * exam[i2, i]; } mean = sum / exam.GetLength(0); sd2 = Math.Sqrt(sd1 / exam.GetLength(0) - mean * mean); textBox2.Text += $"{i+1} {mean} {sd2}\r\n"; } } ``` #### solution2 by育桀 ```csharp= public class Rootobject { public string name { get; set; } public string department { get; set; } public int exam1 { get; set; } public int exam2 { get; set; } public int exam3 { get; set; } public int exam4 { get; set; } public int exam5 { get; set; } public int exam6 { get; set; } public int exam7 { get; set; } } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, EventArgs e) { var json = File.ReadAllText(textBox1.Text); var jd = JsonDocument.Parse(json); var root = jd.RootElement; textBox2.Text = ""; double ind=0; double exam1 = 0, exam2 = 0, exam3 = 0, exam4 = 0, exam5 = 0, exam6 = 0, exam7 = 0; double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0; foreach (JsonElement elem in root.GetProperty("scores").EnumerateArray()) { ind++; var obj = elem.Deserialize<Rootobject>(); exam1 += obj.exam1; exam2 += obj.exam2; exam3 += obj.exam3; exam4 += obj.exam4; exam5 += obj.exam5; exam6 += obj.exam6; exam7 += obj.exam7; sum1 += obj.exam1 * obj.exam1; sum2 += obj.exam2 * obj.exam2; sum3 += obj.exam3 * obj.exam3; sum4 += obj.exam4 * obj.exam4; sum5 += obj.exam5 * obj.exam5; sum6 += obj.exam6 * obj.exam6; sum7 += obj.exam7 * obj.exam7; } double mean1 = exam1 / ind; double mean2 = exam2 / ind; double mean3 = exam3 / ind; double mean4 = exam4 / ind; double mean5 = exam5 / ind; double mean6 = exam6 / ind; double mean7 = exam7 / ind; double sd1= Math.Sqrt(Math.Abs(sum1 / ind - (mean1 * mean1))); double sd2= Math.Sqrt(Math.Abs(sum2 / ind - (mean2 * mean2))); double sd3= Math.Sqrt(Math.Abs(sum3 / ind - (mean3 * mean3))); double sd4= Math.Sqrt(Math.Abs(sum4 / ind - (mean4 * mean4))); double sd5= Math.Sqrt(Math.Abs(sum5 / ind - (mean5 * mean5))); double sd6= Math.Sqrt(Math.Abs(sum6 / ind - (mean6 * mean6))); double sd7= Math.Sqrt(Math.Abs(sum7 / ind - (mean7 * mean7))); textBox2.Text = "Exam\t" + "Mean\t\t " + "Standard Deviation\r\n" + "1\t" + mean1 + "\t " + sd1+"\r\n"+ "2\t" + mean2 +"\t " + sd2+ "\r\n" + "3\t" + mean3 + "\t " + sd3+ "\r\n" + "4\t" + mean4 +"\t " + sd4+ "\r\n" + "5\t" + mean5 +"\t " + sd5+ "\r\n" + "6\t" + mean6 + "\t " + sd6 + "\r\n" + "7\t" + mean7 + "\t " + sd7 + "\r\n"; } ``` ### Assignment3_3 ![](https://i.imgur.com/saasZDN.png) #### solution1 ```csharp= public class scores { public string name { get; set; } public string department { get; set; } public int exam1 { get; set; } public int exam2 { get; set; } public int exam3 { get; set; } public int exam4 { get; set; } public int exam5 { get; set; } public int exam6 { get; set; } public int exam7 { get; set; } } private void Form1_Load(object sender, EventArgs e) { string path = Environment.CurrentDirectory + "/scores.json"; string json = File.ReadAllText(path); var jd = JsonDocument.Parse(json); var rootelement = jd.RootElement; var scropro = rootelement.GetProperty("scores"); foreach(JsonElement element in scropro.EnumerateArray()) { var obj = element.Deserialize<scores>(); listBox1.Items.Add(obj.name); } } private void button1_Click(object sender, EventArgs e) { string path = Environment.CurrentDirectory + "/scores.json"; string json = File.ReadAllText(path); var jd = JsonDocument.Parse(json); var rootelement = jd.RootElement; var scropro = rootelement.GetProperty("scores"); foreach (JsonElement element in scropro.EnumerateArray()) { var obj = element.Deserialize<scores>(); if (listBox1.SelectedItem.ToString() == obj.name) { textBox1.Text = $"Exam1: {obj.exam1}\r\n" + $"Exam2: {obj.exam2}\r\n" + $"Exam3: {obj.exam3}\r\n" + $"Exam4: {obj.exam4}\r\n" + $"Exam5: {obj.exam5}\r\n" + $"Exam6: {obj.exam6}\r\n" + $"Exam7: {obj.exam7}\r\n"; break; } } } ``` ### Assignment3_4 ![](https://i.imgur.com/FHcrGS2.png) #### solution1 ```csharp= public class scores { public string name { get; set; } public string department { get; set; } public int exam1 { get; set; } public int exam2 { get; set; } public int exam3 { get; set; } public int exam4 { get; set; } public int exam5 { get; set; } public int exam6 { get; set; } public int exam7 { get; set; } } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "json files(*.json)|*.json|All files(*.*)|*.*"; openFileDialog1.InitialDirectory = "C:"; openFileDialog1.Title = "開啟"; openFileDialog1.FileName = ""; if (openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, EventArgs e) { string path = textBox1.Text; string json = File.ReadAllText(path); var jd = JsonDocument.Parse(json); var rootelement = jd.RootElement; var scropro = rootelement.GetProperty("scores"); double sum, mean, sd1, sd2; textBox3.Text = "Name Department Mean SD\r\n"; textBox3.Text += "==========================================\r\n"; int num1 = 0,num2 = 0; foreach(JsonElement element in scropro.EnumerateArray()) { sum = 0; sd1 = 0; var obj = element.Deserialize<scores>(); sum += obj.exam1 + obj.exam2 + obj.exam3 + obj.exam4 + obj.exam5 + obj.exam6 + obj.exam7; mean = sum / 7; sd1 += obj.exam1 * obj.exam1 + obj.exam2 * obj.exam2 + obj.exam3 * obj.exam3 + obj.exam4 * obj.exam4 + obj.exam5 * obj.exam5 + obj.exam6 * obj.exam6 + obj.exam7 * obj.exam7; sd2 = Math.Sqrt(sd1 / 7 - mean * mean); if (textBox2.Text == obj.department) { textBox3.Text += obj.name + " " + obj.department + " " + $"{mean:F4}" + " "+ $"{sd2:F4}"+"\r\n"; num2++; num1 = num2; } } textBox3.Text += string.Format("================================\r\n符合資格者共: {0}位", num1); } ``` ## 考題下載 [Download](https://github.com/KXAINGW/wahahahaha)