Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CalDlg.cpp: файл реализации
- //
- #include "pch.h"
- #include <iostream>
- #include "framework.h"
- #include "Cal.h"
- #include "CalDlg.h"
- #include "afxdialogex.h"
- #include <string>
- #include <iomanip>
- #include <map>
- #include <cmath>
- #include <cstring>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- CString SVvod;
- bool Point = 0;
- double Num1 = 0;
- double Num2 = 0;
- int iAction = 0;
- bool NotAction = 0;
- bool bAction = 0;
- double number(std::string&, unsigned&);
- double identifier(std::string&, unsigned&);
- double function(std::string&, std::string&, unsigned&);
- double base(std::string&, unsigned&);
- double term(std::string&, unsigned&);
- double factor(std::string&, unsigned&);
- double expr(std::string&, unsigned&);
- double calc(std::string&);
- std::map< std::string, double > jim;
- // Диалоговое окно CAboutDlg используется для описания сведений о приложении
- class CAboutDlg : public CDialogEx
- {
- public:
- CAboutDlg();
- // Данные диалогового окна
- #ifdef AFX_DESIGN_TIME
- enum { IDD = IDD_ABOUTBOX };
- #endif
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // поддержка DDX/DDV
- // Реализация
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
- END_MESSAGE_MAP()
- // Диалоговое окно CCalDlg
- CCalDlg::CCalDlg(CWnd* pParent /*=nullptr*/)
- : CDialogEx(IDD_CAL_DIALOG, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CCalDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_EDIT1, _Edit);
- DDX_Control(pDX, IDC_LIST2, _List);
- }
- BEGIN_MESSAGE_MAP(CCalDlg, CDialogEx)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON3, &CCalDlg::OnBnClickedButton3)
- ON_BN_CLICKED(IDC_BUTTON1, &CCalDlg::OnBnClickedButton1)
- ON_BN_CLICKED(IDC_BUTTON2, &CCalDlg::OnBnClickedButton2)
- ON_BN_CLICKED(IDC_BUTTON9, &CCalDlg::OnBnClickedButton9)
- ON_BN_CLICKED(IDC_BUTTON8, &CCalDlg::OnBnClickedButton8)
- ON_BN_CLICKED(IDC_BUTTON7, &CCalDlg::OnBnClickedButton7)
- ON_BN_CLICKED(IDC_BUTTON6, &CCalDlg::OnBnClickedButton6)
- ON_BN_CLICKED(IDC_BUTTON5, &CCalDlg::OnBnClickedButton5)
- ON_BN_CLICKED(IDC_BUTTON4, &CCalDlg::OnBnClickedButton4)
- ON_BN_CLICKED(IDC_BUTTON10, &CCalDlg::OnBnClickedButton10)
- ON_BN_CLICKED(IDC_BUTTON11, &CCalDlg::OnBnClickedButton11)
- ON_BN_CLICKED(IDC_BUTTON12, &CCalDlg::ActionPlus)
- ON_BN_CLICKED(IDC_BUTTON13, &CCalDlg::ActionMinus)
- ON_BN_CLICKED(IDC_BUTTON15, &CCalDlg::ActionMult)
- ON_BN_CLICKED(IDC_BUTTON14, &CCalDlg::ActionDel)
- ON_BN_CLICKED(IDC_BUTTON16, &CCalDlg::ActionRavno)
- ON_BN_CLICKED(IDC_BUTTON17, &CCalDlg::ActionClear)
- ON_BN_CLICKED(IDC_BUTTON18, &CCalDlg::PlusMin)
- ON_BN_CLICKED(IDC_BUTTON20, &CCalDlg::Backspace)
- ON_BN_CLICKED(IDC_BUTTON19, &CCalDlg::SkobkaLeft)
- ON_BN_CLICKED(IDC_BUTTON21, &CCalDlg::SkobkaRight)
- ON_BN_CLICKED(IDC_BUTTON23, &CCalDlg::AlgPlus)
- ON_BN_CLICKED(IDC_BUTTON25, &CCalDlg::AlgMinus)
- ON_BN_CLICKED(IDC_BUTTON22, &CCalDlg::AlgMult)
- ON_BN_CLICKED(IDC_BUTTON24, &CCalDlg::AltDel)
- ON_BN_CLICKED(IDC_BUTTON26, &CCalDlg::AlgRavno)
- END_MESSAGE_MAP()
- // Обработчики сообщений CCalDlg
- BOOL CCalDlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- // Добавление пункта "О программе..." в системное меню.
- // IDM_ABOUTBOX должен быть в пределах системной команды.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != nullptr)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Задает значок для этого диалогового окна. Среда делает это автоматически,
- // если главное окно приложения не является диалоговым
- SetIcon(m_hIcon, TRUE); // Крупный значок
- SetIcon(m_hIcon, FALSE); // Мелкий значок
- // TODO: добавьте дополнительную инициализацию
- return TRUE; // возврат значения TRUE, если фокус не передан элементу управления
- }
- void CCalDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialogEx::OnSysCommand(nID, lParam);
- }
- }
- // При добавлении кнопки свертывания в диалоговое окно нужно воспользоваться приведенным ниже кодом,
- // чтобы нарисовать значок. Для приложений MFC, использующих модель документов или представлений,
- // это автоматически выполняется рабочей областью.
- void CCalDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // контекст устройства для рисования
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // Выравнивание значка по центру клиентского прямоугольника
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Нарисуйте значок
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- }
- }
- // Система вызывает эту функцию для получения отображения курсора при перемещении
- // свернутого окна.
- HCURSOR CCalDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- double number(std::string& str, unsigned& index)
- {
- double result = 0.0;
- char digit;
- double k = 10.0;
- while (index < str.length())
- {
- digit = str[index++];
- if (digit >= '0' && digit <= '9')
- result = result * 10.0 + (digit - '0');
- else
- {
- --index;
- break;
- }
- }
- if (index < str.length())
- digit = str[index++];
- if (digit == '.')
- {
- while (index < str.length())
- {
- digit = str[index++];
- if (digit >= '0' && digit <= '9')
- {
- result += (digit - '0') / k;
- k *= 10.0;
- }
- else
- {
- --index;
- break;
- }
- }
- }
- else
- --index;
- return result;
- }
- double function(std::string& name, std::string& str, unsigned& index)
- {
- double argument = expr(str, index);
- if (strcmp(name.c_str(), "acos") == 0)
- return acos(argument);
- if (strcmp(name.c_str(), "asin") == 0)
- return asin(argument);
- if (strcmp(name.c_str(), "atan") == 0)
- return atan(argument);
- if (strcmp(name.c_str(), "cos") == 0)
- return cos(argument);
- if (strcmp(name.c_str(), "cosh") == 0)
- return cosh(argument);
- if (strcmp(name.c_str(), "exp") == 0)
- return exp(argument);
- if (strcmp(name.c_str(), "log") == 0)
- return log(argument);
- if (strcmp(name.c_str(), "log10") == 0)
- return log10(argument);
- if (strcmp(name.c_str(), "sin") == 0)
- return sin(argument);
- if (strcmp(name.c_str(), "sinh") == 0)
- return sinh(argument);
- if (strcmp(name.c_str(), "sqrt") == 0)
- return sqrt(argument);
- if (strcmp(name.c_str(), "tan") == 0)
- return tan(argument);
- if (strcmp(name.c_str(), "tanh") == 0)
- return tanh(argument);
- exit(-1);
- }
- double identifier(std::string& str, unsigned& index)
- {
- std::string name = "";
- double result;
- while (index < str.length() &&
- ((str[index] >= 'a' && str[index] <= 'z') ||
- (str[index] >= 'A' && str[index] <= 'Z') ||
- (str[index] >= '0' && str[index] <= '9') ||
- (str[index] == '_')))
- name += str[index++];
- if (index < str.length() && str[index] == '(')
- {
- ++index;
- result = function(name, str, index);
- if (index >= str.length() || str[index] != ')')
- {
- exit(-1);
- }
- ++index;
- }
- else
- {
- double var;
- std::cin >> var;
- jim.insert(std::pair< const std::string, double >(name, var));
- result = jim[name];
- }
- return result;
- }
- double base(std::string& str, unsigned& index)
- {
- double result;
- if (index >= str.length())
- {
- exit(-1);
- }
- if (str[index] == '(')
- {
- ++index;
- result = expr(str, index);
- if (index >= str.length() || str[index] != ')')
- {
- exit(-1);
- }
- ++index;
- }
- else
- {
- if (str[index] >= '0' && str[index] <= '9')
- result = number(str, index);
- else
- {
- if ((str[index] >= 'A' && str[index] <= 'Z') ||
- (str[index] >= 'a' && str[index] <= 'z') ||
- (str[index] == '_'))
- result = identifier(str, index);
- else
- {
- exit(-1);
- }
- }
- }
- return result;
- }
- double expr(std::string& str, unsigned& index)
- {
- double result;
- char operation;
- result = term(str, index);
- while (index < str.length() &&
- (str[index] == '+' || str[index] == '-'))
- {
- operation = str[index];
- ++index;
- switch (operation)
- {
- case '+':
- result += term(str, index);
- break;
- case '-':
- result -= term(str, index);
- break;
- }
- }
- return result;
- }
- double term(std::string& str, unsigned& index)
- {
- double result;
- char operation;
- double div;
- result = factor(str, index);
- while (index < str.length() &&
- (str[index] == '*' || str[index] == '/'))
- {
- operation = str[index];
- ++index;
- switch (operation)
- {
- case '*':
- result *= factor(str, index);
- break;
- case '/':
- div = factor(str, index);
- if (div == 0.0)
- {
- exit(-1);
- }
- result /= div;
- break;
- }
- }
- return result;
- }
- double factor(std::string& str, unsigned& index)
- {
- double result;
- if (index >= str.length())
- {
- exit(-1);
- }
- switch (str[index])
- {
- case '+':
- ++index;
- result = factor(str, index);
- break;
- case '-':
- ++index;
- result = -factor(str, index);
- break;
- default:
- result = base(str, index);
- if (index <= str.length() - 1 && str[index] == '^')
- {
- ++index;
- result = pow(result, factor(str, index));
- }
- }
- return result;
- }
- double calc(std::string& str)
- {
- unsigned index = 0;
- double result = expr(str, index);
- if (index < str.length() - 1)
- {
- exit(-1);
- }
- return result;
- }
- void CCalDlg::OnBnClickedButton1()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"1";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"1");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton2()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"2";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"2");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton3()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"3";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"3");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton9()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"4";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"4");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton8()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"5";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"5");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton7()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"6";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"6");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton6()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"7";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"7");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton5()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"8";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"8");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton4()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"9";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"9");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton10()
- {
- if (SVvod == L"0" || NotAction == 1)
- {
- SVvod = L"0";
- NotAction = 0;
- }
- else {
- SVvod.Append(L"0");
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::OnBnClickedButton11()
- {
- if (SVvod.GetLength() != 0 && Point==0) {
- SVvod.Append(L".");
- Point = 1;
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::ActionPlus()
- {
- if (bAction == 1)
- {
- ActionRavno();
- }
- else
- {
- Num1 = _wtof(SVvod);
- }
- iAction = 1;
- NotAction = 1;
- Point = 0;
- bAction = 1;
- }
- void CCalDlg::ActionMinus()
- {
- if (bAction == 1)
- {
- ActionRavno();
- }
- else
- {
- Num1 = _wtof(SVvod);
- }
- iAction = 2;
- NotAction = 1;
- Point = 0;
- bAction = 1;
- }
- void CCalDlg::ActionMult()
- {
- if (bAction == 1)
- {
- ActionRavno();
- }
- else
- {
- Num1 = _wtof(SVvod);
- }
- iAction = 3;
- NotAction = 1;
- Point = 0;
- bAction = 1;
- }
- void CCalDlg::ActionDel()
- {
- if (bAction == 1)
- {
- ActionRavno();
- }
- else
- {
- Num1 = _wtof(SVvod);
- }
- iAction = 4;
- NotAction = 1;
- Point = 0;
- bAction = 1;
- }
- void CCalDlg::ActionRavno()
- {
- double ravno = 0;
- CString sList;
- Num2 = _wtof(SVvod);
- if (iAction == 1)
- {
- ravno = Num1 + Num2;
- sList.Format(L"%g + %g = %g", Num1, Num2, ravno);
- }
- if (iAction == 2)
- {
- ravno = Num1 - Num2;
- sList.Format(L"%g - %g = %g", Num1, Num2, ravno);
- }
- if (iAction == 3)
- {
- ravno = Num1 * Num2;
- sList.Format(L"%g * %g = %g", Num1, Num2, ravno);
- }
- if (iAction == 4)
- {
- if (Num2 == 0) { AfxMessageBox(L"#Error!# Division by 0 is not possible"); sList.Format(L"%g / %g = Error division by 0", Num1, Num2);}
- else {
- ravno = Num1 / Num2;
- sList.Format(L"%g / %g = %g", Num1, Num2, ravno);
- }
- }
- SVvod.Format(L"%g", ravno);
- _Edit.SetWindowTextW(SVvod);
- _List.AddString(sList);
- bAction = 0;
- NotAction = 1;
- }
- void CCalDlg::ActionClear()
- {
- iAction = 0;
- SVvod = L"";
- Num1 = 0;
- Num2 = 0;
- Point = 0;
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::PlusMin()
- {
- if (SVvod.GetLength() > 0 && SVvod != L"0") {
- double mNum = _wtof(SVvod);
- mNum *= -1;
- SVvod.Format(L"%g", mNum);
- }
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::Backspace()
- {
- SVvod.Delete(SVvod.GetLength() - 1, 1);
- if (SVvod.GetLength() == 0) SVvod = L"0";
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::SkobkaLeft()
- {
- SVvod.Append(L"(");
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::SkobkaRight()
- {
- SVvod.Append(L")");
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::AlgPlus()
- {
- SVvod.Append(L"+");
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::AlgMinus()
- {
- SVvod.Append(L"-");
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::AlgMult()
- {
- SVvod.Append(L"*");
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::AltDel()
- {
- SVvod.Append(L"/");
- _Edit.SetWindowTextW(SVvod);
- }
- void CCalDlg::AlgRavno()
- {
- CString Сroaton;
- Сroaton = SVvod;
- std::string str{ CStringA{Сroaton} };
- double AlgResult = calc(str);
- SVvod.Format(L"%g", AlgResult);
- _Edit.SetWindowTextW(SVvod);
- }
Advertisement
Add Comment
Please, Sign In to add comment