slogan 专业知识问答平台!
佰学网 >生活问答 > 知识 > 如何用C编写计算器如何用C编写计算器,回答

如何用C编写计算器如何用C编写计算器,回答

原创 2022-03-04 16:55:13
思路:用一个变量来标记操作符(-*/),intflag=0;?//1为加,2为减.. 精选答案
  • 问题相关

思路:

用一个变量来标记操作符( - * /),int flag=0; ? //1为加,2为减,3为乘,4为除,0为未按操作符。

用两个CString变量来存储第一个和第二个操作数,注意如果为除时要检测第二个操作数是否为0。

随时检测输入框edit中输入的内容,最多只能有一个小数点,首先按小数点时显示为“0。”

另注意一些细节:连续操作,结果数如果有小数点,末尾为0的要全部消除0等等

实现:

edit控件:增加控件变量:m_edit

新建上述button 分别更改为:IDC_0。

。。。。。。。IDC_9, IDC_AC, IDC_C , IDC_CHU(除) , IDC_C(乘) , IDC_J(减) , IDC_ADD(加) , IDC_DOT(点) , IDC_DY(等于)

全局变量:?

CString str1, str;

int flag = 0; //0为无,1为加,2为减,3为乘,4为除

bool bflag = false; //输入是否为第二个操作数

代码如下:

void CcalcDlg::OnBnClickedAc() //AC

{

flag = 0;

bflag = false;

str1 = _T("");

str = _T("");

m_edit。

SetWindowTextW(_T(""));

}

void CcalcDlg::OnBnClickedC() //C

{

OnBnClickedAc();

}

void CcalcDlg::OnBnClicked1() //1

{

if (bflag==true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("1");

}

else

{

str = str _T("1");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked2() //2

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("2");

}

else

{

str = str _T("2");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked3() //3

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("3");

}

else

{

str = str _T("3");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked4() //4

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("4");

}

else

{

str = str _T("4");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked5() //5

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("5");

}

else

{

str = str _T("5");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked6() //6

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("6");

}

else

{

str = str _T("6");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked7() //7

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("7");

}

else

{

str = str _T("7");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked8() //8

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("8");

}

else

{

str = str _T("8");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked9() //9

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str == _T("0"))

{

str = _T("9");

}

else

{

str = str _T("9");

}

m_edit。

SetWindowTextW(str);

}

void CcalcDlg::OnBnClicked0() //0

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str != _T("0"))

{

str = str _T("0");

m_edit。

SetWindowTextW(str);

}

}

void CcalcDlg::OnBnClickedDot() //点

{

if (bflag == true)

{

str1 = str;

str = _T("");

bflag = false;

m_edit。

SetWindowTextW(_T(""));

}

if (str。Find('。') != -1)

return;

if (str == _T("0") || str == _T(""))

{

str = _T("0。

");

}

else

{

str = str _T("。");

}

m_edit。SetWindowTextW(str);

}

void CcalcDlg::OnBnClickedChu() //除

{

if (str != _T(""))

{

flag = 4;

bflag = true;

}

}

void CcalcDlg::OnBnClickedCheng() //乘

{

if (str != _T(""))

{

flag = 3;

bflag = true;

}

}

void CcalcDlg::OnBnClickedJ() //减

{

if (str != _T(""))

{

bflag = true;

flag = 2;

}

}

void CcalcDlg::OnBnClickedAdd() //加

{

if (str != _T(""))

{

flag = 1;

bflag = true;

}

}

void CcalcDlg::OnBnClickedDy() //等于

{

double a, b;

b = _ttof(str);

a = _ttof(str1);

CString s;

switch (flag)

{

case 1: //加

s。

Format(_T("%f"), a b);

break;

case 2: //减

s。Format(_T("%f"), a - b);

break;

case 3: //乘

s。

Format(_T("%f"), a * b);

break;

case 4: //除

if (str != _T("0"))

{

s。Format(_T("%f"), a / b);

}

else

{

MessageBox(_T("除数为0"));

OnBnClickedAc();

}

break;

default:

s = _T("");

}

int f = s。

Find('。');

if (f != -1)

{

int i = s。GetLength();

while ('0' == s。GetAt(i - 1))

{

s。

Delete(i - 1, 1);

i--;

}

i = s。GetLength();

if ('。' == s。GetAt(i - 1))

{

s。

Delete(i - 1, 1);

}

}

m_edit。

SetWindowTextW(s);

str = _T("");

str1 = _T("");

flag = 0;

bflag = false;

}。

©本文版权归作者所有,任何形式转载请联系我们:xiehuiyue@offercoming.com。

相关内容推荐

来自百学网

刘老师

刘老师

中国人大教育学硕士

关注

你可能关心