10进制转换与括号匹配算法:(如有BUG请指教,呵呵,一阵乱写)
1:将十进制转换成其他进制,包括2进制,8进制,16进制。
2:括号匹配算法,相应的括号进行匹配。
进制转换算法与字符匹配算法
1 using System.Collections.Generic;
2 using System.Text;
3
4 public partial class DataStruct_ExchangeData : System.Web.UI.Page
5 {
6 protected void Page_Load(object sender, EventArgs e)
7 {
8
9 // ConvertIntToOther();
10 // ConvertionInt(654321,8);
11 MatchBreak("([])([][()])");
12 }
13 /// <summary>
14 /// compare the match of the string
15 /// </summary>
16 /// <param name="input">input string to compare</param>
17 private void MatchBreak(string input)
18 {
19 char[] charnum = input.ToCharArray();
20 Stack<string> stack = new Stack<string>();
21 foreach(char ch in charnum){
22 if(stack.Count==0){//数字为0的时候 压入
23 stack.Push(ch.ToString());
24 }
25 else
26 {
27 if (stack.Peek() == "(" && ch.ToString() == ")" || stack.Peek() == "[" && ch.ToString() == "]")
28 {
29 stack.Pop();
30 }
31 else
32 {
33 stack.Push(ch.ToString());
34 }
35 }
36 }
37 if (stack.Count == 0)
38 {
39 Response.Write("ok");
40 }
41 else
42 {
43 Response.Write("false");
44 }
45 }
46 /// <summary>
47 /// FILO先进后出 10进制转换成相应的其他进制
48 /// </summary>
49 /// <param name="input">input number</param>
50 /// <param name="changeType">will be changed type</param>
51 private void ConvertionInt(int input,int changeType)
52 {
53 Stack<int> stack = new Stack<int>();
54 string str = "";
55 while(input>0){//循环压入栈
56 stack.Push(input%changeType);
57 input = input/changeType;
58 }
59 StringBuilder sb = new StringBuilder();
60 while(stack.Count>0){//循环取出数字
61 if (sb.ToString() == "")
62 {
63 sb.Append(stack.Pop().ToString());
64 }
65 else
66 {
67 sb.Append("-"+stack.Pop().ToString());
68 }
69 }
70 str = sb.ToString();
71 if(changeType==16){//16进制进行替换操作
72 str=str.Replace("10","A").Replace("11","B").Replace("12","C").Replace("13","D").Replace("14","E").Replace("15","F");
73 }
74 Response.Write(str);
75 }
76
77 private void ConvertIntToOther()
78 {
79 string num = "123456";
80 Stack<string> stack = new Stack<string>();
81 foreach(char str in num){
82 // Response.Write(str+"<br />");
83 stack.Push(str.ToString());
84 }
85 }
86 }
2 using System.Text;
3
4 public partial class DataStruct_ExchangeData : System.Web.UI.Page
5 {
6 protected void Page_Load(object sender, EventArgs e)
7 {
8
9 // ConvertIntToOther();
10 // ConvertionInt(654321,8);
11 MatchBreak("([])([][()])");
12 }
13 /// <summary>
14 /// compare the match of the string
15 /// </summary>
16 /// <param name="input">input string to compare</param>
17 private void MatchBreak(string input)
18 {
19 char[] charnum = input.ToCharArray();
20 Stack<string> stack = new Stack<string>();
21 foreach(char ch in charnum){
22 if(stack.Count==0){//数字为0的时候 压入
23 stack.Push(ch.ToString());
24 }
25 else
26 {
27 if (stack.Peek() == "(" && ch.ToString() == ")" || stack.Peek() == "[" && ch.ToString() == "]")
28 {
29 stack.Pop();
30 }
31 else
32 {
33 stack.Push(ch.ToString());
34 }
35 }
36 }
37 if (stack.Count == 0)
38 {
39 Response.Write("ok");
40 }
41 else
42 {
43 Response.Write("false");
44 }
45 }
46 /// <summary>
47 /// FILO先进后出 10进制转换成相应的其他进制
48 /// </summary>
49 /// <param name="input">input number</param>
50 /// <param name="changeType">will be changed type</param>
51 private void ConvertionInt(int input,int changeType)
52 {
53 Stack<int> stack = new Stack<int>();
54 string str = "";
55 while(input>0){//循环压入栈
56 stack.Push(input%changeType);
57 input = input/changeType;
58 }
59 StringBuilder sb = new StringBuilder();
60 while(stack.Count>0){//循环取出数字
61 if (sb.ToString() == "")
62 {
63 sb.Append(stack.Pop().ToString());
64 }
65 else
66 {
67 sb.Append("-"+stack.Pop().ToString());
68 }
69 }
70 str = sb.ToString();
71 if(changeType==16){//16进制进行替换操作
72 str=str.Replace("10","A").Replace("11","B").Replace("12","C").Replace("13","D").Replace("14","E").Replace("15","F");
73 }
74 Response.Write(str);
75 }
76
77 private void ConvertIntToOther()
78 {
79 string num = "123456";
80 Stack<string> stack = new Stack<string>();
81 foreach(char str in num){
82 // Response.Write(str+"<br />");
83 stack.Push(str.ToString());
84 }
85 }
86 }