c语言简单计算器加减乘除函数调用(c语言编写计算器加减乘除有退
c语言实现简单的加减乘除
1、打开C-Free5.0新建一个空白页面,然后将C语言的基础格式写完,注意格式缩进。如下图所示。
2、然后输入“?? int a=10; float b=5,c; ”注意: float 是浮点型,int 是整型。这个是用来定义C语言中的数值的类型,还有如果一个语句结束那就要打上“;”,这个很重要,不要忘记了。上面的语句是定义了一个a 的整型数,值为10。
3、c定义的一个空白的浮点数,用来当作后面的加减后取得值。注意这里有几个注意点,可以看到一个整型的数和一个浮点数的加减乘除得到的数值都是浮点数,所以这里用%f输出c的值。
4、继续将剩下的语言补充完整如下:#include stdio.hmain(){?? int a=10;?? float b=5,c,d,e,f; ?? c=a-b;?? d=a+b;?? e=a*b;?? f=a/b;?? printf("a-b=%f\n",c);?? printf("a+b=%f\n",d);?? printf("a*b=%f\n",e);?? printf("a/b=%f\n",f);}。
5、这样一个简单的C语言的加减乘除算是写好了,运行看看。

C语言简单计算器,支持加减乘除乘方运算,每步要有注释,求助C语言高手解决,谢谢!
//注意,没有考虑*/和+-的优先级。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator3 extends JFrame implements ActionListener {
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator3() {
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn == clear) {
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
} else if (btn == equal) {
operate('=');
equaled = true;
} else if (btn == plus) {
operate('+');
equaled = false;
} else if (btn == minus) {
operate('-');
equaled = false;
} else if (btn == multi) {
operate('*');
equaled = false;
} else if (btn == div) {
operate('/');
equaled = false;
} else if (btn == changePN) {
operate('p');
operate('=');
equaled = true;
} else if (btn == sqrt) {
operate('s');
operate('=');
equaled = true;
} else {
if (equaled)
storedNumber = 0;
for (int i = 0; i 10; i++)
if (btn == numbers[i]) {
if (operation.getText().equals("0"))
operation.setText("" + i);
else if (!operated)
operation.setText(operation.getText() + i);
else {
operation.setText("" + i);
operated = false;
}
}
if (btn == dot !dotExist) {
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator) {
double currentNumber = Double.valueOf(operation.getText())
.doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component,
int row, int col, int width, int height) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton) component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args) {
Calculator3 calc = new Calculator3();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
使用c语言编程,用函数实现一个计算器,在主函数中调用函数,包括加减乘除,乘方,绝对值和sin函数。
#includestdio.h
#includestdlib.h
double jia(double a,double b)
{
return a+b;
}
double jian(double a,double b)
{
return a-b;
}
double cheng(double a,double b)
{
return a*b;
}
double chu(double a,double b)
{
return a/b;
}
double juedui(double a)
{
return a0 ? a : -a;
}
double chengfang(double a,double b)
{
return pow(a,b);
}
double sinx(double a)
{
return sin(a);
}
int main()
{
int m;
double a,b;
while(1)
{
printf("请输入第一个操作数:");
scanf("%lf",a);
printf("0、退出\n1、加\n2、减\n3、乘\n4、除\n5、绝对值\n6、乘方\n7sin、\n请选择一个:");
scanf("%d",m);
if(1==m || 2==m || 3==m || 4==m || 6==m)
{
printf("请输入第二个操作数:");
scanf("%lf",b);
}
switch(m)
{
case 0:
exit(0);
break;
case 1:
printf("%lf+%lf=%lf\n",a,b,jia(a,b));
break;
case 2:
printf("%lf-%lf=%lf\n",a,b,jian(a,b));
break;
case 3:
printf("%lf*%lf=%lf\n",a,b,cheng(a,b));
break;
case 4:
if(0.0==b)
{
printf("除数不能为0。\n");
}
else
{
printf("%lf/%lf=%lf\n",a,b,chu(a,b));
}
break;
case 5:
printf("|%lf|=%lf\n",a,juedui(a));
break;
case 6:
printf("%lf的%lf方=%lf\n",a,b,chengfang(a,b));
break;
case 7:
printf("sin(%lf)=%lf\n",a,sinx(a));
break;
default:
printf("无法处理的命令。\n");
break;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
用C语言怎样实现计算器加减乘除功能?
我学c++时写的
#includeiostream.h
#include "string"
?
?? int??? count(int a,int b,char c)
?? {
?? ?? if(c=='+') return a+b;
?? ?? if(c=='-') return a-b;
????? if(c=='/') return a/b;
?? ?? if(c=='*') return a*b;
?
???????? }
?
void main()
{
?? ?char str[100];
?? ?cinstr;
?? ?int number[10]={0};
?? ?char sign[10];
??? ?
??? int i,j=0,k=0,m;
??? int strlong=strlen(str);//#include "string"
?? ?coutstrlong;
?? ?for(i=0;istrlong;i++)
?? ?{? ?
?? ??? ?if(str[i]='0'str[i]='9')
?? ??? ??? ?number[j]=number[j]*10+str[i]-48;
?
??????? else
?? ??? ?{j++;
?? ??? ??? ?sign[k]=str[i];
?? ??? ??? ?k++;}
?? ?}
?? ?j++;
??? //coutjk;
?? ?for(i=0;ij;i++)
?? ??? ?coutnumber[i]endl;
?? ?for(i=0;ik;i++)
?? ??? ?coutsign[i]endl;
??? ?
for( i=0;ik;i++)
{
? if(sign[i]=='/'||sign[i]=='*')
? {number[i]=count(number[i],number[i+1],sign[i]);
? coutnumber[i];
? for(m=i;mk-i;m++)
? {sign[m]=sign[m+1];number[m+1]=number[m+2];}
? k--;i--;}
}
for( i=0;ik;i++)
{ if(sign[i]=='+'||sign[i]=='-')
{number[i]=count(number[i],number[i+1],sign[i]);
? coutnumber[i];
? for(m=i;mk-i;m++)
? {sign[m]=sign[m+1];number[m+1]=number[m+2];}
? k--;i--;}
?
}
?
for (i=0;i3;i++)
{coutnumber[i];
}
coutk;
}
2.堆栈
#include "string"
#include "iostream" ?
#includevector
#includelist
#includecstdlib
using namespace std;
//自定义类型 用于存储 两种数据类型
class? newType
{
public: ?
?? ?bool flag;//true 为f? false 为 c
?? ?union ?
?? ?{
?? ?float f;
?? ?char c;
?? ?}data;
};
?
//将字符串转换为 数字数组和字符数组? (通用提取字符串中数字)
bool couvert(string str,vectorfloat? numbers,vectorchar chars,vectornewType all)//这里要使用引用
{
??? int len=str.length();
?? ?bool flag=true;
?? ?int pos=0;
?
?? ?for(int i=0;ilen;i++)
?? ?{
?? ??? ?if(str[i]='0'str[i]='9'||str[i]=='.')
?? ??? ?{ ?
?? ??? ??? ?if (flag)
?? ??? ??? ?{
?? ??? ??? ??? ?string substr=str.substr(i,len) ;
?? ??? ??? ??? ?float f=atof(substr.data());
?? ??? ??? ??? ?numbers.push_back(f);
?? ??? ??? ??? ?//添加f到all向量中
?? ??? ??? ??? ?newType n;
?? ??? ??? ??? ?n.data.f=f;
?? ??? ??? ??? ?n.flag=true;
?? ??? ??? ??? ?all.push_back(n);
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ??? ?flag=false;
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?//chars.push_back(str[i]);
?? ??? ??? ?chars.push_back(str[i]);
?? ??? ??? ?newType n;
?? ??? ??? ?n.data.c=str[i];
?? ??? ??? ?n.flag=false;
?? ??? ??? ?all.push_back(n);
?? ??? ??? ?flag=true; ?
?? ??? ?}
?? ?}//for
?? ?return true;
}
?
//计算没有括号的表达式
bool? calculate(vectorfloat numbers,
?? ???????????? vectorchar?? chars,float value)
{
?? ?//计算四者表达式? 无括号类型? 1+2*3+4
?? ?int ii=0;
??? //先计算乘除
?? ?while(iichars.size())
?? ??? ?//注意while(ii(chars.size()-2)) 和while(iichars.size()-2) 区别
?? ?{
?? ??? ?switch(chars[ii])
?? ??? ?{
?? ??? ?case '*':
?? ??? ??? ?numbers[ii]=numbers[ii]*numbers[ii+1];
?? ??? ??? ?numbers.erase(numbers.begin()+ii+1);? //移除number[ii]后面的数
?? ??? ??? ?chars.erase(chars.begin()+ii);??????? //移除chars[ii]
?? ??? ??? ?ii--;
?? ??? ??? ?break;
?? ??? ?case '/':
?? ??? ??? ?numbers[ii]=numbers[ii]/numbers[ii+1];
?? ??? ??? ?numbers.erase(numbers.begin()+ii+1);
?? ??? ??? ?chars.erase(chars.begin()+ii);
?? ??? ??? ?ii--;
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?ii++;
?? ?}
?
?? ?//只剩下加减?? 计算加减
?? ?ii=0;
?? ?while(iichars.size())
?? ??? ?//注意while(ii(chars.size()-2)) 和while(iichars.size()-2) 区别
?? ?{
?? ??? ?switch(chars[ii])
?? ??? ?{
?? ??? ?case '+':
?? ??? ?//?? ?cout"+::"numbers[ii]chars[ii]numbers[ii+1]endl;
?? ??? ??? ?numbers[ii]=numbers[ii]+numbers[ii+1];
?? ??? ??? ?numbers.erase(numbers.begin()+ii+1);? //移除number[ii]后面的数
?? ??? ??? ?chars.erase(chars.begin()+ii);??????? //移除chars[ii]
?? ??? ??? ?break;
?? ??? ?case '-':
?? ??? ?//?? ?cout"-::"numbers[ii]chars[ii]numbers[ii+1]endl;
?? ??? ??? ?numbers[ii]=numbers[ii]-numbers[ii+1];
?? ??? ??? ?numbers.erase(numbers.begin()+ii+1);
?? ??? ??? ?chars.erase(chars.begin()+ii);
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?// ii++;?? ?
?? ?}
??? ?
?? ?value=numbers[0];//得到值
?? ?return true;
}
?
//计算带括号的表达式
?
int calculate1(?? ?vectornewType all,float value)
{? ?
?? ?int pos=0;
?? ?vectorfloat numbers;
?? ?vectorchar?? chars;
?? ?float va=0;
?
? for(int i=0;iall.size();i++)
? {
??? if (all[i].flag)//判断是数字还是字符
??? {
?? ??? ?cout"数字"i":"all[i].data.fendl;
??? }
?? ?else
?? ?{
??????? cout"字符"i":"all[i].data.cendl;
?????? if (all[i].data.c==')') //如果是右括号? 将之前的()之间的括号内容
?????? {?????????????????????? //用calculate计算 并替换
?? ??? ??? ?
???????? for (int j=pos+1;ji;j++) //参数转换
???????? {?? ?
?? ??? ??? ? if (all[j].flag)
?? ??? ??? ? {
?? ??? ??? ??? ? numbers.push_back(all[j].data.f);
?? ??? ??? ? } else{
?? ??? ??? ??? ? chars.push_back(all[j].data.c);
?? ??? ??? ? }
???????? }
?
?? ??? ??? calculate(numbers,chars,va);
?? ??? ??? numbers.clear();
?? ??? ??? chars.clear();
?
?? ??? ??? newType ne;
?? ??? ??? ne.data.f=va;
?
?? ??? ??? all.erase(all.begin()+pos,all.begin()+i+1);
?? ??? ??? all.insert(all.begin()+pos,ne);
?
?? ??? ??? i=pos+1;
?
?????? }
?? ??? else if (all[i].data.c=='(')
?????? {
???????? pos=i;//记录此时左括号的位置
?????? }
?? ?}//else
? }//for
? for(int kk=0;kkall.size();kk++)
? {
?? ?? if (all[kk].flag)
?? ?? {
?? ??? ?? numbers.push_back(all[kk].data.f);
?? ?? } else{
?? ??? ?? chars.push_back(all[kk].data.c);
?? ??? ??? ? }
? }
?
? for( i=0;iall.size();i++)
? { ?
?? ?? if (all[i].flag)
?? ?? { ?
?? ??? ?? coutall[i].data.f" ";
?? ?? } else
?? ?? {
?? ??? ?? coutall[i].data.c" ";
?? ?? }
?? ?? ?
?? ?}
? calculate(numbers,chars,value);
?
return 1;
}
?
?
?
void main()
{?? ?
?? ?//
??? string str="10+(2*3+8)+(10*2)";
?? ?vectorfloat? numbers;
?? ?vectorchar chars;
?? ?vectornewType all;
?? ?couvert(str, numbers, chars,all);
??? for(int i=0;iall.size();i++)
?? ?{ ?
?? ??? ?if (all[i].flag)
?? ??? ?{ ?
?? ??? ??? ?coutall[i].data.f" ";
?? ??? ?} else
?? ??? ?{
?? ??? ??? ?coutall[i].data.c" ";
?? ??? ?}
?? ? ?
?? ?}
?? ?float value,value1;
//?? ?calculate(numbers,chars,value);
?? ?calculate1(all,value1);
?? ?coutvalue1;
??? ?
?
}
这是我翻家底找到的 .
求用C语言编写一简单计算器程序,要求:实现简单地加减乘除就行了
#include?stdio.h
int?jisuan(int?a,int?b,char?fu)
{
if(fu=='+')?return?a+b;
if(fu=='-')?return?a-b;
if(fu=='*')?return?a*b;
if(fu=='/')?return?a/b;
}
int?fun(char?*ss,int?n)
{
int?i,flag=0;
if(n==1)?return?ss[0]-'0';
for(i=0;in;i++)
{
if((ss[i]=='+')||(ss[i]=='-'))//扫描加减号
{
flag?=?1;
return?jisuan(fun(ss,i),fun(ss+i+1,n-i-1),ss[i]);
}
}
if(flag==0)//如果算数中没有+-
{
for(i=0;in;i++)
if((ss[i]=='*')||(ss[i]=='/'))
{
return?jisuan(fun(ss,i),fun(ss+i+1,n-i-1),ss[i]);
}
}
}
void?main(void)
{
char?s[50];
int?n;
printf("输入算数:");
scanf("%s",s);
n?=?strlen(s);
printf("=%d\r\n",fun(s,n));
}
用的递归,这样省去很多麻烦
简单的用c语言写一个计算器程式,加减乘除能用就好
简单的用c语言写一个计算器程式,加减乘除能用就好 #include"stdio.h"
void main()
{
float a,b,c;
char e;
printf("input a,e,b\n");/*输入两个数和符号,例如3+8*/
scanf("%f%c%f",a,e,b);
switch(e)
{
case '+':c=a+b;break;
case '-':c=a-b;break;
case '*':c=a*b;break;
case '/':
if(b==0.0) printf("error\n");
else c=a/b;break;
}
printf("%f%c%f=%f",a,e,b,c);
}
如何用vc++编写一个简单的(只有加减乘除)计算器程式?
先设定介面如下
加法按钮程式码
void CMy03Dlg::OnBnClickedButton1()
{
TODO:在此新增控制元件通知处理程式程式码
UpdateData(TRUE);
m_Nub3=m_Nub1+m_Nub2;
UpdateData(FALSE);
}
减法按钮程式码
void CMy03Dlg::OnBnClickedButton2()
{
TODO:在此新增控制元件通知处理程式程式码
UpdateData(TRUE);
m_Nub3=m_Nub1-m_Nub2;
UpdateData(FALSE);
}
乘法按钮程式码
void CMy03Dlg::OnBnClickedButton3()
{
TODO:在此新增控制元件通知处理程式程式码
UpdateData(TRUE);
m_Nub3=m_Nub1*m_Nub2;
UpdateData(FALSE);
}
除法按钮程式码
void CMy03Dlg::OnBnClickedButton4()
{
TODO:在此新增控制元件通知处理程式程式码
UpdateData(TRUE);
if(m_Nub2!=0)
m_Nub3=m_Nub1 / m_Nub2;
else
AfxMessageBox("被除数不能为0");
UpdateData(FALSE);
}
清除按钮程式码
void CMy03Dlg::OnBnClickedButton5()
{
TODO:在此新增控制元件通知处理程式程式码
UpdateData(TRUE);
m_Nub3=0;
m_Nub1=0;
m_Nub2=0;
UpdateData(FALSE);
}
结束按钮程式码
void CMy03Dlg::OnBnClickedButton6()
{
TODO:在此新增控制元件通知处理程式程式码
CDialog::OnOK();
}
如果只允许在输入框中输入资料应该怎样处理?
制作托盘程式
目的:在工作列中建立一个图示,使该程式永远驻留在记忆体中。例如邮件检查程式可以作为驻留程式,一旦有邮件来了,就可以接收邮件。
Shell_NotifyIcon函式传送讯息来增加、删除、修改工作列的图示
BOOL TrayMessage(HWND hWnd, DWORD dwMessage, HICON hIcon, PSTR pszTip)
{
BOOL res;
NOTIFYICONDATA tnd;
tnd.cbSize = sizeof(NOTIFYICONDATA);
tnd.hWnd = hWnd;
tnd.uID = IDI_ICON1;
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
tnd.uCallbackMessage = WM_MY_TRAY_NOTIFICATION;
tnd.hIcon = hIcon;
lstrcpyn(tnd.szTip, pszTip, sizeof(tnd.szTip));
res = Shell_NotifyIcon(dwMessage, tnd); dwMessage为NIM_ADD从工作列中新增图示、NIM_DELETE从工作列中删除图示、NIM_MODIFY改变工作列中图示
if (hIcon)
DestroyIcon(hIcon);
return res;
}
定义一个回拨讯息:WM_MY_TRAY_NOTIFICATION
在DLG的CPP档案中,
#define WM_MY_TRAY_NOTIFICATION WM_USER+100
为对话方块新增讯息对映ON_MESSAGE(WM_MY_TRAY_NOTIFICATION,OnTrayNotification)
在DLG的标头档案中应该有
public:
long m_Nub1;
float m_Nub3;
CBitmapButton Button;
afx_msg void OnBnClickedButton1();
long m_Nub2;
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton6();
afx_msg void OnBnClickedButton7();
afx_msg LRESULT OnTrayNotification(WPARAM wparam, LPARAM lparam);
在DLG的CPP档案中应该有
BEGIN_MESSAGE_MAP(CMailCheckDlg, CDialog)
……
ON_MESSAGE(WM_MY_TRAY_NOTIFICATION,OnTrayNotification)
……
END_MESSAGE_MAP()
并定义一个回拨讯息函式
LRESULT CMailCheckDlg::OnTrayNotification(WPARAM wparam, LPARAM lparam)
{
switch (lparam )
{
case WM_RBUTTONUP:
case WM_LBUTTONDBLCLK:修改不同的按钮处理事件,以观察图示退出效果。
ShowWindow(SW_SHOW);
TrayMessage(m_hWnd, NIM_DELETE, NULL, "");从工作列中删除图示
}
return 0;
}
在对话方块视窗上新增“驻留”按钮,双击按钮新增程式码
void CMailCheckDlg::OnBnClickedButton1()
{
TODO:在此新增控制元件通知处理程式程式码
下面程式向工作列新增图示
TrayMessage(m_hWnd, NIM_ADD, NULL, "计算器程式");
TrayMessage(m_hWnd, NIM_MODIFY, m_hIcon, "计算器程式");
ShowWindow(SW_HIDE);
用MFC编写一个简单的加减乘除计算器
我有程式,加31782771群
c语言计算器程式设计包含加减乘除简单的函式运算
实用计算器之程式设计
[摘 要]多用计算器的构思及设计程式码
[关键词]多用计算器;设计
数值计算可以说是日常最频繁的工作了,WIN98提供了“计算器”软体供使用者使用,该软体可以处理一般的一步四则运算,例如:3+2、5/3等等,但在日常中使用者经常遇到多步四则运算问题,例如:3+4*5-4/2,45*34/2+18*7等等,那么该个计算器就无法胜任了,作者制作了一个实用的计算器,该计算器新增不少功能:(程式介面如图)
1.可以实现连续的四则运算
2.可以实现输入式子的显示
3.可以方便计算个人所得税
4.滑鼠、键盘均可输入资料
5.操作介面友好
6.击键可发声
构建该个计算器所需研究及解决的核心问题有如下几个:1、连乘求值?2、字元显示 3、键盘输入?4、击键发声?5、个人所得税法规,为了使大家对程式有更一步认识,现将程式码提供给读者参考:
*定义阵列及窗体变数
Dim number2(0 To 50) As Double
Dim number(0 To 50) As Double
Dim z As Integer
Dim k As Integer, r As Integer
Dim j As Integer
Dim str As String
*呼叫名为“playsound”的API函式
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_FILENAME = H20000?
Private Const SND_ASYNC = H1?
Private Const SND_SYNC = H0
*判断通用过程
Sub pianduan(p As String)
r = 0
Dim i As Integer, l As Integer, h As Integer
h = 0
i = 1
If InStr(Trim$(p), "*") 0 Then
k = k + 1
End If
If InStr(Trim$(p), "/") 0 Then
r = r + 1
End If
End Sub
*连乘通用过程(略)
*各按钮事件过程
Private sub Command1_Click(Index As Integer)
PlaySound App.Path "\start.wav", 0, SND_SYNC
Text1.Text = Text1.Text + Command1(Index).Caption
Text2.Text = Text2.Text + Command1(Index).Caption
Text1.SetFocus
End Sub
rivate sub Command10_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
str = Text3.Text
End Sub
Private sub Command11_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
Text3.Text = str
End Sub
rivate sub Command2_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1.Text)
If k = 1 Or r = 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "+"
z = z + 1
Text1.SetFocus
End Sub
rivate sub Command3_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1.Text)
If k = 1 Or r = 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "-"
Text1.Text = Text1.Text "-"
z = z + 1
Text1.SetFocus
End Sub
Private sub Command4_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
Text2.Text = Text2.Text + "*"
Text1.Text = Text1.Text + "*"
Text1.SetFocus
End Sub
rivate sub Command5_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
Text2.Text = Text2 + "/"
Text1.Text = Text1 + "/"
Text1.SetFocus
End Sub
Private sub Command6_Click()
PlaySound App.Path "\sound.wav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1.Text)
If k = 1 Or r = 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
z = z + 1
Dim dengyu As Double
Dim v As Integer
For v = 0 To 50
dengyu = dengyu + number2(v)
Next v
If dengyu 0 Then
Text3.ForeColor = HFF
Else
Text3.ForeColor = HFF0000
End If
Text3.Text = dengyu
Text1.SetFocus
If Len(Text3.Text) = 14 Then
calcresult.Show
End If
End Sub
rivate sub Command7_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
z = 0: k = 0: r = 0: j = 0
Dim i As Integer
For i = 0 To 50
number(i) = 0
number2(i) = 0
Next i
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
rivate sub Command8_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
If Val(Text3.Text) = 0 Then
MsgBox "除数不能为0!"
Exit Sub
End If
Text3.Text = 1 / Val(Text3.Text)
End Sub
Private sub Command9_Click()
PlaySound App.Path "\start.wav", 0, SND_SYNC
Text3.ForeColor = HFF0000
Text3.Text = Val(Text3.Text) * Val(Text3.Text)
End Sub
rivate sub muninter_Click()
Dim i
i = Shell("C:\Program Files\InterExplorer\iexplore.exe", vbMaximizedFocus)
End Sub
rivate sub munmp3_Click()
Dim i
i = Shell("C:\Program Files\Windows Media Player\mplayer2", vbNormalNoFocus)
End Sub
Private sub mun *** _Click()
Dialog.Show
End Sub
rivate sub muntax_Click()
tax.Show
End Sub
rivate sub munver_Click()
ver.Show
End Sub
rivate sub notepad_Click()
Dim i
i = Shell("c:\windows\notepad", vbNormalFocus)
End Sub
Private sub Text1_KeyPress(KeyAscii As Integer)
PlaySound App.Path "\start.wav", 0, SND_SYNC
Dim num As Integer
num = Val(KeyAscii)
If num 47 And num 58 Then
Text1.Text = Text1.Text + CStr(num - 48)
Text2.Text = Text2.Text + CStr(num - 48)
End If
If num = 46 Then
Text1.Text = Text1.Text + "."
Text2.Text = Text2.Text + "."
End If
If KeyAscii = 43 Then
Dim totle As Double
Dim n As Integer
Call pianduan(Text1.Text)
If k = 1 Or r = 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "+"
z = z + 1
End If
If KeyAscii = 45 Then
Call pianduan(Text1.Text)
If k = 1 Or r = 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
Text2.Text = Text2 + "-"
Text1.Text = Text1.Text "-"
z = z + 1
End If
If KeyAscii = 42 Then
Text2.Text = Text2.Text + "*"
Text1.Text = Text1.Text + "*"
End If
If KeyAscii = 47 Then
Text2.Text = Text2.Text + "/"
Text1.Text = Text1.Text + "/"
End If
If KeyAscii = vbKeyReturn Then
PlaySound App.Path "\sound.wav", 0, SND_SYNC
Call pianduan(Text1.Text)
If k = 1 Or r = 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1.Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1.Text)
End If
Text1.Text = ""
z = z + 1
Dim dengyu As Double
Dim v As Integer
For v = 0 To 50
dengyu = dengyu + number2(v)
Next v
If dengyu 0 Then
Text3.ForeColor = HFF
Else
Text3.ForeColor = HFF0000
End If
Text3.Text = dengyu
End If
If KeyAscii = vbKeyEscape Then
z = 0: k = 0: r = 0: j = 0
Dim i As Integer
For i = 0 To 50
number(i) = 0
number2(i) = 0
Next i
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End If
If Len(Text3.Text) = 14 Then
calcresult.Show
End If
End Sub
rivate sub Text3_Change()
tax2.Text1 = Text3.Text
End Sub
用c语言编写能运算加减乘除的计算器程式,用到栈
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
expression evaluate
#define iMUL 0
#define iDIV 1
#define iADD 2
#define iSUB 3
#define iCap 4
#define LtKH 5
#define RtKH 6
#define MaxSize 100
void iPush(float);
float iPop();
float StaOperand[MaxSize];
int iTop=-1;
char Srcexp[MaxSize];
char Capaexp[MaxSize];
char RevPolishexp[MaxSize];
float NumCapaTab[26];
char validexp[]="*/+-()";
char NumSets[]="0123456789";
char StackSymb[MaxSize];
int operands;
void NumsToCapas(char [], int , char [], float []);
int CheckExpress(char);
int PriorChar(char,char);
int GetOperator(char [], char);
void counterPolishexp(char INexp[], int slen, char Outexp[]);
float CalcRevPolishexp(char [], float [], char [], int);
void main()
{
int ilen;
float iResult=0.0;
printf("enter a valid number string:\n");
memset(StackSymb,0,MaxSize);
memset(NumCapaTab,0,26); A--NO.1, B--NO.2, etc.
gets(Srcexp);
ilen=strlen(Srcexp);
printf("source expression:%s\n",Srcexp);
NumsToCapas(Srcexp,ilen,Capaexp,NumCapaTab);
printf("Numbers listed as follows:\n");
int i;
for (i=0; ioperands; ++i)
printf("%.2f ",NumCapaTab[i]);
printf("\nCapaexp listed in the following:\n");
printf("%s\n",Capaexp);
ilen=strlen(Capaexp);
counterPolishexp(Capaexp,ilen,RevPolishexp);
printf("RevPolishexp:\n%s\n",RevPolishexp);
ilen=strlen(RevPolishexp);
iResult=CalcRevPolishexp(validexp, NumCapaTab, RevPolishexp,ilen);
printf("\ncounterPolish expression:\n%s%.6f\n",Srcexp,iResult);
}
void iPush(float value)
{
if(iTopMaxSize) StaOperand[++iTop]=value;
}
float iPop()
{
if(iTop-1)
return StaOperand[iTop--];
return -1.0;
}
void NumsToCapas(char Srcexp[], int slen, char Capaexp[], float NumCapaTab[])
{
char ch;
int i, j, k, flg=0;
int sign;
float val=0.0,power=10.0;
i=0; j=0; k=0;
while (islen)
{
ch=Srcexp[i];
if (i==0)
{
sign=(ch=='-')?-1:1;
if(ch=='+'||ch=='-')
{
ch=Srcexp[++i];
flg=1;
}
}
if (isdigit(ch))
{
val=ch-'0';
while (isdigit(ch=Srcexp[++i]))
{
val=val*10.0+ch-'0';
}
if (ch=='.')
{
while(isdigit(ch=Srcexp[++i]))
{
val=val+(ch-'0')/power;
power*=10;
}
} end if
if(flg)
{
val*=sign;
flg=0;
}
} end if
write Capaexp array
write NO.j to array
if(val)
{
Capaexp[k++]='A'+j;
Capaexp[k++]=ch;
NumCapaTab[j++]=val; A--0, B--1,and C, etc.
}
else
{
Capaexp[k++]=ch;
}
val=0.0;
power=10.0;
i++;
}
Capaexp[k]='\0';
operands=j;
}
float CalcRevPolishexp(char validexp[], float NumCapaTab[], char RevPolishexp[], int slen)
{
float sval=0.0, op1,op2;
int i, rt;
char ch;
recursive stack
i=0;
while((ch=RevPolishexp[i]) islen)
{
switch(rt=GetOperator(validexp, ch))
{
case iMUL: op2=iPop(); op1=iPop();
sval=op1*op2;
iPush(sval);
break;
case iDIV: op2=iPop(); op1=iPop();
if(!fabs(op2))
{
printf("overflow\n");
iPush(0);
break;
}
sval=op1/op2;
iPush(sval);
break;
case iADD: op2=iPop(); op1=iPop();
sval=op1+op2;
iPush(sval);
break;
case iSUB: op2=iPop(); op1=iPop();
sval=op1-op2;
iPush(sval);
break;
case iCap: iPush(NumCapaTab[ch-'A']);
break;
default: ;
}
++i;
}
while(iTop-1)
{
sval=iPop();
}
return sval;
}
int GetOperator(char validexp[],char oper)
{
int oplen,i=0;
oplen=strlen(validexp);
if (!oplen) return -1;
if(isalpha(oper)) return 4;
while(ioplen validexp[i]!=oper) ++i;
if(i==oplen || i=4) return -1;
return i;
}
int CheckExpress(char ch)
{
int i=0;
char ;
while((=validexp[i]) ch!=) ++i;
if (!)
return 0;
return 1;
}
int PriorChar(char curch, char stach)
{
栈外优先顺序高于()栈顶优先顺序时,才入栈
否则(=),一律出栈
if (curch==stach) return 0; 等于时应该出栈
else if (curch=='*' || curch=='/')
{
if(stach!='*' stach!='/')
return 1;
}
else if (curch=='+' || curch=='-')
{
if (stach=='(' || stach==')')
return 1;
}
else if (curch=='(')
{
if (stach==')')
return 1;
}
return 0;
}
void counterPolishexp(char INexp[], int slen, char Outexp[])
{
int i, j, k,pr;
char t;
i=0;
j=k=0;
while (INexp[i]!='=' islen)
{
if (INexp[i]=='(')
StackSymb[k++]=INexp[i];
iPush(*(INexp+i));
else if(INexp[i]==')')
{
if((t=iPop())!=-1)
while((t=StackSymb[k-1])!='(')
{
Outexp[j++]=t;
k--;
}
k--;
}
else if (CheckExpress(INexp[i])) is oparator
{
printf("operator %c k=%d\n",INexp[i],k);
while (k)
{
iPush(*(INexp+i));
if(pr=PriorChar(INexp[i],StackSymb[k-1]))
break;
else
{
if ((t=iPop())!=-1)
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
} end while
StackSymb[k++]=INexp[i]; mon process
}
else if() 变数名
{
printf("operand %c k=%d\n",INexp[i],k);
Outexp[j++]=INexp[i];
}
i++;
}
while (k)
{
t=StackSymb[k-1]; k--;
Outexp[j++]=t;
}
Outexp[j]='\0';
}
注:程式源于“百度知道”
用verilog编写一个最简单的加减乘除的计算器的程式
verilog是有加法器乘法器的。也直接识别 + - * / 符号。
module kjasdja(a,option,b,result);
input option,a,b;
output result;
always @(a,b,option)
begin
result_r=0; 结果暂存器清零
case(option)
+:result_r=a+b;
-:result_r=a-b;
*:result_r=a*b;
/:result_r=a/b;
assign result =result_r;
endmodule
大概演算法就这样。写的仓促,语法可能有误。另外除法reg型别只能储存整数部分,小数通过移位操作实现,比较麻烦。比如3/5=0.6
做的时候先3=30,然后30/5=6,然后对6在数码管的显示进行调整就好。把6显示在小数点后面1位就好
用vb编写一个计算器程式,实现加减乘除,
Dim v As Boolean
Dim s As Integer
Dim X As Double
Dim Y As Double
Private Sub Command1_Click(Index As Integer)
If Form1.Tag = "T" Then
If Index = 10 Then
Text1.Text = "0"
Else
Text1.Text = Command1(Index).Caption
End If
Form1.Tag = ""
Else
Text1.Text = Text1.Text Command1(Index).Caption
End If
End Sub
Private Sub Command2_Click(Index As Integer)
Form1.Tag = "T"
If v Then
X = Val(Text1.Text)
v = Not v
Else
Y = Val(Text1.Text)
Select Case s
Case 0
Text1.Text = X + Y
Case 1
Text1.Text = X - Y
Case 2
Text1.Text = X * Y
Case 3
If Y 0 Then
Text1.Text = X / Y
Else
MsgBox ("不能以0为除数")
Text1.Text = X
v = False
End If
Case 4
Y = 0
v = False
End Select
X = Val(Text1.Text)
End If
s = Index
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
控制元件自己新增吧,空间名要和程式码名一致
求一简单的加减乘除计算器c++程式
#includestdio.h
#includemath.h
void main()
{
float a,b;
char C;
while(1)
{
scanf("%f%c%f",a,C,b);
if((C!='+')(C!='-')(C!='*')(C!='/'))
break;
switch(C)
{
case '+': printf("%f+%f=%f",a,b,a+b);
break;
case '-': printf("%f-%f=%f",a,b,a-b);
break;
case '*': printf("%f*%f=%f",a,b,a*b);
break;
case '/': printf("%f/%f=%f",a,b,a/b);
break;
}
}
}
想改成按1 2 3 4分别为加减乘除,只需要将程式中的+ - * / 改成1 2 3 4即可。按除了+ - * / 以外的键就会退出。
用c++语言编写一个简单的计算器程式,会加减乘除就行,本人初学不太会,特训求帮助
这个是最简单,简陋的计算器。很多情况没考虑进去,例如除数不能为0之类的,真要写完整的话程式码还要更多。
程式码如下: #include iostreamusing namespace std;int main(){ float a, b, result; char operation; cout "请输入算式,如1+2并回车:" endl; cin a operation b; switch(operation) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; default: cout "输入非法,程式退出!" endl; return -1; } cout endl "结果为:" endl a operation b "=" result endl; return 0;}
知道switch函式 吗 用这个就行
建俩个int型变数 一个字元型变数