# CH1 Visual Studio快整入門
## 20240126作業

## 心得感想
為了更好的了解C#,老師讓我們做幾個最基礎的練習題,內容包括C#的寫法、簡介等,我最後也順利了解了C#的基礎語法及使用方式,透過這樣簡單的練習方式,讓我對C#有了基礎的認識,也成為我未來學習C#重要的基石.
# CH2 Visual C#與.NET
## 1.以.NET 8.0產生主控台程式
程式設計:

執行畫面:

## 2.傳統的主控台程式
程式設計:

執行畫面:

## 3.資料做輸入和輸出的處理
程式設計:

執行畫面:

## 4.格式化輸出
程式設計:

執行畫面:

## 5.視窗應用程式
程式設計:

執行畫面:

### 20240219 心得感想
這次的課堂中,老師教導我們練習視窗應用程式,讓我們可以在點其中一個按鈕時,大螢幕可以輸出我們想讓它因此輸出的文字,這樣的程式相當實用,可以簡單方便的輸出自己想要看到的,也讓我明白了原來很多網頁就是用類似的手法設計的.
# ch3資料型別與變數
## 隨堂練習
### 1.數值格式化練習
程式設計:

執行畫面:

### 2.貸款試算練習
程式設計:


執行畫面:
初始畫面:
計算畫面:
錯誤畫面:
清除畫面:
### 20240226心得
在這一章節中,老師教導我們如何把第二章學到的程式複雜化,並讓他更實用.這次的練習讓我學到非長多酐用的程式語法,例如:如何切換執行程式中的圖片、如何寫出清出所有數值的程式等,各個都相當好用,讓我受益良多.
# CH4 基本輸出入介面設計
## 隨堂練習
### login
程式碼:
```
private void button_Click(object sender, EventArgs e)
{
try
{
int money = int.Parse(textBox2.Text);
label3.Text = $"提款金額:{money:n}元";
}
catch
{
MessageBox.Show("請輸入整數", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
```
執行結果:

### 四則運算
程式碼:
```
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ScrollBar;
using System.Drawing;
using System.Net;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int ans = a + b;
MessageBox.Show("結果是" + ans, "兩數相加", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void button2_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int ans = a - b;
MessageBox.Show("結果是" + ans, "兩數相減", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void button3_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int ans = a * b;
MessageBox.Show("結果是" + ans, "兩數相乘", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void button4_Click(object sender, EventArgs e)
{
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
int ans = a / b;
MessageBox.Show("結果是" + ans, "兩數相除", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
```
執行畫面:

### 成績計算:
```
namespace 19 康軒勻
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
float[] score = new float[]
{
float.Parse(textBox1.Text),
float.Parse(textBox2.Text),
float.Parse(textBox3.Text),
float.Parse(textBox4.Text),
float.Parse(textBox5.Text)
};
float sum = 0, max = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
if (score[i] > max)
{
max = score[i];
}
}
float avg = sum / 5;
label6.Text = $"總分={sum}\n平均={avg:f4}\n最高分={max}";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
```
執行畫面:

# CH5 身份證字號產生器/檢查器
程式碼:
```
deDom.Compiler;
using System.Data.SqlTypes;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
string idChar = "abcdefghjklmnpqrstuvxywzio";
string[] idCry ={"台北市","台中市","基隆市","台南市","高雄市","新北市","宜蘭縣","桃園縣",
"新竹縣","苗栗國","台中市","南投縣","彰化縣","雲林縣","嘉義縣","台南市","高雄市",
"屏東縣","花蓮國","台東縣","澎湖縣","陽明山","金門縣","連江縣","嘉義市","新竹市"};
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void produce_Click(object sender, EventArgs e)
{
string id;
int temp;
int chack_number = 0;
temp = rand.Next(26);
chack_number += ((10 + temp) / 10);
chack_number += ((10 + temp) % 10) * 9;
id = idChar.Substring(temp, 1).ToUpper();
temp = rand.Next(1, 3);
id += (Convert.ToString(temp));
chack_number += temp * 8;
for (int i = 0; i < 7; i++)
{
temp = rand.Next(10);
id += Convert.ToString(temp);
chack_number += (7 - i) * temp;
}
id += Convert.ToString((10 - (chack_number % 10)) % 10);
idinput.Text = id;
}
private void check_Click(object sender, EventArgs e)
{
string id = idinput.Text;
string temp_string;
int temp;
int chack_number = 0;
string _output;
if (id.Trim().Length == 10)
{
temp_string = id.Substring(0, 1).ToLower();
if (idChar.Contains(temp_string))
{
temp = idChar.IndexOf(temp_string);
chack_number += (temp + 10) / 10;
chack_number += ((temp + 10) % 10) * 9;
if (temp == -1)
{
output.Text = "縣市字母錯誤";
return;
}
_output = idCry[temp];
}
else
{
output.Text = "縣市字母錯誤";
return;
}
(temp) = Convert.ToInt16(id.Substring(1, 1).ToLower());
if (temp == 2)
_output += ", 女性";
else if (temp == 1)
_output += ", 男性";
else
{
output.Text = "性別碼錯誤";
return;
}
chack_number += temp * 8;
for (int i = 2; i < 9; i++)
{
chack_number += Convert.ToInt16(id.Substring(i, 1)) * (9 - i);
}
if ((10-(chack_number % 10))%10 == Convert.ToInt16(id.Substring(9, 1)))
{
_output = "正確, " + _output;
}
else
{
output.Text = "檢查號碼錯誤";
return;
}
_output = Convert.ToString(chack_number) + ", " + _output;
output.Text = _output;
}
else
{
output.Text = "身分證長度錯誤";
return;
}
}
}
}
```
執行畫面:

# 20240318 自我練習
## 一 數字三角形
程式碼:`// See https://aka.ms/new-console-template for more information
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("資訊一乙 19 康軒勻");
int N = int.Parse(Console.ReadLine());
if (N % 2 == 1) // 奇數
{;
for (int i = N; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
}
else // 偶數
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}`
執行畫面:


## 二 5進制轉換
程式碼: `// See https://aka.ms/new-console-template for more information
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("資訊一乙 19 康軒勻");
int decimalNumber = int.Parse(Console.ReadLine());
string quinaryNumber = ConvertToQuinary(decimalNumber);
Console.WriteLine($"{quinaryNumber}");
}
static string ConvertToQuinary(int decimalNumber)
{
if (decimalNumber == 0)
{
return "0";
}
string result = "";
while (decimalNumber > 0)
{
int remainder = decimalNumber % 5;
result = remainder.ToString() + result;
decimalNumber /= 5;
}
return result;
}
}
`
執行畫面:

## 三 計算字元出現頻率
程式碼:
```
// See https://aka.ms/new-console-template for more information
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
Console.Write("資訊一乙 19 康軒勻");
string input = Console.ReadLine();
SortedDictionary<char, int> charFrequency = new SortedDictionary<char, int>();
for (char c = '0'; c <= '9'; c++)
{
charFrequency[c] = 0;
}
for (char c = 'A'; c <= 'Z'; c++)
{
charFrequency[c] = 0;
}
for (char c = 'a'; c <= 'z'; c++)
{
charFrequency[c] = 0;
}
// 計算字元頻率
foreach (char c in input)
{
if (charFrequency.ContainsKey(c))
{
charFrequency[c]++;
}
}
foreach (KeyValuePair<char, int> pair in charFrequency)
{
if (pair.Value > 0)
{
Console.WriteLine($"'{pair.Key}': {pair.Value}");
}
}
}
}
```
執行畫面:

## 四 累加
程式碼:
```
// See https://aka.ms/new-console-template for more information
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("資訊一乙 19 康軒勻");
int N = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= N; i++)
{
sum += i;
}
Console.WriteLine($"{sum}");
}
}
```
執行畫面:

## 五 考慮如下圖的演算法,並將數列印出
程式碼:
```
// See https://aka.ms/new-console-template for more information
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("資訊一乙 19 康軒勻");
int n = int.Parse(Console.ReadLine());
while (true)
{
Console.WriteLine(n);
if (n == 1)
break;
if (n % 2 == 1)
n = 3 * n + 1;
else
n = n / 2;
}
}
}
```
執行畫面:

# 20240318 加分題
## 一.質數判斷
程式碼:
```
// See https://aka.ms/new-console-template for more information
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("資訊一乙 19 康軒勻");
int number = int.Parse(Console.ReadLine());
if (IsPrime(number))
{
Console.WriteLine($"{number} 是質數。");
}
else
{
Console.WriteLine($"{number} 不是質數。");
}
}
static bool IsPrime(int number)
{
if (number <= 1)
{
return false;
}
if (number == 2)
{
return true;
}
if (number % 2 == 0)
{
return false;
}
for (int i = 3; i <= Math.Sqrt(number); i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
}
```
執行畫面:

## 二 BMI程式
程式碼:`using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("資訊一乙 19 康軒勻");
Console.WriteLine("身高:");
double height = double.Parse(Console.ReadLine()) / 100;
Console.WriteLine("體重:");
double weight = double.Parse(Console.ReadLine());
double bmi = CalculateBMI(height, weight);
Console.WriteLine($"BMI值為:{bmi:F2}");
string obesityDefinition = GetObesityDefinition(bmi);
Console.WriteLine($"{obesityDefinition}");
}
static double CalculateBMI(double height, double weight)
{
return weight / (height * height);
}
static string GetObesityDefinition(double bmi)
{
if (bmi < 18.5)
{
return "體重過輕";
}
else if (bmi < 24)
{
return "正常範圍";
}
else if (bmi < 27)
{
return "過重";
}
else if (bmi < 30)
{
return "輕度肥胖";
}
else if (bmi < 35)
{
return "中度肥胖";
}
else
{
return "重度肥胖";
}
}
}`
執行畫面:

## 三 猜數字遊戲
程式碼:`// See https://aka.ms/new-console-template for more information
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("資訊一乙 19 康軒勻");
Random random = new Random();
int targetNumber = random.Next(1, 101);
int guessCount = 0;
Console.WriteLine("猜猜看我心中所想的數字是多少(1到100之間),輸入0退出遊戲:");
while (true)
{
int guess = int.Parse(Console.ReadLine());
guessCount++;
if (guess == 0)
{
Console.WriteLine("遊戲已退出。");
break;
}
else if (guess < targetNumber)
{
Console.WriteLine("太小了,再大一點!");
}
else if (guess > targetNumber)
{
Console.WriteLine("太大了,再小一點!");
}
else
{
Console.WriteLine($"恭喜你猜中了!答案是 {targetNumber},共猜了 {guessCount} 次。");
break;
}
}
}
}
`
執行畫面:

## 延伸練習- ID Generator
程式碼:
```
using Microsoft.VisualBasic;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
string idChar = "abcdefghjklmnpqrstuvxywzio";
string[] idCry ={"台北市","台中市","基隆市","台南市","高雄市","新北市","宜蘭縣","桃園縣",
"新竹縣","苗栗縣","台中市","南投縣","彰化縣","雲林縣","嘉義縣","台南市","高雄市",
"屏東縣","花蓮縣","台東縣","澎湖縣","陽明山","金門縣","連江縣","嘉義市","新竹市"};
string msg, idBorn;
int num, idPos, sex;
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void go_Click(object sender, EventArgs e)
{
output.Text = "";
if (amount.Text.Length == 0)
{
amount.Text = "1";
}
for (int j = 0; j < Convert.ToInt16(amount.Text); j++)
{
string id;
int temp;
int chack_number = 0;
temp = city_chose.CheckedIndices[0] - 1 == -1 ? rand.Next(26) : city_chose.CheckedIndices[0] - 1;
chack_number += ((10 + temp) / 10);
chack_number += ((10 + temp) % 10) * 9;
id = idChar.Substring(temp, 1).ToUpper();
if (Boy.Checked)
temp = 1;
else if (Girl.Checked)
temp = 2;
else
temp = rand.Next(1, 3);
id += (Convert.ToString(temp));
chack_number += temp * 8;
for (int i = 0; i < 7; i++)
{
temp = rand.Next(10);
id += Convert.ToString(temp);
chack_number += (7 - i) * temp;
}
id += Convert.ToString((10 - (chack_number % 10)) % 10);
output.Text += id + '\n';
}
output.Text = output.Text.Replace("\n", Environment.NewLine);
}
private void amount_TextChanged(object sender, EventArgs e)
{
for (int i = amount.Text.Length - 1; i >= 0; i--)
{
if (amount.Text[i] > 57 || amount.Text[i] < 48)
{
amount.Text = amount.Text.Remove(i, 1);
amount.Select(amount.Text.Length, 0);
}
}
}
private void city_chose_SelectedIndexChanged(object sender, EventArgs e)
{
if (city_chose.CheckedItems.Count > 1)
{
for (int i = 0; i < city_chose.Items.Count; i++)
{
if (i != city_chose.SelectedIndex)
{
city_chose.SetItemChecked(city_chose.CheckedIndices[i], false);
}
}
}
}
private void save_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "txt|*.txt";
saveFileDialog1.FileName = msg;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter sr = new StreamWriter(saveFileDialog1.FileName);
sr.Write(output.Text);
sr.Close();
}
}
}
}
```
執行畫面:

### 20240401心得(100字)
在這次的課程中,我們主要在學習如何製作ID Generator的程式碼,我認為這並非一個簡單的作業,雖然老師已給我們程式碼,但還是有許多小錯誤,例如:沒宣告到等,好在最後有經過同學的幫助,才順利解決這項麻煩的作業.
# CH5 陣列和字串
## 自我練習
### IndexOf (Char, Int32, Int32)
程式碼:
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] stu = new string[] { "趙一", "林二", "張三", "李四", "王五" };
int[] score = new int[] { 95, 100, 100, 92, 100 };
string msg = "一百分學生:";
int index = Array.IndexOf(score, 100);
while (index >= 0)
{
msg += stu[index] + ",";
index = Array.IndexOf(score, 100, index + 1);
};
MessageBox.Show(msg);
}
}
}
```
執行畫面:

### 評分紀錄
程式碼:
```
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 0;
string[] tea = new string[] { "東哥", "拉拉", "迪西", "小波", "努努" };
int[] score = new int[5];
private void button1_Click(object sender, EventArgs e)
{
int temp;
if (int.TryParse(textBox1.Text, out temp))
{
if (temp >= 0 && temp <= 100)
{
score[n] = temp;
label2.Text = "";
for (int i = 0; i <= n; i++)
label2.Text += $"{tea[i]}老師給分:{score[i]}\n";
n++;
if (n == 5)
{
double sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
}
label2.Text += "=================\n";
label2.Text += $"平均分數:{(sum / 5)}\n";
}
else
{
label1.Text = $"{tea[n]}老師給分:";
textBox1.Text = "";
textBox1.Focus();
}
}
else
{
MessageBox.Show("數值不合理!");
textBox1.Text = "";
textBox1.Focus();
}
}
else
{
MessageBox.Show("請輸入整數!");
textBox1.Focus();
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = tea[n] + "老師給分:";
label2.Text = "";
}
}
}
```
執行畫面:
### 歌曲排行榜
程式碼:
```
namespace WinFormsApp3
{
public partial class Form1 : Form
{
string[] song = new string[] { "不為誰而作的歌", "餘波盪漾", "後來的我們", "不該", "年輪說", "滿座", "關鍵詞", "天真有邪", "獨善其身", "一次幸福的機會" };
string[] singer = new string[] { "林俊傑", "田馥甄", "五月天", "周杰倫", "楊丞琳", "李榮浩", "林俊傑", "林宥嘉", "田馥甄", "蕭敬騰" };
int[] no = new int[10];
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < no.Length; i++)
{
no[i] = i + 1;
}
button1_Click_1(sender, e);
}
private void button1_Click_1(object sender, EventArgs e)
{
int[] no2 = new int[no.Length];
no.CopyTo(no2, 0);
Array.Sort(no, song);
Array.Sort(no2, singer);
string msg = "排名\t歌手\t歌曲" + Environment.NewLine;
for (int i = 0; i < song.Length; i++)
{
msg += $"{no[i]}\t{singer[i]}\t{song[i]}" + Environment.NewLine;
}
textBox1.Text = msg;
}
private void button2_Click_1(object sender, EventArgs e)
{
string[] song2 = new string[song.Length];
song.CopyTo(song2, 0);
Array.Sort(song, singer);
Array.Sort(song2, no);
string msg = "排名\t歌手\t歌曲" + Environment.NewLine;
for (int i = 0; i < song.Length; i++)
{
msg += $"{no[i]}\t{singer[i]}\t{song[i]}" + Environment.NewLine;
}
textBox1.Text = msg;
}
private void button3_Click_1(object sender, EventArgs e)
{
{
string search = textBox2.Text;
string msg = $"找不到{search}";
List<int> indices = new List<int>();
int index = Array.IndexOf(singer, search);
if (index >= 0)
{
msg = "排名\t歌手\t歌曲" + Environment.NewLine;
while (index >= 0)
{
msg += $"{no[index]}\t{singer[index]}\t{song[index]}";
msg += Environment.NewLine;
index = Array.IndexOf(singer, search, index + 1);
}
}
textBox1.Text = msg;
}
}
}
}
```
執行畫面:




### 20240408 心得
在經過半個學期學習C#後,我們已經有寫這項程式的介面設計的的基本能力,其實大致邏輯就跟上學期的C++類似,不同的是,介面設計有許多細節需要調整,光是物件就有許多內容需要了解,然而,有物件寫出來的內容就是比沒物間寫出來的有成就感許多.
### 20240429 心得
這次要做的幾個程式碼中,我覺得歌手查詢最難最複雜,他的物件並不難拉,是最基本那幾個,但是程式內容又繁瑣又常,尤其是歌曲找不到那邊,需要思考一下才知道為什麼是這樣,結論,程式最重要的還是邏輯思考與數學推理等.