需求分析
1、控制運算表達式題目數量,控制題目中數值的大小
2、題目中存在分數形式要進行約分
3、運算符個數不能超過3個
4、不能出現重複題目,即兩個運算表達式的計算步驟不能完全相同
5、程式同時支援計算生成的每道題目,并對結果進行約分
6、對使用者所做答案進行判斷,并且輸出正确答案或錯誤答案
功能設計
基本功能
1、根據控制台輸入的數字随機生成指定運算元個數和算式數目的運算符
2、基本的四則運算功能
3、式子中的分數和最終計算結果均約分到最簡
設計實作
環境
windows10+java
程式結構設計
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuMTN4kjM0YDOx0CO2MzMxMDNzEDNykDM3EDMy0yM4cTMxITMvwVOwcTMwIzLcNDO3ETMyEzLcd2bsJ2Lc12bj5ycn9Gbi52YucTMwIzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
程式設計
功能點1:生成随機數
随機數生成方法: public class CreateNum
public class CreateNum{//産生操作數
public String Num() {
String Num = "";
Reduction rdct = new Reduction();
int numerator; //分子
int denominator; //分母
numerator = (int)(Math.random() * 10);
denominator = (int)(Math.random() * 9) + 1;
int i = (int)(Math.random() * 3);
if(i<2){//控制整數機率為2/3
Num += numerator;
}else{
Num= rdct.reductNum(numerator, denominator);
}
return Num;
}
}
功能點2:生成随機運算符
随機運算符生成方法: public class CreateSymbol
public class CreateSymbol {
public String Symbol(){
String symbol = null;
int i = (int) (Math.random() * 4);
switch (i) {//生成加減乘除運算符
case 0:
symbol = "+";
break;
case 1:
symbol = "-";
break;
case 2:
symbol = "*";
break;
case 3:
symbol = "÷";
break;
}
return symbol;
}
}
功能點3:組成算數表達式
組成算數表達式方法: public class CreateExp
public class CreateExp {//産生表達式
public ArrayList<Object> createexp() {
ArrayList<Object> list = new ArrayList<Object>();
CreateNum cnum = new CreateNum();
CreateSymbol csymbol = new CreateSymbol();
int sybno = (int) (Math.random() * 3)+1;//随機運算符個數為1-3
list.add(cnum.Num());
for(int i=0;i<sybno;i++){
list.add(csymbol.Symbol());
list.add(cnum.Num());
}
int p = (int) (Math.random() * 3);//控制生成括号的機率為1/3
if(p<1){
if(sybno != 1){//一個運算符不加括号
int LeftPosition = (int)(Math.random()*sybno)*2;//左括号位置
list.add(LeftPosition, "(");
int tempLeft =list.indexOf("(");
if((tempLeft+4) != list.size()){
int RightPosition =tempLeft+4+(int)(Math.random()*sybno-1)*2;
list.add(RightPosition, ")");
}else{
int RightPosition =list.size();
list.add(RightPosition, ")");
}
}
}
return list;
}
}
功能點4:約分
public static String reductNum(int numerator,int denominator)
public static String reductNum(int numerator,int denominator) {
String rdNum = "";
if(denominator==0) {
System.exit(0);
}
if(numerator%denominator==0) {
rdNum+=(numerator/denominator);
}else {
if(numerator<denominator) {
int maxFactor = getDivisor(numerator,denominator);
if(maxFactor==0) {
rdNum+=0;
}else {
int tempNumerator = numerator/maxFactor;
int tempDenominator = denominator/maxFactor;
rdNum+=tempNumerator+"/"+tempDenominator;
}
}else {
int theMod = numerator%denominator;
int theTimes = numerator/denominator;
int maxFactor = getDivisor(theMod,denominator);
if(maxFactor==0) {
rdNum+=0;
}else {
theMod = theMod/maxFactor;
denominator = denominator/maxFactor;
rdNum+=theTimes+"'"+theMod+"/"+denominator;
}
}
}
return rdNum;
}
功能點5:逆波蘭式
public ArrayList<Object> RPN(String formula);
public ArrayList<Object> RPN(String formula) {
//用哈希鍵值對映射左運算符的優先級
HashMap<String, Integer> lpri = new HashMap<String,Integer>();
lpri.put("=",0);
lpri.put("(",1);
lpri.put("*",5);
lpri.put("÷",5);
lpri.put("+",3);
lpri.put("-",3);
lpri.put(")",6);
//用哈希鍵值對映射右運算符的優先級
HashMap<String, Integer> rpri = new HashMap<String,Integer>();
rpri.put("=",0);
rpri.put("(",6);
rpri.put("*",4);
rpri.put("÷",4);
rpri.put("+",2);
rpri.put("-",2);
rpri.put(")",1);
//建立棧
Stacks op = new Stacks();
op.data = new char[200];
op.top=-1;
op.top++;
op.data[op.top]='=';
//把傳入的表達式轉化成字元數組
char[] exp = formula.toCharArray();
//用來存儲字尾表達式
ArrayList<Object> list = new ArrayList<Object>();
int j=0;
while(j<exp.length) {
if(!inOp(exp[j])) {
while(j<exp.length&&((exp[j]>='0'&& exp[j]<='9')||(exp[j]=='\'')||(exp[j]=='/'))) {
// System.out.println(exp.length);
list.add(exp[j]);
j++;
}
list.add('#');
}else {
switch(precede(lpri.get(op.data[op.top]+""),rpri.get(exp[j]+""))){
case -1:
op.top++;
op.data[op.top]=exp[j];
j++;
break;
case 0:
op.top--;
j++;
break;
case 1:
list.add(op.data[op.top]);
op.top--;
break;
}
}
}
while(op.data[op.top]!='=') {
list.add(op.data[op.top]);
op.top--;
}
return list;
}
運作結果
代碼:https://coding.net/u/chen556644/p/Job1/git
psp表格
PSP2.1 | Personal Software Process Stages | Time Senior Student | Time |
Planning | 計劃 | 10 | |
· Estimate | 估計這個任務需要多少時間 | 8 | |
Development | 開發 | 158 | 280 |
· Analysis | 需求分析 (包括學習新技術) | 13 | 15 |
· Design Spec | 生成設計文檔 | 17 | |
· Design Review | 設計複審 | 5 | 3 |
· Coding Standard | 代碼規範 | ||
· Design | 具體設計 | 20 | 50 |
· Coding | 具體編碼 | 120 | 180 |
· Code Review | 代碼複審 | 30 | |
· Test | 測試(自我測試,修改代碼,送出修改) | ||
Reporting | 報告 | NaN | |
· | 測試報告 | ||
計算工作量 | |||
并提出過程改進計劃 |