怎么简化函数
#include<cstdio>
#include<iostream>
#include<cstdio>
#include<string>
//字符串数组是从后往前储存,最后一个下标为0,往前
using namespace std;
string s1, s2, s;
int a[11000], b[11000];
void rem()
{
{
cin >> s1 >> s2;
//scanf("%s%s", &s1[0], &s2[0]);
int len1 = s1.size();
int len2 = s2.size();
int len = max(len1, len2);
for (int i = 0; i < len1; i++)
{
a[len1 - i] = s1[i] - '0';//char转为int要-48 0对应ASCII为48,从后往前储存
}
for (int i = 0; i < len2; i++)
{
b[len2 - i] = s2[i] - '0';//从后往前储存
}
}
}
void plus()
{
cin >> s1 >> s2;
int len1 = s1.size();
int len2 = s2.size();
int len = max(len1, len2);
rem();
int m = 0;//进位
for (int i = len; i >= 1; i--)//输入是从后往前输入
{
a[i] = a[i] + b[i] + m;//c[i]只存个位数
m = a[i] / 10;
a[i] = a[i] % 10;
}
while (len > 1 && a[len] == 0)
{
len--;
}
if (m == 1)//进位还剩1
{
//c[++len] = 1;必须是前置递增
//len=len=1;
//c[len] = 1;
a[len + 1] = 1;
}
}
void plus2()
{
cin >> s1 >> s2;
int len1 = s1.size();
int len2 = s2.size();
int len = max(len1, len2);
for (int i = len; i >= 1; i--)a[i] = a[i] + b[i];//c没用了
for (int i = 1; i <= len; i++)//输入是从后往前输入
{
a[i + 1] = a[i + 1] + a[i] / 10;
a[i] = a[i] % 10;
}
while (len > 1 && a[len] == 0)
{
len--;
}
for (int i = len; i >= 1; i--)
{
printf("%d", a[i]);
}
}
int sub()
{
cin >> s1 >> s2;
int len1 = s1.size();
int len2 = s2.size();
if (s1 == s2)
{
printf("0");
return 0;
}
if (len1 < len2 || len1 == len2 && s1 < s2)//len1 < len2 或者 len1 = len2并且s1 < s2
{
printf("-");
s = s1;
s1 = s2;
s2 = s;
swap(len1, len2);//交换长度
}
int len = max(len1, len2);//交换完之后再取最长
for (int i = 1; i <= len; i++)
{
if (a[i] < b[i])
{
a[i + 1] = a[i + 1] - 1;
a[i] = a[i] + 10;
}
a[i] = a[i] - b[i];
}
while (len > 1 && a[len] == 0)
{
len--;
}
for (int i = len; i >= 1; i--)
{
printf("%d", a[i]);
}
}
int main()
{
plus();
return 0;
}
plus不明确
函数重复的太多了,有没有办法简化一下,谢谢