安卓版四則運算

08林達發表於2015-05-28
  1 package com.example.add;
  2 
  3 import java.io.BufferedWriter;
  4 import java.io.File;
  5 import java.io.FileWriter;
  6 import java.io.IOException;
  7 
  8 import com.example.add.opt;
  9 
 10 import java.text.DecimalFormat;
 11 
 12 import android.support.v7.app.ActionBarActivity;
 13 import android.support.v7.app.ActionBar;
 14 import android.support.v4.app.Fragment;
 15 import android.text.InputType;
 16 import android.os.Bundle;
 17 import android.view.LayoutInflater;
 18 import android.view.Menu;
 19 import android.view.MenuItem;
 20 import android.view.View;
 21 import android.view.ViewGroup;
 22 import android.os.Build;
 23 import android.app.Activity;
 24 import android.os.Bundle;
 25 import android.view.View;
 26 import android.widget.Button;
 27 import android.widget.EditText;
 28 import android.widget.TextView;
 29 import android.widget.Toast;
 30 
 31     public class MainActivity extends Activity {
 32         
 33         int x;//題目個數
 34         int y=0;
 35         int n;
 36         int i=1;
 37         char[] op = { '+', '-', '*', '/' };
 38         int[] nov = new int[4];
 39         int zf;//控制正負
 40         int zz;//題型控制
 41         String str = new String();
 42         String str1 = new String();
 43         int[] num1 = new int[5];
 44         String s[] = new String[5];
 45         int num2=0;
 46         float[] anser=new float[5];//使用者的答案
 47         float[] trueanser=new float[5];//正確答案
 48         File file;
 49         int a=0;
 50         int b=0;
 51         int bb=0;
 52         // int c = 0;
 53         opt opt=new opt();;
 54         EditText editText,xs;
 55         StringBuffer text,text1;
 56         String number_1="",number_2="";
 57         boolean clicked=false;
 58         byte style=0;
 59         Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0;
 60         Button start,btn_eq,btn_dot,btn_clear;
 61         /** Called when the activity is first created. */
 62         @Override
 63         public void onCreate(Bundle savedInstanceState) {
 64             super.onCreate(savedInstanceState);
 65             setContentView(R.layout.fragment_main);
 66             text=new StringBuffer();
 67             editText=(EditText)findViewById(R.id.result);
 68             xs=(EditText)findViewById(R.id.xs);
 69             //editText.setFocusable(false);
 70             btn_1=(Button) findViewById(R.id.btn_1);
 71             btn_1.setOnClickListener(new Button.OnClickListener(){
 72 
 73                 public void onClick(View v) {
 74                     // TODO Auto-generated method stub
 75                     if(clicked){
 76                         editText.setText("");
 77                         clicked=false;
 78                         text.setLength(0);
 79                     }
 80                     text.append("7");
 81                     editText.setText(text.toString());
 82                     
 83                 }
 84                 
 85             });
 86             btn_2=(Button) findViewById(R.id.btn_2);
 87             btn_2.setOnClickListener(new Button.OnClickListener(){
 88                 
 89                 public void onClick(View v) {
 90                     // TODO Auto-generated method stub
 91                     if(clicked){
 92                         editText.setText("");
 93                         clicked=false;
 94                         text.setLength(0);
 95                     }
 96                     text.append("8");
 97                     editText.setText(text.toString());
 98                 }
 99             });
100             btn_3=(Button) findViewById(R.id.btn_3);
101             btn_3.setOnClickListener(new Button.OnClickListener(){
102                 
103                 public void onClick(View v) {
104                     // TODO Auto-generated method stub
105                     if(clicked){
106                         editText.setText("");
107                         clicked=false;
108                         text.setLength(0);
109                     }
110                     text.append("9");
111                     editText.setText(text.toString());
112                 }
113                 
114             });
115             btn_4=(Button) findViewById(R.id.btn_4);
116             btn_4.setOnClickListener(new Button.OnClickListener(){
117                 
118                 public void onClick(View v) {
119                     // TODO Auto-generated method stub
120                     if(clicked){
121                         editText.setText("");
122                         clicked=false;
123                         text.setLength(0);
124                     }
125                     text.append("4");
126                     editText.setText(text.toString());
127                 }
128                 
129             });
130             btn_5=(Button) findViewById(R.id.btn_5);
131             btn_5.setOnClickListener(new Button.OnClickListener(){
132                 
133                 public void onClick(View v) {
134                     // TODO Auto-generated method stub
135                     if(clicked){
136                         editText.setText("");
137                         clicked=false;
138                         text.setLength(0);
139                     }
140                     text.append("5");
141                     editText.setText(text.toString());
142                 }
143                 
144             });
145             btn_6=(Button) findViewById(R.id.btn_6);
146             btn_6.setOnClickListener(new Button.OnClickListener(){
147                 
148                 public void onClick(View v) {
149                     // TODO Auto-generated method stub
150                     if(clicked){
151                         editText.setText("");
152                         clicked=false;
153                         text.setLength(0);
154                     }
155                     text.append("6");
156                     editText.setText(text.toString());
157                 }
158                 
159             });
160             btn_7=(Button) findViewById(R.id.btn_7);
161             btn_7.setOnClickListener(new Button.OnClickListener(){
162                 
163                 public void onClick(View v) {
164                     // TODO Auto-generated method stub
165                     if(clicked){
166                         editText.setText("");
167                         clicked=false;
168                         text.setLength(0);
169                     }
170                     text.append("1");
171                     editText.setText(text.toString());
172                 }
173                 
174             });
175             btn_8=(Button) findViewById(R.id.btn_8);
176             btn_8.setOnClickListener(new Button.OnClickListener(){
177                 
178                 public void onClick(View v) {
179                     // TODO Auto-generated method stub
180                     if(clicked){
181                         editText.setText("");
182                         clicked=false;
183                         text.setLength(0);
184                     }
185                     text.append("2");
186                     editText.setText(text.toString());
187                 }
188                 
189             });
190             btn_9=(Button) findViewById(R.id.btn_9);
191             btn_9.setOnClickListener(new Button.OnClickListener(){
192                 
193                 public void onClick(View v) {
194                     // TODO Auto-generated method stub
195                     if(clicked){
196                         editText.setText("");
197                         clicked=false;
198                         text.setLength(0);
199                     }
200                     text.append("3");
201                     editText.setText(text.toString());
202                 }
203                 
204             });
205             btn_0=(Button) findViewById(R.id.btn_0);
206             btn_0.setOnClickListener(new Button.OnClickListener(){
207                 
208                 public void onClick(View v) {
209                     // TODO Auto-generated method stub
210                     if(clicked){
211                         editText.setText("");
212                         clicked=false;
213                         text.setLength(0);
214                     }
215                     text.append("0");
216                     editText.setText(text.toString());
217                 }
218                 
219             });
220             btn_dot=(Button) findViewById(R.id.btn_dot);
221             btn_dot.setOnClickListener(new Button.OnClickListener(){
222                 
223                 public void onClick(View v) {
224                     // TODO Auto-generated method stub
225                     if(clicked){
226                         editText.setText("");
227                         clicked=false;
228                         text.setLength(0);
229                     }
230                     if(editText.getText().toString().indexOf(".")==-1){//已經有.了
231                         if(text.length()==0){
232                             text.append("0.");
233                         }else{
234                             text.append(".");
235                         }
236                         editText.setText(text.toString());
237                     }
238                 }
239                 
240             });
241             btn_eq=(Button) findViewById(R.id.btn_eq);
242             btn_eq.setOnClickListener(new Button.OnClickListener(){
243                 
244                 public void onClick(View v) {
245                     // TODO Auto-generated method stub
246                     if(b==1)
247                     {
248                         if(editText.getText().toString().length()!=0)
249                         {
250                             try
251                             {
252                                 x=Integer.parseInt(editText.getText().toString());
253                                 if(x>=0 && x<=5)b++;
254                                 else Toast.makeText(MainActivity.this, "輸入有誤", 1).show();
255                             }
256                             catch(NumberFormatException x)
257                             {
258                                 Toast.makeText(MainActivity.this, "輸入有誤", 1).show();
259                             }
260                         }
261                         else Toast.makeText(MainActivity.this, "輸入有誤", 1).show();
262                         editText.setText("");
263                         number_1="";
264                         number_2="";
265                         style=0;
266                         clicked=false;
267                         text.setLength(0);
268                     }
269                     if(b==2)
270                     {
271                         if(n!=0)anser[i-2]=Float.parseFloat(editText.getText().toString());
272                         str1="第"+i+"條題目為:";
273                         n=(int)(Math.random() * 4 + 2);//隨機出運算子的數量
274                         zz=(int)(Math.random()*2);
275                         for(int j=0;j<n;j++)
276                         {
277                             //zf=(int)(Math.random()*2);
278                             //if(zf==0)//279                             //    num1[j]=-(int)(Math.random()*100);//產生隨機數
280                             //else        //
281                                 num1[j]=(int)(Math.random()*100);
282                                 if(zz==1)num1[j]=(int)(Math.random()*10+1);
283                         }
284                         for(int k=0;k<n-1;k++)
285                         {
286                             nov[k]=(int)(Math.random()*4);
287                             if(nov[k]==3&&num1[k+1]==0)
288                             {
289                                 do
290                                 {
291                                     num1[k+1]=(int)(Math.random()*100);
292                                 }while(num1[k+1]==0);
293                             }
294                         }
295                         str="";
296                         if(zz==0)
297                         {
298                             for(int j=0;j<n;j++)
299                             {
300                                 //if(num1[j]<0)str=str+"("+String.valueOf(num1[j])+")";
301                                 str=str+String.valueOf(num1[j]);
302                                 if(j==n-1)
303                                 {
304                                     //try
305                                     //{
306                                         //int ii=0;
307                                         try{
308                                             trueanser[i-1]=opt.Math(str);
309                                             DecimalFormat df = new DecimalFormat("#.00");
310                                             trueanser[i-1]=Float.parseFloat(df.format(trueanser[i-1]));
311                                         } catch (Exception e1) {
312                                             // TODO 自動生成的 catch 塊
313                                             e1.printStackTrace();
314                                         }
315                                     //}
316                                     /*catch (Exception e1)
317                                     {
318                                         System.out.println(e1);
319                                         //e1.printStackTrace();
320                                     }*/
321                                     str=str+"=";
322                                     //str=str+String.valueOf(trueanser[i-1]);
323                                     xs.setText(str1+str);
324                                     break;
325                                 }
326                                 str=str+op[nov[j]];
327                             }
328                         }
329                         if(zz==1)
330                         {
331                             trueanser[i-1]=1;
332                             str=String.valueOf(num1[0])+"!=";
333                             for(int j=1;j<=num1[0];j++)
334                             trueanser[i-1]=trueanser[i-1]*j;
335                             //str=str+String.valueOf(trueanser[i-1]);
336                             xs.setText(str1+str);
337                         }
338                         if(i==x+1)
339                         {
340                             b=3;
341                             anser[i-2]=Float.parseFloat(editText.getText().toString());
342                         }
343                         else i++;
344                         editText.setText("");
345                         number_1="";
346                         number_2="";
347                         style=0;
348                         clicked=false;
349                         text.setLength(0);
350                     }
351                     if(b==3)
352                     {
353                         y=0;
354                         for(int j=0;j<x;j++)
355                         {
356                             if(anser[j]==trueanser[j])y++;
357                         }
358                         xs.setText("答題完成!答對"+y+"道!");
359                         b=0;
360                         editText.setText("");
361                         number_1="";
362                         number_2="";
363                         style=0;
364                         clicked=false;
365                         text.setLength(0);
366                     }
367                 }
368             });
369             start=(Button) findViewById(R.id.start);
370             start.setOnClickListener(new Button.OnClickListener(){
371                 
372                 public void onClick(View v) {
373                     // TODO Auto-generated method stub
374                     xs.setText("請輸入出題數量!(1-5)");
375                     n=0;
376                     i=1;
377                     b=1;
378                 }
379             });
380             btn_clear=(Button) findViewById(R.id.btn_clear);
381             btn_clear.setOnClickListener(new Button.OnClickListener(){
382                 
383                 public void onClick(View v) {
384                     // TODO Auto-generated method stub
385                     editText.setText("");
386                     number_1="";
387                     number_2="";
388                     style=0;
389                     clicked=false;
390                     text.setLength(0);
391                 }
392             });
393         }
394     }
MainActivity
  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     android:layout_width="fill_parent"
  3     android:layout_height="fill_parent"
  4     android:orientation="vertical" >
  5 
  6     <EditText
  7         android:id="@+id/result"
  8         android:layout_width="match_parent"
  9         android:layout_height="wrap_content"
 10         android:cursorVisible="false"
 11         android:digits="1234567890."
 12         android:ems="10"
 13         android:focusable="false"
 14         android:numeric="decimal"
 15         android:singleLine="true" />
 16 
 17     <TableLayout
 18         android:layout_width="fill_parent"
 19         android:layout_height="wrap_content"
 20         android:stretchColumns="0,1,2,3" >
 21 
 22         <TableRow>
 23 
 24             <Button
 25                 android:id="@+id/btn_1"
 26                 android:text="7" />
 27 
 28             <Button
 29                 android:id="@+id/btn_2"
 30                 android:text="8" />
 31 
 32             <Button
 33                 android:id="@+id/btn_3"
 34                 android:text="9" />
 35         </TableRow>
 36 
 37         <TableRow>
 38 
 39             <Button
 40                 android:id="@+id/btn_4"
 41                 android:text="4" />
 42 
 43             <Button
 44                 android:id="@+id/btn_5"
 45                 android:text="5" />
 46 
 47             <Button
 48                 android:id="@+id/btn_6"
 49                 android:text="6" />
 50         </TableRow>
 51 
 52         <TableRow>
 53 
 54             <Button
 55                 android:id="@+id/btn_7"
 56                 android:text="1" />
 57 
 58             <Button
 59                 android:id="@+id/btn_8"
 60                 android:text="2" />
 61 
 62             <Button
 63                 android:id="@+id/btn_9"
 64                 android:text="3" />
 65         </TableRow>
 66 
 67         <TableRow>
 68 
 69             <Button
 70                 android:id="@+id/btn_dot"
 71                 android:text="." />
 72 
 73             <Button
 74                 android:id="@+id/btn_0"
 75                 android:text="0" />
 76 
 77             <Button
 78                 android:id="@+id/btn_eq"
 79                 android:text="確定" />
 80         </TableRow>
 81     </TableLayout>
 82 
 83     <Button
 84         android:id="@+id/btn_clear"
 85         android:layout_width="fill_parent"
 86         android:layout_height="wrap_content"
 87         android:text="清除" />
 88 
 89     <EditText
 90         android:id="@+id/xs"
 91         android:layout_width="wrap_content"
 92         android:layout_height="wrap_content"
 93         android:focusable="false"
 94         android:text="歡迎使用四則運算程式!" >
 95 
 96         <requestFocus />
 97     </EditText>
 98 
 99     <Button
100         android:id="@+id/start"
101         android:layout_width="wrap_content"
102         android:layout_height="wrap_content"
103         android:text="開始" />
104 
105 </LinearLayout>
fragment_main
 1 package com.example.add;
 2 import java.util.Stack;
 3 public class opt
 4 {
 5     public float Math(String str) throws Exception
 6     { 
 7         String[] operater =new String[20]; 
 8         String[] number = new String[20]; 
 9         Stack countStack1 = new Stack(); 
10         Stack countStack2 = new Stack(); 
11         float result =0;
12         float x=0;
13         number = str.split("\\/|\\*|\\+|\\-"); 
14         operater= str.split("\\d+");
15         if(number.length<2)throw new Exception("請輸入兩位計算數以上!");
16         for(int i = 0; i<number.length;i++)
17         {
18             try
19             {
20                 x=Float.parseFloat(number[i]);
21             }
22             catch(NumberFormatException e)
23             {
24                 throw new Exception("輸入錯誤!請重新輸入!");
25             }
26             if(operater[i].equals("/"))if(x==0)throw new Exception("除數不能為0!");
27             countStack1.push(number[i]);
28             if(i!=number.length-1)
29             {
30                 if(operater[i+1]!=null)countStack1.push(operater[i+1]);
31             }
32         }
33         while(!countStack1.isEmpty())countStack2.push(countStack1.pop());
34         String op;
35         while(!countStack2.isEmpty())
36         { 
37             result=0;
38             op=countStack2.pop().toString();
39             if(op.equals("*"))
40             {
41                 result=Float.parseFloat(countStack1.pop().toString())*Float.parseFloat(countStack2.pop().toString());
42                 if(number.length==2)return result;
43                 countStack1.push(result);
44                 continue;
45             }
46             if(op.equals("/"))
47             {
48                 result=Float.parseFloat(countStack1.pop().toString())/Float.parseFloat(countStack2.pop().toString());
49                 if(number.length==2)return result;
50                 countStack1.push(result);
51                 continue;
52             }
53             countStack1.push(op);
54         }
55         while(!countStack1.isEmpty())countStack2.push(countStack1.pop());
56         while(!countStack2.isEmpty())
57         {
58             result=0;
59             op = countStack2.pop().toString();
60             if(op.equals("+"))
61             {
62                 result=Float.parseFloat(countStack1.pop().toString())+Float.parseFloat(countStack2.pop().toString());
63                 countStack1.push(result);
64                 continue;
65             }
66             if(op.equals("-"))
67             {
68                 result=Float.parseFloat(countStack1.pop().toString())-Float.parseFloat(countStack2.pop().toString());
69                 countStack1.push(result);
70                 continue;
71             }
72             countStack1.push(op);
73         }
74         return result;
75     }
76 }
opt

這一次將我們之前的程式轉換成了安卓應用版,主要功能尚未有多大變化,接下來幾天將會繼續改進!

程式截圖:

PS:程式介面比較簡陋!我們會試著改進的!

 

相關文章