码迷,mamicode.com
首页 > Windows程序 > 详细

【曾经】进制转换器C#

时间:2015-10-13 21:10:52      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace numconversion
 12 {
 13     public partial class Form1 : Form
 14     {
 15         const int n=4,m=4;
 16         string str=null;
 17         string hex, dec, oct, bin;
 18         Button[,] buttons = new Button[n, n];
 19         RadioButton[] rbs = new RadioButton[m];
 20         TextBox[] TB = new TextBox[m+1];
 21         public Form1()
 22         {
 23             InitializeComponent();
 24         }
 25 
 26         private void Form1_Load(object sender, EventArgs e)
 27         {
 28             MaximizeBox = false;
 29             LoadTB();
 30             GenerateButton();
 31             GenerateRB();
 32             rbs[0].Checked = true;
 33 
 34         }
 35         //数组引用文本框便于利用循环
 36         void LoadTB()
 37         {
 38             TB[1] = textBox1;
 39             TB[2] = textBox2;
 40             TB[3] = textBox3;
 41             TB[4] = textBox4;
 42 
 43         }
 44         //初始化radiobutton
 45         void GenerateRB()
 46         {
 47             const int h=27;
 48             int right = label1.Left,top=label1.Top-6;
 49             for(int i=0;i<m;i++)
 50             {
 51                 RadioButton rb = new RadioButton();
 52                 
 53                 rb.Text = null;
 54                 rb.Top = top+i*h;
 55                 rb.Left = right-15;
 56                 rbs[i] = rb;
 57                 this.Controls.Add(rb);
 58                 rb.Click += new EventHandler(rb_Click);
 59                 
 60             }
 61         }
 62 
 63         void rb_Click(object sender, EventArgs e)
 64         {
 65             DisenableAllBtns();
 66             switch(GetRbNum())
 67             {
 68                 case 1: EnableBtns(16); str = textBox1.Text; break;
 69                 case 2: EnableBtns(10); str = textBox2.Text; break;
 70                 case 3: EnableBtns(8); str = textBox3.Text; break;
 71                 case 4: EnableBtns(2); str = textBox4.Text; break;
 72                 
 73 
 74             }
 75             
 76 
 77         }
 78         
 79         void DisenableAllBtns()
 80         {
 81             for(int i=0;i<n;i++)
 82             {
 83                 for(int j=0;j<n;j++)
 84                 {
 85                     buttons[i, j].Enabled = false;
 86                 }
 87             }
 88         }
 89         void EnableBtns(int num)
 90         {
 91             for (int i = 0; i < n; i++)
 92             {
 93                 for (int j = 0; j < n; j++)
 94                 {
 95                     if (Convert.ToInt32(buttons[i, j].Tag)<=num)buttons[i, j].Enabled = true;
 96                 }
 97             }            
 98         }
 99         //获取选中的radiobutton的num
100         int GetRbNum()
101         {
102             for(int i=0;i<m;i++)
103             {
104                 if (rbs[i].Checked == true) return i+1;
105                 
106             }
107             return 0;
108         }
109         //初始化所有数值按钮
110         void GenerateButton()
111         {
112             int btn_wid=45,d=50;
113             int left0=textBox1.Left ,top0=btn_backspace.Top+btn_backspace.Height+10;
114             for(int i=0;i<n;i++)
115             {
116                 for (int j=0;j<n;j++)
117                 {
118                     Button btn=new Button();
119                     btn.Text=SwitchCharByNum(i*n+j).ToString();
120                     btn.Top=top0+i*d;
121                     btn.Left=left0+j*d;
122                     btn.Width=btn_wid;
123                     btn.Height=btn_wid;
124                     btn.Tag=(i*n+j+1).ToString();
125                     btn.Visible = true;
126                     btn.Click+=new EventHandler(btn_Click);
127                     buttons[i, j] = btn;
128                     this.Controls.Add(btn);
129 
130                     
131                 }
132             }
133         }
134         //通过数值来获取字符给按钮文本
135         char SwitchCharByNum(int num)
136         {
137            char a= ;
138            if(num<=9&&num>=0)a=(char)(0+num);
139            else if(num>=10&&num<=16)a=(char)(A-10+num);
140            
141            return a;
142         }
143         //从字符得到数值
144         int SwitchNumByChar(char a)
145         {
146             int n=-1;
147             if(a<=9&&a>=0)n=a-0;
148             if(a<=F&&a>=A)n=a-A+10;
149             return n;
150         }
151 
152         void btn_Click(object sender,EventArgs e)
153         {
154             Button bta=sender as Button;
155             int num = GetRbNum();
156             str = str + bta.Text;
157             TB[num].Text = str;
158             conver(TB[num].Text, SwitchSysByNum(num));
159         }
160 
161 
162 
163         private void btn_empty_Click(object sender, EventArgs e)
164         {
165             for (int i = 1; i <= m; i++) TB[i].Text = null;
166             hex = null;
167             dec = null;
168             oct = null;
169             bin = null;
170             str = null;
171         }
172 
173         private void btn_backspace_Click(object sender, EventArgs e)
174         {
175             int num = GetRbNum();
176             if (TB[num].Text != "")
177             {
178                 TB[num].Text = TB[num].Text.Remove(TB[num].Text.Length - 1, 1);
179                 str = TB[num].Text;
180                 conver(TB[num].Text, SwitchSysByNum(num));
181             }
182     
183         }
184         //s字符串 x字符串进制
185         void conver(string s,int x)
186         {            
187             int decn=ToDec(s, x);
188             dec=decn.ToString();
189             hex = DecTo(decn,16);
190             oct= DecTo(decn, 8);
191             bin = DecTo(decn, 2);
192             textBox1.Text = hex;
193             textBox2.Text = dec;
194             textBox3.Text = oct;
195             textBox4.Text = bin;
196         }
197 
198         //由c进制转为10进制
199         int ToDec(string b,int c)
200         {
201             int num=0;
202             for (int i = 0; i < b.Length; i++)
203             {
204                 num = num + SwitchNumByChar(b[i]) *(int) Math.Pow(c, b.Length - i-1);
205             }
206             
207             return num;
208         }
209         //由10进制转为c进制
210         string DecTo(int b,int c)
211         {
212             string a = null;
213             int d=-1;
214             do
215             {
216                
217                 d=b%c;
218                 b = b / c;
219                 a=SwitchCharByNum(d).ToString()+a;
220             }while(b>=1);
221              return a;
222         }
223         //由radiobutton获取当前进制
224         int SwitchSysByNum(int x)
225         {
226             switch(x)
227             {
228                 case 1: return 16;
229                 case 2: return 10;
230                 case 3: return 8;
231                 case 4: return 2;
232             }
233             return 1;
234         }
235 
236         private void pictureBox1_Click(object sender, EventArgs e)
237         {
238             Form2 form = new Form2();
239             form.ShowDialog();
240         }
241 
242 
243 
244     }
245 }
View Code

 

百度云下载

 

【曾经】进制转换器C#

标签:

原文地址:http://www.cnblogs.com/tyks/p/4875745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!