1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class Default5 : System.Web.UI.Page
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12 if (IsPostBack)
13 {
14 txtDivisor.Text = txtDivisor.Text;
15 txtDividend.Text = txtDividend.Text;
16 txtCount.Text = txtCount.Text;
17 }
18 }
19 protected void btnResults_Click(object sender, EventArgs e)
20 {
21 if (txtDivisor.Text.Trim().Length < 1)
22 {
23 return;
24 }
25 if (txtDividend.Text.Trim().Length < 1)
26 {
27 return;
28 }
29 if (txtCount.Text.Trim().Length < 1)
30 {
31 return;
32 }
33
34 int _divisor = Convert.ToInt32(txtDivisor.Text);
35 int _dividend = Convert.ToInt32(txtDividend.Text);
36 int _count = Convert.ToInt32(txtCount.Text);
37 if (_dividend == 0)
38 {
39 return;
40 }
41 lbResults.Text = DivResult(_divisor, _dividend, _count);
42
43 }
44 //运算主体
45 protected string DivResult(int divisor, int dividend, int count)
46 {
47 string _divresult = "";
48 int tempdivisor = 0;
49 for (int i = 0; i <= count; i++)
50 {
51 if (i == 0)
52 {
53 _divresult = (divisor / dividend).ToString() + ".";
54 tempdivisor = divisor % dividend;
55 }
56 else
57 {
58 int _tempdivisor = MaxMin(tempdivisor, dividend);
59 if (_tempdivisor != 0)
60 {
61 _divresult = _divresult + ReturnZero(_tempdivisor, tempdivisor) + (_tempdivisor / dividend).ToString();
62 tempdivisor = _tempdivisor % dividend;
63 }
64 else
65 {
66 _divresult = _divresult + '0';
67 }
68 }
69 }
70 string[] temp = _divresult.Split('.');
71 string temp1;
72 temp1= temp[1] = temp[1].Length < count ? temp[1] : temp[1].Substring(0, count);
73 string _repeatstr = CheckRepeat(temp[1]);
74 if (_repeatstr.Length > 0)
75 {
76 temp[1] = temp[1].Remove(temp[1].IndexOf(_repeatstr)) + "(" + _repeatstr + ")";
77 }
78
79 return temp[0] + "." + temp[1] + "<br/>" + temp[0] + "." + temp1;
80 }
81 //判断返回的零
82 private string ReturnZero(int _tempdivisor, int tempdivisor)
83 {
84 int n = (int)Math.Log10(_tempdivisor / tempdivisor);
85 string ret = "";
86 for (int i = 1; i < n; i++)
87 {
88 ret += '0';
89 }
90 return ret;
91 }
92 //判断除数与被除数的大小
93 private int MaxMin(int divisor, int dividend)
94 {
95 if (divisor > dividend && divisor != 0)
96 {
97 return divisor;
98 }
99 else if (divisor == 0)
100 {
101 return divisor;
102 }
103 else
104 {
105 return MaxMin(divisor * 10, dividend);
106 }
107 }
108 //判断小数点后的循环部分
109 private string CheckRepeat(string oldstr)
110 {
111 int[] olditems = new int[oldstr.Length];
112 for (int i = 0; i < oldstr.Length; i++)
113 {
114 olditems[i] = Convert.ToInt32(oldstr.Substring(i, 1));
115 }
116
117 int n = 1; //每次比较的次数
118 int equalcount = 0;//每次比较相等的次数
119 for (int i = olditems.Length - 1; i > olditems.Length / 2; i--)
120 {
121 equalcount = 0;
122 for (int j = 0; j < n; j++)
123 {
124 if (olditems[olditems.Length - 1 - j] != olditems[olditems.Length - 1 - j - n])
125 {
126 break;
127 }
128 equalcount++;
129 }
130 if (n == equalcount)
131 {
132 break;
133 }
134 n++;
135 }
136 string ret = "";
137 for (int i = equalcount - 1; i >= 0; i--)
138 {
139 ret += olditems[olditems.Length - 1 - i].ToString();
140 }
141 return ret;
142 }
143 }
144