代码如下,有时候运算结果不会出现乱码,有时会出现乱码,很奇怪
比如:(第一行是a,第二行是b,第三行是输出结果a*b)
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
string str;
string a, b;
cin >> a >> b;
int temp = 0,temp1=0,temp2=0;
int aa = a.length()-1;
int bb = b.length()-1;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (int i = 0; i <= aa + bb+1; i++)
str+= '0';
for (int i = 0; i <= aa; i++)
{
for (int j = 0; j <=bb; j++)
{
str[i+j]+= ((int)a[i]-48) * ((int)b[j]-48);
}
}
int m = 0;
for (int i = 0; i <= aa + bb; i++)
{
if (str[i] + temp > '9')
{
str[i] += temp;
temp = ((int)str[i] - 48) / 10;
str[i] = ((int)str[i] - 48) % 10+'0';
}
else
temp = 0;
}
str[aa + bb + 1] += temp;
reverse(str.begin(), str.end());
for (int i = 0, test = 0; i <= aa + bb + 1; i++)
{
if (str[i] == '0' && test == 0)
{
continue;
}
else
{
test++;
cout << str[i];
}
}
}
不知各位可有什么类似的经验吗,万分感谢您的帮助!