Hi, I created a calculator with MFC (C++), but I’m having trouble implementing parenthesis functionality

I am working on implementing a scientific calculator in MFC, C++.

I have implemented most of the functionality of a scientific calculator, except for the parentheses feature.

In fact, when I type the parentheses button, the parentheses are entered in the input window.

Once I type 5+5 without parentheses, I get 10.

However, when I put a number inside the parentheses, the number is treated as a zero. For example, I get (5)+5= 5.

I’ve spent a lot of time thinking about it, Googling, and asking GPT, but I can’t get an answer.

Please let me know if I missed or doing wrong anything, thanks.

My code :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> ** void CMFCApplication20Dlg::OnBnClickedButtonLeftParen()**
{
CString temp;
GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp);
temp += _T("("); // 왼쪽 괄호 추가
SetDlgItemText(IDC_STATIC_VIEW, temp);
m_LeftParenCount++;
}
</code>
<code> ** void CMFCApplication20Dlg::OnBnClickedButtonLeftParen()** { CString temp; GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp); temp += _T("("); // 왼쪽 괄호 추가 SetDlgItemText(IDC_STATIC_VIEW, temp); m_LeftParenCount++; } </code>
 ** void CMFCApplication20Dlg::OnBnClickedButtonLeftParen()** 
{
    CString temp;
    GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp);
    temp += _T("("); // 왼쪽 괄호 추가
    SetDlgItemText(IDC_STATIC_VIEW, temp);
    m_LeftParenCount++;
} 
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>** void CMFCApplication20Dlg::OnBnClickedButtonRightParen()**
{
CString temp;
GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp);
if (m_LeftParenCount > m_RightParenCount)
{
temp += _T(")"); // 오른쪽 괄호 추가
SetDlgItemText(IDC_STATIC_VIEW, temp);
m_RightParenCount++;
}
else
{
AfxMessageBox(_T("Cannot add right parenthesis. Mismatched parentheses."));
}
}
</code>
<code>** void CMFCApplication20Dlg::OnBnClickedButtonRightParen()** { CString temp; GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp); if (m_LeftParenCount > m_RightParenCount) { temp += _T(")"); // 오른쪽 괄호 추가 SetDlgItemText(IDC_STATIC_VIEW, temp); m_RightParenCount++; } else { AfxMessageBox(_T("Cannot add right parenthesis. Mismatched parentheses.")); } } </code>
** void CMFCApplication20Dlg::OnBnClickedButtonRightParen()** 
{
    CString temp;
    GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp);
    if (m_LeftParenCount > m_RightParenCount)
    {
        temp += _T(")"); // 오른쪽 괄호 추가
        SetDlgItemText(IDC_STATIC_VIEW, temp);
        m_RightParenCount++;
    }
    else
    {
        AfxMessageBox(_T("Cannot add right parenthesis. Mismatched parentheses."));
    }
} 
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> ** void CMFCApplication20Dlg::OnBnClickedButtonEquals()**
{
CString temp;
GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp);
m_LeftParenCount = 0;
m_RightParenCount = 0;
AfterData = _ttof(temp); // 지수값(추가함)
// CString을 double로 변환 (소수점 지원)
double result = 0.0;
double beforeData = _ttof(ViewStrData); // ViewStrData를 CString에서 double로 변환
double afterData = _ttof(temp); // temp를 CString에서 double로 변환
// % 코드임
int percentPos = temp.Find(_T("%"));
if (percentPos != -1)
{
// `%` 기호가 있을 경우, 해당 기호를 기준으로 문자열 분리
CString numberStr = temp.Left(percentPos); // '%' 기호 이전 부분
double number = _ttof(numberStr); // 문자열을 double로 변환
double result = number / 100.0;
// 결과를 문자열로 변환하고 IDC_STATIC_VIEW에 표시
CString resultStr;
resultStr.Format(_T("%.2f"), result);
SetDlgItemText(IDC_STATIC_RESULT, resultStr);
ChangeCheck = true;
Sign = 0;
return;
}
// 계산 로직
switch (Sign)
{
case 1: result = beforeData + afterData; break;
case 2: result = beforeData - afterData; break;
case 3: result = beforeData * afterData; break;
case 4:
if (afterData != 0.0)
result = beforeData / afterData;
else
{
AfxMessageBox(_T("Zero division error!"));
return;
}
break;
case 5:
if (afterData != 0.0)
result = fmod(beforeData, afterData);
else
AfxMessageBox(_T("Zero division error!"));
break;
// 추가적인 연산자 처리...
default: AfxMessageBox(_T("Invalid operation!")); return;
}
// 결과를 결과 창에 표시
UpdateResultView(result);
GetDlgItem(IDC_STATIC_VIEW)->SetWindowText(_T(""));
ChangeCheck = true;
Sign = 0;
}
</code>
<code> ** void CMFCApplication20Dlg::OnBnClickedButtonEquals()** { CString temp; GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp); m_LeftParenCount = 0; m_RightParenCount = 0; AfterData = _ttof(temp); // 지수값(추가함) // CString을 double로 변환 (소수점 지원) double result = 0.0; double beforeData = _ttof(ViewStrData); // ViewStrData를 CString에서 double로 변환 double afterData = _ttof(temp); // temp를 CString에서 double로 변환 // % 코드임 int percentPos = temp.Find(_T("%")); if (percentPos != -1) { // `%` 기호가 있을 경우, 해당 기호를 기준으로 문자열 분리 CString numberStr = temp.Left(percentPos); // '%' 기호 이전 부분 double number = _ttof(numberStr); // 문자열을 double로 변환 double result = number / 100.0; // 결과를 문자열로 변환하고 IDC_STATIC_VIEW에 표시 CString resultStr; resultStr.Format(_T("%.2f"), result); SetDlgItemText(IDC_STATIC_RESULT, resultStr); ChangeCheck = true; Sign = 0; return; } // 계산 로직 switch (Sign) { case 1: result = beforeData + afterData; break; case 2: result = beforeData - afterData; break; case 3: result = beforeData * afterData; break; case 4: if (afterData != 0.0) result = beforeData / afterData; else { AfxMessageBox(_T("Zero division error!")); return; } break; case 5: if (afterData != 0.0) result = fmod(beforeData, afterData); else AfxMessageBox(_T("Zero division error!")); break; // 추가적인 연산자 처리... default: AfxMessageBox(_T("Invalid operation!")); return; } // 결과를 결과 창에 표시 UpdateResultView(result); GetDlgItem(IDC_STATIC_VIEW)->SetWindowText(_T("")); ChangeCheck = true; Sign = 0; } </code>
 ** void CMFCApplication20Dlg::OnBnClickedButtonEquals()** 
{
    CString temp;
    GetDlgItem(IDC_STATIC_VIEW)->GetWindowText(temp);
    m_LeftParenCount = 0;
    m_RightParenCount = 0;
    AfterData = _ttof(temp); // 지수값(추가함)

    // CString을 double로 변환 (소수점 지원)
    double result = 0.0;
    double beforeData = _ttof(ViewStrData); // ViewStrData를 CString에서 double로 변환
    double afterData = _ttof(temp); // temp를 CString에서 double로 변환

    // % 코드임 
    int percentPos = temp.Find(_T("%"));
    if (percentPos != -1)
    {
        // `%` 기호가 있을 경우, 해당 기호를 기준으로 문자열 분리
        CString numberStr = temp.Left(percentPos); // '%' 기호 이전 부분
        double number = _ttof(numberStr); // 문자열을 double로 변환
        double result = number / 100.0;

        // 결과를 문자열로 변환하고 IDC_STATIC_VIEW에 표시
        CString resultStr;
        resultStr.Format(_T("%.2f"), result);
        SetDlgItemText(IDC_STATIC_RESULT, resultStr);
        ChangeCheck = true;
        Sign = 0;
        return;
    }

    // 계산 로직
    switch (Sign)
    {
        case 1: result = beforeData + afterData; break;
        case 2: result = beforeData - afterData; break;
        case 3: result = beforeData * afterData; break;
        case 4: 
            if (afterData != 0.0)
                result = beforeData / afterData;
            else
            {
                AfxMessageBox(_T("Zero division error!"));
                return;
            }
            break;
        case 5: 
            if (afterData != 0.0)
                result = fmod(beforeData, afterData);
            else
                AfxMessageBox(_T("Zero division error!"));
            break;
        // 추가적인 연산자 처리...
        default: AfxMessageBox(_T("Invalid operation!")); return;
    } 

    // 결과를 결과 창에 표시
    UpdateResultView(result);
    GetDlgItem(IDC_STATIC_VIEW)->SetWindowText(_T(""));
    ChangeCheck = true;
    Sign = 0;
} 

New contributor

user26760798 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật