用c++实现有界面的计算器,求源代码

如题所述

第1个回答  推荐于2016-05-06
我用的是MFC实现的,功能基本上都有了,而且还加了矩阵以及行列式的计算,你适当修改吧。不过这个东西最好自己做才有收获。
下面的代码是核心的部分的代码,我已经写了很久了,所以很多东西都忘记了,还好有注释,最好自己做吧,有了思想,什么都好解决。
你要是有邮箱的话我打包给你,毕竟这个只是一部分,其它的调用你是看不到的。
// MyDialog.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "MyDialog.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MyDialog dialog
CString str="dddd";
char num[200]; //记录输入的数据
int count=0; //记录数据个数,回退时要减一
char a; //记录操作符
double x,y; //两个操作数
double result; //得到结果
#define PI 3.141592653

MyDialog::MyDialog(CWnd* pParent /*=NULL*/)
: CDialog(MyDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(MyDialog)
str = _T("");
//}}AFX_DATA_INIT
}

void MyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(MyDialog)
DDX_Control(pDX, IDC_BUTTON2, m_2);
DDX_Control(pDX, IDC_BUTTON1, m_1);
DDX_Text(pDX, IDC_EDIT1, str);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(MyDialog, CDialog)
//{{AFX_MSG_MAP(MyDialog)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
ON_BN_CLICKED(IDC_BUTTON14, OnButton14)
ON_BN_CLICKED(IDC_BUTTON15, OnButton15)
ON_BN_CLICKED(IDC_BUTTON16, OnButton16)
ON_NOTIFY(MCN_GETDAYSTATE, IDC_MONTHCALENDAR1, OnGetdaystateMonthcalendar1)
ON_BN_CLICKED(IDC_BUTTON17, OnButton17)
ON_BN_CLICKED(IDC_BUTTON18, OnButton18)
ON_BN_CLICKED(IDC_BUTTON19, OnButton19)
ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
ON_BN_CLICKED(IDC_BUTTON20, OnButton20)
ON_BN_CLICKED(IDC_BUTTON21, OnButton21)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// MyDialog message handlers
void MyDialog::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here
// GetDlgItem(IDC_EDIT1)-> SetWindowText(str);
// CEdit* pBoxOne;
// pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);
// pBoxOne->SetWindowText(str);
}

void MyDialog::OnButton1()
{
// TODO: Add your control notification handler code here
str=str+"1";
num[count]='1';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton2()
{
// TODO: Add your control notification handler code here
str=str+"2";
num[count]='2';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);

}

void MyDialog::OnButton3()
{
// TODO: Add your control notification handler code here
num[count]='3';
++count;
str=str+"3";
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton4()
{
// TODO: Add your control notification handler code here
num[count]='4';
++count;
str=str+"4";
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton5()
{
// TODO: Add your control notification handler code here
str=str+"5";
num[count]='5';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton6()
{
// TODO: Add your control notification handler code here
str=str+"6";
num[count]='6';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton7()
{
// TODO: Add your control notification handler code here
str=str+"7";
num[count]='7';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton8()
{
// TODO: Add your control notification handler code here
str=str+"8";
num[count]='8';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton9()
{
// TODO: Add your control notification handler code here
str=str+"9";
num[count]='9';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton10()
{
// TODO: Add your control notification handler code here
str=str+"0";
num[count]='0';
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton11()
{
// TODO: Add your control notification handler code here
str=str+".";
++count;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton12() //等号
{
// TODO: Add your control notification handler code here
char c;
c=a;
y=0;
for(int i=0;i<count;++i)
{
y=(num[i]-48)+y*10;
}
count=0;
switch(c)
{
case'+':
result=x+y;break;
case'-':
result=x-y;break;
case'*':
result=x*y;break;
case'/':
{
if(y==0)
{
str="除数为0 ! 出错 !";
goto Lable;
}
result=x/y;break;
}
default:
break;
}
str.Format("%f",result);
Lable:
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
str="";
}

void MyDialog::OnButton13()
{
// TODO: Add your control notification handler code here
str=str+"+";
a='+';
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
count=0;
// str.Format("%f",x);
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton14()
{
// TODO: Add your control notification handler code here
str=str+"-";
a='-';
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
count=0;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton15()
{
// TODO: Add your control notification handler code here
str=str+"*";
a='*';
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
count=0;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton16()
{
// TODO: Add your control notification handler code here
str=str+"/";
a='/';
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
count=0;
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
}

void MyDialog::OnButton17()
{
// TODO: Add your control notification handler code here
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
x=x*PI/180; //转换为弧度
result=sin(x);
str.Format("%f",result);
GetDlgItem(IDC_EDIT1)->SetWindowText(str); //在EDIT文本框中显示数据
count=0;
str="";
}

void MyDialog::OnButton18()
{
// TODO: Add your control notification handler code here
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
x=x*PI/180; //转换为弧度
result=cos(x);
str.Format("%f",result);
GetDlgItem(IDC_EDIT1)->SetWindowText(str); //在EDIT文本框中显示数据
count=0;
str="";
}

void MyDialog::OnButton19()
{
// TODO: Add your control notification handler code here
x=0;
for(int i=0;i<count;++i)
{
x=(num[i]-48)+x*10;
}
x=x*PI/180; //转换为弧度
result=tan(x);
str.Format("%f",result);
GetDlgItem(IDC_EDIT1)->SetWindowText(str); //在EDIT文本框中显示数据
count=0;
str="";
}

void MyDialog::OnGetdaystateMonthcalendar1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here

*pResult = 0;
}

///有关于求逆矩阵的函数
int numbers=0; //记录矩阵元素的个数

void MyDialog::OnChangeEdit2()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here

}

void MyDialog::OnButton20()
{
// TODO: Add your control notification handler code here

int i=0,j=0;
CString str1="";
//*************************统计整数个数!************************
GetDlgItem(IDC_EDIT2)->GetWindowText(str);
while(str[i]!=';')
{
++i;
if(str[i]==',')
{
++j;
}
}
++j;
numbers=j;
int n;
if(Judge(numbers)) //矩阵元素个数符合要求
{
// str.Format("%d",numbers);
// GetDlgItem(IDC_EDIT1)->SetWindowText(str);
n=sqrt(numbers);
}
else
{
str1="元素个数有误 ! 请在已经输入的基础上改正 !";
GetDlgItem(IDC_EDIT1)->SetWindowText(str1);
str1="";
}
//*************************************************

}

bool MyDialog::Judge(int numbers) //判断矩阵的元素个数是否符合条件,及是否是n X n的矩阵
{
if(numbers!=int(sqrt(numbers))*int(sqrt(numbers)))
{
return 0;
}
else
return 1;
}

void Hanglieshi(double **a,int N); //求行列式的值,使用以前的代码,在函数参数方面有点调整
void plus(double **a,double **b,double **c,int m,int n);//m x n 的矩阵相加
void multiply(double **a,double **b,double **result,int m,int n,int l);//m x n 的矩阵 与 n x l 的矩阵的乘积
double **allocation(int m,int n); //动态分配二维数组,大小:m x n
void swap(double **a,int m,int n,int N); //交换m行和n行的内容
void add(double **a,int m,int n,int N); //把第n行加到第m行上面
void jian(double **a,int m,int n,double x,int N); //用m行减去n行的k倍
int judgeZero(double **a,int j,int N); //返回j列没有0的元素的位置
void dealZero(double **a,int N); //判断主对角线是否有值为0的元素,如果有,则加上对应列上没有0的行
void dealArray(double **a,int N); //将行列式化为上三角行列式
double jieguo(double **a,int N); //主对角线元素之积,即行列式的值

void MyDialog::OnButton21() //求行列式的值
{
// TODO: Add your control notification handler code here
int i=0,j=0;
double res;
CString str1="";
CString str2="";
//*************************统计整数个数!************************
GetDlgItem(IDC_EDIT2)->GetWindowText(str1); //str1为文本框中的数据字符串
while(str1[i]!=';')
{
++i; //统计总字符个数
if(str1[i]==',')
{
++j; //统计整数个数
}
}
++j;
int N=i;
numbers=j;

int num;
if(Judge(numbers)) //矩阵元素个数符合要求
{
str.Format("%d",numbers);
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
num=sqrt(numbers);
double **a; // 动态二维数组
double var; //转换使用的变量
int m,n,l;
m=n=num;
a=allocation(m,n);
//***************************无法解决转换中出现的问题,设置临时一维数组参与转换,然后再赋值到二维数组中
double *Arr=new double[numbers];
//
m=0;
n=0;
str2=str1;
i=0;
while(1) //对数组进行赋值
{
if(str2[n]!=',')
{
var=atoi(str2);
Arr[m]=var;
++m;
++n;
}
else
{
str2="";
for(l=i;l<N;++l) //将文本框里面的已经转换的数据去掉,取其余下的部分
{
str2=str2+str1[l];
}
n=0;
}
++i;
if(i==N+1)
break;
}
// str.Format("%f",Arr[1]); //调试时使用
// GetDlgItem(IDC_EDIT1)->SetWindowText(str);

m=0;
for(i=0;i<num;++i)
{
for(j=0;j<num;++j)
{
a[i][j]=Arr[m];
++m;
}
}

// str.Format("%f",Arr[3]); //调试时使用
// GetDlgItem(IDC_EDIT1)->SetWindowText(str);

n=num;
Hanglieshi(a,n);
var=jieguo(a,n);
str.Format("%f",var);
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
delete []Arr;
}
else
{
str1="元素个数有误 ! 请在已经输入的基础上改正 !";
GetDlgItem(IDC_EDIT1)->SetWindowText(str1);
str1="";
}
}

//*******************行列式求值部分
/*void print(double **a,int m,int n)//输出m x n 的矩阵的内容
{
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
cout<<setw(3)<<a[i][j];
}
cout<<endl;
}
}
*/
void plus(double **a,double **b,double **c,int m,int n)//m x n 的矩阵相加
{
for(int i=0;i<m;++i)
{
for(int j=0;j<n;++j)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}

void multiply(double **a,double **b,double **result,int m,int n,int l)//m x n 的矩阵 与 n x l 的矩阵的乘积
{
// cout<<"两个矩阵相乘的结果为: "<<endl;
int i,j,k;
for(i=0;i<m;++i)
{
for(j=0;j<l;++j)
{
result[i][j]=0;
for(k=0;k<n;++k)
{
result[i][j]=result[i][j]+a[i][k]*b[k][j];
}
}
}
}

double **allocation(int m,int n) //动态分配二维数组,大小:m x n
{
double **a;
a=new double *[m];
for(int i=0;i<m;++i)
{
a[i]=new double [n];
}
return a;
}
/*
void printArray(double **a,int N) //输出矩阵内容
{
for(int i =0 ; i<N ; ++i )
{
for(int j = 0; j<N; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
}
*/
void swap(double **a,int m,int n,int N) //交换m行和n行的内容
{
int i,j;
double *temp=new double[N];
for(j=0;j<N;++j)
{
temp[j]=a[m][j];
a[m][j]=a[n][j];
a[n][j]=temp[j];
}

}

void add(double **a,int m,int n,int N) //把第n行加到第m行上面
{
int i;
for(i=0;i<N;++i)
{
a[m][i]+=a[n][i];
}
}

void jian(double **a,int m,int n,double x,int N) //用m行减去n行的k倍
{
for(int j=0;j<N;++j)
{
a[m][j]=a[m][j]-x*a[n][j];
}
}

int judgeZero(double **a,int j,int N) //返回j列没有0的元素的位置
{
int i;
for(i=0;i<N;++i)
{
if(a[i][j]!=0)
return i;
}
return -1; //如果某一列都为0的话
}

void dealZero(double **a,int N) //判断主对角线是否有值为0的元素,如果有,则加上对应列上没有0的行
{
int j,m=-1;
for(j=0;j<N;++j)
{
if(a[j][j]==0)
{
m=judgeZero(a,j,N);
if(m==-1)
;
// cout<<"行列式的值为0 !"<<endl;
else
{
add(a,j,m,N);
}
}
}
}

void dealArray(double **a,int N) //将行列式化为上三角行列式
{
int i,j;
double x;
for(i=0;i<N-1;++i)
{
for(j=i+1;j<N;++j)
{
if(a[i][i]==0)
break;
x=a[j][i]/a[i][i];
jian(a,j,i,x,N);
}
}
}

double jieguo(double **a,int N) //主对角线元素之积,即行列式的值
{
double r=1;
for(int i=0;i<N;++i)
r*=a[i][i];
return r;
}

void Hanglieshi(double **a,int N) //求行列式的值,使用以前的代码,在函数参数方面有点调整
{
// printArray(a,N);
dealZero(a,N);

dealArray(a,N);
// printArray(a,N);

// cout<<jieguo(a,N)<<endl<<endl;
}本回答被提问者采纳
相似回答