#include
<
iostream
>
#include
<
string
>
#include
<
algorithm
>
using
namespace
std;
int
str_compare(
string
a,
string
b)
{
int
rtn;
int
aLen
=
a.length();
int
bLen
=
b.length();
if
(aLen
>
bLen)
{
rtn
=
1
;
}
else
if
(aLen
<
bLen)
{
rtn
=
-
1
;
}
else
{
if
(a
==
b)
{
rtn
=
0
;
}
else
if
(a
>
b)
{
rtn
=
1
;
}
else
if
(a
<
b)
{
rtn
=
-
1
;
}
}
return
rtn;
}
string
str_add(
string
a,
string
b)
{
string
c, c1;
int
p, q;
int
i;
int
tmp;
bool
flag;
if
(a.length()
<
b.length())
{
swap(a, b);
}
p
=
a.length()
-
1
;
q
=
b.length()
-
1
;
flag
=
false
;
while
(q
>=
0
)
{
tmp
=
a[p
--
]
+
b[q
--
]
-
96
;
if
(flag)
{
tmp
++
;
}
flag
=
false
;
if
(tmp
>=
10
)
{
tmp
-=
10
;
flag
=
true
;
}
c
+=
char
(tmp
+
48
);
}
while
(p
>=
0
)
{
tmp
=
a[p
--
]
-
48
;
if
(flag)
{
tmp
++
;
}
flag
=
false
;
if
(tmp
>=
10
)
{
tmp
-=
10
;
flag
=
true
;
}
c
+=
char
(tmp
+
48
);
}
if
(flag)
{
c
+=
'
1
'
;
}
for
(i
=
c.length()
-
1
; i
>=
0
; i
--
)
{
c1
+=
c[i];
}
return
c1;
}
//
a > b
string
str_minus(
string
a,
string
b)
{
if
(str_compare(a,b)
==
-
1
)
{
return
"
-1
"
;
}
string
c, c1;
int
i;
int
p
=
a.length()
-
1
;
int
q
=
b.length()
-
1
;
while
(q
>=
0
)
{
if
(a[p]
<
b[q])
{
a[p
-
1
]
--
;
c
+=
char
(a[p]
-
b[q]
+
58
);
}
else
{
c
+=
char
(a[p]
-
b[q]
+
48
);
}
p
--
;
q
--
;
}
while
(p
>=
0
)
{
if
(a[p]
<
'
0
'
)
{
a[p
-
1
]
--
;
c
+=
char
(a[p]
+
10
);
}
else
{
c
+=
a[p];
}
p
--
;
}
i
=
c.length()
-
1
;
while
(c[i]
==
'
0
'
)
{
i
--
;
}
for
( ; i
>=
0
; i
--
)
{
c1
+=
c[i];
}
if
(c1.length()
==
0
)
{
c1
+=
'
0
'
;
}
return
c1;
}
string
str_multiply(
string
a,
string
b)
{
if
(str_compare(a, b)
==
-
1
)
{
swap(a, b);
}
string
c, c1;
string
bb
=
b, aa
=
a;
int
i, j, k, bit;
int
p;
int
tmp;
p
=
bb.length()
-
1
;
for
(i
=
0
; i
<
b.length(); i
++
)
{
b[i]
=
bb[p
--
];
}
p
=
aa.length()
-
1
;
for
(i
=
0
; i
<
a.length(); i
++
)
{
a[i]
=
aa[p
--
];
}
for
(i
=
0
; i
<
b.length(); i
++
)
{
bit
=
0
;
for
(j
=
0
; j
<
a.length(); j
++
)
{
if
(i
+
j
>=
c.length())
{
tmp
=
(b[i]
-
48
)
*
(a[j]
-
48
)
+
bit;
}
else
{
tmp
=
(b[i]
-
48
)
*
(a[j]
-
48
)
+
bit
+
(c[i
+
j]
-
48
);
}
bit
=
tmp
/
10
;
tmp
=
tmp
%
10
;
if
(i
+
j
>=
c.length())
{
c
+=
char
(tmp
+
48
);
}
else
{
c[i
+
j]
=
char
(tmp
+
48
);
}
}
if
(bit
>
0
)
{
k
=
i
+
j;
if
(k
>=
c.length())
{
c
+=
char
(bit
+
48
);
}
else
{
while
(bit
!=
0
)
{
tmp
=
bit
+
(c[k]
-
48
);
bit
=
tmp
/
10
;
tmp
=
tmp
%
10
;
if
(k
>=
c.length())
{
c
+=
char
(tmp
+
48
);
}
else
{
c[k
++
]
=
char
(tmp
+
48
);
}
}
}
}
}
for
(i
=
c.length()
-
1
; i
>=
0
; i
--
)
{
c1
+=
c[i];
}
return
c1;
}
//
a < b 精确到100位
string
str_divide(
string
a,
string
b)
{
string
c;
string
y, tmp, tmp3;
int
i;
int
flag;
int
last;
if
(str_compare(a, b)
==
1
)
{
swap(a, b);
}
a
+=
'
0
'
;
for
(i
=
0
; i
<
100
; i
++
)
{
if
(str_compare(a, b)
==
-
1
)
{
a
+=
'
0
'
;
c
+=
'
0
'
;
}
else
{
c
+=
'
1
'
;
last
=
c.length()
-
1
;
tmp
=
b;
tmp3
=
str_minus(a, tmp);
while
(str_compare(tmp3, b)
!=
-
1
)
{
tmp
=
str_add(tmp, b);
c[last]
++
;
tmp3
=
str_minus(a, tmp);
}
if
(str_compare(tmp3,
"
0
"
)
==
0
)
{
break
;
}
a
=
tmp3;
a
+=
'
0
'
;
}
}
return
c;
}
posted on 2006-05-07 16:13
豪 阅读(927)
评论(0) 编辑 收藏 引用 所属分类:
数据结构与算法