194677四则运算第二次作业
一、需求分析
一、自动生成10道100以内的2个操作数的四则运算算式(+ - * /),要求运算结果也在100以内。剔除重复算式。 2 + 3 = 和 2 + 3 = 是重复算式 2 + 3 = 和 3 + 2 = 不属于重复算式。题目数量可定制100
二、 相关参数可控制
包含乘法和除法
操作数数值范围可控(如操作数 在100以内 )
操作数是含负数
二、生成的运算题存储到外部文件result.txt中
二、功能扩展
(1)算出答案,生成四则运算
(2)多操作数运算
(3)用户可以自己选择用加减,乘除,正负,整分
(4)预留了多余的空间
三、测试运行
五、编码
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate;
import java.util.Scanner;
// num 成程习题的数量
// is_include_Multiplication_division 是否包含乘除法
// size_operationNum 范围大小
// is_include_negative 是在否包负数
public String[][] CreateQuestion(int num, boolean is_include_Multiplication_division, int size_operationNum,
boolean is_include_negative) {
int num1, num2, result;
int type;
int count = 0;
String[][] Question_Answer = new String[99999][2];
while (true) {
if (count >= num)
break;
if (is_include_negative) {
num1 = (int) ((Math.random() * size_operationNum) - 50);
num2 = (int) ((Math.random() * size_operationNum) - 50);
} else {
num1 = (int) (Math.random() * size_operationNum);
num2 = (int) (Math.random() * size_operationNum);
}
if (!is_include_negative)
if (num1 < 0 || num2 < 0)
continue;
type = 1 + (int) (Math.random() * 4);
if (type == 1) {
result = num1 + num2;
if (result < 100) {
Question_Answer[count][0] = "("+num1+")" + " + " +"("+ num2+")" + " =";
Question_Answer[count][1] = result+"";
count++;
}
} else if (type == 2) {
result = num1 - num2;
if (result < 100) {
Question_Answer[count][0] = "("+num1+")" + " - " +"("+ num2+")" + " =";
Question_Answer[count][1] = result+"";
count++;
}
} else if (type == 3 && is_include_Multiplication_division) {
result = num1 * num2;
if (result < 100) {
Question_Answer[count][0] = "("+num1+")" + " × " +"("+ num2+")" + " =";
Question_Answer[count][1] = result+"";
count++;
}
} else if (type == 4 && is_include_Multiplication_division) {
Double result_;
if (num2 == 0)
continue;
result_ = (double) num1 / num2;
if (result_ < 100) {
Question_Answer[count][0] = "("+num1+")" + " ÷ " +"("+ num2+")" + " =";
Question_Answer[count][1] = String.format("%.2f", result_); // 精度控制
count++;
}
}
}
return Question_Answer;
}
* rows控制输出列
*/
public void SaveToDisk(String[][] question_answer, int lines, int rows) {
// 定义输入输出类对象
OutputStream outputStream = null;
// 获取时间,用于生成随时间变化的文件名
LocalDate date = LocalDate.now();
// 创建result.txt文件
try {
outputStream = new FileOutputStream("./result" + date + ".txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 标题格式化并写入文件
byte[] header = "-*--*--*--*--*--*--*--*--*--*--*- 四 则 算 术 题 -*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-\r\n"
.getBytes();
try {
outputStream.write(header);
} catch (IOException e1) {
e1.printStackTrace();
}
// 题目主体部分格式化并写入文件
try {
byte[] bs = null;
String str = null;
int count = 0;
for (int i = 0; i < lines; i++) {
str = "";
for (int j = 0; j < rows; j++) {
if (question_answer[count][0] == null) // 如果处算式为空,则表明到了算式尾,结束拼接
break;
str += "\t"+(count+1)+"、" + question_answer[count][0] + "____\t"; // 将一行算式拼接,指定rows列
count++;
}
str += "\r\n";
bs = str.getBytes();
outputStream.write(bs);
if (count >= lines)
break;
}
} catch (IOException e1) {
e1.printStackTrace();
}
header = "\r\n\r\n-*--*--*--*--*--*--*--*--*--*--*- 参 考 答 案 -*--*--*--*--*--*--*--*--*--*--*--*--*--*-\r\n"
.getBytes();
try {
outputStream.write(header);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
String str_answer = "";
int count_answer = 0;
byte[] bs = null;
for (int i = 0; i < lines; i++) {
str_answer="";
for (int j = 0; j < 5; j++) {
if (question_answer[count_answer][1] == null)
break;
str_answer += "\t" +(count_answer+1)+ "、"+question_answer[count_answer][1] + "\t";
count_answer++;
}
str_answer += "\r\n";
bs = str_answer.getBytes();
outputStream.write(bs);
if (count_answer >= lines)
break;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
OutputStream outputStream = null;
LocalDate date = LocalDate.now();
try {
outputStream = new FileOutputStream("./result" + date + ".txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
outputStream.write(header);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bs = (question_answer[i][0] + "\r\n").getBytes();
outputStream.write(bs);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
outputStream.write(header);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] bs = question_answer[i][1].getBytes();
outputStream.write(bs);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int i = 0;
while (question_answer[i][0] != null) {
i++;
}
return i;
}
Question_ q = new Question_();
int num = 10;
int size_operationNum = 100;
boolean is_include_Multiplication_division = false;
boolean is_include_negative = false;
Scanner input = new Scanner(System.in);
num = input.nextInt();
size_operationNum = input.nextInt();
int flag = input.nextInt();
if (flag == 1)
is_include_Multiplication_division = true;
else
is_include_Multiplication_division = false;
flag = input.nextInt();
if (flag == 1)
is_include_negative = true;
else
is_include_negative = false;
rows = input.nextInt();
is_include_negative);
int size = q.Question_Size(question_answer);
q.SaveToDisk(question_answer, size, rows);
System.out.print("作业已生成,存放目录:./");
六、 总结
创建1个类,数字随机,选择题目数量,结果大小,乘法,除法,并且去除重复参数
最后输入结果,导出到外部文件设计四则运算算法,在运行无误后,根据需求逐一增加代码功能,每增加一步进行测试,测试无误后,写下一个功能
七、psp
PSP2.1 |
任务内容 |
计划共完成需要的时间(min) |
实际完成需要的时间(min) |
Planning |
计划 |
8 |
6 |
· Estimate |
· 估计这个任务需要多少时间,并规划大致工作步骤 |
8 |
6 |
Development |
开发 |
82 |
160 |
·· Analysis |
需求分析 (包括学习新技术) |
6 |
50 |
· Design Spec |
· 生成设计文档 |
5 |
6 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
4 |
6 |
· Coding Standard |
代码规范 (为目前的开发制定合适的规范) |
3 |
20 |
· Design |
具体设计 |
10 |
12 |
· Coding |
具体编码 |
36 |
21 |
· Code Review |
· 代码复审 |
7 |
9 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
13 |
21 |
Reporting |
报告 |
9 |
6 |
·· Test Report |
· 测试报告 |
3 |
10 |
· Size Measurement |
计算工作量 |
2 |
5 |
· Postmortem & Process Improvement Plan |
· 事后总结 ,并提出过程改进计划 |
3 |
20 |