#include <stdio.h>
#include <string.h>
#define MAX 500
void reverse(char a[]){
char temp;
int len = strlen(a), i;
for(i = 0; i < (len+1)/2; i++){
temp = a[i];
a[i] = a[len-i-1];
a[len-i-1] = temp;
}
}
void func(char num1[], char num2[]){
int l1 = strlen(num1), l2 = strlen(num2);
//让p1、p2指向数字末尾
char *p1 = &num1[l1-1], *p2 = &num2[l2-1];
char result[MAX], *p3 = result;
//是否需要借位
int n = 0;
int rn1, rn2;
while (p1 >= num1 || p2 >= num2){
//获取真实数值
if(p1 >= num1){
rn1 = *p1 - '0';
p1--;
}else{
rn1 = 0;
}
if(p2 >= num2){
rn2 = *p2 - '0';
p2--;
}else{
rn2 = 0;
}
*p3 = (10 + rn1 - rn2 - n) % 10 + '0';
if(rn1-n < rn2){
n = 1;
}else{
n = 0;
}
p3++;
}
*p3 = '\0';
while (1){
if(*--p3 == '0' && p3 != result){
*p3 = '\0';
}else{
break;
}
}
reverse(result);
printf("%s\n", result);
}
void BigNumSub(char num1[], char num2[]){
int len;
if(strlen(num1) == strlen(num2)){
len = strlen(num1);
if(num1[len-1] > num2[-1]){
func(num1, num2);
} else{
func(num2, num1);
}
}else if(strlen(num1) > strlen(num2)){
func(num1, num2);
}else{
func(num2, num1);
}
}
int main(int argc, char *argv[]) {
BigNumSub("10113", "1013421");
return 0;
}