#include <string>
//求差 a-b a和b都大于零 但是返回值可能有负号 有bug欢迎评论qwq
std::string maths_subtraction(std::string a, std::string b){
size_t lena = a.length();
size_t lenb = b.length();
bool tuiwei = false;
if (lena > lenb) {
while (lenb != -1) {
a[lena] -=b[lenb] + tuiwei - '0';
if (a[lena] < '0') {
a[lena] += 10;
tuiwei = 1;
}
else tuiwei = 0;
lena--; lenb--;
}
if (tuiwei) {
while (lena != -1) {
if (a[lena] != '0') {
a[lena] -= 1;
if (lena == 0) {
for (; a[lena++] == '0';);
return a.substr(lena-1);
}
return a;
}
a[lena--] = '9';
}
}
return a;
}
else if (lena == lenb) {
if (a >= b) {
while (lenb != -1) {
a[lenb] -= b[lenb] + tuiwei - '0';
if (a[lenb] < '0') {
a[lenb] += 10;
tuiwei = 1;
}
else tuiwei = 0;
lenb--;
}
for (; a[++lenb] == '0'&&lenb<lena;);
if (lenb == lena)return "0";
return a.substr(lena);
}
else {
while (lena != -1) {
b[lenb] -= a[lena] + tuiwei - '0';
if (b[lenb] < '0') {
b[lenb] += 10;
tuiwei = 1;
}
else tuiwei = 0;
lena--; lenb--;
}
for (; b[++lenb] == '0';);
return b.substr(lenb);
}
}
else {
while (lena != -1) {
b[lenb] -= a[lena] + tuiwei - '0';
if (b[lenb] < '0') {
b[lenb] += 10;
tuiwei = 1;
}
else tuiwei = 0;
lena--; lenb--;
}
if (tuiwei) {
while (lenb != -1) {
if (b[lenb] != '0') {
b[lenb] -= 1;
if (lenb == 0) {
for (; b[lenb++] == '0';);
return b.substr(lenb-1).insert(0,"-");
}
return b.insert(0, "-");
}
b[lenb] = '9';
}
}
return b.insert(0, "-");
}
}
顺便求求有没有更好的实现方式qwq