6-練習題目
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 WindowsFormsApp18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//textBox1.Text = "ABCDEF";
//textBox2.Text = "ABy4hhC1D2E3F";
label1.Text = "";
var charA = textBox1.Text.ToArray();
var charB = textBox2.Text.ToArray();
int index = 0;
for (int i = 0; i < charB.Length; i++)
{
if (index >= charA.Length || charA[index] != charB[i])
label1.Text += charB[i];
else
index++;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox3.Text = "AAAABBCCC";
string temp,result=""; //temp 存放 textbox3.text substring 的字
int counter=1; //用來計算重覆字的次數
temp = textBox3.Text.Substring(0, 1);
for(int i=1;i<textBox3.Text.Length;i++)
{
if (temp == textBox3.Text.Substring(i, 1)) //符合前一個字,累計 重覆字的次數
{
counter++;
}
else
{
result += counter.ToString() + temp; //若不符合前一個字, 重覆字的次數 重計
counter = 1;
}
temp = textBox3.Text.Substring(i, 1);
}
result += counter.ToString() + temp;
label2.Text = result; //將壓縮字串結果顯示在 label
}
private void button3_Click(object sender, EventArgs e)
{
label3.Text = "";
// 將兩個數處理成相同長度的字符串,短的小的數字前面補0
for (int i = 0; i < (textBox4.Text.Length > textBox5.Text.Length ? textBox4.Text.Length : textBox5.Text.Length); i++)
{
if (i >= textBox4.Text.Length)
{
textBox4.Text = "0" + textBox4.Text;
}
if (i >= textBox5.Text.Length)
{
textBox5.Text = "0" + textBox5.Text;
}
}
//分割為字元陣列
var charA = textBox4.Text.ToArray();
var charB = textBox5.Text.ToArray();
int[] result = new int[textBox4.Text.Length+1];
int k=0; //暫存器
int a, b;
for(int i=textBox4.Text.Length-1;i>=0;i--)
{
a = Convert.ToInt32(charA[i].ToString());
b = Convert.ToInt32(charB[i].ToString());
result[i+1] += (a+b+k)%10;
k = (a+b+k) / 10;
//result[i] += k;
}
result[0] = k;
//數字陣列轉入 label3.text
for(int i=0;i<result.Length;i++)
{
if(i!=0 | result[0] != 0)
label3.Text += result[i].ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
/* char[] s1 = new char[5]; //
char[] s2 = new char[5];
int[] s3 = new int[100];*/
/*var charA = textBox4.Text.ToArray();
var charB = textBox5.Text.ToArray();*/
// 將兩個數處理成相同長度的字符串,短的小的數字前面補0
// textbox4 較長,則用textbox4 ,否則用 textbox5
/*
// 將兩個數處理成相同長度的字符串,短的小的數字前面補0
for (int i = 0; i < (a.Length > b.Length ? a.Length : b.Length); i++)
{
if (i >= a.Length)
one.Insert(i - a.Length, 0);
else
one.Add(int.Parse(a[i].ToString()));
if (i >= b.Length)
two.Insert(i - b.Length, 0);
else
two.Add(int.Parse(b[i].ToString()));
}
//Array.Resize(ref s, charA.Length + 1);
*/
}
}
}