安卓开发的自制计算器源代码

合集下载

24点计算器源代码

24点计算器源代码
goto begin;
}
if((a*b)%c==0)
{if(a*b/c+d==24)
{printf("%d*%d/%d+%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b/c-d==24)
getchar();
goto begin;
}
if(a+b+c-d==24)
{printf("%d+%d+%d-%d=24\n",a,b,c,d);
getchar();
goto begin;
}
if(a*b+c+d==24)
{printf("%d*%d+%d+%d=24\n",a,b,c,d);
SetScreen(0);
printf("请输入4个数字(1-13)");
for(num[aaa=0]=0;num[aaa=0]<1||num[aaa=0]>13;num[aaa=0]=inputn(20));
for(num[aaa=1]=0;num[aaa=1]<1||num[aaa=1]>13;num[aaa=1]=inputn(25));
long value;
char numberc[11];
char string[12];
row=start/20;
col=start%20;
strcpy(numberc,"0bnmghjtyu");
memset(string,0,12);

简易计算器(源码)

简易计算器(源码)

简易计算器(源码)以下是一个简易计算器的Python源码,用于执行基本的加、减、乘、除运算:```python#简易计算器def add(x, y):"""相加"""return x + ydef subtract(x, y):"""相减"""return x - ydef multiply(x, y):"""相乘"""return x * ydef divide(x, y):"""相除"""if y != 0:return x / yelse:return "除数不能为零!"print("选择操作:")print("1. 相加")print("2. 相减")print("3. 相乘")print("4. 相除")choice = input("输入你的选择(1/2/3/4): ")num1 = float(input("输入第一个操作数: "))num2 = float(input("输入第二个操作数: "))if choice == '1':print(num1, "+", num2, "=", add(num1, num2))elif choice == '2':print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3':print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4':print(num1, "/", num2, "=", divide(num1, num2)) else:print("无效的选择")```以上代码中,我们定义了四个函数:`add`、`subtract`、`multiply`和`divide`,分别用于执行相加、相减、相乘和相除的操作。

计算器源代码

计算器源代码

#include<stdio.h>#include<iostream.h>#include<stdlib.h>#include<string.h>#include<ctype.h>typedef float DataType;typedef struct{DataType *data;int max;int top;}Stack;void SetStack(Stack *S,int n){S->data=(DataType*)malloc(n*sizeof(DataType)); if(S->data==NULL){printf("overflow");exit(1);}S->max=n;S->top=-1;}void FreeStack(Stack *S){free(S->data);}int StackEmpty(Stack *S){if(S->top==-1)return(1);return(0);}DataType Peek(Stack *S){if(S->top==S->max-1){printf("Stack is empty!\n");exit(1);}return(S->data[S->top]);}void Push(Stack *S,DataType item){if(S->top==S->max-1){printf("Stack is full!\n");exit(1);}S->top++;S->data[S->top]=item;}DataType Pop(Stack *S){if(S->top==-1){printf("Pop an empty stack!\n");exit(1);}S->top--;return(S->data[S->top+1]);}typedef struct{char op;int inputprecedence;int stackprecedence;}DataType1;typedef struct{DataType1 *data;int max;int top;}Stack1;void SetStack1(Stack1 *S,int n){S->data=(DataType1*)malloc(n*sizeof(DataType1)); if(S->data==NULL){printf("overflow");exit(1);}S->max=n;S->top=-1;}void FreeStack1(Stack1 *S){free(S->data);}int StackEmpty1(Stack1 *S){if(S->top==-1)return(1);return(0);}DataType1 Peek1(Stack1 *S){if(S->top==S->max-1){printf("Stack1 is empty!\n");exit(1);}return(S->data[S->top]);}void Push1(Stack1 *S,DataType1 item) {if(S->top==S->max-1){printf("Stack is full!\n");exit(1);}S->top++;S->data[S->top]=item;}DataType1 Pop1(Stack1 *S){if(S->top==-1){printf("Pop an empty stack!\n");exit(1);}S->top--;return(S->data[S->top+1]);}DataType1 MathOptr(char ch){DataType1 optr;optr.op=ch;switch(optr.op){case'+':case'-':optr.inputprecedence=1;optr.stackprecedence=1;break;case'*':case'/':optr.inputprecedence=2;optr.stackprecedence=2;break;case'(':optr.inputprecedence=3;optr.stackprecedence=-1;break;case')':optr.inputprecedence=0;optr.stackprecedence=0;break;}return(optr);}void Evaluate(Stack *OpndStack,DataType1 optr) {DataType opnd1,opnd2;opnd1=Pop(OpndStack);opnd2=Pop(OpndStack);switch(optr.op){case'+':Push(OpndStack,opnd2+opnd1);break;case'-':Push(OpndStack,opnd2-opnd1);break;case'*':Push(OpndStack,opnd2*opnd1);break;case'/':Push(OpndStack,opnd2/opnd1);break;}}int isoptr(char ch){if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='(')return(1);return(0);}void Infix(char *str){int i,k,n=strlen(str);char ch,numstr[10];DataType opnd;DataType1 optr;Stack OpndStack;Stack1 OptrStack;SetStack(&OpndStack,n);SetStack1(&OptrStack,n);k=0;ch=str[k];while(ch!='=')if(isdigit(ch)||ch=='.'){for(i=0;isdigit(ch)||ch=='.';i++){numstr[i]=ch;k++;ch=str[k];}numstr[i]='\0';opnd= atof(numstr);Push(&OpndStack,opnd);}elseif(isoptr(ch)){optr=MathOptr(ch);while(Peek1(&OptrStack).stackprecedence>=optr.inputprecedence) Evaluate(&OpndStack,Pop1(&OptrStack));Push1(&OptrStack,optr);k++;ch=str[k];}else if(ch==')'){optr=MathOptr(ch);while(Peek1(&OptrStack).stackprecedence>=optr.inputprecedence) Evaluate(&OpndStack,Pop1(&OptrStack));Pop1(&OptrStack);k++;ch=str[k];}while(!StackEmpty1(&OptrStack))Evaluate(&OpndStack,Pop1(&OptrStack));opnd=Pop(&OpndStack);cout<<"你输入表达式的计算结果为"<<endl;printf("%-6.2f\n",opnd);FreeStack(&OpndStack);FreeStack1(&OptrStack);}void main(){cout<<"请输入你要计算的表达式,并以“=”号结束。

简易计算器代码

简易计算器代码
connect(button[9],SIGNAL(clicked()),this,SLOT(setOper()));
button[10] = new QPushButton(buttontext[10],this,buttontext[10]); // “—”
button[10]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
edit->setText(tr("%1").arg(0)); //这个函数可获取文本编辑框中光标的位置,并显示在状态栏中
//setText(tr(“%1行%2列”).arg(rowNum).arg(colNum));
initialize(); //调用初始化函数
}
void CWidget::initialize()
cwidget.cpp
#include "cwidget.h"
#define KEY_CLR "CLR"
#define KEY_ADD "+"
#define KEY_SUB "-"
#define KEY_MUL "*"
#define KEY_DIV "/"
#define KEY_EQ "="
#define KEY_0 "0"
{
delete edit;
delete *button;
delete mainLayout;
delete topLayout;
delete bottomLayout;
}
void CWidget::calculate() //响应按键“=”,调用计算功能

基于安卓的计算器的设计与实现

基于安卓的计算器的设计与实现

基于安卓的计算器的设计与实现在设计和实现基于安卓的计算器时,需要考虑以下几个重要的方面:用户界面设计、计算逻辑实现和功能扩展。

首先,用户界面设计是一个计算器的重要组成部分。

一个简洁直观、易于使用的界面可以提供良好的用户体验。

可以考虑使用标准计算器的布局,包括数字键盘、运算符按钮和显示结果的文本框。

此外,还可以添加一些额外的功能,比如一键清除按钮、括号按钮和科学计算模式切换按钮等。

考虑到不同屏幕尺寸和定制化需求,应该使用相对布局或者网格布局来放置UI元素。

其次,计算逻辑实现是计算器的核心部分。

需要支持基本的四则运算,包括加法、减法、乘法和除法。

为了实现连续运算和操作优先级,可以使用栈或者逆波兰表示法。

在用户输入数字和运算符时,应该及时更新显示结果的文本框。

另外,还应该考虑异常情况的处理,比如除数为零的情况。

最后,功能扩展是一个好的设计的关键。

除了基本的四则运算,还可以考虑添加一些其他功能,比如开方、求平方、取余、倒数、阶乘等。

另外,还可以添加一个历史记录功能,记录用户的计算过程和结果,方便用户查看和回顾。

此外,还可以添加一个单位转换功能,支持常见的长度、重量、温度、时间、速度等单位之间的转换。

在实际开发中,可以使用Android Studio作为开发工具。

Android Studio提供了丰富的界面设计工具和模拟器,可以方便地进行界面布局和调试。

可以使用Java语言进行开发,使用Android提供的API来处理用户输入和展示结果。

在代码实现中,可以使用按钮点击事件来处理用户输入。

当用户点击数字按钮时,将所点击的数字添加到一个临时变量中,当用户点击运算符按钮时,将临时变量中的数字和运算符添加到运算式中,并清空临时变量。

当用户点击等号按钮时,根据运算式中的数字和运算符进行实际的计算,并将结果显示在结果文本框中。

当用户点击清空按钮时,将运算式和结果文本框清空。

除了基本逻辑实现外,还可以进行一些优化和改进。

在Android Studio中开发一个简单的计算器应用

在Android Studio中开发一个简单的计算器应用

在Android Studio中开发一个简单的计算器应用随着智能手机的普及,计算器应用成为了人们生活中不可或缺的工具之一。

为了满足用户的需求,我们决定在Android Studio中开发一个简单的计算器应用。

这个应用将提供基本的四则运算功能,并且界面简洁易用。

首先,在Android Studio中创建一个新的项目。

选择合适的应用名称和包名。

接下来,我们需要设计应用的用户界面。

我们希望用户界面简洁明了,方便用户使用。

可以考虑使用一个TextView显示计算结果,以及多个Button用于输入数字和操作符。

在开发计算器的过程中,我们需要处理用户的输入并进行相应的计算。

为此,我们可以使用一个EditText控件来接收用户的输入,并通过按钮的点击事件来获取用户点击的数字和操作符。

然后,我们可以使用Java中的数学库来进行四则运算,并将结果显示在TextView中。

通过Android Studio提供的布局编辑器,我们可以轻松地将TextView和Button布局在界面上。

可以使用LinearLayout或RelativeLayout进行布局,根据需要设置各个控件之间的位置和大小。

此外,还可以设置按钮的点击事件,使其能够响应用户的输入。

在处理用户输入时,需要考虑一些特殊情况。

例如,当用户输入非法字符或进行除法时需要进行错误处理。

可以通过在代码中添加条件判断语句来实现这些功能。

另外,还可以添加一些额外的功能,如清除输入、撤销操作等,以提升用户体验。

在开发过程中,可以使用Android Studio提供的调试工具来检查代码是否正常运行。

可以设置断点,逐步执行代码,并查看变量的值。

这些工具能够帮助我们快速定位和修复错误。

为了使应用具有良好的用户体验,我们还可以添加一些动画效果和过渡效果。

例如,当用户点击按钮时,可以添加按钮缩放、淡入淡出等动画效果,使界面更加生动有趣。

除了基本功能以外,我们还可以考虑添加更多的功能来完善应用。

JAVA编写的计算器源代码

JAVA编写的计算器源代码

JAVA编写的计算器源代码// Calculator.javaimport javax.swing.*; // 引入swing库import java.awt.*; // 引入awt库import java.awt.event.*; // 引入awt.event库public class Calculator extends JFrame implements ActionListener//定义按钮private JButton zero;private JButton one;private JButton two;private JButton three;private JButton four;private JButton five;private JButton six;private JButton seven;private JButton eight;private JButton nine;private JButton point;private JButton equal; private JButton plus; private JButton minus; private JButton multiply; private JButton divide; private JButton backspace; private JButton ac;private JButton ce;private JButton sqrt; private JButton sqr;private JButton plus_minus; private JButton delete; private JButton sin;private JButton cos;private JButton tan;private JButton log;private JButton nfactorial; private JButton cubic; private JButton coln;private JButton factorial;//定义文本框private JTextField resulttext;// 定义boolean变量boolean clrdisp = true; // 昵称确定是否清除计算器显示boolean isCalculate = false; // 是否可以执行计算// 定义String变量,用于存储操作符String optr;//定义存储第一个操作数double num1;//初始化构造函数public Calculato//设置布局setLayout(new BorderLayout();//创建面板JPanel northPanel = new JPanel(;JPanel centerPanel = new JPanel(;JPanel southPanel = new JPanel(;//设置面板布局northPanel.setLayout(new FlowLayout(); centerPanel.setLayout(new GridLayout(4, 5)); southPanel.setLayout(new FlowLayout();//设置计算器显示resulttext = new JTextField(28); northPanel.add(resulttext);resulttext.setEditable(false);//初始化按钮zero = new JButton("0");one = new JButton("1");two = new JButton("2");three = new JButton("3");four = new JButton("4");five = new JButton("5");six = new JButton("6");seven = new JButton("7");eight = new JButton("8");nine = new JButton("9");point = new JButton(".");equal = new JButton("=");plus = new JButton("+");minus = new JButton("-"); multiply = new JButton("*"); divide = new JButton("/"); backspace = new JButton("<-"); ac = new JButton("AC");ce = new JButton("CE");sqrt = new JButton("sqrt");sqr = new JButton("sqr");plus_minus = new JButton("+/-");。

JAVA计算器源代码

JAVA计算器源代码

计算器源代码一、计算器源代码文件名:computer1.javaimport java.awt.*;import java.awt.event.*;public class computer1 extends Frame implements ActionListener{//声明窗口类并实现动作事件接口。

Button n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;//声明数字按钮Button op,os,om,od,oe,oc;//声明操作按钮TextField tfd;//声明文本框String flg,rslt;//声明标志串、结果串Panel p1,p2,p3;//声明面板int i1,i2;float flt;computer1(){super("加减乘除计算器");n0 = new Button("0");//实现各按钮n1 = new Button("1");n2 = new Button("2");n3 = new Button("3");n4 = new Button("4");n5 = new Button("5");n6 = new Button("6");n7 = new Button("7");n8 = new Button("8");n9 = new Button("9");op = new Button("加");os = new Button("减");om = new Button("乘");od = new Button("除");oe = new Button("=");oc = new Button("c");tfd = new TextField(20);//实现文本框p1=new Panel();//实现各面板p2=new Panel();p3=new Panel();setLayout(new FlowLayout());//布局设计,用于安排按钮位置p1.add(n0);//将各数字按钮放入p1中p1.add(n1);p1.add(n2);p1.add(n3);p1.add(n4);p1.add(n5);p1.add(n6);p1.add(n7);p1.add(n8);p1.add(n9);p2.add(op);//将各操作按钮放入p2、p3中p2.add(os);p2.add(om);p2.add(od);p3.add(oe);p3.add(oc);setLayout(new BorderLayout());//布局设计,用于安排面板位置add("North",tfd);add("West",p1);add("Center",p2);add("East",p3);n0.addActionListener(this);//注册监听器到各按钮n1.addActionListener(this);n2.addActionListener(this);n3.addActionListener(this);n4.addActionListener(this);n5.addActionListener(this);n6.addActionListener(this);n7.addActionListener(this);n8.addActionListener(this);n9.addActionListener(this);op.addActionListener(this);os.addActionListener(this);om.addActionListener(this);od.addActionListener(this);oe.addActionListener(this);oc.addActionListener(this);addWindowListener(new closeWin());setSize(600,100);//确定窗口的尺寸setVisible(true);}public static void main (String args[]){new computer1();}public void actionPerformed(ActionEvent e){//处理鼠标事件的方法try{//异常处理if(e.getSource()==n0)//按数字键时tfd.setText(tfd.getText()+"0");if(e.getSource()==n1)tfd.setText(tfd.getText()+"1");if(e.getSource()==n2)tfd.setText(tfd.getText()+"2");if(e.getSource()==n3)tfd.setText(tfd.getText()+"3");if(e.getSource()==n4)tfd.setText(tfd.getText()+"4");if(e.getSource()==n5)tfd.setText(tfd.getText()+"5");if(e.getSource()==n6)tfd.setText(tfd.getText()+"6");if(e.getSource()==n7)tfd.setText(tfd.getText()+"7");if(e.getSource()==n8)tfd.setText(tfd.getText()+"8");if(e.getSource()==n9)tfd.setText(tfd.getText()+"9");if(e.getSource()==op){//按加号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "op";}if(e.getSource()==os){//按减号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "os";}if(e.getSource()==om){//按乘号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "om";}if(e.getSource()==od){//按除号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "od";}if(e.getSource()==oe){//按等号键时i2 = Integer.parseInt(tfd.getText());if(flg=="op"){rslt=Integer.toString(i1+i2);}if(flg=="os"){rslt=Integer.toString(i1-i2);}if(flg=="om"){rslt=Integer.toString(i1*i2);}if(flg=="od"){//除法需做小数处理flt=((float)i1)/((float)i2);rslt=Float.toString(flt);}tfd.setText(rslt);}if(e.getSource()==oc){//按清除键时tfd.setText("");flg = "";}}catch(Exception ex){}//扑捉到异常,但不进行处理}}class closeWin extends WindowAdapter{ //关闭窗口public void windowClosing(WindowEvent e){Frame frm=(Frame)(e.getSource());frm.dispose();System.exit(0);}}二、计算器界面三、修改后计算器界面。

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

这是本人自己用安卓编写的计算器,代码的优化可能有所问题,不过大家都在学习阶段,欢迎大家批评指正,还是上源码吧。

先来运行图package com.example.nanchen.exam_1_1;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private Button btn0;private Button btn1;private Button btn2;private Button btn3;private Button btn4;private Button btn5;private Button btn6;private Button btn7;private Button btn8;private Button btn9;private Button btnAdd;private Button btnSub;private Button btnMul;private Button btnDiv;private Button btnDec;private Button btnEqual;private Button btnClear;private Button btnBackSpace;private TextView textView_result;double num1 = 0,num2 = 0,result = 0;//定义存储结果和两个操作数boolean isCheckEqu = false;//检测是否点击了等于int op = 0;//定义运算符boolean hasDec = false;boolean hasOp = false;@Overrideprotected void onCreate Bundle savedInstanceStatesuper.onCreate savedInstanceState;setContentView yout.activity_main;textView_result = TextView findViewById R.id.result;btn0 = Button findViewById R.id.btn0;btn1 = Button findViewById R.id.btn1;btn2 = Button findViewById R.id.btn2;btn3 = Button findViewById R.id.btn3;btn4 = Button findViewById R.id.btn4;btn5 = Button findViewById R.id.btn5;btn6 = Button findViewById R.id.btn6;btn7 = Button findViewById R.id.btn7;btn8 = Button findViewById R.id.btn8;btn9 = Button findViewById R.id.btn9;btnAdd = Button findViewById R.id.btn10;btnSub = Button findViewById R.id.btn11;btnMul = Button findViewById R.id.btn12;btnDiv = Button findViewById R.id.btn13;btnDec = Button findViewById R.id.btn14;btnEqual = Button findViewById R.id.btn15;btnClear = Button findViewById R.id.clear;btnBackSpace = Button findViewById R.id.backSpace;btn0.setOnClickListener this;btn1.setOnClickListener this;btn2.setOnClickListener this;btn3.setOnClickListener this;btn4.setOnClickListener this;btn5.setOnClickListener this;btn6.setOnClickListener this;btn7.setOnClickListener this;btn8.setOnClickListener this;btn9.setOnClickListener this;btnAdd.setOnClickListener this;btnSub.setOnClickListener this;btnDec.setOnClickListener this;btnDiv.setOnClickListener this;btnEqual.setOnClickListener this;btnMul.setOnClickListener this;btnClear.setOnClickListener this;btnBackSpace.setOnClickListener this;@Overridepublic void onClick View vswitch v.getIdcase R.id.btn0:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString0 =textView_result.getText.toString;if myString0.equals"0"myString0 = "";myString0 += "0";textView_result.setText myString0;break;case R.id.btn1:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString1 =textView_result.getText.toString;if myString1.equals"0"myString1 = "";myString1 += "1";textView_result.setText myString1;break;case R.id.btn2:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString2 =textView_result.getText.toString;if myString2.equals"0"myString2 = "";myString2 += "2";textView_result.setText myString2;break;case R.id.btn3:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString3 =textView_result.getText.toString;if myString3.equals"0"myString3 = "";myString3 += "3";textView_result.setText myString3;break;case R.id.btn4:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString4 =textView_result.getText.toString;if myString4.equals"0"myString4 = "";myString4 += "4";textView_result.setText myString4;break;case R.id.btn5:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString5 =textView_result.getText.toString;if myString5.equals"0"myString5 = "";myString5 += "5";textView_result.setText myString5;break;case R.id.btn6:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString6 =textView_result.getText.toString;if myString6.equals"0"myString6 = "";myString6 += "6";textView_result.setText myString6;break;case R.id.btn7:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString7 =textView_result.getText.toString;if myString7.equals"0"myString7 = "";myString7 += "7";textView_result.setText myString7;break;case R.id.btn8:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString8 =textView_result.getText.toString;if myString8.equals"0"myString8 = "";myString8 += "8";textView_result.setText myString8;break;case R.id.btn9:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString9 =textView_result.getText.toString;if myString9.equals"0"myString9 = "";myString9 += "9";textView_result.setText myString9;break;case R.id.btn10:if hasOpdoEqual;//显示出来当前的答案String myStringAdd =textView_result.getText.toString;if myStringAdd.equals nullreturn;num1 = Double.valueOf myStringAdd;textView_result.setText null;// 这里被清除了op = 1;isCheckEqu = false;hasDec = false;hasOp = true;break;case R.id.btn11:if hasOpdoEqual;String myStringSub =textView_result.getText.toString;if myStringSub.equals nullreturn;num1 = Double.valueOf myStringSub;textView_result.setText null;op = 2;isCheckEqu = false;hasDec = false;hasOp = true;break;case R.id.btn12:if hasOpdoEqual;String myStringMul =textView_result.getText.toString;if myStringMul.equals nullreturn;num1 = Double.valueOf myStringMul;textView_result.setText null;op = 3;isCheckEqu = false;hasDec = false;hasOp = true;break;case R.id.btn13:if hasOpdoEqual;String myStringDiv =textView_result.getText.toString;if myStringDiv.equals nullreturn;num1 = Double.valueOf myStringDiv;textView_result.setText null;op = 4;hasDec = false;isCheckEqu = false;hasOp = true;break;case R.id.btn14:if isCheckEqutextView_result.setText null;isCheckEqu=false;if hasDec//如果已经有小数点了,则弹出一个对话框AlertDialog.Builder builder = new AlertDialog.Builder MainActivity.this;builder.setTitle"警告";builder.setMessage"输入错误,一个数字不能有两个小数点!";builder.setNegativeButton"取消", new DialogInterface.OnClickListener@Overridepublic void onClick DialogInterface dialog, int which;builder.show;return;hasDec = true;String myString14 =textView_result.getText.toString;myString14 += ".";textView_result.setText myString14;break;case R.id.btn15:doEqual;hasOp = false;break;case R.id.backSpace:String myString =textView_result.getText.toString;trytextView_result.setText myString.substring0,myString.length -1;catch Exception etextView_result.setText"0";break;case R.id.clear:textView_result.setText"0";break;default:break;/*** 按下等于*/private void doEqualStringmyStringEqu=textView_result.getText.toString;if myStringEqu.equals nullreturn;num2=Double.valueOf myStringEqu;textView_result.setText null;switch opcase 0:result=num2;break;case 1:result=num1+num2;break;case 2:result=num1-num2;break;case 3:result=num1*num2;break;case 4:result=num1/num2;break;default:result=0;break;textView_result.setText String.valueOf result;isCheckEqu=true;<?xml version="1.0" encoding="utf-8"?>LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.nanchen.exam_1_1.MainActivity"<!--显示结果-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/result"android:gravity="end"android:textSize="40sp"android:text="@string/zero"LinearLayoutLinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:orientation="horizontal"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="@string/clear"android:id="@+id/clear"android:textAllCaps="false"android:gravity="center"android:textSize="25sp"Buttonandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="@string/backspace"android:id="@+id/backSpace"android:textAllCaps="false"android:gravity="center"android:textSize="25sp" LinearLayout<!--789/-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/seven"android:id="@+id/btn7"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/eight"android:id="@+id/btn8"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/nine"android:id="@+id/btn9"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/del"android:id="@+id/btn13"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1" LinearLayout<!--456*-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/four"android:id="@+id/btn4"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/five"android:id="@+id/btn5"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/six"android:id="@+id/btn6"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/mul"android:id="@+id/btn12"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1" LinearLayout<!--123- -->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/one"android:textSize="30sp"android:id="@+id/btn1"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/two"android:id="@+id/btn2"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/three"android:id="@+id/btn3"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/sub"android:id="@+id/btn11"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1" LinearLayout<!--0.=+-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/zero1"android:id="@+id/btn0"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/dec"android:id="@+id/btn14"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/equal"android:id="@+id/btn15"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/add"android:id="@+id/btn10"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"LinearLayoutLinearLayoutandroid:layout_weight="4"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="0dp"TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="@string/string1"TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/string2"android:textSize="20sp"LinearLayoutLinearLayout。

相关文档
最新文档