码迷,mamicode.com
首页 > 其他好文 > 详细

第一次作业

时间:2017-09-24 12:57:02      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:char   表达   rect   -name   null   目的   标记   rpg   red   

需求分析:

1. 控制生成题目的个数
2. 控制题目中数值(自然数、真分数和真分数分母)的范围
3. 生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数
4. 每道题目中出现的运算符个数不超过3个。
5. 程序一次运行生成的题目不能重复,生成的题目存入执行程序的当前目录下的Exercises.txt文件,格式如下:
     1. 四则运算题目1
     2. 四则运算题目2
6. 在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件,格式如下:
    1. 答案1
    2. 答案2
    特别的,真分数的运算如下例所示:1/6 + 1/8 = 7/24。
7. 程序应能支持一万道题目的生成。
8. 程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计,并会输出所有题目中重复的题目,输入参数如下:
 统计结果输出到文件Grade.txt,格式如下: 
 Correct: 5 (1, 3, 5, 7, 9)
 Wrong: 5 (2, 4, 6, 8, 10)
 Repeat:2
 RepeatDetail:
 (1)   2,45+32  Repeat 3,32+45                    
 (2)   5,3+(2+1)  Repeat 7,1+2+3

 

功能设计:

1.在控制台输入题目的数量及生成数的范围

2.在文本中记录所有生成的题目

3.在文本中记录每道题对应的正确答案

4.把正确答案和做题者输入的答案进行对比,在文本中计算出正确和错误的题目数量并标记题号

 

实现:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Test{


public static int zf = 0; 
public static int f = 0;    
public static int fm = 0;


public static void main(String[] args) {
String[] form= new String[10000];
form[0] = "4";
form[1] = "+";
form[2] = "13";
String result;
result=result(form);
System.out.print(result);
}

public static String result(String[] eq){ //算式计算
int eqnum=10;
String res = new String();
String[] RPG = new String[10000];
boolean control = true;
if (eqnum >= 1){
if (eq[1] != "/" && eq[1] != "*"){
for (int s = 3; s < arraylength(eq); s = s + 2){
if (eq[s] == "*" || eq[s] == "/"){
control = false;
String[] RPG2 = new String[10000];
for (int i = 0; i < arraylength(eq) - 2; i++) {
RPG2[i] = eq[s - 1 + i];
}
for (int i = 0; i < arraylength(eq) - 2; i++){
if (i < s - 1){
RPG[i] = eq[i];
}
else if (i == s - 1){
eqnum = 0;
RPG[i] = result(RPG2);
}
else {
RPG[i] = eq[i + 2];
}
if (i == arraylength(eq) - 3){
eqnum = 0;
res = result(RPG);
}
}
}
else {
if (control == true){
String[] RPG2 = new String[10000];

for (int i = 0; i < arraylength(eq) - 2; i++){
RPG2[i] = eq[i];
}
eqnum = 0;
for (int i = 0; i < arraylength(eq) - 2; i++){
if (i == 0){
RPG[i] = result(RPG2);
}
else {
RPG[i] = eq[i + 2];
}
if (i == arraylength(eq) - 3){
res = result(RPG);
}
}
}
}
}
}
else{
if (control == true){
String[] RPG2 = new String[10000];

for (int i = 0; i < 3; i++){
RPG2[i] = eq[i];
}
eqnum = 0;
for (int i = 0; i < arraylength(eq) - 2; i++){
if (i == 0){
RPG[i] = result(RPG2);
}
else {
RPG[i] = eq[i + 2];
}
if (i == arraylength(eq) - 3){
res = result(RPG);
}
}
}
}
}
else {
int con0 = 0;
int con2 = 0;
for (int i = 0; i < eq[0].length(); i++){
if (eq[0].charAt(i) == ‘/‘){
con0 = 1;
}
}
for (int i = 0; i < eq[2].length(); i++){
if (eq[2].charAt(i) == ‘/‘){
con2 = 1;
}
}
if (con0 == 0 && con2 == 0){
switch (eq[1]) {
case "+":
res = Integer.toString(Integer.parseInt(eq[0]) + Integer.parseInt(eq[2]));
break;
case "-":
res = Integer.toString(Integer.parseInt(eq[0]) - Integer.parseInt(eq[2]));
break;
case "*":
res = Integer.toString(Integer.parseInt(eq[0]) * Integer.parseInt(eq[2]));
break;
case "/":
zf = Integer.parseInt(eq[0]) / Integer.parseInt(eq[2]);
f = Integer.parseInt(eq[0]) % Integer.parseInt(eq[2]);
fm = Integer.parseInt(eq[2]);
if (Integer.parseInt(eq[0]) >= Integer.parseInt(eq[2])){
if (f != 0){
int k = maxmath(f, Integer.parseInt(eq[2]));
f = f / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + Integer.parseInt(eq[2]) / k;
}
else {
res = Integer.toString(zf);
}
}
else {
int k = maxmath(f, Integer.parseInt(eq[2]));
f = f / k;
res = Integer.toString(f) + "/" + Integer.parseInt(eq[2]) / k;
}
break;

default:
break;
}
}
else if (con0 == 1 && con2 == 0){
int fz = zf * fm + f;
switch (eq[1]) {
case "+":
fz = fz + Integer.parseInt(eq[2]) * fm;
zf = fz / fm;
f = fz % fm;
if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;
case "-":
fz = fz - Integer.parseInt(eq[2]) * fm;
zf = fz / fm;
f = fz % fm;
if (fz == 0){
res = "0";
}
else if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;
case "*":
fz = fz * Integer.parseInt(eq[2]);
zf = fz / fm;
f = fz % fm;
if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;
case "/":
fm = Integer.parseInt(eq[2]) * fm;
zf = fz / fm;
f = fz % fm;
if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;

default:
break;
}
}
else if (con0 == 0 && con2 == 1){
int fz = zf * fm + f;
switch (eq[1]) {
case "+":
fz = Integer.parseInt(eq[0]) * fm +fz;
zf = fz / fm;
f = fz % fm;
if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;
case "-":
fz = Integer.parseInt(eq[0]) * fm - fz;
zf = fz / fm;
f = fz % fm;
if (fz == 0){
res = "0";
}
else if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;
case "*":
fz = Integer.parseInt(eq[0]) * fz;
zf = fz / fm;
f = fz % fm;
if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fm;
}
else {
int k = maxmath(f, fm);
f = f / k;
fm = fm / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fm;
}
}
break;
case "/":
int fz1 = fz;
fz = Integer.parseInt(eq[0]) * fm;
zf = fz / fm;
f = fz % fm;
if (zf == 0 && f != 0){
int k = maxmath(fz, fm);
fz = fz / k;
fm = fm / k;
res = Integer.toString(fz) + "/" + fm;
}
else if (zf != 0 && f == 0){
res = Integer.toString(zf);
}
else if (zf != 0 && f != 0){
if (f > 0){
int k = maxmath(f, fz1);
f = f / k;
fz1 = fz1 / k;
res = Integer.toString(zf) + "‘" + Integer.toString(f) + "/" + fz1;
}
else {
int k = maxmath(f, fz1);
f = f / k;
fz1 = fz1 / k;
res = Integer.toString(zf) + "‘" + Integer.toString(-f) + "/" + fz1;
}
}
break;

default:
break;
}
}
else {

}
}
return res;
}

public static int arraylength(String[] array){ 
int j = 0;
for (int i = 0; i < array.length; i++){
if (array[i] != null){
j++;
}
}
return j;
}

public static int maxmath(int a,int b){    
int k = 1;
int math = 0;
if (a < 0){
a = -a;
}
if (b < 0 ){
b = -b;
}
while (k <= a && k <= b){
if (a % k == 0 && b % k == 0){
math = k;
}
k++;
}
return math;
}
}

 

总结:

由于时间问题,目前一直在仔细研究关于算式的计算,还未完成,后续继续补

第一次作业

标签:char   表达   rect   -name   null   目的   标记   rpg   red   

原文地址:http://www.cnblogs.com/pjishu/p/7586662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!