求两个数相除的商,并计算出小数点后的循环部分。例如1/3 商为0.333333333... ,则表示为0.(3)
简单实现以下:
aspx页:
代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5"%>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <p>
13 除数:<asp:TextBox ID="txtDivisor" runat="server" onkeyup="if(event.keyCode!=37&&event.keyCode!=39)value=value.replace(/\D/g,'');"></asp:TextBox>
14 </p>
15 <p>
16 被除数:<asp:TextBox ID="txtDividend" runat="server" onkeyup="if(event.keyCode!=37&&event.keyCode!=39)value=value.replace(/\D/g,'');"></asp:TextBox>
17 </p>
18 <p>
19 小数个数:<asp:TextBox ID="txtCount" runat="server" onkeyup="if(event.keyCode!=37&&event.keyCode!=39)value=value.replace(/\D/g,'');"></asp:TextBox>
20 </p>
21 <asp:Button ID="btnResults" runat="server" Text="结果"
22 onclick="btnResults_Click"/>
23 <p>
24 <asp:Label ID="lbResults" runat="server" Text=""></asp:Label>
25 </p>
26 </div>
27 </form>
28 </body>
29 </html>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12 <p>
13 除数:<asp:TextBox ID="txtDivisor" runat="server" onkeyup="if(event.keyCode!=37&&event.keyCode!=39)value=value.replace(/\D/g,'');"></asp:TextBox>
14 </p>
15 <p>
16 被除数:<asp:TextBox ID="txtDividend" runat="server" onkeyup="if(event.keyCode!=37&&event.keyCode!=39)value=value.replace(/\D/g,'');"></asp:TextBox>
17 </p>
18 <p>
19 小数个数:<asp:TextBox ID="txtCount" runat="server" onkeyup="if(event.keyCode!=37&&event.keyCode!=39)value=value.replace(/\D/g,'');"></asp:TextBox>
20 </p>
21 <asp:Button ID="btnResults" runat="server" Text="结果"
22 onclick="btnResults_Click"/>
23 <p>
24 <asp:Label ID="lbResults" runat="server" Text=""></asp:Label>
25 </p>
26 </div>
27 </form>
28 </body>
29 </html>
aspx.cs页:
代码
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 publicpartialclass Default5 : System.Web.UI.Page
9 {
10 protectedvoid 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 protectedvoid 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 protectedstring 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 privatestring 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 privateint MaxMin(int divisor, int dividend)
94 {
95 if (divisor > dividend && divisor !=0)
96 {
97 return divisor;
98 }
99 elseif (divisor ==0)
100 {
101 return divisor;
102 }
103 else
104 {
105 return MaxMin(divisor *10, dividend);
106 }
107 }
108 //判断小数点后的循环部分
109 privatestring CheckRepeat(string oldstr)
110 {
111 int[] olditems =newint[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
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 publicpartialclass Default5 : System.Web.UI.Page
9 {
10 protectedvoid 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 protectedvoid 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 protectedstring 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 privatestring 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 privateint MaxMin(int divisor, int dividend)
94 {
95 if (divisor > dividend && divisor !=0)
96 {
97 return divisor;
98 }
99 elseif (divisor ==0)
100 {
101 return divisor;
102 }
103 else
104 {
105 return MaxMin(divisor *10, dividend);
106 }
107 }
108 //判断小数点后的循环部分
109 privatestring CheckRepeat(string oldstr)
110 {
111 int[] olditems =newint[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
我只是简单的实现了一下!没有专门测试!如果有bug,请园友发消息告诉我啊!! 呵呵..