zoukankan
html css js c++ java
不限位数的十进制正整数类,可进行加和乘操作
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
TestFactorial
{
/**/
///
<summary>
///
10机制正整数类
///
</summary>
public
class
DecimalNumber
{
List
<
byte
>
_Data;
public
List
<
byte
>
Data
{
get
{
return
_Data;
}
}
public
int
Length
{
get
{
return
_Data.Count;
}
}
public
DecimalNumber()
{
_Data
=
new
List
<
byte
>
();
}
public
DecimalNumber(
byte
[] data)
{
foreach
(
byte
b
in
data)
{
System.Diagnostics.Debug.Assert(b
>=
0
&&
b
<=
9
);
}
_Data
=
new
List
<
byte
>
(data);
}
public
DecimalNumber(List
<
byte
>
data)
{
foreach
(
byte
b
in
data)
{
System.Diagnostics.Debug.Assert(b
>=
0
&&
b
<=
9
);
}
_Data
=
data;
}
/**/
///
<summary>
///
1位10机制数和10进制序列相乘
///
///
</summary>
///
<param name="s">
10进制序列
</param>
///
<param name="d">
1位10机制数
</param>
///
<param name="power">
10的幂数
</param>
///
<returns></returns>
private
static
List
<
byte
>
Multiply(List
<
byte
>
s,
byte
d,
int
power)
{
System.Diagnostics.Debug.Assert(power
>=
0
);
System.Diagnostics.Debug.Assert(d
>=
0
&&
d
<=
9
);
List
<
byte
>
result
=
new
List
<
byte
>
();
for
(
int
i
=
0
; i
<
power; i
++
)
{
result.Add(
0
);
}
byte
carry
=
0
;
//
进位
foreach
(
byte
si
in
s)
{
System.Diagnostics.Debug.Assert(si
>=
0
&&
si
<=
9
);
byte
r
=
(
byte
)(si
*
d
+
carry);
byte
m
=
(
byte
)(r
%
10
);
carry
=
(
byte
)(r
/
10
);
result.Add(m);
}
if
(carry
>
0
)
{
result.Add(carry);
}
return
result;
}
/**/
///
<summary>
///
两个10进制序列相加
///
</summary>
///
<param name="s1">
序列1
</param>
///
<param name="s2">
序列2
</param>
///
<returns>
相加后的序列
</returns>
private
static
List
<
byte
>
Plus(List
<
byte
>
s1, List
<
byte
>
s2)
{
List
<
byte
>
result
=
new
List
<
byte
>
();
int
c1
=
s1.Count;
int
c2
=
s2.Count;
if
(c1
>
c2)
{
for
(
int
i
=
0
; i
<
c1
-
c2; i
++
)
{
s2.Add(
0
);
}
}
else
if
(c1
<
c2)
{
for
(
int
i
=
0
; i
<
c2
-
c1; i
++
)
{
s1.Add(
0
);
}
}
byte
carry
=
0
;
//
进位
for
(
int
i
=
0
; i
<
s1.Count; i
++
)
{
System.Diagnostics.Debug.Assert(s1[i]
>=
0
&&
s1[i]
<=
9
);
System.Diagnostics.Debug.Assert(s2[i]
>=
0
&&
s2[i]
<=
9
);
byte
r
=
(
byte
)(s1[i]
+
s2[i]
+
carry);
byte
m
=
(
byte
)(r
%
10
);
carry
=
(
byte
)(r
/
10
);
result.Add(m);
}
if
(carry
>
0
)
{
result.Add(carry);
}
return
result;
}
public
static
implicit
operator
DecimalNumber(
string
value)
{
List
<
byte
>
data
=
new
List
<
byte
>
();
for
(
int
i
=
value.Length
-
1
; i
>=
0
; i
--
)
{
data.Add(
byte
.Parse(value[i].ToString()));
}
return
new
DecimalNumber(data);
}
public
static
implicit
operator
DecimalNumber(
int
value)
{
System.Diagnostics.Debug.Assert(value
>=
0
);
return
value.ToString();
}
public
static
DecimalNumber
operator
++
(DecimalNumber d)
{
return
d
+
new
DecimalNumber(
new
byte
[]
{
1
}
);
}
public
static
DecimalNumber
operator
+
(DecimalNumber d1,
int
d2)
{
System.Diagnostics.Debug.Assert(d2
>=
0
);
return
d1
+
d2.ToString();
}
public
static
DecimalNumber
operator
+
(DecimalNumber d1, DecimalNumber d2)
{
return
new
DecimalNumber(Plus(d1.Data, d2.Data));
}
public
static
DecimalNumber
operator
*
(DecimalNumber d1, DecimalNumber d2)
{
List
<
List
<
byte
>>
multiplicationSerial
=
new
List
<
List
<
byte
>>
();
for
(
int
i
=
0
; i
<
d1.Data.Count; i
++
)
{
multiplicationSerial.Add(Multiply(d2.Data, d1.Data[i], i));
}
List
<
byte
>
result
=
new
List
<
byte
>
();
foreach
(List
<
byte
>
s
in
multiplicationSerial)
{
result
=
Plus(s, result);
}
return
new
DecimalNumber(result);
}
public
override
string
ToString()
{
StringBuilder str
=
new
StringBuilder();
for
(
int
i
=
_Data.Count
-
1
; i
>=
0
; i
--
)
{
str.Append(_Data[i].ToString());
}
return
str.ToString();
}
}
class
Program
{
static
void
Main(
string
[] args)
{
int
d
=
1
;
DecimalNumber factorial
=
1
;
while
(factorial.Length
<
3
)
{
d
++
;
factorial
=
factorial
*
d;
//
Console.WriteLine(factorial);
//
Console.WriteLine(d);
}
Console.WriteLine(d);
}
}
}
查看全文
相关阅读:
SQL SERVER全面优化-------写出好语句是习惯
SQL SERVER全面优化-------索引有多重要?
Expert 诊断优化系列------------------冤枉磁盘了
SQL语句调优三板斧
SQL Server死锁产生原因及解决办法 .
探讨SQL Server并发处理存在就更新七种解决方案
Entity Framework查询,EF执行SQl
在Asp.Net中操作PDF – iTextSharp
postman发送json格式的post请求
通过配置web.config使WCF向外提供HTTPS的Restful Service
原文地址:https://www.cnblogs.com/eaglet/p/1352199.html
最新文章
linux每日命令(11):cat命令
linux每日命令(10):touch命令
linux每日命令(9):cp命令
linux每日命令(8):mv命令
linux每日命令(7):rmdir命令
MySQL排序原理与MySQL5.6案例分析【转】
MySQL Binlog Mixed模式记录成Row格式
MySQL进程常见的State【转】
MySQL binlog 组提交与 XA(分布式事务、两阶段提交)【转】
MySQL5.6 PERFORMANCE_SCHEMA 说明
热门文章
MySQL 5.6 my.cnf 模版
pt-query-digest使用介绍【转】
初识 MySQL 5.6 新功能、参数
MySQL 5.6 Threadpool(优先队列)介绍及性能测试【转】
MySQL SQL优化之in与range查询【转】
C#中Post请求的两种方式发送参数链和Body的
c# json 序列化如何去掉null值
C# 之泛型详解
SQL Server 创建索引的 5 种方法
复合索引的优点和注意事项
Copyright © 2011-2022 走看看