package size; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JTextField; import javax.swing.JTextArea; import java.awt.Color; import java.util.*; import java.util.regex.*; import java.lang.*; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JScrollPane; import com.ibm.icu.text.DecimalFormat; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.*; public class vvvv extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; File File; int x;//题目个数 int y=0; int n; int i=1; char[] op = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ }; int[] nov = new int[4]; int zf;//控制正负 int zz;//题型控制 String str = new String(); String str1 = 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; opt opt=new opt();; public JFrame vvvv; JTextArea ck; private JTextField xmt; private JButton yes; private JButton no; private JButton start; private JButton huan; private JPanel panel; private JScrollPane scrollPane; /** * Launch the application. */ public static void main(String[] args)// 主方法 { EventQueue.invokeLater(new Runnable() { public void run() { try { vvvv window = new vvvv(); window.vvvv.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public vvvv() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { vvvv = new JFrame(); vvvv.getContentPane().setEnabled(false); vvvv.getContentPane().setBackground(Color.GRAY); vvvv.setTitle("\u56DB\u5219\u8FD0\u7B97"); vvvv.setBounds(100, 100, 494, 270); vvvv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); vvvv.setResizable(false); vvvv.getContentPane() .setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); panel = new JPanel(); vvvv.getContentPane().add(panel); ck = new JTextArea(10, 40); // panel.add(ck); // ck.setEditable(false); scrollPane = new JScrollPane(ck); panel.add(scrollPane); xmt = new JTextField(); vvvv.getContentPane().add(xmt); xmt.addActionListener(this); xmt.setColumns(10); yes = new JButton("\u786E\u8BA4"); vvvv.getContentPane().add(yes); yes.addActionListener(this); no = new JButton("\u6E05\u7A7A"); no.addActionListener(this); vvvv.getContentPane().add(no); huan = new JButton("\u6362\u80A4"); huan.addActionListener(this); vvvv.getContentPane().add(huan); start = new JButton("开始"); start.addActionListener(this); vvvv.getContentPane().add(start); try { String b=""; file =new File("name.txt"); FileReader fr = new FileReader(file.getName()); BufferedReader br = new BufferedReader(fr); b=br.readLine(); fr.close(); ck.setText("你好!"+b); bb=2; } catch(FileNotFoundException x) { ck.setText("你是第一次使用该程序!请输入您的名称!"); bb=1; } catch(IOException x) { x.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (e.getSource() == yes) { if(bb==1) { try { file =new File("name.txt"); if(!file.exists()) { file.createNewFile(); } FileWriter fileWritter = new FileWriter(file.getName()); BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferWritter.write(xmt.getText()); bufferWritter.close(); ck.setText("保存成功!"); bb=2; } catch(IOException x) { x.printStackTrace(); } } if(b==1) { if(xmt.getText().length()!=0) try { x=Integer.parseInt(xmt.getText()); b++; } catch(NumberFormatException x) { ck.setText("输入有误!"); } else ck.setText("输入有误!"); } if(b==2) { if(n!=0)anser[i-2]=Float.parseFloat(xmt.getText()); str1="第"+i+"条题目为:"; 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]); ck.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]); ck.setText(str1+str); } if(i==x+1) { b=3; anser[i-2]=Float.parseFloat(xmt.getText()); } else i++; } if(b==3) { y=0; for(int j=0;j<x;j++) { if(anser[j]==trueanser[j])y++; } ck.setText("答题完成!答对"+y+"道!"); b=0; } } if (e.getSource() == no) { xmt.setText(null); } if (e.getSource() == huan) { if (a == 0) { vvvv.getContentPane().setBackground(Color.blue); a++; } else if (a == 1) { vvvv.getContentPane().setBackground(Color.yellow); a++; } else if (a == 2) { vvvv.getContentPane().setBackground(Color.gray); a = 0; } } if (e.getSource() == start && bb==2) { ck.setText("欢迎使用四则运算练习系统\n请输入题目的数量(1-5)"); n=0; i=1; b=1; } } /*public static int opt(int[] num1,int[] nov,int n,int i) { if(i>=n)return num1[i]; int sum=0; if(nov[0]==0 && i<n) { sum=num1[i]+opt(num1,nov,n,i+1); } if(nov[0]==1 && i<n) { sum=num1[i]-opt(num1,nov,n,i+1); } if(nov[0]==2 && i<n) { sum=num1[i]*opt(num1,nov,n,i+1); } if(nov[0]==3 && i<n) { sum=num1[i]/opt(num1,nov,n,i+1); } return sum; }*/ }
vvvv
package size; import java.util.Stack; public class opt { 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; } }
opt
package size; import size.opt; import java.util.*; import com.ibm.icu.text.DecimalFormat; public class Test { public static void main(String[] args) { // TODO 自动生成的方法存根 String a=new String(); opt opt=new opt(); float x=0; Scanner in=new Scanner(System.in); a=in.nextLine(); try { x=opt.Math(a); DecimalFormat df = new DecimalFormat("#.00"); x=Float.parseFloat(df.format(x)); System.out.println(x); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
Test
以上3个类分别是:
A.界面函数(包括杂项)
B.计算类
C.测试类
不知道什么原因,我使用Junit总是会报错 can not create test"TestMath"
所以这一次我自己手动测试了,测试结果在我的伙伴陈思明的博客那里 地址 http://www.cnblogs.com/chensiming/
心得体会:这一次的作业也就在上一次的基础上进行更改,但是重点的Junit并不能成功,有点失望,希望下次能解决
时间: 2024-11-08 13:18:07