程序主要要求 主程序讀入兩個字符串類型的大數(小數),通過函數實現小數的高精度減法。求完整的代碼和注釋,比較急,謝謝
你這是什麼編輯器啊。連作用域都不分,我把變量名都改了,代碼如下:
#include <iostream>
using namespace std;
#define MAX_NUMBER_LEN 1024
bool isGreaterThan(const int* pNum1, const int* pNum2)
{
if (pNum1 == NULL || pNum2 == NULL)
{
cout << "您輸入的數字有誤" << endl;
return false;
}
for(int nIndx = 0; nIndx < MAX_NUMBER_LEN; ++nIndx)
{
if (pNum1[nIndx] > pNum2[nIndx])
return true;
}
return false;
}
int main()
{
char szNum1[1024] = {0};
char szNum2[1024] = {0};
int arNumber1[1024] = {0};
int arNumber2[1024] = {0};
cout << "請輸入兩個小數(小數位數不要超過500位),空格分隔:" << endl;
cin >> szNum1;
cin >> szNum2;
// 把第一個字符串轉化成數組
int nCount = 0;
int nNumLen = strlen(szNum1);
bool bFindDot = false;
for (int nIndex = 0, pos = nIndex + 1; nIndex < nNumLen; ++nIndex)
{
if (szNum1[nIndex] == '.')
{
bFindDot = true;
}
else
{
if(szNum1[nIndex] < '0' || szNum1[nIndex] > '9')
{
cout << "請輸入不是正確的數字" << endl;
return 0;
}
else
{
arNumber1[pos] = szNum1[nIndex] - '0';
}
pos++;
}
if (!bFindDot)
nCount++;
}
arNumber1[0] = nCount;
// 把第二個字符串轉化成數組
nCount = 0;
nNumLen = strlen(szNum2);
bFindDot = false;
for (int nIndex1 = 0, pos = nIndex1 + 1; nIndex1 < nNumLen; ++nIndex1)
{
if (szNum2[nIndex1] == '.')
{
bFindDot = true;
}
else
{
if(szNum2[nIndex1] < '0' || szNum2[nIndex1] > '9')
{
cout << "請輸入不是正確的數字" << endl;
break;
}
else
{
arNumber2[pos] = szNum2[nIndex1] - '0';
}
pos++;
}
if (!bFindDot)
nCount++;
}
arNumber2[0] = nCount;
bool isGreater = isGreaterThan(arNumber1, arNumber2);
// 比較小數前的位數
if (arNumber1[0] != arNumber2[0])
{
int nshift = abs(arNumber1[0] - arNumber2[0]);
int *pNum;
int nTotal;
if(isGreater)
{
pNum = arNumber2;
nTotal = strstr(szNum2, ".") ? strlen(szNum2) - 1 : strlen(szNum2);
}
else
{
pNum = arNumber1;
nTotal = strstr(szNum1, ".") ? strlen(szNum1) - 1 : strlen(szNum1);
}
for(int nIndex2 = nTotal; nIndex2 >= 0; --nIndex2)
{
pNum[nIndex2 + nshift] = pNum[nIndex2];
}
for(int nIndex2 = nshift; nIndex2 >= 1; --nIndex2)
{
pNum[nIndex2] = 0;
}
}
// 做簡單減法
int nlen1 = strstr(szNum1, ".") ? strlen(szNum1) - 1 : strlen(szNum1);
int nlen2 = strstr(szNum2, ".") ? strlen(szNum2) - 1 : strlen(szNum2);
int forcount= nlen1 > nlen2 ?nlen1 : nlen2;
if (isGreater)
{
for (int nIndex3 = 1; nIndex3 <= forcount; nIndex3++)
{
arNumber1[nIndex3] = arNumber1[nIndex3] - arNumber2[nIndex3];
}
}
else
{
for (int nIndex4 = 1; nIndex4 <= forcount; nIndex4++)
{
arNumber1[nIndex4] = arNumber2[nIndex4] - arNumber1[nIndex4];
}
}
// 處理減法中的負數
arNumber1[0] = arNumber1[0] > arNumber2[0] ? arNumber1[0] : arNumber2[0];
for ( int nIndex5 = forcount; nIndex5 > 0; --nIndex5)
{
if (arNumber1[nIndex5] < 0)
{
arNumber1[nIndex5] += 10;
arNumber1[nIndex5 - 1] -= 1;
}
}
cout << "結果為" << endl;
if (!isGreater)
cout << "-";
//輸出結果
bool zerooutpput = false;
bool putpucount = 0;
for( int nIndex6 = 1; nIndex6 <= forcount; nIndex6++ )
{
if (arNumber1[nIndex6] != 0 || nIndex6 > arNumber1[0])
{
zerooutpput = true;
cout << arNumber1[nIndex6];
putpucount++;
}
else if (zerooutpput)
{
cout << arNumber1[nIndex6];
putpucount++;
}
if (nIndex6 == arNumber1[0])
{
if (putpucount == 0)
cout << "0";
if (nIndex6 != forcount)
cout << '.';
}
}
system("pause");
return 0;
}