代碼已經超過600行了,這裏只有部分代碼,因為已經超過字數限制了。其余的代碼可以在源文件中查看。
#包含?& ltiostream & gt
#包含?& ltconio.h & gt
#包含?& ltmath.h & gt
使用?命名空間?std
#定義?Mc_MaxByte_MenuItem?61//菜單項字符串的最大長度為61字節。
#定義?Mc_MaxBufferSize17//緩存的最大字節數。因為程序只支持小於等於2字節的整數輸入,而2字節整數的二進制位有16位,所以需要17字節來保存二進制串,最後壹位是終止符。
#定義?Mc_Back(-1)?//返回上壹級菜單。
#定義?Mc_Exit(-2)?//退出程序
#定義?Mc_Error?(-3)?//輸入錯誤
#定義?Mc_Binary?0?//二進位
#定義?Mc_Octonary?1?//八進制
#定義?Mc_Decimalism?2?//十進制系統
#定義?Mc_Hexadecimal3?//十六進制
班級?計算器?//計算器類
{
受保護:
夏爾?chr 1 buffer[Mc _ MaxBufferSize];?//用於接收用戶輸入的數學表達式,如“23+6*5-400/20=”
布爾?Chr1SameAsInt(char*?chrPt,int?號);?//確定兩個整數是否相等。前者是用字符串表示的整數,後者是int類型的整數。
int chr 1 point(const?char*?chrPt);?//將字符串表示的整數轉換為int整數。
intMenu(char*?chrPtMenuTitle,char?chr 2 MenuItem[][Mc _ max byte _ MenuItem],?int?intItemCount,bool?bCreateBackItem);?//顯示菜單。最後壹個參數指示是否創建Return選項。
作廢?clear screen();//清除屏幕
作廢?ShowInf(const?char*?chr INF);//顯示壹條消息
作廢?NextLine(int?計數);//換行
intNumberFormTransitionUI(char *?標題);?//"整數十進制轉換"功能界面
作廢?InputToBuffer(const?char*?標題);
const?char * get buffer();
作廢?(計算器::*funPt)(const?char*?chrSource,char*?chr result);?//函數指針
intNumberFormTransition(int?source,int?目標);?//二進制轉換函數。二進制形式的源源數據,二進制形式的目標目標
作廢?BinaryToOctonary(const?char*?chrPtBinary,char*?chrPtOctonaryResult);?//二進制到八進制。結果將由壹個字符串表示,轉換後的結果將用chrPtOctonaryResult接收。
作廢?binarytodedimalism(const?char*?chrPtBinary,char*?chrPtDecimalismResult);?//二進制到十進制
作廢?BinaryToHexadecimal(const?char*?chrPtBinary,char*?chrPtHexadecimalResult);?//二進制到十六進制
作廢?十進制(const?char*?chrPtOctonary,char*?chrPtDecimalismResult);?//八進制到十進制
作廢?OctonaryToBinary(const?char*?chrPtOctonary,char*?chrPtBinaryResult);?//八進制到二進制
作廢?十進制(const?char*?chrPtOctonary,char*?chrPtHexadecimalResult);?//八進制到十六進制
作廢?decimalismtobbinary(const?char*?chrPtDecimalism,char*?chrPtBinaryResult);?//十進制到二進制
作廢?decimalismtoctorial(const?char*?chrPtDecimalism,char*?chrPtOctonaryResult);?//十進制到八進制
作廢?decimalismtoxidal(const?char*?chrPtDecimalism,char*?chrPtHexadecimalResult);?//十進制到十六進制
作廢?十六進制十進制(const?char*?chrpt十六進制,char*?chrPtDecimalismResult);?//十六進制到十進制
作廢?HexadecimalToOctonary(常量?char*?chrpt十六進制,char*?chrPtOctonaryResult);?//十六進制到八進制
作廢?十六進制二進制(常量?char*?chrpt十六進制,char*?chrPtBinaryResult);?//十六進制到二進制
公共:
intUI();?//計算器界面
};
int?計算器::UI()
{
int?代碼?=?!Mc_Exit,?CodeTmp?=?1;
夏爾?chr 2 menu[][Mc _ max byte _ MenuItem]?=?{?“數字的二進制轉換”?};
什麽時候?(代碼?!=?Mc_Exit)
{
代碼?=?菜單(“計算器”,?chr2Menu,?1,?假);
切換?(代碼)
{
案子?1:
clear screen();
CodeTmp?=?NumberFormTransitionUI(chr 2 menu[0]);
打破;
案子?Mc_Error:
show INF(" \ n & lt;?輸入錯誤!請重新輸入。?& gt\ n & lt?按任意鍵繼續嗎?>);
getch();
clear screen();
}
如果?(CodeTmp?==?Mc_Exit)
代碼?=?Mc _ Exit
}
回歸?代碼;
}
布爾?計算器::Chr1SameAsInt(char*?chrPt,int?編號)
{
int?萊恩。=?strlen(chrPt)?-?1;
而(號?& amp& amp?萊恩。& gt=?0)
{
如果?(chrPt[len]?-?48?!=?號碼?%?10)
打破;
萊恩。- ,?號碼?/=?10;
}
如果?(len?& lt?0?& amp& amp?!編號)
回歸?真實;
回歸?假的;
}
int?計算器::chr 1 point(const?char*?chrPt)
{
int?萊恩。=?strlen(chrPt),?num?=?0,?我;
為了什麽?(我?=?0;?萊恩。& gt?0;?萊恩。- ,?我?++)?//將字符串表示的數字轉換為int整數。
num?+=?(chrPt[i]?-?48)?*?(int)pow(10,?萊恩。-?1);
回歸?num
}
int?計算器::菜單(char*?chrPtMenuTitle,char?chr 2 MenuItem[][Mc _ max byte _ MenuItem],int?intItemCount,bool?bCreateBackItem)
{
int?指數,?選擇?=?0,?萊恩。我;
cout & lt& lt“& lt- ?"& lt& ltchrPtMenuTitle & lt& lt"?-->;& lt& ltendl & lt& ltendl
為了什麽?(索引?=?1;?指數?& lt=?intItemCount?指數?++)
cout & lt& lt" " " & lt& ltindex & lt& lt"."& lt& ltchr2MenuItem[index?-?1]& lt;& ltendl
如果?(bCreateBackItem)
cout & lt& lt" " " & lt& lt指數?++ & lt;& lt"."& lt& lt“返回”
cout & lt& lt" " " & lt& ltindex & lt& lt"."& lt& lt"退出"
InputToBuffer("請輸入相應選項的序號:");
如果?(bCreateBackItem)
{
如果?(chr 1 same asint(chr 1 buffer,?指數?-?1))
回歸?Mc _ Back//用戶選擇了返回選項。
}
如果?(chr 1 same asint(chr 1 buffer,?索引))
回歸?Mc _ Exit//用戶選擇了退出選項。
萊恩。=?strlen(chr 1 buffer);
為了什麽?(我?=?0;?萊恩。& gt?0;?萊恩。- ,?我?++)?//將字符串表示的數字轉換為int整數。
選擇?+=?(chr1Buffer[i]?-?48)?*?(int)pow(10,?萊恩。-?1);
如果?(選擇?& lt?1?||?選擇?& gt?索引)
回歸?Mc _ Error
回歸?選擇;
}
作廢?計算器::ClearScreen()
{
系統(“cls”);
}
作廢?計算器::ShowInf(const?char*?chrInf)
{
cout & lt& ltchrInf
}
作廢?計算器::NextLine(int?計數)
{
什麽時候?(計數-?& gt?0)
{
cout & lt& lt\ n ';
}
}
作廢?計算器::InputToBuffer(const?char*?標題)
{
cout & lt& lt標題;
CIN & gt;& gtchr 1緩沖區;
}
const?char*?計算器::GetBuffer()
{
回歸?chr 1緩沖區;
}
作廢?計算器::BinaryToOctonary(const?char*?chrPtBinary,char*?chrPtOctonaryResult)
{
int?指數,?x,?numTmp?=?0;
boolbBreak
const?char*?chrPt
夏爾?chr 1 tmp[6];?//十進制65535等於八進制177777。這個最大八進制數有6個數字,因此至少需要6個字節。存儲在這個臨時數組中的字符串不需要終止符。
chrPt?=?chrPtBinary?+?strlen(chrPtBinary)?-?1;?//讓chrPt指向最後壹個字符。
指數?=?0,?bBreak?=?假的;
什麽時候?(bBreak?==?假)
{
為了什麽?(x?=?0,?numTmp?=?0;?x?& lt?3;?x?++,?chrPt?- )
{?//?01011011?-& gt;每個3位二進制數對應壹個八進制數。從最低位開始,從右開始:011 = 3,011 = 3,01 = 1。二進制數的八進制形式是133(最終結果需要反過來)。
如果?(chrPt?& gt?chrPtBinary)
numTmp?+=?(*chrPt?-?48)?*?(int)pow(2,x);
其他
{
numTmp?+=?(*chrPt?-?48)?*?(int)pow(2,x);
bBreak?=?真實;
打破;
}
}
chr1Tmp[index?++]?=?numTmp?+?48;
}
如果?(索引?& gt?1)
{
為了什麽?( - ?指數;?指數?& gt?0;?指數?- )
如果?(chr1Tmp[index]?!=?'0')
打破;
}
其他
{
chr1Tmp[0]?=?'0';
指數?=?0;
}
為了什麽?(;?指數?& gt=?0;?chrPtOctonaryResult?++,?指數?- )
*chrPtOctonaryResult?=?chr 1 tmp[index];
*chrPtOctonaryResult?=?'\0';?//在返回參數中添加終止符。
}
作廢?calculator::decimalis MTO binary(const?char*?chrPtDecimalism,char*?chrPtBinaryResult)?//十進制到二進制
{
int?num?=?chr 1 point(chrPtDecimalism),?指數?=?0;
夏爾?chr 1 tmp[16];
什麽時候?(數字)
{
chr1Tmp[index?++]?=?num?%?2?+?48;
num?/=?2;
}
如果?(!索引)
{
chr1Tmp[0]?=?'0';
指數?=?1;
}
對於(-?指數;?指數?& gt=?0;?chrPtBinaryResult?++,?指數?- )
*chrPtBinaryResult?=?chr 1 tmp[index];
*chrPtBinaryResult?=?'\0';
}
作廢?calculator::decimalis MTO十六進制(const?char*?chrPtDecimalism,char*?chrPtHexadecimalResult)?//十進制到十六進制
{
int?num?=?chr 1 point(chrPtDecimalism),?指數?=?0,?tmp
夏爾?chr 1 tmp[16];
什麽時候?(數字)
{
tmp?=?num?%?16;
num?/=?16;
切換?(tmp)
{
案子?10:
chr1Tmp[index?++]?=?a’;
打破;
案子?11:
chr1Tmp[index?++]?=?b’;
打破;
案子?12:
chr1Tmp[index?++]?=?c’;
打破;
案子?13:
chr1Tmp[index?++]?=?d’;
打破;
案子?14:
chr1Tmp[index?++]?=?e’;
打破;
案子?15:
chr1Tmp[index?++]?=?f’;
打破;
默認值:
chr1Tmp[index?++]?=?tmp?+?48;
}
}
如果?(!索引)
{
chr1Tmp[0]?=?'0';
指數?=?1;
}
對於(-?指數;?指數?& gt=?0;?chrPtHexadecimalResult?++,?指數?- )
*chrPtHexadecimalResult?=?chr 1 tmp[index];
*chrPtHexadecimalResult?=?'\0';
}
作廢?計算器::hexadecimaltodedimalism(const?char*?chrpt十六進制,char*?chrPtDecimalismResult)?//十六進制到十進制
{
int?x,?指數,?NumTmp?=?0;
const?char*?chrPt?=?chrpt十六進制?+?strlen(chrpt十六進制)?-?1;
夏爾?chr 1 tmp[4];
為了什麽?(?x?=?0;?chrPt?& gt?chrpt十六進制;?chrPt?- )
{
切換?(*chrPt)
{
案子?答:
案子?答:
NumTmp?+=?10?*?(int)pow(16,?x?++);
打破;
案子?b ':
案子?b ':
NumTmp?+=?11?*?(int)pow(16,?x?++);
打破;
案子?c ':
案子?c ':
NumTmp?+=?12?*?(int)pow(16,?x?++);
打破;
案子?d ':
案子?d ':
NumTmp?+=?13?*?(int)pow(16,?x?++);
打破;
案子?e ':
案子?e ':
NumTmp?+=?14?*?(int)pow(16,?x?++);
打破;
案子?f ':
案子?f ':
NumTmp?+=?15?*?(int)pow(16,?x?++);
打破;
默認值:
NumTmp?+=?(*chrPt?-?48)?*?(int)pow(16,?x?++);
}
}
切換?(*chrPt)
{
案子?答:
案子?答:
NumTmp?+=?10?*?(int)pow(16,?x);
打破;
案子?b ':
案子?b ':
NumTmp?+=?11?*?(int)pow(16,?x);
打破;
案子?c ':
案子?c ':
NumTmp?+=?12?*?(int)pow(16,?x);
打破;
案子?d ':
案子?d ':
NumTmp?+=?13?*?(int)pow(16,?x);
打破;
案子?e ':
案子?e ':
NumTmp?+=?14?*?(int)pow(16,?x);
打破;
案子?f ':
案子?f ':
NumTmp?+=?15?*?(int)pow(16,?x);
打破;
默認值:
NumTmp?+=?(*chrPt?-?48)?*?(int)pow(16,?x?++);
}
為了什麽?(索引?=?0;?NumTmp?指數?++)
{
chr1Tmp[index]?=?NumTmp?%?10?+?48;
NumTmp?/=?10;
}
如果?(!索引)
{
chr1Tmp[0]?=?'0';
指數?=?1;
}
為了什麽?( - ?指數;?指數?& gt=?0;?chrPtDecimalismResult?++,?指數?- )
*chrPtDecimalismResult?=?chr 1 tmp[index];
*chrPtDecimalismResult?=?'\0';
}
作廢?計算器::hexadecimaltoocotonary(const?char*?chrpt十六進制,char*?chrPtOctonaryResult)?//十六進制到八進制
{
夏爾?chr 1 tmp[6];
十六進制十進制chr 1 tmp);//先把十六進制轉換成十進制。
十進制八進制(chr1Tmp,?chrPtOctonaryResult);//然後把十進制轉換成八進制。
}
作廢?計算器::十六進制二進制(const?char*?chrpt十六進制,char*?chrPtBinaryResult)?//十六進制到二進制
{
夏爾?chr 1 tmp[6];
十六進制十進制chr 1 tmp);//先把十六進制轉換成十進制。
decimalismtobinical(chr 1 tmp,?chrPtBinaryResult);//然後把十進制轉換成二進制。
}
int?主()
{
計算器?cal
加州UI();
回歸?0;
}