//把字符串颠倒,如:123变为321
#include <stdio.h>
#include <string.h>
char* rev(char* res, int t)
{
int idx1 = t - 1, idx2 = 0;
for (; idx2 < idx1; idx2++, idx1--) {
char ch = res[idx2];
res[idx2] = res[idx1];
res[idx1] = ch;//交换位置
}
return res;
}
char* multiply(char* n1, char* n2)
{
int n1len = strlen(n1);
int n2len = strlen(n2);
int maxlen = n1len;
if (maxlen < n2len)
maxlen = n2len;
char* res = (char*)malloc(maxlen + 2);
int i, j, index = 0;
int pro = 0;
for (i = n1len - 1, j = n2len - 1; i >= 0 && j >= 0; i--, j--)
{
int tag = (n1[i] - '0') + (n2[j] - '0') + pro;
pro = tag / 10;
res[index++] = (tag % 10) + '0';
}
for (; i >= 0; i--)
{//n1数组中还有数据
int tag = (n1[i] - '0') + pro;
pro = tag / 10;
res[index++] = (tag % 10) + '0';
}
for (; j >= 0; j--)
{//n2数组中还有数据
int tag = (n2[j] - '0') + pro;
pro = tag / 10;
res[index++] = (tag % 10) + '0';
}
if (pro)//需要进位,把进位结果添加进res
res[index++] = pro + '0';
res[index] = '\0';
return rev(res, strlen(res));
}
int main() {
char* data = multiply("2", "1");
return 0;
}