天天看點

MFC實作浮點/進制轉換電腦簡介

簡介

VS2019編寫的電腦,支援括号和小數運算和進制轉換

當進制發生轉換時,如果表達式框内有表達式,會計算後轉化,支援小數轉換,如果表達式有誤,将直接清空。

界面

MFC實作浮點/進制轉換電腦簡介

實作方法

表達式框添加控制變量CString類型的m_sEquation,除了求值運算其他按鍵都可以認為是對字元串的操作

下面重點介紹值的運算,為此寫了一個MyCalculator類,它有兩個成員函數:

bool MyCalculator::Calc(CString Equation, CString &ans, int radix)

傳入表達式Equation,和進制radix(預設為十進制),将計算結果存入ans中

當表達式有錯誤時,如"(1+1++1)","(1+6",函數傳回值FALSE,否則傳回TRUE

被除數為0時,ans指派為"除0異常"

為了擷取小數,需要下面的GetNum函數,傳入表達式Eq,起始位置i,進制radix,注意這個函數不可以直接處理負數

  • 對于整數部分,和普通整數變換一樣 x = x ∗ 10 + E q [ i ] − ′ 0 ′ x=x*10+Eq[i]-'0' x=x∗10+Eq[i]−′0′,但是現在不一定是十進制,換成 ∗ r a d i *radi ∗radix就可以;
  • 判斷是否有小數點
  • 如果有,小數部分第i位要乘上 1 / r a d i x 1/radix 1/radix的i次方
double GetNum(CString& Eq, int& i, int radix)
{
	double x = 0;
	while (i<Eq.GetLength() && IsNum(Eq[i]))
	{
		x = x * radix + Eq[i] - '0';
		i++;
	}
	double rad = 1.0 / radix;
	if (i < Eq.GetLength() && Eq[i] == '.') 
	{
		i++;
		while (i < Eq.GetLength() && IsNum(Eq[i]))
		{
			x = x + (Eq[i] - '0') * rad;
			rad = rad / radix;
			i++;
		}
	}
	return x;
}
           

計算表達式的整體過程是通過表達式棧實作

num數字表達式

opt操作符表達式

因為有負數,已經要判斷"2++3"之類錯誤,不友善直接判斷某一位到底是數還是操作符,但是所有表達式都有一個特點:除了括号數字後面一定是操作符,操作符後面一定是數字,左括号左是操作符右是數字,右括号左是數字又是操作符,是以可以寫以下循環

w h i l e while while 表達式未處理完:

  • 判斷左括号,可能沒有
  • 得到數字(先判斷符号再GetNum),入棧
  • 判斷右括号,如果有,出棧至左括号
  • 得到操作符,把操作符棧頂優先級不比它低的出棧,自己入棧

最後所有操作符出棧

這裡的出棧指取出操作符和兩個操作數,運算後結果數入棧這一整個過程

bool MyCalculator::Calc(CString Equation, CString &ans, int radix) 
{
	double num[MAXNUM];
	int ntop = 0;
	char opt[MAXOPT];
	int otop = 0;
	int Len = Equation.GetLength();

	for (int i = 0 ; i < Len; )
	{
		if (Equation[i] == '(')
		{
			opt[++otop] = Equation[i++];
			if (i >= Len) return FALSE;
		}

		bool flag = false;
		if (Equation[i] == '-')
		{
			flag = true;
			i++;
			if (i >= Len) return FALSE;
		}
		if (!IsNum(Equation[i]))
			return FALSE;
		num[++ntop] = GetNum(Equation, i, radix) * (flag ? -1 : 1);
		if (i >= Len) break;

		if (Equation[i] == ')')
		{
			while (otop > 0 && opt[otop] != '(') 
			{
				INT cas = GetOutSta(num, ntop, opt, otop);
				if (cas == -1) return FALSE;
				if (cas == 0) {
					ans = _T("除0異常");
					return TRUE;
				}
			}
			if (otop == 0) return FALSE;
			otop--;
			i++;
			if (i >= Len) break;
		}

		if (!IsOpt(Equation[i]))
			return FALSE;
		while (otop > 0 && opt[otop]!='(' && !Greater(Equation[i],opt[otop])) 
		{
			INT cas = GetOutSta(num, ntop, opt, otop);
			if (cas == -1) return FALSE;
			if (cas == 0) {
				ans = _T("除0異常");
				return TRUE;
			}
		}
		opt[++otop] = Equation[i++];
		if (i >= Len) return FALSE;
	}
	//return 0;
	while (otop) {
		INT cas = GetOutSta(num,ntop,opt,otop);
		if (cas == -1) return FALSE;
		if (cas == 0) {
			ans = _T("除0異常");
			return TRUE;
		}
	}
	if (ntop != 1) return FALSE;
	ans.Format(_T("%g"), num[ntop]);
	if (radix != 10) {
		ConvertRad(ans, 10, radix);
	}
	return TRUE;
}
           

bool MyCalculator::ConvertRad(CString& Num, int radixFrom, int radixTo)

傳入原數字Num,轉換前的進制radixFrom,轉換後的進制radixTo,轉換失敗時(Num不是數字)傳回FALSE

首先用GeNum轉換為double型dtmp

  • 如果是負數dtmp=-dtmp,ans加‘-’
  • 整數部分用自帶的_itoa_s轉換
  • 小數部分不斷乘radixTo,順序取整數位,預設最多轉成8位小數
bool MyCalculator::ConvertRad(CString& Num, int radixFrom, int radixTo)
{
	int Len = Num.GetLength();
	int j = 0;
	bool flag = false;
	if (Num[j] == '-') {
		j++;
		flag = true;
	}
	double dtmp = GetNum(Num,j,radixFrom);
	if (j != Len) return FALSE;
	Num = "";
	if (flag) {
		Num = "-";
	}
	int itmp = (int)dtmp;
	char str[100];
	_itoa_s(itmp, str, 100, radixTo);
	Num += (CString)str;
	dtmp -= itmp;
	int dotLen = 0;
	if (dtmp > 1e-7) {
		Num += ".";
		while (dtmp > 1e-7 && dotLen <= 8) {
			dtmp *= radixTo;
			Num += static_cast<char>('0'+(int)dtmp);
			dtmp = dtmp - (int)dtmp;
		}
	}
	return TRUE;
}
           

完整代碼

CSDN下載下傳

碼雲

繼續閱讀