- /*
- * This is My First C# WinForm Program.
- * I was doing a problem on poj.org.
- * There is a problem occured me to write this program to help me solve that problem.
- *
- * Author: iStack
- *
- * Email:honi.linux@gmail.com
- *
- */
- 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 BinDecExchanger
- {
- public partial class Form1 : Form
- {
- // public Val
- int MAX_DEC_ARR_SIZE = 1000;
- int MAX_BIN_ARR_SIZE = 4000;
- int zero_2_one = 1;
- int zero_2_nine = 2;
- string[] str_digital;
- // function
- public Form1()
- {
- InitializeComponent();
- str_digital = new string[10];
- str_digital[0] = "0";
- str_digital[1] = "1";
- str_digital[2] = "2";
- str_digital[3] = "3";
- str_digital[4] = "4";
- str_digital[5] = "5";
- str_digital[6] = "6";
- str_digital[7] = "7";
- str_digital[8] = "8";
- str_digital[9] = "9";
- }
- //---- Bin --> Dec : by string ----//
- private string Bin2DecByString(string str_bin)
- {
- int[] n_dec_value;
- int top_index = 0;
- n_dec_value = new int[MAX_DEC_ARR_SIZE];
- for (int i = 0; i < MAX_DEC_ARR_SIZE; i++)
- {
- n_dec_value[i] = 0;
- }
- //---- str_bin --> n_dec_value[];
- for (int str_index = 0; str_index < str_bin.Length; str_index++)
- {
- //---- n_dec_value = n_dec_value * 2 + (str_index - '0')
- for (int j = 0; j <= top_index; j++)
- {
- n_dec_value[j] *= 2;
- }
- n_dec_value[0] += (str_bin[str_index] - '0');
- //---- Manage It.
- top_index += 5;
- if (top_index >= MAX_DEC_ARR_SIZE)
- {
- MessageBox.Show("Bin Size Oveflow!");
- Application.Exit();
- }
- for (int i = 0; i < top_index; i++)
- {
- n_dec_value[i + 1] += (n_dec_value[i] / 10);
- n_dec_value[i] %= 10;
- }
- while (0 == n_dec_value[top_index] && top_index > 0)
- {
- top_index--;
- }
- }
- //---- n_dec_value --> str_dec;
- System.Text.StringBuilder str_dec = new System.Text.StringBuilder();
- for (int i = top_index; i >= 0; i--)
- {
- str_dec.Append(str_digital[n_dec_value[i]]);
- }
- return Convert.ToString(str_dec);
- }
- //---- Dec --> Bin : by string ----//
- private string Dec2BinByString(string str_dec)
- {
- int[] n_bin_value;
- int[] n_dec_value;
- int bin_index;
- int dec_index;
- //---- Init
- n_bin_value = new int[MAX_BIN_ARR_SIZE];
- for (int i = 0; i < MAX_BIN_ARR_SIZE; i++)
- {
- n_bin_value[i] = 0;
- }
- n_dec_value = new int[MAX_DEC_ARR_SIZE];
- for (int i = 0; i < MAX_DEC_ARR_SIZE; i++)
- {
- n_dec_value[i] = 0;
- }
- //---- Transform
- //--- copy : str_dec --> n_dec_value[]
- dec_index = str_dec.Length - 1;
- for (int i = 0; i < str_dec.Length; i++)
- {
- n_dec_value[str_dec.Length - i - 1] = (str_dec[i] - '0');
- }
- bin_index = -1;
- while (!(dec_index == 0 && n_dec_value[dec_index] == 0))
- {
- bin_index++;
- n_bin_value[bin_index] = n_dec_value[0] % 2;
- for (int i = dec_index; i >= 1; i--)
- {
- n_dec_value[i - 1] += (10 * (n_dec_value[i] % 2));
- n_dec_value[i] /= 2;
- }
- n_dec_value[0] /= 2;
- while (0 == n_dec_value[dec_index] && dec_index > 0)
- {
- dec_index--;
- }
- }
- System.Text.StringBuilder str_bin;
- str_bin = new System.Text.StringBuilder();
- for (int i = bin_index; i >= 0; i--)
- {
- str_bin.Append(str_digital[n_bin_value[i]]);
- }
- return Convert.ToString(str_bin);
- }
- //---- Judge Function.----//
- private bool JudgeString(string str, int judge_type)
- {
- //---- set
- char ch_stop;
- if (zero_2_one == judge_type)
- {
- ch_stop = '1';
- }
- else if (zero_2_nine == judge_type)
- {
- ch_stop = '9';
- }
- else
- {
- MessageBox.Show("Wrong Judge Type!");
- ch_stop = '0';
- Application.Exit();
- }
- bool bFlag = true;
- for (int index = 0; bFlag && index < str.Length; index++)
- {
- if (!(str[index] >= '0' && str[index] <= ch_stop))
- {
- bFlag = false;
- }
- }
- return bFlag;
- }
- private bool IsBinString(string str_bin)
- {
- int judge_type = zero_2_one ;
- return JudgeString(str_bin, judge_type);
- }
- private bool IsDecString(string str_dec)
- {
- int judge_type = zero_2_nine;
- return JudgeString(str_dec, judge_type);
- }
- //------- When TextBox1Text Changed..
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- string str_bin;
- str_bin = textBox1.Text;
- if (IsBinString(str_bin))
- {
- textBox2.Text = Bin2DecByString(str_bin);
- }
- else
- {
- MessageBox.Show("\\"" + str_bin + "\\"" + " is not Bin String!");
- }
- }
- //------- When TextBox2Text Changed..
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- string str_dec;
- str_dec = textBox2.Text;
- if (str_dec.Length <= 0)
- {
- }
- else if (IsDecString(str_dec))
- {
- textBox1.Text = Dec2BinByString(str_dec);
- }
- else
- {
- MessageBox.Show("\\"" + str_dec + "\\"" + " is not Dec String!");
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2808201513567.html
来源: http://www.codesnippet.cn/detail/2808201513567.html