#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char number1[40];
char number2[40];
char a[80];
cout << "請輸入第一個整數(不超過40位數):";
cin >> number1;
cout << "請輸入第二個整數(不超過40位數):";
cin >> number2;
int size1 = strlen(number1);
int size2 = strlen(number2);
int size3 = strlen(number1) + strlen(number2);
for( int count = 0 ; count < size2 ; count++ )
{
for( int i = 0 ; i < size1 ; i++ )
{
a[count+i] += number1[i] * number2[count];
}
}
for( int i = 0 ; i < size3 ; i++ )
if( a[i] > 9 )
{
a[i+1] = a[i+1] + a[i] / 10;
a[i] = a[i] % 10;
}
for( int i = 0 ; i < size1 ; i++ )
cout << number1[i];
cout << " * ";
for( int i = 0 ; i < size2 ; i++ )
cout << number2[i];
cout << " = ";
for( int i = 0 ; i < size3 ; i++ )
cout << a[i];
system("pause");
return 0;
}
這是我寫的CODE 可是會算出亂碼,是否把char轉成int就OK了?
想請大家幫我解答 謝囉
以下是題目:
設計一個程式,可以做大數「整數」相乘。整數包含正整數、零、負整數。
輸入範例:
請輸入第一個整數(不超過40位數): 987654321123456789
請輸入第二個整數(不超過40位數): 21
結果:987654321123456789 * 21 = 20740740743592592569