作业5.3 升级版

助教您好!我们小组由于昨天的程序是未完成版,在此我感到抱歉,现在我们小组弄好了一个好一点的,毕竟计划赶不上变化,种种原因导致,还请谅解。当然我们组现在这个程序也有不足之处,如时间我们组就没有弄好,还请多多指教,我们组的所有成员在此万分感激!

//主要界面代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hhhhhh.MainActivity$PlaceholderFragment"
    android:background="@drawable/aaa" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:text="0" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_toRightOf="@+id/button2"
        android:text="1" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="2" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button4"
        android:layout_alignBottom="@+id/button4"
        android:layout_toRightOf="@+id/button4"
        android:text="3" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:text="4" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button6"
        android:layout_alignBottom="@+id/button6"
        android:layout_toRightOf="@+id/button6"
        android:text="5" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button7"
        android:layout_alignBottom="@+id/button7"
        android:layout_toRightOf="@+id/button7"
        android:text="6" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button8"
        android:layout_alignBottom="@+id/button8"
        android:layout_toRightOf="@+id/button8"
        android:text="7" />

    <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button6"
        android:layout_toLeftOf="@+id/button7"
        android:text="8" />

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button10"
        android:layout_alignBottom="@+id/button10"
        android:layout_toRightOf="@+id/button10"
        android:text="9" />

    <Button
        android:id="@+id/button12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button11"
        android:layout_alignBottom="@+id/button11"
        android:layout_toRightOf="@+id/button11"
        android:text="." />

    <Button
        android:id="@+id/button13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button12"
        android:layout_alignBottom="@+id/button12"
        android:layout_alignLeft="@+id/button9"
        android:text="确定" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignRight="@+id/button5"
        android:text="开始" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:ems="10"
        android:focusable="false"
        android:text="四则运算小程序欢迎您!" />

    <Button
        android:id="@+id/button14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button13"
        android:layout_below="@+id/button13"
        android:text="清除" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/button14"
        android:layout_alignBottom="@+id/button14"
        android:layout_alignLeft="@+id/button11"
        android:layout_marginLeft="14dp"
        android:ems="10"
        android:inputType="time" 
        android:text="待定!">
        
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText3"
        android:layout_alignBottom="@+id/editText3"
        android:layout_toLeftOf="@+id/editText3"
        android:text="时间" />

</RelativeLayout>




//主要的类方法用来计算

package com.example.jjjjfcfy;



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.example.jjjjfcfy.ooo;

import java.text.DecimalFormat;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

    public class MainActivity extends Activity {
        
        int x;//题目个数
        int y=0;
        int n;
        //int time=0;
        int i=1;
        char[] op = { '+', '-', '*', '/' };
        int[] nov = new int[4];
        int zf;//控制正负
        int zz;//题型控制
        String str = new String();
        String str1 = new String();
        //String str2 = new String();
        int[] num1 = new int[5];
        String s[] = new String[5];
        int num2=0;
        float[] anser=new float[5];//用户的答案
        float[] trueanser=new float[5];//正确答案
        File file;
        int a=0;
        int b=0;
        int bb=0;
        // int c = 0;
        ooo opt=new ooo();
        EditText editText1,editText2,editText3;
        StringBuffer text,text1;
        String number_1="",number_2="";
        boolean clicked=false;
        byte style=0;
        Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10;
        Button button11,button12,button13,button14,button15;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            //time++;
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_main);
            text=new StringBuffer();
            editText1=(EditText)findViewById(R.id.editText2);
            editText2=(EditText)findViewById(R.id.editText1);
            editText3=(EditText)findViewById(R.id.editText3);
            //editText.setFocusable(false);
            button9=(Button) findViewById(R.id.button9);
            button9.setOnClickListener(new Button.OnClickListener(){

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("7");
                    editText1.setText(text.toString());
                    
                }
                
            });
            button10=(Button) findViewById(R.id.button10);
            button10.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("8");
                    editText1.setText(text.toString());
                }
            });
            button11=(Button) findViewById(R.id.button11);
            button11.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("9");
                    editText1.setText(text.toString());
                }
                
            });
            button6=(Button) findViewById(R.id.button6);
            button6.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("4");
                    editText1.setText(text.toString());
                }
                
            });
            button7=(Button) findViewById(R.id.button7);
            button7.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("5");
                    editText1.setText(text.toString());
                }
                
            });
            button8=(Button) findViewById(R.id.button8);
            button8.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("6");
                    editText1.setText(text.toString());
                }
                
            });
            button3=(Button) findViewById(R.id.button3);
            button3.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("1");
                    editText1.setText(text.toString());
                }
                
            });
            button4=(Button) findViewById(R.id.button4);
            button4.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("2");
                    editText1.setText(text.toString());
                }
                
            });
            button5=(Button) findViewById(R.id.button5);
            button5.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("3");
                    editText1.setText(text.toString());
                }
                
            });
            button2=(Button) findViewById(R.id.button2);
            button2.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    text.append("0");
                    editText1.setText(text.toString());
                }
                
            });
            button12=(Button) findViewById(R.id.button12);
            button12.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(clicked){
                        editText1.setText("");
                        clicked=false;
                        text.setLength(0);
                    }
                    if(editText1.getText().toString().indexOf(".")==-1){//已经有.了
                        if(text.length()==0){
                            text.append("0.");
                        }else{
                            text.append(".");
                        }
                        editText1.setText(text.toString());
                    }
                }
                
            });
            button13=(Button) findViewById(R.id.button13);
            button13.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(b==1)
                    {
                        if(editText1.getText().toString().length()!=0)
                        {
                            try
                            {
                                x=Integer.parseInt(editText1.getText().toString());
                                if(x>=0 && x<=5)b++;
                                else Toast.makeText(MainActivity.this, "输入错误", 1).show();
                            }
                            catch(NumberFormatException x)
                            {
                                Toast.makeText(MainActivity.this, "输入错误", 1).show();
                            }
                        }
                        else Toast.makeText(MainActivity.this, "输入误", 1).show();
                        editText1.setText("");
                        number_1="";
                        number_2="";
                        style=0;
                        clicked=false;
                        text.setLength(0);
                    }
                    if(b==2)
                    {
                        if(n!=0)anser[i-2]=Float.parseFloat(editText1.getText().toString());
                        str1="第"+i+"条题目为:\n";
                        n=(int)(Math.random() * 4 + 2);//随机出运算符的数量
                        zz=(int)(Math.random()*2);
                        for(int j=0;j<n;j++)
                        {
                            //zf=(int)(Math.random()*2);
                            //if(zf==0)////    num1[j]=-(int)(Math.random()*100);//产生随机数
                            //else        //
                                num1[j]=(int)(Math.random()*100);
                                if(zz==1)num1[j]=(int)(Math.random()*10+1);
                        }
                        for(int k=0;k<n-1;k++)
                        {
                            nov[k]=(int)(Math.random()*4);
                            if(nov[k]==3&&num1[k+1]==0)
                            {
                                do
                                {
                                    num1[k+1]=(int)(Math.random()*100);
                                }while(num1[k+1]==0);
                            }
                        }
                        str="";
                        if(zz==0)
                        {
                            for(int j=0;j<n;j++)
                            {
                                //if(num1[j]<0)str=str+"("+String.valueOf(num1[j])+")";
                                str=str+String.valueOf(num1[j]);
                                if(j==n-1)
                                {
                                    //try
                                    //{
                                        //int ii=0;
                                        try{
                                            trueanser[i-1]=opt.Math(str);
                                            DecimalFormat df = new DecimalFormat("#.00");
                                            trueanser[i-1]=Float.parseFloat(df.format(trueanser[i-1]));
                                        } catch (Exception e1) {
                                            // TODO 自动生成的 catch 块
                                            e1.printStackTrace();
                                        }
                                    //}
                                    /*catch (Exception e1)
                                    {
                                        System.out.println(e1);
                                        //e1.printStackTrace();
                                    }*/
                                    str=str+"=";
                                    //str=str+String.valueOf(trueanser[i-1]);
                                    editText2.setText(str1+str);
                                    break;
                                }
                                str=str+op[nov[j]];
                            }
                        }
                        if(zz==1)
                        {
                            trueanser[i-1]=1;
                            str=String.valueOf(num1[0])+"!=";
                            for(int j=1;j<=num1[0];j++)
                            trueanser[i-1]=trueanser[i-1]*j;
                            //str=str+String.valueOf(trueanser[i-1]);
                            editText2.setText(str1+str);
                        }
                        if(i==x+1)
                        {
                            b=3;
                            anser[i-2]=Float.parseFloat(editText1.getText().toString());
                        }
                        else i++;
                        editText1.setText("");
                        number_1="";
                        number_2="";
                        style=0;
                        clicked=false;
                        text.setLength(0);
                    }
                    if(b==3)
                    {
                        y=0;
                        for(int j=0;j<x;j++)
                        {
                            if(anser[j]==trueanser[j])y++;
                        }
                        editText2.setText("答题完成了,谢谢您的参与!"+"\n"+"恭喜您!答对了"+y+"道!");
                        b=0;
                        editText1.setText("");
                        number_1="";
                        number_2="";
                        style=0;
                        clicked=false;
                        text.setLength(0);
                    }
                }
            });
            button1=(Button) findViewById(R.id.button1);
            button1.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    editText2.setText("请输入题量!(5以内)");
                    n=0;
                    i=1;
                    b=1;
                }
            });
            button14=(Button) findViewById(R.id.button14);
            button14.setOnClickListener(new Button.OnClickListener(){
                
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    editText1.setText("");
                    number_1="";
                    number_2="";
                    style=0;
                    clicked=false;
                    text.setLength(0);
                }
            });
      //      button15=(Button) findViewById(R.id.button15);
   //         button15.setOnClickListener(new Button.OnClickListener(){
                
          //      public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //for(time = 0; time < 10000; time++){
            //            editText3.setText("");
                //        str2="待定";//+time+"s";
               //        editText3.setText(str2);
                        //System.out.println("读取:" + i);
                  //  }
                    
        //        }
         //   });
        }
    }





//其中的内部类

package com.example.jjjjfcfy;

import java.util.Stack;
public class ooo
{
    public float Math(String str) throws Exception
    { 
        String[] operater =new String[20]; 
        String[] number = new String[20]; 
        Stack countStack1 = new Stack(); 
        Stack countStack2 = new Stack(); 
        float result =0;
        float x=0;
        number = str.split("\\/|\\*|\\+|\\-"); 
        operater= str.split("\\d+");
        if(number.length<2)throw new Exception("请输入两位计算数以上!");
        for(int i = 0; i<number.length;i++)
        {
            try
            {
                x=Float.parseFloat(number[i]);
            }
            catch(NumberFormatException e)
            {
                throw new Exception("输入错误!请重新输入!");
            }
            if(operater[i].equals("/"))if(x==0)throw new Exception("除数不能为0!");
            countStack1.push(number[i]);
            if(i!=number.length-1)
            {
                if(operater[i+1]!=null)countStack1.push(operater[i+1]);
            }
        }
        while(!countStack1.isEmpty())countStack2.push(countStack1.pop());
        String op;
        while(!countStack2.isEmpty())
        { 
            result=0;
            op=countStack2.pop().toString();
            if(op.equals("*"))
            {
                result=Float.parseFloat(countStack1.pop().toString())*Float.parseFloat(countStack2.pop().toString());
                if(number.length==2)return result;
                countStack1.push(result);
                continue;
            }
            if(op.equals("/"))
            {
                result=Float.parseFloat(countStack1.pop().toString())/Float.parseFloat(countStack2.pop().toString());
                if(number.length==2)return result;
                countStack1.push(result);
                continue;
            }
            countStack1.push(op);
        }
        while(!countStack1.isEmpty())countStack2.push(countStack1.pop());
        while(!countStack2.isEmpty())
        {
            result=0;
            op = countStack2.pop().toString();
            if(op.equals("+"))
            {
                result=Float.parseFloat(countStack1.pop().toString())+Float.parseFloat(countStack2.pop().toString());
                countStack1.push(result);
                continue;
            }
            if(op.equals("-"))
            {
                result=Float.parseFloat(countStack1.pop().toString())-Float.parseFloat(countStack2.pop().toString());
                countStack1.push(result);
                continue;
            }
            countStack1.push(op);
        }
        return result;
    }
}

以下是一部分虚拟安卓系统的运行图 :

               首先进入后页面是这样的:

然后点击开始后是这样的:

这个时候通过点击按键来输入题量,效果是这样的:

 

 

然后点击确定,就会有题目出来:

 

 

再点击按键输入答案:

 

 

输完答案按确定,会顺势出来下一条题目:

 

 

 

重复上面,当最后一道题输完答案后就会出现提示:

 

 

 

     我们小组一直耿耿于怀,亦是这道程序的很大缺憾就是时间的读取,暂时还没有完善,我们小组会努力的!谢谢!

 

 

附带:背景图

posted @ 2015-05-29 13:57  02侯圣燊  阅读(155)  评论(3编辑  收藏  举报