用Java语言编写代码,要求批量产生并存储习题(CSV文件格式),加、减、混合题各一套;加一个类ExerciseFiles、在其中增加方法,从CSV格式的文件中读入习题。屏幕显示读到的文件内容。在上述基础上添加运算结果,例如33+22=55,屏幕显示,要求复用算式类;
时间: 2024-02-29 12:53:21 浏览: 13
好的,让我为您提供一份Java代码样例,实现您的需求:
BinaryOperation.java
```java
public class BinaryOperation {
private int operand1;
private int operand2;
private char operator;
public BinaryOperation(int operand1, int operand2, char operator) {
this.operand1 = operand1;
this.operand2 = operand2;
this.operator = operator;
}
public int calculate() {
switch (operator) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
default:
throw new IllegalArgumentException("Unsupported operator: " + operator);
}
}
@Override
public String toString() {
return operand1 + " " + operator + " " + operand2;
}
}
```
Exercise.java
```java
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Exercise {
private List<BinaryOperation> operations;
public Exercise(int numAdd, int numSubtract, int numMixed) {
operations = new ArrayList<BinaryOperation>();
Random rand = new Random();
for (int i = 0; i < numAdd; i++) {
int a = rand.nextInt(100);
int b = rand.nextInt(100);
operations.add(new BinaryOperation(a, b, '+'));
}
for (int i = 0; i < numSubtract; i++) {
int a = rand.nextInt(100);
int b = rand.nextInt(100);
operations.add(new BinaryOperation(a, b, '-'));
}
for (int i = 0; i < numMixed; i++) {
int a = rand.nextInt(100);
int b = rand.nextInt(100);
char operator = rand.nextBoolean() ? '+' : '-';
operations.add(new BinaryOperation(a, b, operator));
}
}
public void saveToFile(String filename) {
try (FileWriter writer = new FileWriter(filename)) {
for (BinaryOperation op : operations) {
writer.write(op.toString() + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public List<BinaryOperation> getOperations() {
return operations;
}
}
```
ExerciseSheet.java
```java
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExerciseSheet {
private List<Exercise> exercises;
public ExerciseSheet(int numAdd, int numSubtract, int numMixed, int numSheets) {
exercises = new ArrayList<Exercise>();
for (int i = 0; i < numSheets; i++) {
exercises.add(new Exercise(numAdd, numSubtract, numMixed));
}
}
public void saveToFile(String prefix) {
for (int i = 0; i < exercises.size(); i++) {
String filename = prefix + "-" + (i + 1) + ".csv";
exercises.get(i).saveToFile(filename);
}
}
public List<Exercise> getExercises() {
return exercises;
}
}
```
ExerciseFiles.java
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExerciseFiles {
public static List<BinaryOperation> readFromFile(String filename) {
List<BinaryOperation> operations = new ArrayList<BinaryOperation>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\s+");
int a = Integer.parseInt(parts[0]);
char operator = parts[1].charAt(0);
int b = Integer.parseInt(parts[2]);
operations.add(new BinaryOperation(a, b, operator));
}
} catch (IOException e) {
e.printStackTrace();
}
return operations;
}
public static void printOperations(List<BinaryOperation> operations) {
for (BinaryOperation op : operations) {
int result = op.calculate();
System.out.println(op.toString() + " = " + result);
}
}
}
```
这份代码样例中,BinaryOperation类表示二元操作,Exercise类表示一组二元操作,ExerciseSheet类表示多组二元操作,ExerciseFiles类表示从文件中读取和显示二元操作。
您可以将这份代码保存为ExerciseDemo.java文件,并执行以下代码:
```java
public class ExerciseDemo {
public static void main(String[] args) {
ExerciseSheet sheet = new ExerciseSheet(10, 10, 10, 3);
sheet.saveToFile("exercises");
List<BinaryOperation> operations = ExerciseFiles.readFromFile("exercises-1.csv");
ExerciseFiles.printOperations(operations);
}
}
```
这份代码会生成三组加、减、混合题各十道的习题,保存在exercises-1.csv、exercises-2.csv、exercises-3.csv三个文件中,并从exercises-1.csv文件中读取二元操作,并计算并显示结果。