四则运算出题4
Posted David~Wang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算出题4相关的知识,希望对你有一定的参考价值。
1、题目要求:
将之前的四则运算出题,做成手机APP 或以网页版发布,可以让用户答题,并提示结果正确与否。
2、实现思路:
出题思路不变,将开发工具改到eclipse 上,开发成安卓程序;
3、思路整理(实现步骤):
出题步骤不变,按照之前的四则运算程序的思路,将出题结果显示在textview ,用户输入,并可以判断是否正确,并弹出对话框,提示正确与否,并对每一次的判断进行计数,结束时,提示计数结果。
4、源代码:
MainActivity.java文件:
package com.er.er0; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.content.Intent; import android.view.View.OnClickListener; import android.view.View; public class MainActivity extends Activity { private EditText e_min; private EditText e_max; private Button b1; private Button b2; private MenuInflater act; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); e_min=(EditText)findViewById(R.id.editText1); e_max=(EditText)findViewById(R.id.editText2); b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); act=new MenuInflater(this); b1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ String num_min=e_min.getText().toString(); String num_max=e_max.getText().toString(); Bundle n_min=new Bundle(); Bundle n_max=new Bundle(); Intent intent = new Intent(MainActivity.this,Sec.class); if(num_min.length()==0||num_max.length()==0){ Toast.makeText(MainActivity.this, "请输入数值范围!", Toast.LENGTH_LONG).show(); } else{ n_min.putString("num_min", num_min); n_max.putString("num_max", num_max); intent.putExtras(n_min); intent.putExtras(n_max); startActivity(intent); } } }); b2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ exitAlert("真的要退出吗?"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. act.inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item){ if(item.getItemId()==R.id.action_settings){ exitAlert("真的要退出吗?"); } return true; } private void exitAlert(String msg){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setMessage(msg) .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub return; } }); AlertDialog alert=builder.create(); alert.show(); } }
Sec.java文件
package com.er.er0; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.net.Uri; import java.math.BigDecimal; @SuppressLint("ShowToast") public class Sec extends Activity { private TextView t1; private EditText e1; private Button b1; private Button b2; private Button b3; private Button b4; public int num_min; public int num_max; public int num_z=0; public int num_w=0; public boolean flag=false; public boolean flag1=false; public int num_right=0; public int num_wrong=0; private int top; public float answer; private int size=100; private String s[]=new String[100]; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.sec); t1=(TextView)findViewById(R.id.textView2); e1=(EditText)findViewById(R.id.editText1); b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b4=(Button)findViewById(R.id.button4); Intent intent=getIntent(); Bundle numb=intent.getExtras(); String n_min=numb.getString("num_min"); num_min=Integer.parseInt(n_min); String n_max=numb.getString("num_max"); num_max=Integer.parseInt(n_max); b1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ String s; s=e1.getEditableText().toString(); if(s.length()==0){ Toast.makeText(Sec.this, "请输入答案!", Toast.LENGTH_LONG).show(); } else{ panduan(); } } }); b3.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ num_w=num_z-num_right-num_wrong; exitAlert("本次做题共计"+num_z+"道题,正确"+num_right+"道,错误"+num_wrong+"道,"+num_w+"道未做。\n"+"确定要结束答题吗?"); } }); b2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ e1.setText(""); chuti(); } }); b4.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ flag1=true; e1.setText(""); chuti(); b4.setVisibility(View.INVISIBLE); b2.setVisibility(View.VISIBLE); } }); } private void exitAlert(String msg){//退出项选择 AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setMessage(msg) .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub return; } }); AlertDialog alert=builder.create(); alert.show(); } private int suijishu(int num_mi,int num_ma ){//产生随机数 int x=(int)(num_ma*Math.random()+num_mi); return x; } private void inttochar(int num,char numtochar[]){//整型转换为char型 int i=0; int mid_num; char mid_ch; do{ mid_num=num%10; numtochar[i]=(char) (mid_num+‘0‘); num=num/10; i++; }while(num!=0); for(int j=0;j<i/2;j++){ mid_ch=numtochar[j]; numtochar[j]=numtochar[i-1-j]; numtochar[i-1-j]=mid_ch; } } private int length(int num){//计算数的长度 int i=0; do{ num=num/10; i++; }while(num!=0); return i; } private int length_char(char fenshu[]){//计算分数长度 int i=0; do{ i++; }while(fenshu[i]!=‘)‘); i++; return i; } private int length_str(char str[]){//计算完整表达式长度 int i=0; do{ i++; }while(str[i]!=‘\0‘); //i++; return i; } private void clear_char(char mid_cha[]){//初始化字符数组 for(int i=0;i<10;i++){ mid_cha=null; } } private int fen_huajian(int num_first,int num_second){//求分子分母的最大公约数 int num_yuf; int num_yus; int num_yumid = 1; for(int i=2;i<=num_first;i++){ num_yuf=num_first%i; num_yus=num_second%i; if(num_yuf==0&&num_yus==0){ num_yumid=i; } } return num_yumid; } private void fen(char fe[]){//产生分数 char num1[]=new char[10];//保存分子 char num2[]=new char[10];//保存分母 int num_first; int num_second; int l1; int l2; int i; int num_maxyue; if(num_min==0){ num_first=suijishu(1,num_max); } else{ num_first=suijishu(num_min,num_max); } num_second=suijishu(num_first,num_max); num_maxyue=fen_huajian(num_first,num_second); num_first=num_first/num_maxyue;//分子分母化简 num_second=num_second/num_maxyue; l1=length(num_first); l2=length(num_second); inttochar(num_first,num1); inttochar(num_second,num2); fe[0]=‘(‘; for(i=0;i<l1;i++){ fe[i+1]=num1[i]; } fe[l1+1]=‘/‘; for(int j=0;j<l2;j++){ fe[l1+2+j]=num2[j]; } fe[l1+l2+2]=‘)‘; } private void chuti(){ int num_yunsuanfu;//运算符个数 int ch_intorfen;//选择整数或分数 int number;//保存整数 int len=0;//保存整个式子长度 int mid_len=0;//保存单个数长度 char yunsuanfu[]=new char[3];//保存运算符 int yunsuan;//产生随机数1-4,用以判断运算符 //计数(运算数个数) char mid_num[]=new char[10];//保存中间产生的数 char num[]=new char[100];//保存完整式子 num_yunsuanfu=suijishu(1,3);//判断产生的运算符为几个 switch(num_yunsuanfu){ case 1://产生一个运算符的情况 int i1=0; yunsuan=suijishu(1,4); switch(yunsuan){ case 1: yunsuanfu[0]=‘+‘; break; case 2: yunsuanfu[0]=‘-‘; break; case 3: yunsuanfu[0]=‘*‘; break; case 4: yunsuanfu[0]=‘/‘; break; } do{ ch_intorfen=suijishu(1,2); switch(ch_intorfen){ case 1: do{ number=suijishu(num_min,num_max); }while(i1==1&&yunsuanfu[0]==‘/‘&&number==0); inttochar(number,mid_num); mid_len=length(number); for(int l=0;l<mid_len;l++){ num[l+len]=mid_num[l]; } break; case 2: fen(mid_num); mid_len=length_char(mid_num); for(int l=0;l<mid_len;l++){ num[l+len]=mid_num[l]; } clear_char(mid_num); break; } len+=mid_len; if(i1<1){ num[len]=yunsuanfu[0]; } i1++; len++; }while(i1!=2); len=0; break; case 2://产生两个运算符的情况 int k2; int i2=0; for(k2=0;k2<2;k2++){ yunsuan=suijishu(1,4); switch(yunsuan){ case 1: yunsuanfu[k2]=‘+‘; break; case 2: yunsuanfu[k2]=‘-‘; break; case 3: yunsuanfu[k2]=‘*‘; break; case 4: yunsuanfu[k2]=‘/‘; break; } } do{ ch_intorfen=suijishu(1,2); switch(ch_intorfen){ case 1: do{ number=suijishu(num_min,num_max); }while(i2!=0&&yunsuanfu[i2-1]==‘/‘&&number==0); inttochar(number,mid_num); mid_len=length(number); for(int l=0;l<mid_len;l++){ num[l+len]=mid_num[l]; } break; case 2: fen(mid_num); mid_len=length_char(mid_num); for(int l=0;l<mid_len;l++){ num[l+len]=mid_num[l]; } clear_char(mid_num); break; } len+=mid_len; if(i2<3){ num[len]=yunsuanfu[i2]; } i2++; len++; }while(i2!=3); len=0; break; case 3: int k3; int i3=0; for(k3=0;k3<3;k3++){ yunsuan=suijishu(1,4); switch(yunsuan){ case 1: yunsuanfu[k3]=‘+‘; break; case 2: yunsuanfu[k3]=‘-‘; break; case 3: yunsuanfu[k3]=‘*‘; break; case 4: yunsuanfu[k3]=‘/‘; break; } } do{ ch_intorfen=suijishu(1,2); switch(ch_intorfen){ case 1: do{ number=suijishu(num_min,num_max); }while(i3!=0&&yunsuanfu[i3-1]==‘/‘&&number==0); inttochar(number,mid_num); mid_len=length(number); for(int l=0;l<mid_len;l++){ num[l+len]=mid_num[l]; } break; case 2: fen(mid_num); mid_len=length_char(mid_num); for(int l=0;l<mid_len;l++){ num[l+len]=mid_num[l]; } clear_char(mid_num); break; } len+=mid_len; if(i3<3){ num[len]=yunsuanfu[i3]; } i3++; len++; }while(i3!=4); len=0; break; } kuohao(num,num_yunsuanfu); } private void kuohao(char num[],int b){ int wz1=0;//记录第一个运算符的位置 int wz2=0;//记录第二个运算符的位置 int wz3=0;//记录第三个运算符的位置 int i=0;//数组下标 int kh_wz;//括号位置种类 int len_str = 0;//表达式长度 char num2[]=new char[100]; String Sstr; if(b==1){ len_str=length_str(num); for(i=0;i<len_str;i++){ num2[i]=num[i]; } num2[len_str]=‘=‘; } else{ switch(b){ case 2: len_str=length_str(num); for(;;){ if(num[i]==‘(‘){ for(;;){ if(num[i]==‘)‘){ break; } i++; } wz1=i+1; break; } else{ if(num[i]==‘+‘||num[i]==‘-‘||num[i]==‘*‘||num[i]==‘/‘){ wz1=i; break; } i++; } } for(i=wz1+1;;){ if(num[i]==‘(‘){ for(;;){ if(num[i]==‘)‘){ break; } i++; } wz2=i+1; break; } else{ if(num[i]==‘+‘||num[i]==‘-‘||num[i]==‘*‘||num[i]==‘/‘){ wz2=i; break; } i++; } } kh_wz=suijishu(0,3); switch(kh_wz){ case 1: num2[0]=‘(‘; for(i=0;i<wz2;i++){ num2[i+1]=num[i]; } num2[wz2+1]=‘)‘; for(i=wz2;i<len_str;i++){ num2[i+2]=num[i]; } num2[len_str+2]=‘=‘; break; case 2: for(i=0;i<wz1+1;i++){ num2[i]=num[i]; } num2[wz1+1]=‘(‘; for(i=wz1+1;i<len_str;i++){ num2[i+1]=num[i]; } num2[len_str+1]=‘)‘; num2[len_str+2]=‘=‘; break; default: for(i=0;i<len_str;i++){ num2[i]=num[i]; } num2[len_str]=‘=‘; break; } break; case 3: len_str=length_str(num); for(;;){ if(num[i]==‘(‘){ for(;;){ if(num[i]==‘)‘){ break; } i++; } wz1=i+1; break; } else{ if(num[i]==‘+‘||num[i]==‘-‘||num[i]==‘*‘||num[i]==‘/‘){ wz1=i; break; } i++; } } for(i=wz1+1;;){ if(num[i]==‘(‘){ for(;;){ if(num[i]==‘)‘){ break; } i++; } wz2=i+1; break; } else{ if(num[i]==‘+‘||num[i]==‘-‘||num[i]==‘*‘||num[i]==‘/‘){ wz2=i; break; } i++; } } for(i=wz2+1;;){ if(num[i]==‘(‘){ for(;;){ if(num[i]==‘)‘){ break; } i++; } wz3=i+1; break; } else{ if(num[i]==‘+‘||num[i]==‘-‘||num[i]==‘*‘||num[i]==‘/‘){ wz3=i; break; } i++; } } kh_wz=suijishu(0,9); switch(kh_wz){ case 1: num2[0]=‘(‘; for(i=0;i<wz2;i++){ num2[i+1]=num[i]; } num2[wz2+1]=‘)‘; for(i=wz2;i<len_str;i++){ num2[i+2]=num[i]; } num2[len_str+2]=‘=‘; break; case 2: for(i=0;i<wz1+1;i++){ num2[i]=num[i]; } num2[wz1+1]=‘(‘; for(i=wz1+1;i<wz3;i++){ num2[i+1]=num[i]; } num2[wz3+1]=‘)‘; for(i=wz3;i<len_str;i++){ num2[i+2]=num[i]; } num2[len_str+2]=‘=‘; break; case 3: for(i=0;i<wz2+1;i++){ num2[i]=num[i]; } num2[wz2+1]=‘(‘; for(i=wz2+1;i<len_str;i++){ num2[i+1]=num[i]; } num2[len_str+1]=‘)‘; num2[len_str+2]=‘=‘; break; case 4: num2[0]=‘(‘; num2[1]=‘(‘; for(i=0;i<wz2;i++){ num2[i+2]=num[i]; } num2[wz2+2]=‘)‘; for(i=wz2;i<wz3;i++){ num2[i+3]=num[i]; } num2[wz3+3]=‘)‘; for(i=wz3;i<len_str;i++){ num2[i+4]=num[i]; } num2[len_str+4]=‘=‘; break; case 5: num2[0]=‘(‘; for(i=0;i<wz1+1;i++){ num2[i+1]=num[i]; } num2[wz1+2]=‘(‘; for(i=wz1+1;i<wz3;i++){ num2[i+2]=num[i]; } num2[wz3+2]=‘)‘; num2[wz3+3]=‘)‘; for(i=wz3;i<len_str;i++){ num2[i+4]=num[i]; } num2[len_str+4]=‘=‘; break; case 6: for(i=0;i<wz1+1;i++){ num2[i]=num[i]; } num2[wz1+1]=‘(‘; num2[wz1+2]=‘(‘; for(i=wz1+1;i<wz3;i++){ num2[i+2]=num[i]; } num2[wz3+2]=‘)‘; for(i=wz3;i<len_str;i++){ num2[i+3]=num[i]; } num2[len_str+3]=‘)‘; num2[len_str+4]=‘=‘; break; case 7: for(i=0;i<wz1+1;i++){ num2[i]=num[i]; } num2[wz1+1]=‘(‘; for(i=wz1+1;i<wz2+1;i++){ num2[i+1]=num[i]; } num2[wz2+2]=‘(‘; for(i=wz2+1;i<len_str;i++){ num2[i+2]=num[i]; } num2[len_str+2]=‘)‘; num2[len_str+3]=‘)‘; num2[len_str+4]=‘=‘; break; case 8: num2[0]=‘(‘; for(i=0;i<wz2;i++){ num2[i+1]=num[i]; } num2[wz2+1]=‘)‘; num2[wz2+2]=num[wz2]; num2[wz2+3]=‘(‘; for(i=wz2+1;i<len_str;i++){ num2[i+3]=num[i]; } num2[len_str+3]=‘)‘; num2[len_str+4]=‘=‘; break; default: for(i=0;i<len_str;i++){ num2[i]=num[i]; } num2[len_str]=‘=‘; break; } break; } } Sstr=String.valueOf(num2); char2str(num2,Sstr); } private void char2str(char str[],String Sstr){ int l; 以上是关于四则运算出题4的主要内容,如果未能解决你的问题,请参考以下文章