int
Dec(
char
*
a,
char
*
b,
char
*
ans)
{
int
aLen,bLen;
int
aLast,bLast,ansLast;
aLen
=
strlen(a);
aLast
=
aLen
-
1
;
bLen
=
strlen(b);
bLast
=
bLen
-
1
;
ansLast
=
aLast;
ans[ansLast
+
1
]
=
'
\0
'
;
while
(bLast
>=
0
)
{
if
(a[aLast]
<
b[bLast])
{
a[aLast
-
1
]
--
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
-
int
(b[bLast
--
])
+
58
);
}
else
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
-
int
(b[bLast
--
])
+
48
);
}
while
(aLast
>=
0
)
{
if
(a[aLast]
<
'
0
'
)
{
a[aLast
-
1
]
--
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
+
10
);
}
else
ans[ansLast
--
]
=
a[aLast
--
];
}
return
0
;
}
int
Add(
char
*
a,
char
*
b,
char
*
ans)
{
int
aLen,bLen;
int
aLast,bLast,ansLast;
int
flag
=
false
;
//
进位标志
aLen
=
strlen(a);
aLast
=
aLen
-
1
;
bLen
=
strlen(b);
bLast
=
bLen
-
1
;
ansLast
=
aLen;
ans[ansLast
+
1
]
=
'
\0
'
;
ans[
0
]
=
'
0
'
;
while
(bLast
>=
0
)
{
if
(flag)
{
if
(
int
(a[aLast])
+
int
(b[bLast])
>=
105
)
{
flag
=
true
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
+
int
(b[bLast
--
])
-
105
+
48
);
}
else
{
flag
=
false
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
+
int
(b[bLast
--
])
-
95
+
48
);
}
}
else
{
if
(
int
(a[aLast])
+
int
(b[bLast])
>=
106
)
{
flag
=
true
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
+
int
(b[bLast
--
])
-
106
+
48
);
}
else
{
flag
=
false
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
+
int
(b[bLast
--
])
-
96
+
48
);
}
}
}
while
(aLast
>=
0
)
{
if
(flag)
{
if
(
int
(a[aLast])
>=
57
)
{
flag
=
true
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
-
57
+
48
);
}
else
{
flag
=
false
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
+
1
);
}
}
else
{
if
(
int
(a[aLast])
>=
58
)
{
flag
=
true
;
ans[ansLast
--
]
=
char
(
int
(a[aLast
--
])
-
58
+
48
);
}
else
{
flag
=
false
;
ans[ansLast
--
]
=
a[aLast
--
];
}
}
}
if
(flag)
ans[ansLast]
=
'
1
'
;
return
0
;
}
//第二版string_add And string compare
string 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;
}
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;
}
posted on 2006-03-02 21:59
豪 阅读(792)
评论(0) 编辑 收藏 引用 所属分类:
数据结构与算法