个人开发流程-----四则运算 计应192第一组王金科
需求分析
- 生成登录页面
- 可以自动生成四则运算题目
- 用户要在限定的时间内答题
- 用户可以填写答案
- 用户可以查看正确率
- 程序可以判断对错并生成正确答案
代码规范
- 尽量使用英文单词 清晰明了
- 使用java代码
具体代码
登录页面
public class Login {
public Login()
{
JFrame f=new JFrame();
f.setTitle("小学生四则运算答题系统");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
//设置窗口的大小和位置
f.setSize(500,500);
f.setLocation(200,200);
Container con=f.getContentPane();//生成一个容器
con.setLayout(new GridLayout(7,1));
JPanel pan1 =new JPanel();
JLabel title=new JLabel("欢迎登陆小学生四则运算答题系统");
title.setFont(new Font("宋体",Font.BOLD, 20));
pan1.add(title);
con.add(pan1);
//生成一个新的版
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
JPanel pan5 = new JPanel();
con.add(pan2);
con.add(pan3);
con.add(pan4);
con.add(pan5);
JLabel name=new JLabel("用户名");
pan2.add(name);
TextField tf_name=new TextField(20);
pan2.add(tf_name);
JLabel pass = new JLabel("密码");
pan3.add(pass);
TextField password=new TextField(20);
password.setEchoChar('*');
pan3.add(password);
JButton b_log=new JButton("登陆");
b_log.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取用户名和密码,进行校验
String myUsername=tf_name.getText();
String myPassword=password.getText();
if(myUsername.equals("admin")&& myPassword.equals("123456")){
JOptionPane.showMessageDialog(null, "登陆成功!");
f.dispose();
new MyExGUI();
}
else{
JOptionPane.showMessageDialog(null, "账号或密码错误!");
name.setText( "");
password.setText( "");
}
}
});
pan4.add(b_log);
JButton b_exit=new JButton("退出");
b_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "谢谢使用!");
f.setVisible(false);//隐藏窗体
System.exit(0);//退出程序
}
});
pan5.add(b_exit);
//登陆和退出这两个按钮放在第四个版面上
}
}
生成运算
public class arithmetic
{
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list_timu = new ArrayList<String>();
ArrayList<String> list_answer = new ArrayList<String>();
public arithmetic()
{
FileOutputStream outSTr = null;
BufferedOutputStream Buff = null;
int number_n=20,count;
ArrayList<String> list_temp = new ArrayList<String>();
String[] operator = new String[]{"+","-","*","/"};
Random rand = new Random();
File file1 = new File("result.txt");
if (file1.exists()) {
// 创建文件
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
Scanner sb = new Scanner(System.in);
System.out.print("输入参数n:");
try{
number_n = sb.nextInt();
}catch(Exception e){
e.printStackTrace();
return;
}
*/
while(number_n>0)
{
int[] number_temp = new int[rand.nextInt(2)+3];
String[] str_temp = new String[number_temp.length-1];
for(int i=0;i<number_temp.length;i++)
{
if(i<number_temp.length-1)
{
number_temp[i]=rand.nextInt(100);
list_temp.add(String.valueOf(number_temp[i]));
str_temp[i]=operator[rand.nextInt(4)];
list_temp.add(str_temp[i]);
}
else
{
number_temp[i]=rand.nextInt(100);
list_temp.add(String.valueOf(number_temp[i]));
}
}
count=calculate_RPN(produce_RPN(list_temp));
if(count !=-1)
{
list_timu.add(transform_string(list_temp));
list_answer.add(String.valueOf(count));
list_temp.add(" = "+count);
list.add(transform_string(list_temp));
number_n--;
list_temp.clear();
}
else
list_temp.clear();
System.out.println(number_n);
}
try {
outSTr = new FileOutputStream(file1);
Buff = new BufferedOutputStream(outSTr);
try {
Buff.write("201571030104 丁炜轩".getBytes());
Buff.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++)
{
try {
Buff.write(list.get(i).getBytes());
Buff.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
i--;
}
}
Buff.flush();
Buff.close();
} catch (IOException e) {
e.printStackTrace();
}
//Buff.close();
try {
outSTr.close();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++)
{
System.out.print(list.get(i));
System.out.println();
}
System.out.print("计算完毕!");
}
public static int calculate_RPN(ArrayList<String> list_temp)
{
int i=0,t;
double a=0,b=0;
String l_temp;
Stack sk=new Stack(20);
for(t=0;t<list_temp.size();t++)
{
l_temp = list_temp.get(i++);
if(!isInteger(l_temp))
{
b = sk.mypop();
a = sk.mypop();
switch(l_temp)
{
case "+":sk.mypush(a+b);break;
case "-":sk.mypush(a-b);break;
case "*":sk.mypush(a*b);break;
case "/":
if(b==0)
return -1;
sk.mypush(a/b);break;
}
System.out.println("st.mytop: "+sk.mypeek());
}
else{
sk.mypush((double)Integer.parseInt(l_temp));
}
}
if(!sk.myisempty())
{
a = sk.mypop();
b = a-(int)a;
System.out.println("a: "+a);
if(a>0 && b==0 )
{
return (int)a;
}
else
return -1;
}
else
return -1;
}
public static ArrayList<String> produce_RPN(ArrayList<String> list_temp)
{
int t=0,i=0;
String tmp;
Tack mytack = new Tack(20);
ArrayList<String> lt_temp = new ArrayList<String>();
while(true)
{
tmp = list_temp.get(i++);
if(isInteger(tmp))
{
lt_temp.add(tmp);
}
else{
if(mytack.myisempty())
{
mytack.mypush(tmp);
}
else{
if(isCPriority(tmp, mytack.mypeek()))
mytack.mypush(tmp);
else{
lt_temp.add(mytack.mypop());
mytack.mypush(tmp);
}
}
}
if(i>=list_temp.size())
{
while(!mytack.myisempty())
lt_temp.add(mytack.mypop());
System.out.println(transform_string(list_temp));
list_temp = lt_temp;
System.out.println(list_temp);
return list_temp;
}
}
}
public static boolean isInteger(String str) {
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
public static boolean isCPriority(String str,String s) {
if((str+s).equals("*+") || (str+s).equals("*-") || (str+s).equals("/+") || (str+s).equals("/-"))
return true;
else
return false;
}
public static String transform_string(ArrayList<String> list_temp)
{
String s="";
for(int i=0;i<list_temp.size();i++)
{
s+=list_temp.get(i);
}
return s;
}
static class Stack
{
int mytop;
double stk[];
public Stack(int num) {
mytop=-1;
stk=new double[num];
}
/*出栈*/
double mypop()
{
double peek=stk[mytop];
mytop--;
return peek;
}
/*入栈*/
void mypush(double x)
{
mytop++;
stk[mytop]=x;
}
/*判空*/
Boolean myisempty()
{
if(mytop==-1)
return true;
else
return false;
}
/*取栈顶元素*/
double mypeek()
{
double peek=stk[mytop];
return peek;
}
/*栈大小*/
int mysize()
{
return mytop+1;
}
}
static class Tack
{
int mytop;
String tk[];
public Tack(int num) {
mytop=-1;
tk=new String[num];
}
/*出栈*/
String mypop()
{
String peek=tk[mytop];
mytop--;
return peek;
}
/*入栈*/
void mypush(String x)
{
mytop++;
tk[mytop]=x;
}
/*判空*/
Boolean myisempty()
{
if(mytop==-1)
return true;
else
return false;
}
/*取栈顶元素*/
String mypeek()
{
String peek=tk[mytop];
return peek;
}
/*栈大小*/
int mysize()
{
return mytop+1;
}
}
}
编辑页面格式
public class MyExGUI extends JFrame{
ArrayList<String> user_zongti = new ArrayList<String>();
ArrayList<String> user_zonganswer = new ArrayList<String>();
ArrayList<String> user_answer = new ArrayList<String>();
ArrayList<String> true_answer = new ArrayList<String>();
ArrayList<String> jta_timu = new ArrayList<String>();
ArrayList<String> jta_zong = new ArrayList<String>();
ArrayList<Integer> user_fenshu = new ArrayList<Integer>();
JMenuBar jmb; //菜单条组件
JMenu menu1, menu2, menu3, menu4, menu5;//菜单
JMenuItem item1, item2, item3, item4, item5, item6;//菜单项
JMenu build; //二级菜单
JMenuItem file, project;
TextArea answer_all = new TextArea();
TextField jta = new TextField();
TextField jta_answer = new TextField();
JLabel num_answer = new JLabel();
JLabel answer1;
JToolBar jtb;//工具条
JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7,jb_next;
int answer_count;
int answer_fenshu;
public MyExGUI()
{
//创建菜单
jmb = new JMenuBar();
menu1 = new JMenu("文件(F)");
menu1.setMnemonic('f'); //助记符
menu2 = new JMenu("编辑");
menu2.setMnemonic('E');
menu3 = new JMenu("格式");
menu4 = new JMenu("查看");
menu5 = new JMenu("帮助");
build = new JMenu("新建");
file = new JMenuItem("文件");
project = new JMenuItem("答题");
item1 = new JMenuItem("打开");
item2 = new JMenuItem("保存(S)");
item3 = new JMenuItem("另存为");
item4 = new JMenuItem("页面设置");
item5 = new JMenuItem("打印");
item6 = new JMenuItem("退出");
answer1 = new JLabel("第 1 题");
//添加菜单项至菜单上
build.add(file);
build.add(project);
menu1.add(build);
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu1.addSeparator();
menu1.add(item4);
menu1.add(item5);
menu1.add(item6);
//将菜单加入至菜单栏
jmb.add(menu1);
jmb.add(menu2);
jmb.add(menu3);
jmb.add(menu4);
jmb.add(menu5);
JPanel contentPanel = new JPanel();
contentPanel.setLayout(null);
JLabel daan = new JLabel("答案");
JLabel dengyu = new JLabel("=");
num_answer=answer1;
num_answer.setFont(new Font("宋体",Font.BOLD, 22));
jb_next = new JButton("下一题");
jta.setFont(new Font("宋体",Font.BOLD, 22));
jta_answer.setFont(new Font("宋体",Font.BOLD, 22));
jb_next.setFont(new Font("宋体",Font.BOLD, 22));
daan.setFont(new Font("宋体",Font.BOLD, 22));
dengyu.setFont(new Font("宋体",Font.BOLD, 22));
contentPanel.add(num_answer);
contentPanel.add(daan);
contentPanel.add(dengyu);
contentPanel.add(jta);
contentPanel.add(jta_answer);
contentPanel.add(answer_all);
contentPanel.add(jb_next);
num_answer.setBounds(90, 20, 130, 50);
daan.setBounds(250, 20, 90, 50);
jta.setBounds(50, 70, 150, 30);
dengyu.setBounds(205, 70, 20, 20);
jta_answer.setBounds(230, 70, 100, 30);
jb_next.setBounds(350, 70, 110, 30);
answer_all.setBounds(50, 120, 400, 300);
this.setJMenuBar(jmb); //添加菜单栏,不能设定位置,会自动放在最上部
this.add(contentPanel);
this.setTitle("在线答题系统");
this.setSize(600, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
if(file.isDirectory()){
// System.out.println("文件夹:"+file.getAbsolutePath());
}else if(file.isFile()){
String s=file.getAbsolutePath();
}
}
});
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
FileOutputStream outSTr = null;
BufferedOutputStream Buff = null;
boolean flag = true;
File file;
//String test ;
do{
//test = "test"+count;
String inputValue = JOptionPane.showInputDialog("Please input file name");
file = new File(inputValue+".txt");
if (!file.exists()) {
// 创建文件
try {
//System.out.println("文件夹:"+flag);
flag=file.createNewFile();
//System.out.println("文件夹:"+flag);
} catch (IOException e) {
e.printStackTrace();
//JOptionPane.showMessageDialog(null, "ERROR", "该文件名已存在,请重新输入", JOptionPane.ERROR_MESSAGE);
}
flag=false;
}
else{
JOptionPane.showMessageDialog(null, "该文件名已存在,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE);
flag=true;
}
}while(flag);
//写入文件
String u_answer;
try {
outSTr = new FileOutputStream(file);
Buff = new BufferedOutputStream(outSTr);
System.out.println("选择是后执行的代码"+user_zongti.size()+user_answer.size());
for (int i = 0; i < user_zongti.size(); i++)
{
try {
Buff.write(user_zongti.get(i).getBytes());
Buff.write(" ".getBytes());
u_answer = user_answer.get(i);
if(u_answer.equals(""))
u_answer ="没有作答";
Buff.write(u_answer.getBytes());
Buff.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
i--;
}
}
Buff.flush();
Buff.close();
} catch (IOException e) {
e.printStackTrace();
}
//Buff.close();
try {
outSTr.close();
} catch (IOException e) {
e.printStackTrace();
}
user_zongti.clear();
user_answer.clear();
}
});
project.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
arithmetic art = new arithmetic();
true_answer=art.list_answer;
jta_timu = art.list_timu;
jta_zong = art.list;
answer_count=1;
answer_all.setText("");
for (int i = 0; i < art.list_timu.size(); i++)
{
user_zongti.add(jta_zong.get(i));
answer_all.append(jta_timu.get(i));
answer_all.append("\r\n");
}
num_answer.setText("第 "+answer_count+" 题");
jta.setText(jta_timu.get(answer_count-1));
answer_count++;
}
});
jb_next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String temp;
temp = jta_answer.getText();
if(jta.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "错误,请导入题库", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
jta_answer.setText("");
if(answer_count<=20)
{
if(isInteger(temp))
{
user_answer.add(temp);
System.out.println("选择否后执行的代码"+temp+"user_size"+user_answer.size());
num_answer.setText("第 "+answer_count+" 题");
jta.setText(jta_timu.get(answer_count-1));
answer_count++;
}
else{
JOptionPane.showMessageDialog(null, "错误", "请输入数字", JOptionPane.ERROR_MESSAGE);
}
}
else{
user_answer.add(temp);
System.out.println("选择否后执行的代码"+temp+"user_size"+user_answer.size());
answer_fenshu=0;
for(int i=0;i<user_answer.size();i++)
{
if(user_answer.get(i).equals(true_answer.get(i)))
answer_fenshu+=5;
}
user_fenshu.add(answer_fenshu);
Object[] options = { "是", "取消" };
int res=JOptionPane.showOptionDialog(null, "点击以继续 查看成绩", "答题完毕",
JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION,
null, options, options[0]);
if(res==JOptionPane.YES_OPTION){
chart ct =new chart(user_fenshu);
ct.setVisible(true);
//ct.paint(Graphics g);
//System.out.println("选择是后执行的代码"); //点击“是”后执行这个代码块
}else{
Object[] option = { "是", "取消" };
int res1=JOptionPane.showOptionDialog(null, "继续新一轮答题", "新一轮答题",
JOptionPane.DEFAULT_OPTION, JOptionPane.YES_NO_OPTION,
null, option, option[0]);
//System.out.println("选择否后执行的代码"); //点击“否”后执行这个代码块
if(res1==JOptionPane.YES_OPTION){
arithmetic art = new arithmetic();
true_answer=art.list_answer;
//jta_timu = art.list_timu;
jta_timu = art.list;
answer_count=1;
answer_all.setText("");
jta_answer.setText("");
for (int i = 0; i < art.list_timu.size(); i++)
{
user_zongti.add(jta_timu.get(i));
answer_all.append(jta_timu.get(i));
answer_all.append("\r\n");
}
num_answer.setText("第 "+answer_count+" 题");
jta.setText(jta_timu.get(answer_count-1));
answer_count++;
//System.out.println("选择是后执行的代码"); //点击“是”后执行这个代码块
}else{
}
}
}
}
});
}
public static boolean isInteger(String str) {
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
}
public class Main {
public static void main(String[] args) {
new Login();
// new MyExGUI();
}
}
psp
| PSP | 任务内容 | 计划完成需要的时间(min) | 实际完成需要的时间(min) |
| 计划 | 10 | 12 | |
| Estimate | 需求分析,函数实现 | 10 | 10 |
| 开发 | 30 | 30 | |
| Analysis | 需求分析 | 5 | 10 |
| Design Spec | 设计 | 5 | 5 |
| Design Review | 设计复审 | 2 | 2 |
| Design | 具体设计 | 5 | 5 |
| Coding | 具体编码 | 20 | 20 |
| Code Review | 代码复审,查找语法错误 | 2 | 2 |
| Test | 测试 | 5 | 5 |
| 报告 | 10 | 10 | |
| Test Report | 测试报告 | 2 | 2 |
| Size Measurement | 计算工作量 | 2 | 2 |
| Postmortem | 事后总结,并提出改进计划 | 2 | 2 |
浙公网安备 33010602011771号