uj5u.com熱心網友回復:
debug單步除錯,好好學習下:int SwitchDecOctPow(int nBase, int nY)
{
int nResult = 1;
for (int i = 0; i < nY; ++i)
{
nResult *= nBase;
}
return nResult;
}
void SwitchDecOct(void)
{
bool bRun = true;
while (bRun)
{
printf("*********************\r\n");
printf(" 進制轉換系統\r\n");
printf("1、八進制轉換為十進制\r\n");
printf("2、十進制轉換為八進制\r\n");
printf("*********************\r\n");
printf("請選擇序號1或2:");
int nType = 0;
if (scanf_s("%d", &nType) != 1
|| nType != 1 && nType != 2)
{
printf("輸入錯誤\r\n");
return;
}
printf("請輸入欲轉換的資料:");
int nNumber = 0;
if (scanf_s("%d", &nNumber) != 1
|| nNumber < 0)
{
printf("輸入錯誤\r\n");
return;
}
int nResult = 0;
// oct -> dec
if (nType == 1)
{
for(int i = 0; nNumber != 0; ++i)
{
int nBit = nNumber % 10;
nNumber /= 10;
if (nBit >= 8)
{
printf("輸入錯誤\r\n");
return;
}
// nResult += nBit * pow(8, i);
nResult += nBit * SwitchDecOctPow(8, i);
}
}
// dec -> oct
else
{
for (int i = 0; nNumber != 0; ++i)
{
int nBit = nNumber % 8;
nNumber /= 8;
// nResult += nBit * pow(10, i);
nResult += nBit * SwitchDecOctPow(10, i);
}
}
printf("轉換為:%d\r\n", nResult);
printf("是否繼續轉換?y/n:");
char cContinue = 0;
scanf_s("\n%c", &cContinue, sizeof(char));
bRun = false;
if (cContinue == 'y')
bRun = true;
else if (cContinue == 'n')
bRun = false;
else
printf("輸入錯誤\r\n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/196234.html
標籤:C++ 語言
上一篇:動態陣列記憶體釋放 求助!!!!
下一篇:COC或VIM 命令轉換格式
