[MFC] 계산기 (3) - 16진수, 8진수, 2진수

당근한박스·2023년 8월 28일
0

C++

목록 보기
5/23

#define 대신 const사용

숫자버튼

void MfcProject_1Dlg_pro::OnBnClickedPro0()
{
	// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
	if (m_str_result.GetLength() < 19) { //18자리일 때 추가 입력 가능하게 하기 위해 '<' 사용
		m_str_result += _T("0");

		int64_t decimalValue = _wtoi64(m_str_result); // 문자열을 숫자로 변환해서 저장

		CString hexValue;
		hexValue.Format(_T("%I64X"), decimalValue);
		CString formattedHex;
		int hexLength = hexValue.GetLength();
		for (int i = 0; i < hexLength; i++) {
			formattedHex += hexValue[i];
			if ((hexLength - i) % 4 == 1 && i != hexLength - 1) {
				formattedHex += _T(" ");
			}
		}
		m_str_HEX = formattedHex;

		CString decValue;
		decValue.Format(_T("%I64d"), decimalValue);
		//NUMBERFMT 숫자 포맷팅에 사용되는 정보를 담는 구조체
		//소수점 이하 자릿수, 앞에 0표시 여부, 그룹화 간격, 소수점 구분자, 천 단위 구분자, 부호 위치(부호가 숫자 앞에 오도록)
		NUMBERFMT nFmt = { 0, 0, 3, _T("."), _T(","), 1 };
		TCHAR szBuffer[64];
		// GetNumberFormat의 인자
		// LOCALE_SYSTEM_DEFAULT, 플래그, 문자열 숫자, 문자열 포맷, 변환된 문자열 숫자, 버퍼 사이즈
		::GetNumberFormat(NULL, NULL, decValue, &nFmt, szBuffer, sizeof(szBuffer));
		decValue = szBuffer; // 문자열 갱신
		m_str_DEC = decValue;

		CString octalValue;
		octalValue.Format(_T("%I64o"), decimalValue);
		CString formattedOct;
		int octLength = octalValue.GetLength();
		for (int i = 0; i < octLength; i++) {
			formattedOct += octalValue[i];
			if ((octLength - i) % 3 == 1 && i != octLength - 1) {
				formattedOct += _T(" ");
			}
		}
		m_str_OCT = formattedOct;

		CString binaryValue;
		int bitCount = 0; //문자열 비트 개수
		do {
			bool bit = decimalValue & 1; //가장 오른쪽 비트를 확인하여 bit에 저장
			binaryValue = (bit ? _T("1") : _T("0")) + binaryValue; // 0=false, 1=true
			decimalValue >>= 1; //decimalValue값을 오른쪽으로 1비트만큼 이동
			bitCount++;
			//4비트마다 공백 추가
			if (bitCount % 4 == 0 && decimalValue != 0) {
				binaryValue = _T(" ") + binaryValue;
			}
		} while (decimalValue > 0);

		m_str_BIN = binaryValue;
	}
	UpdateData(false);
}

연산자

void MfcProject_1Dlg_pro::OnBnClickedProplus()
{
	// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
	CString recodeStr;
	recodeStr.Format(m_str_result);
	int reclength = recodeStr.GetLength();

	if (m_str_result.IsEmpty() || (!m_str_result.IsEmpty() && (recodeStr[reclength - 1] == '.' || recodeStr[reclength - 1] == '+' ||
		recodeStr[reclength - 1] == '-' || recodeStr[reclength - 1] == '*' || recodeStr[reclength - 1] == '/')))
	{
		return; //연산자 추가입력 막음
	}

	if (m_b_btnClk) {
		m_b_btnClk = false; // 변수를 컨트롤에 출력
	}

	m_type = plus; //1
	m_str_num = m_str_result;
	m_str_result.Empty();
	UpdateData(false);
}

계산

void MfcProject_1Dlg_pro::OnBnClickedProequal()
{
	// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
	double op1, op2, result;
	if (!m_str_num.IsEmpty() && !m_str_result.IsEmpty())
	{
		op1 = _tstof(m_str_num); // 문자열을 숫자로 변환
		op2 = _tstof(m_str_result);

		switch (m_type)
		{
		case plus:
			result = op1 + op2;
			break;
		case minus:
			result = op1 - op2;
			break;
		case multi:
			result = op1 * op2;
			break;
		case divide:
			result = trunc(op1 / op2); // trunc < 내림
			break;
			
		}
		m_str_result.Format(_T("%d"), static_cast<int>(result));
		
		int64_t decimalValue = _wtoi64(m_str_result);

		CString hexValue;
		hexValue.Format(_T("%I64X"), decimalValue);
		CString formattedHex;
		int hexLength = hexValue.GetLength();
		for (int i = 0; i < hexLength; i++) {
			formattedHex += hexValue[i];
			if ((hexLength - i) % 4 == 1 && i != hexLength - 1) {
				formattedHex += _T(" ");
			}
		}
		m_str_HEX = formattedHex;

		CString decValue;
		decValue.Format(_T("%I64d"), decimalValue);
		NUMBERFMT nFmt = { 0, 0, 3, _T("."), _T(","), 1 };
		TCHAR szBuffer[64];
		::GetNumberFormat(NULL, NULL, decValue, &nFmt, szBuffer, sizeof(szBuffer));
		decValue = szBuffer;
		m_str_DEC = decValue;

		CString octalValue;
		octalValue.Format(_T("%I64o"), decimalValue);
		CString formattedOct;
		int octLength = octalValue.GetLength();
		for (int i = 0; i < octLength; i++) {
			formattedOct += octalValue[i];
			if ((octLength - i) % 3 == 1 && i != octLength - 1) {
				formattedOct += _T(" ");
			}
		}
		m_str_OCT = formattedOct;

		CString binaryValue;
		int bitCount = 0;
		do {
			bool bit = decimalValue & 1;
			binaryValue = (bit ? _T("1") : _T("0")) + binaryValue;
			decimalValue >>= 1;
			bitCount++;

			if (bitCount % 4 == 0 && decimalValue != 0) {
				binaryValue = _T(" ") + binaryValue;
			}
		} while (decimalValue > 0);

		m_str_BIN = binaryValue;
	}
	else
	{
		return;
	}
	UpdateData(false);
}

지우기

void MfcProject_1Dlg_pro::OnBnClickedProclear()
{
	// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
	m_str_result.Empty();
    m_str_num.Empty();
	m_str_HEX.Empty();
	m_str_DEC.Empty();
	m_str_OCT.Empty();
	m_str_BIN.Empty();
	UpdateData(false);
}

결과

0개의 댓글