结对项目--四则运算
结对项目--四则运算
这个作业属于哪个课程 | https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/ |
---|---|
这个作业要求在哪里 | https://edu.cnblogs.com/campus/gdgy/CSGrade22-34/homework/13230 |
这个作业的目标 | 四则运算 |
成员 | 3122004564方伟城 |
项目地址
https://github.com/fwc031505/jiedui
psp表格
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 40 |
Estimate | 估计这个任务需要多少时间 | 20 | 15 |
Development | 开发 | 360 | 400 |
Analysis | 需求分析 (包括学习新技术) | 80 | 60 |
Design Spec | 生成设计文档 | 30 | 30 |
Design Review | 设计复审 | 30 | 30 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 20 | 20 |
Design | 具体设计 | 60 | 70 |
Coding | 具体编码 | 90 | 110 |
Code Review | 代码复审 | 30 | 15 |
Test | 测试(自我测试,修改代码,提交修改) | 60 | 45 |
Reporting | 报告 | 60 | 50 |
Test Repor | 测试报告 | 20 | 20 |
Size Measurement | 计算工作量 | 20 | 20 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 40 | 30 |
合计 | 1020 | 955 |
代码设计
1. Fraction类:两个真分数的四则运算,如果不是真分数就先转换城真分数 `public class Fraction { private int numerator; // 分子 private int denominator; // 分母// 构造器,number:
public Fraction(int[] numbers) {
int len = numbers.length;
switch (len) {
case 1://整数
pre(numbers[0], 1);
break;
case 2://真分数
pre(numbers[0], numbers[1]);
break;
case 3://假分数
pre(numbers[1] + numbers[2] * numbers[0], numbers[2]);
break;
}
}
//numerator:分子 denominator分母
public void pre(int numerator, int denominator) {
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be zero.");
}
this.numerator = numerator;
this.denominator = denominator;
simplify(); // 初始化时简化分数
}
// 简化分数
private void simplify() {
int gcd = gcd(Math.abs(numerator), Math.abs(denominator));
numerator /= gcd;
denominator /= gcd;
// 确保分母为正
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
// 最大公约数算法(欧几里得算法)
private int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 加法
public Fraction add(Fraction other) {
int newNumerator = numerator * other.denominator + other.numerator * denominator;
int newDenominator = denominator * other.denominator;
return new Fraction(new int[]{newNumerator, newDenominator});
}
// 减法
public Fraction subtract(Fraction other) {
int newNumerator = numerator * other.denominator - other.numerator * denominator;
int newDenominator = denominator * other.denominator;
return new Fraction(new int[]{newNumerator, newDenominator});
}
// 乘法
public Fraction multiply(Fraction other) {
int newNumerator = numerator * other.numerator;
int newDenominator = denominator * other.denominator;
return new Fraction(new int[]{newNumerator, newDenominator});
}
// 除法
public Fraction divide(Fraction other) {
if (other.numerator == 0) {
throw new ArithmeticException("Division by zero.");
}
int newNumerator = numerator * other.denominator;
int newDenominator = denominator * other.numerator;
return new Fraction(new int[]{newNumerator, newDenominator});
}
public boolean compare(Fraction other) {
Fraction fraction = subtract(other);
return fraction.numerator >= 0;
}
// 重写toString方法,方便输出
@Override
public String toString() {
return numerator + "/" + denominator;
}
// 测试
public static void main(String[] args) {
Fraction f1 = new Fraction(new int[]{2, 1, 6});
Fraction f2 = new Fraction(new int[]{3, 1, 3});
System.out.println(f1);
System.out.println("f1 + f2 = " + f1.add(f2));
System.out.println("f1 - f2 = " + f1.subtract(f2));
System.out.println("f1 * f2 = " + f1.multiply(f2));
System.out.println("f1 / f2 = " + f1.divide(f2));
}
}`
- MathQuizGenerator类:生成题目并测试
`
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class MathQuizGenerator {
//生成题目的个数
public static int count;
// //题目中数值(自然数、真分数和真分数分母)的范围
public static int range;
public static Random random = new Random();
public static final String[] OPERATORS = {" + ", " - ", " × ", " ÷ "};
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("Exercises.txt");
Scanner sc = new Scanner(System.in);
count = sc.nextInt();
range = sc.nextInt();
for (int i = 0; i < count; i++) {
writer.write( i+1+". "+ generate()+"\n");
} writer.close();
;
}
//生成真分数
private static String createZhengFenshu() {
// 随机生成两个操作数,且num2必须大于num1,所以也保证了分母不为0
int num1 = random.nextInt(range);
int num2 = random.nextInt(range);
if (num1 > num2) {
return num2 + "/" + num1;
} else {
return num1 + "/" + num2;
}
}
//生成假分数
private static String createJiaFenshu() {
int num1 = random.nextInt(range);
int num2 = random.nextInt(range);
int num3 = random.nextInt(range);
;
if (num3 < num2) {
return num1 + "'" + num3 + "/" + num2;
}
return num1 + "'" + num2 + "/" + num3;
}
//生成整数
private static String createZhengshu() {
return random.nextInt(range) + "";
}
private static String addKuohao(String s) {
return "(" + s + ")";
}
//生成数,包括可能的括号
private static String createNum() {
//选择是整数/真分数/假分数
int number1 = random.nextInt(3);
String num1 = "";
switch (number1) {
case 0:
num1 = createZhengshu();
break;
case 1:
num1 = createZhengFenshu();
break;
case 2:
num1 = createJiaFenshu();
break;
}
return canAddKuohao(num1);
}
private static String canAddKuohao(String s) {
if (random.nextInt(10) == 1) {
s = addKuohao(s);
}
return s;
}
//生成运算符
private static String creanteoperator() {
// 随机选择运算符:0为加法,1为减法,2为乘法,3为除法
return OPERATORS[random.nextInt(4)];
}
//创建简单表达式,即只有一个运算符
// 连接2个子表达式,并括号
private static String link(String a, String b) {
String operator = creanteoperator();
return canAddKuohao(a + operator + b);
}
private static String link(String a, String b, String operator) {
return canAddKuohao(a + operator + b);
}
//生成题目
private static String generate() {
//运算符个数
int operatorCount = random.nextInt(3) + 1;
String[] nums = new String[operatorCount + 1];
for (int j = 0; j < operatorCount + 1; j++) {
nums[j] = createNum();
}
// System.out.println(Arrays.toString(nums));
//题目
String result = nums[0];
//计算结果
String out = nums[0];
for (int i = 0; i < operatorCount; i++) {
String operator = creanteoperator();
result = link(result, nums[i + 1], operator);
/* boolean right = isRight(out, nums[i + 1],operator);
if (right){
out=calulate(out,nums[i+1],operator);
result=link(result,nums[i+1],operator);
}
else {
out=calulate(nums[i+1],out,operator);
result=link(nums[i+1],result,operator);
}*/
}
return result;
}
//根据表达式s得出结果
private static String out(String s) {
s = s.trim();
return null;
}
private static boolean isRight(String num1, String num2, String operator) {
if (operator.equals(OPERATORS[1]) && !compare(num1, num2)) {
return false;
}
if (operator.equals(OPERATORS[3]) && compare(num1, num2)) {
return false;
}
return true;
}
//比较两个数大小,num1不小于num2返回ture
private static boolean compare(String num1, String num2) {
//根据数组大小判断是整数/真分数/假分数,真分数<1,假分数<其中的整数+1
String[] split1 = num1.split("'|//");
String[] split2 = num2.split("'|//");
int[] number1 = new int[split1.length];
for (int i = 0; i < split1.length; i++) {
number1[i] = Integer.parseInt(split1[i]);
}
int[] number2 = new int[split2.length];
for (int i = 0; i < split2.length; i++) {
number2[i] = Integer.parseInt(split2[i]);
}
Fraction fraction21 = new Fraction(number1);
Fraction fraction = new Fraction(number2);
return fraction21.compare(fraction);
}
//计算两个数的运算
private static String calulate(String num1, String num2, String operator) {
//根据数组大小判断是整数/真分数/假分数,真分数<1,假分数<其中的整数+1
String[] split1 = num1.split("'|//");
String[] split2 = num2.split("'|//");
int[] number1 = new int[split1.length];
for (int i = 0; i < split1.length; i++) {
number1[i] = Integer.parseInt(split1[i]);
}
int[] number2 = new int[split2.length];
for (int i = 0; i < split2.length; i++) {
number2[i] = Integer.parseInt(split2[i]);
}
Fraction fraction21 = new Fraction(number1);
Fraction fraction = new Fraction(number2);
if (operator.equals(OPERATORS[0])) {
return fraction21.add(fraction).toString();
}
if (operator.equals(OPERATORS[1])) {
return fraction21.subtract(fraction).toString();
}
if (operator.equals(OPERATORS[2])) {
return fraction21.multiply(fraction).toString();
}
return fraction21.divide(fraction).toString();
}
}
`