超长加减计算器(免费)

合集下载

用java代码写的简易计算器(可以实现基本的加减乘除功能)

用java代码写的简易计算器(可以实现基本的加减乘除功能)

⽤java代码写的简易计算器(可以实现基本的加减乘除功能)package A;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.*;public class Calcular3 extends JFrame implements ActionListener,MouseListener{private int m1=0,n=0;//private double m2=0;//运算的数private int flag=0;JFrame f;JPanel p1,p2,p3;JTextField t;JButton b1[]=new JButton[18];String b[]= {"1","2","3","4","5","6","7","8","9","0","清空","退格",".","=","+","-","*","/"};public Calcular3(){f=new JFrame("计算器");t=new JTextField(35);p1=new JPanel();p2=new JPanel();p3=new JPanel();f.setBounds(100, 100, 400, 200);f.add(p1,BorderLayout.NORTH);f.add(p2,BorderLayout.CENTER);f.add(p3,BorderLayout.EAST);p2.setLayout(new GridLayout(5,3));p3.setLayout(new GridLayout(4,1));p1.add(t);for(int i=0;i<14;i++) {b1[i]=new JButton(b[i]);p2.add(b1[i]);b1[i].addActionListener(this);}for(int i=14;i<18;i++) {b1[i]=new JButton(b[i]);p3.add(b1[i]);b1[i].addActionListener(this);}/*for(int i=0;i<18;i++) {b1[i].addActionListener(this);}*/f.setVisible(true);}//实现接⼝的⽅法public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void actionPerformed(ActionEvent e) {String str="";int i;for(i=0;i<=9;i++) {if(e.getSource()==b1[i]) {if(i==9) {n=n*10;}else {n=n*10+i+1;}str=String.valueOf(n);//整形n转换成字符串strt.setText(str);//显⽰到⽂本框上}}for(i=14;i<18;i++) {//+、-、*、/if(e.getSource()==b1[i]) {//匹配运算符m1=Integer.parseInt(t.getText());if(flag==15) {m2=m1+m2;}else if(flag==16) {m2=m1-m2;}else if(flag==17) {m2=m1*m2;}else if(flag==18) {m2=m1/m2;}else m2=m1;//若⽆连续的运算符运算,保存当前数据到m2 if(i==14) flag=15;else if(i==15) flag=16;else if(i==16) flag=17;else flag=18;str=String.valueOf(b[i]);t.setText(str);//显⽰到⽂本框上n=0;//还原,记录下次数据break;//找到匹配数据退出循环}}if(e.getSource()==b1[13]) {//=m1=Integer.parseInt(t.getText());if(flag==15) {m2=m2+m1;}else if(flag==16) {m2=m2-m1;}else if(flag==17) {m2=m2*m1;}else if(flag==18) {m2=m2/m1;}else m2=m1;str=String.valueOf(m2);t.setText(str);//显⽰到⽂本框上n=0;//还原,记录下次数据flag=0;//flag还原0,表明没有未处理的运算符}if(e.getSource()==b1[10]) {//各变量变为0 清空m1=0;m2=0;flag=0;n=0;t.setText("0");//显⽰到⽂本框上}if(e.getSource()==b1[11]) {//退格m1=(int)(Double.parseDouble(t.getText())/10);n=m1;str=String.valueOf(m1);t.setText(str);}if(e.getSource()==b1[12]) {//⼩数点m1=Integer.parseInt(t.getText());str=String.valueOf(m1+b[12]);t.setText(str);//显⽰到⽂本框上int j=0;for(i=0;i<=9;i++) {if(e.getSource()==b1[i]) {j++;m2=Math.pow(0.1, j)*Integer.parseInt(b[i]);str=String.valueOf(m1+m2);t.setText(str);//显⽰到⽂本框上}}}}//主函数public static void main(String[] args) {new Calcular3();}}。

超级简易的JS计算器(实现加减乘除)

超级简易的JS计算器(实现加减乘除)

超级简易的JS计算器(实现加减乘除)<!DOCTYPE html><html><head><meta charset="UTF-8"><title>简单的计算器</title><style>body{margin: 0;}.tab{border: 3px solid black ;border-radius: 2px;border-collapse:collapse;width: 268px;height: 402px;margin: 100px auto;}.tr{height: 67px;width: 268px;border: 3px solid black ;text-align: right;}.tr1{width: 268px;height: 67px;border: 3px solid black ;text-align: center;}.tr2{width: 67px;height: 67px;border: 3px solid black ;text-align: center;}</style><script>var s1 = "";var s2 = "";var s3 = "";var s4 = "";var s5 = "";function view(val){var a = document.getElementById(val);s1 = a.innerHTML;if(s1=="+" || s1=="-" || s1=="*" || s1=="/"){s3 = s1;s1 = "";}if(s3=="+" || s3=="-" || s3=="*" || s3=="/"){add2(s1);}if(s3==""){add1(s1);}}function add1(s1){s2 = s2 + s1;show();}function add2(s1){s4 = s4 + s1;show();}function cal(){switch(s3){case "+":{s5 = (String)((parseFloat(s2)+parseFloat(s4)).toFixed(2));result.innerHTML = s5;break;}case "-":{s5 = (String)((parseFloat(s2)-parseFloat(s4)).toFixed(2));result.innerHTML = s5;break;}case "*":{s5 = (String)((parseFloat(s2)*parseFloat(s4)).toFixed(2));result.innerHTML = s5;break;}case "/":{s5 = (String)((parseFloat(s2)/parseFloat(s4)).toFixed(2));result.innerHTML = s5;break;}}}function show(){var result = document.getElementById("result");result.innerHTML = s2+s3+s4;}</script></head><body><table class="tab"><tr class = "tr2"><td colspan="4">简易计算器</td></tr><tr class="tr"><td id="result" colspan="4"></td></tr><tr ><td id="+" class="tr1" onclick="view('+')">+</td><td id="-" class="tr1" onclick="view('-')">-</td><td id="*" class="tr1" onclick="view('*')">*</td><td id="/" class="tr1" onclick="view('/')">/</td></tr><tr ><td id="7" class="tr1" onclick="view('7')">7</td><td id="8" class="tr1" onclick="view('8')">8</td><td id="9" class="tr1" onclick="view('9')">9</td><td id="0" class="tr1" onclick="view('0')">0</td></tr><tr><td id="4" class="tr1" onclick="view('4')">4</td><td id="5" class="tr1" onclick="view('5')">5</td><td id="6" class="tr1" onclick="view('6')">6</td><td id="." class="tr1" onclick="view('.')">.</td></tr><tr><td id="1" class="tr1" onclick="view('1')">1</td><td id="2" class="tr1" onclick="view('2')">2</td><td id="3" class="tr1" onclick="view('3')">3</td><td id="=" class="tr1" onclick="cal()">=</td></tr></table></body></html>对于这个程序来说,判断的顺序⾮常重要,不然就会把"+"号存在s2,s4中⽽不是s3中。

如何用手持计算器计算加减乘除

如何用手持计算器计算加减乘除

如何用手持计算器计算加减乘除手持计算器是一种方便快捷的工具,可以轻松进行加减乘除运算。

无论是日常生活中还是工作中,我们都可能遇到需要进行精确计算的情况。

下面是一些关于如何使用手持计算器进行加减乘除运算的详细介绍。

一、加法运算使用手持计算器进行加法运算非常简单。

首先,打开计算器并选择“加法(+)”运算符。

然后,输入第一个数字,通常以按键“数字键”为主。

接下来,按下“+”按键。

最后,输入第二个数字并按下“=(等于)”按键。

在显示屏上,你将看到两个数字的和。

例如,要计算5+3的和,首先输入数字5,然后按下“+”按键,再输入数字3,最后按下“=(等于)”按键。

显示屏上将显示结果“8”。

二、减法运算使用手持计算器进行减法运算与加法运算类似。

首先,打开计算器并选择“减法(-)”运算符。

接下来,输入第一个数,然后按下“-”按键。

最后,输入第二个数并按下“=(等于)”按键。

在显示屏上,你将看到两个数的差。

例如,要计算8-4的差,首先输入数字8,然后按下“-”按键,再输入数字4,最后按下“=(等于)”按键。

显示屏上将显示结果“4”。

三、乘法运算使用手持计算器进行乘法运算也很简单。

首先,打开计算器并选择“乘法(×)”运算符。

然后,输入第一个数并按下“×”按键。

接下来,输入第二个数并按下“=(等于)”按键。

在显示屏上,你将看到两个数的乘积。

例如,要计算7×2的乘积,首先输入数字7,然后按下“×”按键,再输入数字2,最后按下“=(等于)”按键。

显示屏上将显示结果“14”。

四、除法运算使用手持计算器进行除法运算也很简单。

首先,打开计算器并选择“除法(÷)”运算符。

然后,输入被除数并按下“÷”按键。

接下来,输入除数并按下“=(等于)”按键。

在显示屏上,你将看到两个数的商。

需要注意的是,除数不能为零,否则会触发错误提示。

例如,要计算10÷5的商,首先输入数字10,然后按下“÷”按键,再输入数字5,最后按下“=(等于)”按键。

计算器练习题(打印版)

计算器练习题(打印版)

计算器练习题(打印版)### 计算器练习题(打印版)#### 一、基础加减法1. 计算下列各题,并使用计算器验证你的答案。

- 56 + 34 = ?- 89 - 23 = ?2. 完成以下计算,并检查你的答案。

- 123 + 456 + 789 = ?- 987 - 654 - 321 = ?#### 二、乘除法练习3. 使用计算器计算以下乘法题目。

- 12 × 34 = ?- 56 × 78 = ?4. 完成以下除法题目,并确保你的答案是准确的。

- 240 ÷ 6 = ?- 1800 ÷ 150 = ?#### 三、小数运算5. 计算以下小数加法。

- 2.5 + 3.75 = ?- 15.3 + 4.76 = ?6. 完成以下小数减法。

- 8.9 - 2.35 = ?- 10.75 - 5.49 = ?#### 四、混合运算7. 解决以下混合运算问题。

- 34 + 56 × 2 - 89 = ?- 98 ÷ 7 + 12 × 3 = ?8. 完成以下混合运算。

- (25 + 18) × 4 - 67 = ?- 82 - (34 × 2 + 15) = ?#### 五、分数运算9. 使用计算器计算以下分数加法。

- 1/2 + 3/4 = ?- 5/6 + 7/8 = ?10. 完成以下分数减法。

- 3/4 - 1/2 = ?- 7/8 - 5/6 = ?#### 六、百分比问题11. 计算以下百分比问题。

- 20% of 500 = ?- 75% of 800 = ?12. 解决以下百分比减法问题。

- 100% - 25% of 200 = ?#### 七、综合应用题13. 如果一个班级有40名学生,每名学生需要支付50元的学费。

使用计算器计算全班学生的总学费。

- 总学费 = ?14. 一家商店在一天内卖出了120件商品,每件商品的利润是10元。

如何用手持计算器计算加减乘除

如何用手持计算器计算加减乘除

如何用手持计算器计算加减乘除
手持计算器是一种便携式的数学工具,可以帮助我们进行简单的加减乘除运算。

下面是使用手持计算器计算加减乘除的步骤。

加法
1. 打开手持计算器。

2. 在计算器的屏幕上输入第一个加数。

3. 按下加号(+)按钮。

4. 输入第二个加数。

5. 按下等号(=)按钮。

6. 屏幕将显示出两个加数的和。

减法
1. 打开手持计算器。

2. 在计算器的屏幕上输入被减数。

3. 按下减号(-)按钮。

4. 输入减数。

5. 按下等号(=)按钮。

6. 屏幕将显示出两个数相减的差。

乘法
1. 打开手持计算器。

2. 在计算器的屏幕上输入第一个乘数。

3. 按下乘号(*)按钮。

4. 输入第二个乘数。

5. 按下等号(=)按钮。

6. 屏幕将显示出两个乘数的积。

除法
1. 打开手持计算器。

2. 在计算器的屏幕上输入被除数。

3. 按下除号(/)按钮。

4. 输入除数。

5. 按下等号(=)按钮。

6. 屏幕将显示出两个数相除的商。

请注意,手持计算器的操作方法可能会略有不同,具体请参考您所使用计算器的说明书。

此文档提供的步骤仅供参考。

手持计算器是一种方便快捷的工具,可以帮助我们进行基本的数学运算,而且携带非常方便。

希望这份文档对您有所帮助!。

简易计算器(加减乘除幂e)

简易计算器(加减乘除幂e)

此计算机包含+,-,/,*,!,sin,tan,cos,^,e。

#include<iostream.h>#include<math.h>#include <iomanip.h>#include<stdlib.h>#define max 100#define MaxOp 100struct //设定运算符优先级{char ch; //运算符int pri; //优先级}lpri[]={{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6},{'^', 7},{'e',7},{'!',7},{'s',7},{'t',7},{'c',7}},rpri[]={{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1},{'^', 6},{'e',6},{'!',6},{'s',6},{'t',6},{'c',6}};int leftpri(char op) //求左运算符op的优先级{int i;for(i=0;i<MaxOp;i++)if(lpri[i].ch==op)return lpri[i].pri;}int rightpri(char op) //求右运算符op的优先级{int i;for(i=0;i<MaxOp;i++)if(rpri[i].ch==op)return rpri[i].pri;}int InOp(char ch) //判断ch是否运算符{if(ch=='('||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'| |ch=='e'||ch=='!'||ch=='t'||ch=='s'||ch=='c')return 1;elsereturn 0;}int precede(char op1,char op2) //op1和op2运算符的比较结果{if(leftpri(op1)==rightpri(op2))return 0;else if(leftpri(op1)<rightpri(op2))return -1;elsereturn 1;}void trans(char*exp,char postexp[]) //将算术表达式exp转换为后缀表达式postexp{struct{char data[max]; //存放运算符int top; //栈指针}op; //定义运算符栈int i=0; //i作为postexp的下标op.top=-1;op.top++;op.data[op.top]='='; //将“=”进栈char *t;cout<<"-----------------------------------------------------------------"<<endl;cout<<"当前符号输入区 op栈输出区"<<endl;cout<<"-----------------------------------------------------------------"<<endl;while(*exp!='\0') //*exp未扫描完时循环{if(!InOp(*exp)) //为数字字符情况下{t=exp;while(*exp>='0'&&*exp<='9'||*exp=='.') //判定为数字{cout<<*exp; //输出中缀表达式的头指针postexp[i++]=*exp;exp++;}cout<<"\t";cout.setf(ios::left);cout<<setw(20);cout<<t;for(int n=1;n<=op.top;n++)cout<<op.data[n];cout<<"\t\t ";for(n=0;n<i;n++){cout<<postexp[n];if(!(postexp[n]>='0'&&postexp[n]<='9')&&postexp[n]!=' ')cout<<' ';}cout<<endl;postexp[i++]=' '; //用' '标识一个数值串结束}else{int n;switch(precede(op.data[op.top],*exp)){case -1: //为运算符情况下t=exp;op.top++;op.data[op.top]=*exp;cout<<*exp;if(*exp=='s')cout<<"in";if(*exp=='c')cout<<"os";if(*exp=='t')cout<<"an";cout<<"\t";exp++;cout.setf(ios::left);cout<<setw(20);cout<<t;for(n=1;n<=op.top;n++)cout<<op.data[n];cout<<"\t\t ";for(n=0;n<i;n++){cout<<postexp[n];if(!(postexp[n]>='0'&&postexp[n]<='9')&&postexp[n]!=' ')cout<<' ';}cout<<' ';cout<<endl;break;case 0: //只有括号满足这一情况op.top--; //此时将退栈cout<<*exp;if(*exp=='s')cout<<"in";if(*exp=='c')cout<<"os";if(*exp=='t')cout<<"an";cout<<"\t";exp++;cout.setf(ios::left);cout<<setw(20);cout<<t;for(n=1;n<=op.top;n++)cout<<op.data[n];cout<<"\t\t ";for(n=0;n<i;n++){cout<<postexp[n];if(!(postexp[n]>='0'&&postexp[n]<='9')&&postexp[n]!=' ')cout<<' ';}cout<<endl;break;case 1: //栈顶运算符优先级高于当前运算符postexp[i++]=op.data[op.top];op.top--;break;}}}while(op.data[op.top]!='=') //此时exp扫描完毕,退栈到'='为止{postexp[i++]=op.data[op.top];op.top--;}postexp[i]='\0'; //给postexp表达式添加结束标识cout<<"\t\t\t\t\t\t";for(int p=0;postexp[p]!='\0';p++){cout<<postexp[p];if(!(postexp[p]>='0'&&postexp[p]<='9')&&postexp[p]!=' ') cout<<' ';}cout<<endl;}/////////////////////////////////////////////////////////////////////int check(char *exp){int i=0,flag=1;while(exp[i]!='\0') //判断运算符号是否表达正确{if(exp[i]>='0' && exp[i]<='9' || exp[i]=='.' || exp[i]=='-' || exp[i]=='+' || exp[i]=='*' || exp[i]=='(' || exp[i]==')'|| exp[i]=='/' || exp[i]=='^' ||exp[i]=='!'|| exp[i]=='d' || exp[i]=='s' || exp[i]=='c' || exp[i]=='t' || exp[i]=='e');else flag=0;if(exp[0]=='+'||exp[0]=='-'||exp[0]=='*'||exp[0]=='/'||exp[0]=='^ '||exp[0]=='.'||exp[0]=='!') //表达式第一项不出现加减乘除flag=0;if(i!=0&&(exp[i]=='s' || exp[i]=='c' || exp[i]=='t' || exp[i]=='e')&&(exp[i-1]>='0' && exp[i-1]<='9' || exp[i-1]=='.'))flag=0;if((exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/'||exp[i]==' ^'||exp[i]=='!')&& //'+'、'-'、'*'、'/'、'^'等运算符号后面必须接一个数值或其他!(exp[i+1]>='0' && exp[i+1]<='9'|| exp[i+1]=='e' || exp[i+1]=='s'|| exp[i+1]=='c' || exp[i+1]=='t' || exp[i+1]=='('))flag=0;i++;}return flag;}///////////////////////////////////////////////////////////////////// ///////double compvalue(char *postexp) //计算后缀表达式的值{struct{double data[max]; //存放数值int top; //栈指针}st; //定义数值栈int k;double d1=0,d,a,b,c;st.top=-1;while(*postexp!='\0') //postexp字符串未扫描完时循环{switch(*postexp){case'+': //判定为'+'号a=st.data[st.top];st.top--; //退栈取数值ab=st.data[st.top];st.top--; //退栈取数值bc=a+b; //计算cst.top++;st.data[st.top]=c; //将计算结果进栈break;case'-': //判定为'-'号a=st.data[st.top];st.top--; //退栈取数值ab=st.data[st.top];st.top--; //退栈取数值bc=b-a; //计算cst.top++;st.data[st.top]=c; //将计算结果进栈break;case'*': //判定为'*'号a=st.data[st.top];st.top--; //退栈取数值ab=st.data[st.top];st.top--; //退栈取数值bc=a*b; //计算cst.top++;st.data[st.top]=c; //将计算结果进栈break;case'/': //判定为'/'号a=st.data[st.top];st.top--; //退栈取数值ab=st.data[st.top];st.top--; //退栈取数值bif(a!=0){c=b/a; //计算cst.top++;st.data[st.top]=c; //将计算结果进栈}else{cout<<"\n\t除零错误\n";exit(0);//异常退出}break;case'^': //判定为'^'号int i;a=st.data[st.top];st.top--; //退栈取数值ab=st.data[st.top];st.top--; //退栈取数值bc=1; //计算cfor(i=0;i<a;i++)c*=b;st.top++;st.data[st.top]=c; //将计算结果进栈break;case'e': //判定为'exp'号a=st.data[st.top];st.top--; //退栈取数值ac=exp(a); //计算cst.top++;st.data[st.top]=c; //将计算结果进栈break;case'!': //判定为'^'号a=st.data[st.top];st.top--; //退栈取数值a c=1; //计算cfor(i=1;i<a+1;i++)c=c*i;st.top++;st.data[st.top]=c; //将计算结果进栈break;case 't':a=st.data[st.top];st.top--;c=tan(a);st.top++;st.data[st.top]=c;break;case 's':a=st.data[st.top];st.top--;c=sin(a);st.top++;st.data[st.top]=c;break;case 'c':a=st.data[st.top];st.top--;c=cos(a);st.top++;st.data[st.top]=c;break;default://处理数字字符d=0;while(*postexp>='0'&&*postexp<='9'||*postexp=='.') //判定为数字字符{if(*postexp>='0'&&*postexp<='9'){d=10*d+*postexp-'0';//将连续的数字字符转换成对应的数值存放到d中postexp++;}else{*postexp++;i=0;while(*postexp>='0'&&*postexp<='9'){d1=10*d1+*postexp-'0';*postexp++;i++;}for(k=0;k<i;k++)d1=d1/10;}}d=d+d1;d1=0;st.top++;st.data[st.top]=d;break;}postexp++; //继续处理其他字符}return(st.data[st.top]);}int check2(char *postexp) ///判断是否括号对称{while(*postexp!='\0'){if(*postexp=='('||*postexp==')')return 1;*postexp++;}return 0;}void main(){cout<<"----------------------------------------------"<<endl;cout<<"本计算器包含的运算符有:"<<endl;cout<<"+ - * /"<<endl;cout<<"^ !"<<endl;cout<<"sin() cos() tan() exp()"<<endl;cout<<"(s表示sin,c表示cos,t表示tan,e表示exp)\n"<<endl;cout<<" By WeizhongYoung"<<endl;cout<<"------------------------------------------------"<<endl;char h='y';while(h=='y'||h=='Y') //当st=='y'/'Y'时循环{char exp[max],postexp[max];cout<<"请输入表达式:";cin>>exp;cout<<endl;if(!check(exp)&&!check2(postexp)) //判断是否输入错误{cout<<"输入有误,请重新输入!"<<endl;continue;}else{cout<<"输出中缀表达式:"<<exp<<endl;trans(exp,postexp);cout<<"输出n逆波兰表达式:";for(int p=0;postexp[p]!='\0';p++){cout<<postexp[p];if(!(postexp[p]>='0'&&postexp[p]<='9')&&postexp[p]!=' ')cout<<' ';}cout<<endl;cout<<"输出表达式的值为:"<<compvalue(postexp)<<endl;}cout<<"是否继续运行(Y,N),输入Y/y继续,输入n/N退出: "; //用户输入是否要继续使用计算器cin>>h;cout<<endl;}exit(0);}。

c语言计算器加减乘除开方混合计算和清零代码

c语言计算器加减乘除开方混合计算和清零代码

C语言计算器加减乘除开方混合计算和清零代码一、介绍计算器是人们日常生活中常用的工具之一,通过计算器可以进行加减乘除等基本运算,还可以进行开方等复杂运算。

本文将介绍使用C语言编写计算器的加减乘除开方混合计算和清零代码。

二、加法运算代码加法运算是计算器最基本的运算之一,以下是C语言中加法运算的代码示例:```c#include <stdio.h>int m本人n() {int num1, num2, sum;printf("请输入两个整数:");scanf("d d", num1, num2);sum = num1 + num2;printf("它们的和是:d\n", sum);return 0;}```三、减法运算代码接下来是减法运算的C语言代码示例:```c#include <stdio.h>int m本人n() {int num1, num2, diff;printf("请输入两个整数:");scanf("d d", num1, num2);diff = num1 - num2;printf("它们的差是:d\n", diff);return 0;}```四、乘法运算代码乘法运算是计算器中常用的运算之一,以下是C语言中乘法运算的代码示例:```c#include <stdio.h>int m本人n() {int num1, num2, product;printf("请输入两个整数:");scanf("d d", num1, num2);product = num1 * num2;printf("它们的积是:d\n", product);return 0;}```五、除法运算代码除法运算涉及到除数不能为0的情况,以下是C语言中除法运算的代码示例:```c#include <stdio.h>int m本人n() {float num1, num2, quotient;printf("请输入两个数:");scanf("f f", num1, num2);if(num2 != 0) {quotient = num1 / num2;printf("它们的商是:.2f\n", quotient);} else {printf("除数不能为0!\n");}return 0;}```六、开方运算代码开方运算是比较复杂的运算之一,以下是C语言中开方运算的代码示例:```c#include <stdio.h>#include <math.h>int m本人n() {float num, sqrt_num;printf("请输入一个数:");scanf("f", num);if(num >= 0) {sqrt_num = sqrt(num);printf("它的开方是:.2f\n", sqrt_num);} else {printf("输入的数不能为负数!\n");}return 0;}```七、混合计算代码有时候计算器需要进行混合运算,即在一个表达式中包含多种运算,以下是C语言中混合计算的代码示例:```c#include <stdio.h>int m本人n() {float num1, num2, result;char operator;printf("请输入一个表达式(如:2+3*4):");scanf("f c f", num1, operator, num2);switch(operator) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '*':result = num1 * num2;break;case '/':if(num2 != 0) {result = num1 / num2;} else {printf("除数不能为0!\n"); return 1;}break;default:printf("无效的运算符!\n"); return 1;}printf("结果是:.2f\n", result);return 0;}```八、清零代码最后一个功能是清零,即将计算器上的结果归零,以下是C语言中清零的代码示例:```c#include <stdio.h>int m本人n() {float result = 0;printf("当前结果为:.2f\n", result);char choice;printf("是否清零?(Y/N):");scanf(" c", choice);if(choice == 'Y' || choice == 'y') {result = 0;printf("已清零,当前结果为:.2f\n", result);} else if(choice == 'N' || choice == 'n') {printf("未清零,当前结果为:.2f\n", result);} else {printf("无效的输入!\n");}return 0;}```九、结论在本文中,我们以C语言的形式介绍了计算器的加减乘除开方混合计算和清零代码。

C语言实现简易计算器(可作加减乘除)

C语言实现简易计算器(可作加减乘除)

C语⾔实现简易计算器(可作加减乘除)C语⾔实现简易计算器(加减乘除)计算器作为课设项⽬,已完成答辩,先将代码和思路(注释中)上传⼀篇博客已增添、修改、整理⾄⽆错且可正常运⾏虽使⽤了栈,但初学者可在初步了解栈和结构语法后理解代码#include <stdlib.h>#include <stdio.h>#include <string.h>#define IsDouble 0#define IsChar 1//_______________________________________________________________________________________________________________________________________________________ //1.⽀持浮点数和字符的栈typedef struct {char * buffer;int typesize;int top;int max;} stack;stack * CreateStack(int max, int typesize);//创建⼀个栈void DestroyStack(stack *);//释放栈的空间void ClearStack(stack *);//清空栈int Push(stack *, void *);//⼊栈int Pop(stack *, void *);//出栈int GetSize(stack *s);//得到栈的⼤⼩int GetTop(stack *, void *);//找到栈顶int IsEmpty(stack *);//判断是否为空栈,空则下溢int IsFull(stack *);//判断栈是否已满,满则溢出stack * CreateStack(int max, int typesize){stack * s = (stack*)malloc(sizeof(stack));//为栈s malloc内存if (!s) return 0;//为结构中buffer元素malloc内存s->buffer = (char *)malloc(sizeof(char) * max * typesize);if (!s->buffer) return 0;//初始化结构中的栈顶,最⼤值,类型⼤⼩s->top = -1;s->max = max;s->typesize = typesize;return s;}void DestroyStack(stack* s){free(s->buffer);//先释放buffer的空间free(s);//在释放s的空间}void ClearStack(stack * s){s->top = -1;//清空栈(栈头位置归零)}int Push(stack * s, void * data){if (IsFull(s)) return 0;//如果栈已满则return 0,防⽌溢出//栈未满则将栈头移动打动下⼀位置,并将data中的元素拷⼊栈中buffer的第top位置s->top++;memcpy(s->buffer + s->top*s->typesize, data, s->typesize);//⼊栈成功return 1return 1;}int Pop(stack * s, void * data){if (IsEmpty(s)) return 0;//出栈判断栈是否为空,若为空则return 0//栈未空则将buffer中top位置的字符拷⼊data记录,并让栈头向前移动⼀个位置memcpy(data, s->buffer + s->top*s->typesize, s->typesize);s->top--;//成功则return 1return 1;}int GetSize(stack *s){return s -> top+1;//栈头位置+1得到⼤⼩}int GetTop(stack *s, void * data){if (IsEmpty(s)) return 0;//如果栈空return 0//栈不为空则将top位置的字符拷回data记录,得到栈头memcpy(data, s->buffer + s->top*s->typesize, s->typesize);//成功则return 1;return 1;}int IsEmpty(stack * s){return s->top == -1;//如果top为-1则栈空}int IsFull(stack * s){return s->top == s->max-1;//如果top为max-1则栈满}//___________________________________________________________________________________________________________________________________________________ //2.定义⼀个cal类型,其中data存数时sign为IsDouble,存字符时,sign为Ischartypedef struct {double data;char sign;} cal;//3.查找对应符号(找到则返回该符号下标)(找不到则说明该部分为数字返回-1)int SearchCode(char ch){char * code = "+-*/()@";//@为终⽌符,算式输⼊结束int index = 0;//while (code[index]){if (code[index] == ch) return index;index++;}return -1;}//4.得到两个符号间的优先级//与SearchCode相对应,char GetPriority(char ch, char next){//创建⼀个perferen表,第i⾏(列)对应SearchCode函数中code中的第i个字符char perferen[7][7] = {">><<<>>",">><<<>>",">>>><>>",">>>><>>","<<<<<=E",">>>>E>>","<<<<<E="};//找到两个形参对应的字符int c = SearchCode(ch);int n = SearchCode(next);//如果找不到对应运算符(字符不是运算符⽽是为数字)return Eif (c==-1 || n==-1) return 'E';//如果找到两个对应运算符则按照优先级表返回两个运算符的优先级return perferen[c][n];}//5.四则运算double add(double a, double b) { return a+b; }double sub(double a, double b) { return a-b; }double mul(double a, double b) { return a*b; }double ddiv(double a, double b) { return a/b; }//整合四种运算double calcu(double a, char ch, double b){double (*calculation[4])(double,double) = {add,sub,mul,ddiv};return calculation[SearchCode(ch)](a,b);}//6.检测字符串int CheckStr(char * buffer){int n;//遍历字符串确保算式中⽆⾮法字符若检测到⾮法字符return 0,若都合法则return 1for (n = 0;buffer[n];n++){if ((SearchCode(buffer[n]) != -1 || buffer[n] == '.' || (buffer[n] >= '0' && buffer[n] <= '9')) && buffer[n] != '@') continue;else return 0;}buffer[n] = '@';//加上终⽌符,表⽰算式结束buffer[n+1] = '\0';return 1;}//7.得到数据转化为double类型存⼊rsint GetDigit(char * buffer, int * n, double * rs){char str[30];int i,j = 0;for (i = 0;SearchCode(buffer[*n]) == -1;i++){str[i] = buffer[*n];//从*n位置开始,将这⼀串数字字符存⼊str(*n)++;}str[i] = '\0';for (i = 0;str[i];i++){if (str[i] == '.') j++;}//如果⼀段⼩数有多个⼩数点或⼩数点在数字⾸尾,return 0if (j>1 || str[i-1] == '.' || str[0] == '.') return 0;//rs接收转化为double的数据*rs = atof(str);//操作成功return 1return 1;}//8.将⽤户输⼊的buffer字符串转化为可供程序运算的calstr数组int resolu(char * buffer, cal * calstr){int i = 0, j = 0;cal c;while (buffer[i]){if (SearchCode(buffer[i]) == -1){//如果得到数据不成功则return 0if (GetDigit(buffer,&i, &c.data) == 0) return 0;//如果成功得到数据则在c.sign标记为浮点数c.sign = IsDouble;//将c存⼊数组calstr中calstr[j++] = c;}else{//若符号为运算符//判断正负号if (buffer[i] == '-' && (buffer[i-1] == '('||buffer[i-1] == '+'||buffer[i-1] == '-'||buffer[i-1] == '*'||buffer[i-1] == '/') || (i==0 && buffer[0] == '-')){ i++;if (GetDigit(buffer,&i, &c.data) == 0) return 0;//在符号的下⼀位开始查找,若找不到数字return 0//否则,给数字取相反数,c.sign标记为浮点数,存⼊calstr中c.data = 0 - c.data;c.sign = IsDouble;calstr[j++] = c;} else//如果是正号,与符号处理⽅式同理if (buffer[i] == '+' && (buffer[i-1] == '('||buffer[i-1] == '+'||buffer[i-1] == '-'||buffer[i-1] == '*'||buffer[i-1] == '/') || (i==0 && buffer[0] == '+')){ i++;if (GetDigit(buffer, &i, &c.data) == 0) return 0;c.sign = IsDouble;calstr[j++] = c;}else{//如果不是正负号,则为运算符,先强制转换为double类型存在c.data⾥,然后c.sign标记为char类型,存⼊calstrc.data = (double)buffer[i++];c.sign = IsChar;calstr[j++] = c;}}}//操作蔡成功则return 1return 1;}//9.计算出结果int result(cal * calstr, double * rs){stack * pst = CreateStack(100,sizeof(char));//运算符栈stack * pnd = CreateStack(100,sizeof(double));//数据栈double num1,num2;int n = 0;char ch = '@';Push(pst, &ch);//在转换得到的calstr中遍历直到终⽌符'@"while(ch != '@' || !(calstr[n].sign == IsChar && (char)calstr[n].data == '@')){//如果calstr的n位上是浮点数,则将这个data压栈进⼊数据栈pnd中if (calstr[n].sign == IsDouble){Push(pnd, &(calstr[n].data));n++;}//反之,如果是运算符,则要检测优先级else{switch( GetPriority(ch, (char)calstr[n].data)){//如果运算符优先级较⼩,则让ch等于优先级⼤的符号并压⼊符号栈pst中case '<':ch = (char)calstr[n].data;Push(pst, &ch);n++;break;//如果结果为等号,让符号出栈暂存到ch中case '=':if (!Pop(pst, &ch)) return 0;n++;break;//如果ch优先级较⾼,则将前两个数字及运算符出栈,分别储存⾄num2,ch,num1中,进⾏运算,得到的结果再次压栈进⼊pnd中 case '>':if (!(Pop(pnd,&num2) && Pop(pst,&ch) && Pop(pnd,&num1))) return 0;num1 = calcu(num1,ch,num2);Push(pnd, &num1);break;//如果符号顺序出错,return 0case 'E':return 0;}}//检测是否可以得到栈顶符号,栈空则return 0if (!GetTop(pst, &ch)) return 0;}//如果栈中得到了最终结果,并且取出pnd中的最终结果到rs,return 1if (GetSize(pnd) == 1 && GetTop(pnd,rs)){DestroyStack(pst);DestroyStack(pnd);return 1;}//否则 return 0else{return 0;}}//10.⽤户交互函数void treatment(){char buffer[100];//⽤户输⼊的字符串(算式)cal calstr[50];//计算⽤的数组double rs = 0;//计算结果printf("Enter your equation:");gets(buffer);//让⽤户输⼊算式buffer//⽤户不输⼊"exit"就不退出while (!(buffer[0]=='e' && buffer[1]=='x' && buffer[2]=='i' && buffer[3]=='t')){//检查buffer中字符君合法,成功将buffer转化为⽤于计算的calstr数组,成功计算出结果存⼊rsif (CheckStr(buffer) && resolu(buffer,calstr) && result(calstr,&rs)){printf("\n%lf\n",rs);}else{printf("\nError!\n");}printf("Enter \"exit\"to quit");printf("\nEnter your equation:");gets(buffer);//再次让⽤户输⼊算式}printf("\nbye\n");}//11.主函数int main(){treatment();}参考⽂献链接如下[参考⽂献]()。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define N 500
void shift(int *,int n);
void plus(int x[], int y[], int z[]);
void minus(int x[], int y[], int z[]);
void show(int z[]);
void input(int x[], int y[], char *op);
int length(int a[]);
int greater(int x[], int y[]);
void calculation(int x[], int y[], int z[], char oper, int *sign); void main()
{
int exp1[10][N], exp2[10][N], result[10][N + 1], sign[10];
char option[10];
int i, n;
printf("请输入你要计算的算式数目:\n");
scanf("%d", &n);
getchar();
printf("请依次输入各算式:\n");
for(i = 0; i < n; i++)
{
printf("第%d个算式的表达式为: ", i + 1);
input(exp1[i], exp2[i], &option[i]);
calculation(exp1[i], exp2[i], result[i], option[i], &sign[i]); }
for(i = 0; i < n; i++)
{
printf("第%d个算式的结果是: ", i + 1);
if (!sign[i])
putchar('-');
show(result[i]);
}
system("pause");
}
void plus(int x[], int y[], int z[])
{
int i, carry = 0;
for(i = 0;i < N + 1; i++)
*(z+i) = 0;
for(i = 0;i < N; i++)
{
*(z+i)=*(x+i)+*(y+i)+carry;
carry=(*(z+i)-*(z+i)%10)/10;
*(z+i)%=10,*(z+i)+='0';
}
*(z+N)=carry+'0';
}
void minus(int x[], int y[], int z[])
{
int i, n, carry = 0, temp;
for(i = 0;i < N + 1; i++)
*(z+i) = 0;
n = length(x) > length(y) ? length(x) : length(y); for(i = 0;i < n; i++)
{
temp = *(x + i) - *(y + i);
if(temp >= 0)
{
*(z + i) = temp - carry;
if(*(z + i) < 0)
{
*(z + i) += 10;
carry = 1;
}
else
carry = 0;
}
else
{
*(z + i) = temp + 10 - carry;
carry = 1;
}
if(*(z + i) > 9)
*(z + i) -= 10;
*(z+i)+='0';
}
for(i = N; i >= n; i--)
*(z + i) = '0';
}
void shift(int *a,int n)
{
int k,len,sft;
len=N-1-n;
sft=N-len;
for(k=0;k<len;k++)
*(a+k)=*(a+k+sft);
for(k=len;k<N;k++)
*(a+k)=0;
}
void show(int z[])
{
int i, flag;
flag = 0;
for(i = N; i >= 0; i--)
{
if(!flag && *(z + i)!='0') flag = 1;
if(flag) putchar(*(z+i));
}
putchar('\n');
}
void input(int x[], int y[], char *op)
{
int i;
i = N - 1;
while(i >= 0 && isdigit(*(x + i) = getchar())) *(x + i--) -= '0';
*op = (char)(*(x + i));
if(i >= 0) shift(x,i);
i = N - 1;
while(i >= 0 && isdigit(*(y + i) = getchar())) *(y + i--) -= '0';
if(i >= 0) shift(y,i);
}
int length(int a[])
{
int i, n = 0;
if(!a[N - 1])
{
n = 1;
for(i = N - 1; i >= 0; i--)
{
if(!a[i - 1])
n++;
else
break;
}
}
return N - n;
}
int greater(int x[], int y[])
{
int m = length(x), n = length(y);
if(m > n)
return 1;
else if(m < n)
return 0;
else if(x[n - 1] > y[n - 1])
return 1;
else
return 0;
}
void calculation(int x[], int y[], int z[], char oper, int* sign) {
switch(oper)
{
case '+':
plus(x, y, z);
break;
case '-':
if (greater(x, y))
{
minus(x, y, z);
*sign = 1;
}
else
{
minus(y, x, z);
*sign = 0;
}
break;
}
}。

相关文档
最新文档