码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算(28日)

时间:2015-05-28 19:43:23      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

     今天是冲刺的最后几天了,我们已经完成了界面的设计以及四则运算的基本功能,但在编程的过程中还遇到了一些问题,我们还在努力尝试调试中。在这个软件中,用户可以计算基本的四则运算,由用户输入算式进行计算,我们先提交一个简单的版本,在以后我们还在继续完善。

 

 

Not

CHECKED

OUT  

CHECKED

OUT

DONE!:

SPRINT

GOAL:

BETA-READY

RELEASE 

四则运算的界面设计

 

 安装ADT,在Java环境下设计

 

四则运算的基本功能

 

 

对按钮事件的处理 

 

 

 四则运算的错误处理

 

 由用户输入及计算输出  

四则运算功能的测试   支持加减乘除的运算  
四则运算功能的新功能   暂无  

 

                                                                   技术分享

 下面是我们的代码,但是在测试中还存在一些错误,我们还在努力找出错误的原因。

技术分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/led"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="3dip"
            android:background="#ccc"
            android:gravity="top|right"
            android:text=""
            android:textColor="#000"
            android:textSize="12pt" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/time"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="time"
            android:textSize="10pt" />

        <Button
            android:id="@+id/pow"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="pow"
            android:textSize="10pt" />

        <Button
            android:id="@+id/sqrt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="sqrt"
            android:textSize="10pt" />

        <Button
            android:id="@+id/clear"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="clear"
            android:textSize="10pt" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/seven"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="7"
            android:textSize="12pt" />

        <Button
            android:id="@+id/eight"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="8"
            android:textSize="12pt" />

        <Button
            android:id="@+id/nine"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="9"
            android:textSize="12pt" />

        <Button
            android:id="@+id/division"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="÷"
            android:textSize="12pt" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/four"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="4"
            android:textSize="12pt" />

        <Button
            android:id="@+id/five"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="5"
            android:textSize="12pt" />

        <Button
            android:id="@+id/six"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="6"
            android:textSize="12pt" />

        <Button
            android:id="@+id/mult"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="*"
            android:textSize="12pt" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/one"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="1"
            android:textSize="12pt" />

        <Button
            android:id="@+id/two"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="2"
            android:textSize="12pt" />

        <Button
            android:id="@+id/three"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="3"
            android:textSize="12pt" />

        <Button
            android:id="@+id/sub"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="-"
            android:textSize="12pt" />
    </LinearLayout>
View Code
技术分享
package com.layoutclss;

import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private Button one;
    private Button two;
    private Button three;
    private Button four;
    private Button five;
    private Button six;
    private Button seven;
    private Button eight;
    private Button nine;
    private Button zero;
    private Button dot;
    private Button add;
    private Button sub;
    private Button mult;
    private Button division;
    private Button amount;
    private Button clear;
    private Button time;
    private Button sqrt;
    private Button pow;
    private EditText led;
    String num1 = "";
    String num2 = "";
    double num3 = 0;
    String rezult = "";
    String sign = null;
    int mark = 0;
    boolean flag = true;
    boolean dotmark = true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        zero = (Button) findViewById(R.id.zero);
        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.two);
        three = (Button) findViewById(R.id.three);
        four = (Button) findViewById(R.id.four);
        five = (Button) findViewById(R.id.five);
        six = (Button) findViewById(R.id.six);
        seven = (Button) findViewById(R.id.seven);
        eight = (Button) findViewById(R.id.eight);
        nine = (Button) findViewById(R.id.nine);
        dot = (Button) findViewById(R.id.dot);
        add = (Button) findViewById(R.id.add);
        sub = (Button) findViewById(R.id.sub);
        mult = (Button) findViewById(R.id.mult);
        division = (Button) findViewById(R.id.division);
        clear = (Button) findViewById(R.id.clear);
        amount = (Button) findViewById(R.id.amount);
        time = (Button) findViewById(R.id.time);
        pow = (Button) findViewById(R.id.pow);
        sqrt = (Button) findViewById(R.id.sqrt);
        led = (EditText) findViewById(R.id.led);

        zero.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "0";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "0";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "0";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        one.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "1";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "1";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "1";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        two.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "2";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "2";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "2";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        three.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "3";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "3";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "3";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        four.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "4";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "4";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "4";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        five.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "5";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "5";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "5";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        six.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "6";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "6";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "6";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        seven.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "7";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "7";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "7";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        eight.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "8";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "8";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "8";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        nine.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0) {
                    num1 = num1 + "9";
                    led.setText(num1);
                } else if (mark == 1) {
                    num2 = num2 + "9";
                    led.setText(num1 + sign + num2);
                } else {
                    num1 = String.valueOf(num3);
                    num2 = num2 + "9";
                    led.setText(num1 + sign + num2);
                }
            }
        });
        add.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (flag) {
                    sign = "+";
                    mark++;
                    flag = false;
                    dotmark = true;
                }
            }
        });
        sub.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (flag) {
                    sign = "-";
                    mark++;
                    flag = false;
                    dotmark = true;
                }
            }
        });
        mult.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (flag) {
                    sign = "*";
                    mark++;
                    flag = false;
                    dotmark = true;
                }
            }
        });
        division.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (flag) {
                    sign = "÷";
                    mark++;
                    flag = false;
                    dotmark = true;
                }
            }
        });
        pow.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (flag) {
                    sign = "^";
                    mark++;
                    flag = false;
                    dotmark = true;
                }
            }
        });
        dot.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (dotmark) {
                    if (mark == 0) {
                        num1 = num1 + ".";
                        led.setText(num1);
                    } else if (mark == 1) {
                        num2 = num2 + ".";
                        led.setText(num1 + sign + num2);
                    } else {
                        num1 = String.valueOf(num3);
                        num2 = num2 + ".";
                        led.setText(num1 + sign + num2);
                    }
                    dotmark = false;
                }
            }
        });
        amount.setOnClickListener(new OnClickListener() { // 等于时发生的运算
            public void onClick(View v) {
                flag = true;
                dotmark = true;
                if (sign.equals("+")) {
                    double x = Double.parseDouble(num1);
                    double y = Double.parseDouble(num2);
                    num3 = x + y;
                    rezult = String.valueOf(num3);
                    led.setText(num1 + sign + num2 + "=" + rezult);
                    num2 = "";
                } else if (sign.equals("-")) {
                    double x = Double.parseDouble(num1);
                    double y = Double.parseDouble(num2);
                    num3 = x - y;
                    rezult = String.valueOf(num3);
                    led.setText(num1 + sign + num2 + "=" + rezult);
                    num2 = "";
                } else if (sign.equals("*")) {
                    double x = Double.parseDouble(num1);
                    double y = Double.parseDouble(num2);
                    num3 = x * y;
                    rezult = String.valueOf(num3);
                    led.setText(num1 + sign + num2 + "=" + rezult);
                    num2 = "";
                } else if (sign.equals("÷")) {
                    double x = Double.parseDouble(num1);
                    double y = Double.parseDouble(num2);
                    num3 = x / y;
                    rezult = String.valueOf(num3);
                    led.setText(num1 + sign + num2 + "=" + rezult);
                    num2 = "";
                } else if (sign.equals("^")) {
                    double x = Double.parseDouble(num1);
                    double y = Double.parseDouble(num2);
                    num3 = Math.pow(x, y);
                    rezult = String.valueOf(num3);
                    led.setText(num1 + sign + num2 + "=" + rezult);
                    num2 = "";
                }
            }
        });
        clear.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                num1 = "";
                num2 = "";
                num3 = 0;
                mark = 0;
                sign = "";
                led.setText("0");
                flag = true;
                dotmark = true;
            }
        });
        sqrt.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mark == 0 && !num1.equals("") && num2.equals("")) {
                    double parent = Double.parseDouble(num1);
                    double root = Math.sqrt(parent);
                    rezult = String.valueOf(root);
                    led.setText(rezult);
                    num1 = "";
                }
            }
        });
        time.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Date date = new Date();
                led.setText(date.toLocaleString());
            }
        });
    }
View Code

 

四则运算(28日)

标签:

原文地址:http://www.cnblogs.com/chenqiuliang/p/4536623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!