用C++编写计算器

用C++编写计算器能够实现加减乘出、三角函数、指数函数、对数函数、倒数... 用C++编写计算器能够实现加减乘出、三角函数、指数函数、对数函数、倒数 展开
 我来答
叶早早回扉0
推荐于2016-06-10 · TA获得超过2.5万个赞
知道大有可为答主
回答量:5625
采纳率:92%
帮助的人:505万
展开全部

思路:

用一个变量来标记操作符(+ - * /),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;
}

匿名用户
2013-07-15
展开全部
阁下的问题过于宽泛了,你没有指定使用 什么图形API,只是指定了语言是没有意义的,比如你是要使用MFC框架还是Qt框架等等,你应该指定才行
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-15
展开全部
#include<iostream>
#include<math.h>
#include<string>
#define PI 3.14
using namespace std;
void choice_menu(); //函数声明
char menu();
void sanjiao();
void calcur();
void duishu();
void zhishu();
int main()
{
choice_menu();
return 0;
}
char menu()
{
cout<<"\t\t----------欢迎使用----------"<<endl;
cout<<"\t\t\t*********计算器应用程序********"<<endl;
cout<<"\t\t\t1.数值运算\n"
<<"\t\t\t2.三角函数运算\n"
<<"\t\t\t3.指数运算\n"
<<"\t\t\t4.对数运算\n"
<<"\t\t\t5.结束运行\n"
<<"\n\t\t左边数字对应功能的选择,请选择1-5:";
string str;
char choice;
while(true)
{
cin>>str;
cin.ignore();
choice=str[0];
if(choice<'0'||choice>'5')
cout<<"\n输入错误,重选1-5:";
else
break;
}
return choice;
}
void choice_menu()
{
while(true)
{
switch(menu())
{
case '1':
calcur();
break;
case '2':
sanjiao();
break;
case '3':
duishu();
break;
case '4':
zhishu();
break;
case '5':
exit(1);
break;
}

}
}
void calcur() //常数运算
{
double a,b;
char c;
cout<<"请输入第一个操作数:";
cin>>a;
cout<<"输入一个运算符:";
cin>>c;
cout << "请输入第二个操作数:";
cin >> b;
while (c != 'Q')
{
switch(c)
{
case 'C': a = 0.0;break;
case '+': cout<<a<<c<<b<<"="<<a+b<<endl;break;
case '-': cout<<a<<c<<b<<"="<<a-b<<endl;break;
case '*': cout<<a<<c<<b<<"="<<a*b<<endl;break;
case '/': cout<<a<<c<<b<<"="<<a/b<<endl;break;
default: cout << "不可理解的输入!";break;
}
break;
}
}
void sanjiao() //三角函数运算
{
double angle = 0;
double hudo = 0;
double a=0;
cout<<"Please Enter the number of the angular:(0<@<90) @ = ";
cin>>angle;
a = PI/180;
hudo = angle*a;
cout<<"a的正弦值为: "<<sin(hudo)<<endl;
cout<<"a的余弦值为: "<<cos(hudo)<<endl;
cout<<"a的正切值为: "<<tan(hudo)<<endl;
cout<<"a的余切值为: "<<1/tan(hudo)<<endl;
cout<<"*****************************************************"<<endl;
}
void duishu() //对数运算
{
int choice;
cout<<"1.底为e和10的运算\n"
<<"2.输入底为a,对数为b的运算\n"
<<"否则(结束输出,退出)"<<endl;
cout<<"请输入你的选择:";
cin>>choice;
if(choice== 1)
{
double x;
cout<<"x=";
cin>>x;
if(x>0)
{
cout<<"log(x)="<<log(x)<<endl; //log(x)表示以2为底,x的对数
cout<<"log10(x)="<<log10(x)<<endl; //log10(x)表示以10为底,x的对数
}
else
{
cout<<"底数不能为0"<<endl;
}
}
else if(choice==2)
{
unsigned int a ,b;
cout<<"请输入a、b的值:";
cin>>a>>b;
for(;;)
{
if(a<=0)
break;
else
{
if(b<0)
{
break;
}
else if(b==0)
{
cout<<"对数为:"<<0<<endl;
break;
}
else
{
double x,y,z;
x=log(b);
y=log(a);
z=x/y;
cout<<"对数为:"<<x/y<<endl;
break;
}

}
}
}
else
{
cout<<"结束输出"<<endl;

}
cout<<"*****************************************************"<<endl;
}
void zhishu()
{
double a,b;
cout<<"请输入a、b的值:";
cin>>a>>b;
cout<<"a^b="<<pow(a,b)<<endl;
cout<<"*****************************************************"<<endl;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
风筝lk人生
2015-11-01 · TA获得超过1.9万个赞
知道大有可为答主
回答量:7330
采纳率:67%
帮助的人:1355万
展开全部
// arithmeticAnalysis.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<stdlib.h>using namespace std;
#define MAX 100class Cstack
{ private:
char arr[MAX];// Contains all the Data
int top;
int c;
//Contains location of Topmost Data pushed onto Stack
public:
Cstack() //Constructor
{
top = -1; //Sets the Top Location to -1 indicating an empty stack
}
void push(char a) // Push ie. Add Value Function
{ top++; // increment to by 1
if(top < MAX)
{ arr[top] = a; //If Stack is Vacant store Value in Array
}
else
{ cout<<"STACK FULL!!"<<endl;
top--;
}
}
char pop() // Push ie. Add Value Function
{
top--; // increment to by 1
if(top >= -1)
{
c = top + 1;
return arr[c]; //If Stack is Vacant store Value in Array
}
else
return NULL;
/*
else
{
cout<<"STACK EMPTY!!"<<endl;
top++;
return NULL;
}
*/
}
bool isEmpty()
{
if (top <= -1)
return true;
else
return false; }
}stack1;
class Nstack
{ private:
float arr[MAX];// Contains all the Data
int top;
int c;
//Contains location of Topmost Data pushed onto Stack
public:
Nstack()
//Constructor
{ top=-1; //Sets the Top Location to -1 indicating an empty stack
}
void push(float a) // Push ie. Add Value Function
{ top++; // increment to by 1
if(top < MAX)
{ arr[top]=a; //If Stack is Vacant store Value in Array
}
else
{ cout<<"STACK FULL!!"<<endl;
top--;
}
}
float pop() // Push ie. Add Value Function
{
top--; // increment to by 1
if(top >= -1)
{
c = top + 1;
return arr[c]; //If Stack is Vacant store Value in Array

}

/*
else
{
cout<<"STACK EMPTY!!"<<endl;
top++;
return NULL;
}
*/

}
bool isEmpty()
{
if (top <= -1)
return true;
else
return false; }
}stack2;
int compute(char Opr)
{ if(Opr == '=')
{
cout<<stack2.pop()<<endl;
return 0;
}//输出结果
float Num1 = stack2.pop() ,Num2 = stack2.pop();

switch(Opr)
{
case '+':stack2.push(Num2+Num1);break; //结果压栈
case '-':stack2.push(Num2-Num1);break;
case '*':stack2.push(Num2*Num1);break;
case '/':stack2.push(Num2/Num1);break;

}
return 0;
}
/*
bool isDigit(char ch)
{
if(ch > '0' && ch < '10')
return true;
else
false;
}
*/int main(int argc, char* argv[])
{
cout<<"输入算术表达式:"<<endl;
char str[50];
bool b1 =true,b2 = true,b3 = true;
char ch,ch1;
int k = -1;

while(b1)
{
cin>>ch;
if((ch >= '0' && ch <= '9') || ch == '.')
{
k++;
str[k] = ch;
//cout<<ch;
}
else if(ch != '(' && b3)//第一个')'后面紧跟的那个个操作符在此会出问题
{
k++;
str[k] = '\0';
float a = atof(str);
stack2.push(a);
k = -1;
}
switch(ch)
{
case '(':b3 = true;stack1.push(ch);break;
case '+':
case '-':
b3 = true;
while(!stack1.isEmpty())
{
ch1 = stack1.pop();
if(ch1 == '(')
{
stack1.push(ch1);
break;
}
else
{
compute(ch1);
} }
stack1.push(ch);
break;
case '*':
case '/':
b3 = true;
while(!stack1.isEmpty() && b2)
{
ch1 = stack1.pop();
if(ch1 == '(')
{
stack1.push(ch1);
break;
}
else if(ch1 == '*' || ch1 == '/')
{
compute(ch1);
}
else
{
stack1.push(ch1);
b2 = false;
}
}
stack1.push(ch);
b2 = true;
break;
case ')':
b3 = false;
ch1 = stack1.pop();
while(ch1 != '(')
{

compute(ch1);
ch1 = stack1.pop(); }
break;
case '=' :
while(!stack1.isEmpty())
{
ch1=stack1.pop();
if(ch1 == '(')
{
cout<<"Enter is error!"<<endl;
break;
}
else
compute(ch1);
}
compute(ch);
break; }
}
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-07-15
展开全部
// arithmeticAnalysis.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include<iostream>
#include<math.h>
#include<stdlib.h>using namespace std;
#define MAX 100class Cstack
{ private:
char arr[MAX];// Contains all the Data
int top;
int c;
//Contains location of Topmost Data pushed onto Stack
public:
Cstack() //Constructor
{
top = -1; //Sets the Top Location to -1 indicating an empty stack
}
void push(char a) // Push ie. Add Value Function
{ top++; // increment to by 1
if(top < MAX)
{ arr[top] = a; //If Stack is Vacant store Value in Array
}
else
{ cout<<"STACK FULL!!"<<endl;
top--;
}
}
char pop() // Push ie. Add Value Function
{
top--; // increment to by 1
if(top >= -1)
{
c = top + 1;
return arr[c]; //If Stack is Vacant store Value in Array
}
else
return NULL;
/*
else
{
cout<<"STACK EMPTY!!"<<endl;
top++;
return NULL;
}
*/
}
bool isEmpty()
{
if (top <= -1)
return true;
else
return false; }
}stack1;
class Nstack
{ private:
float arr[MAX];// Contains all the Data
int top;
int c;
//Contains location of Topmost Data pushed onto Stack
public:
Nstack()
//Constructor
{ top=-1; //Sets the Top Location to -1 indicating an empty stack
}
void push(float a) // Push ie. Add Value Function
{ top++; // increment to by 1
if(top < MAX)
{ arr[top]=a; //If Stack is Vacant store Value in Array
}
else
{ cout<<"STACK FULL!!"<<endl;
top--;
}
}
float pop() // Push ie. Add Value Function
{
top--; // increment to by 1
if(top >= -1)
{
c = top + 1;
return arr[c]; //If Stack is Vacant store Value in Array

}

/*
else
{
cout<<"STACK EMPTY!!"<<endl;
top++;
return NULL;
}
*/

}
bool isEmpty()
{
if (top <= -1)
return true;
else
return false; }
}stack2;
int compute(char Opr)
{ if(Opr == '=')
{
cout<<stack2.pop()<<endl;
return 0;
}//输出结果
float Num1 = stack2.pop() ,Num2 = stack2.pop();

switch(Opr)
{
case '+':stack2.push(Num2+Num1);break; //结果压栈
case '-':stack2.push(Num2-Num1);break;
case '*':stack2.push(Num2*Num1);break;
case '/':stack2.push(Num2/Num1);break;

}
return 0;
}
/*
bool isDigit(char ch)
{
if(ch > '0' && ch < '10')
return true;
else
false;
}
*/int main(int argc, char* argv[])
{
cout<<"输入算术表达式:"<<endl;
char str[50];
bool b1 =true,b2 = true,b3 = true;
char ch,ch1;
int k = -1;

while(b1)
{
cin>>ch;
if((ch >= '0' && ch <= '9') || ch == '.')
{
k++;
str[k] = ch;
//cout<<ch;
}
else if(ch != '(' && b3)//第一个')'后面紧跟的那个个操作符在此会出问题
{
k++;
str[k] = '\0';
float a = atof(str);
stack2.push(a);
k = -1;
}
switch(ch)
{
case '(':b3 = true;stack1.push(ch);break;
case '+':
case '-':
b3 = true;
while(!stack1.isEmpty())
{
ch1 = stack1.pop();
if(ch1 == '(')
{
stack1.push(ch1);
break;
}
else
{
compute(ch1);
} }
stack1.push(ch);
break;
case '*':
case '/':
b3 = true;
while(!stack1.isEmpty() && b2)
{
ch1 = stack1.pop();
if(ch1 == '(')
{
stack1.push(ch1);
break;
}
else if(ch1 == '*' || ch1 == '/')
{
compute(ch1);
}
else
{
stack1.push(ch1);
b2 = false;
}
}
stack1.push(ch);
b2 = true;
break;
case ')':
b3 = false;
ch1 = stack1.pop();
while(ch1 != '(')
{

compute(ch1);
ch1 = stack1.pop(); }
break;
case '=' :
while(!stack1.isEmpty())
{
ch1=stack1.pop();
if(ch1 == '(')
{
cout<<"Enter is error!"<<endl;
break;
}
else
compute(ch1);
}
compute(ch);
break; }
}
return 0;
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式