Github專案地址:https://github.com/bravedreamer/test/tree/master/Arithmetic
在線預覽:https://bravedreamer.github.io/test/Arithmetic/index.html
專案合作者:吳尚謙 3118004977 吳茂平3118004976
1.題目說明
實作一個自動生成小學四則運算題目的命令列程式(也可以用影像界面,具有相似功能),
自然數:0, 1, 2, …,
真分數:1/2, 1/3, 2/3, 1/4, 1’1/2, …,
運算子:+, ?, ×, ÷,
括號:(, ),
等號:=,
分隔符:空格(用于四則運算子和等號前后),
算術運算式:
e = n | e1 + e2 | e1 ? e2 | e1 × e2 | e1 ÷ e2 | (e),
其中e, e1和e2為運算式,n為自然數或真分數,
四則運算題目:e = ,其中e為算術運算式,
需求:
-
使用 -n 引數控制生成題目的個數,例如Myapp.exe -n 10,將生成10個題目,
-
使用 -r 引數控制題目中數值(自然數、真分數和真分數分母)的范圍,例如Myapp.exe -r 10
將生成10以內(不包括10)的四則運算題目,該引數可以設定為1或其他自然數,該引數必須給定,否則程式報錯并給出幫助資訊, -
生成的題目中計算程序不能產生負數,也就是說算術運算式中如果存在形如e1? e2的子運算式,那么e1≥ e2,
-
生成的題目中如果存在形如e1÷ e2的子運算式,那么其結果應是真分數,
-
每道題目中出現的運算子個數不超過3個,
-
程式一次運行生成的題目不能重復,即任何兩道題目不能通過有限次交換+和×左右的算術運算式變換為同一道題目,例如,23 + 45 = 和45 + 23 = 是重復的題目,6 × 8 = 和8 × 6 = 也是重復的題目,3+(2+1)和1+2+3這兩個題目是重復的,由于+是左結合的,1+2+3等價于(1+2)+3,也就是3+(1+2),也就是3+(2+1),但是1+2+3和3+2+1是不重復的兩道題,因為1+2+3等價于(1+2)+3,而3+2+1等價于(3+2)+1,它們之間不能通過有限次交換變成同一個題目,
生成的題目存入執行程式的當前目錄下的Exercises.txt檔案,格式如下:
四則運算題目1
四則運算題目2
……
其中真分數在輸入輸出時采用如下格式,真分數五分之三表示為3/5,真分數二又八分之三表示為2’3/8, -
在生成題目的同時,計算出所有題目的答案,并存入執行程式的當前目錄下的Answers.txt檔案,格式如下:
答案1
答案2
特別的,真分數的運算如下例所示:1/6 + 1/8 = 7/24, -
程式應能支持一萬道題目的生成,
-
程式支持對給定的題目檔案和答案檔案,判定答案中的對錯并進行數量統計,輸入引數如下:
Myapp.exe -e.txt -a .txt
統計結果輸出到檔案Grade.txt,格式如下:
Correct: 5 (1, 3, 5, 7, 9)
Wrong: 5 (2, 4, 6, 8, 10)
其中“:”后面的數字5表示對/錯的題目的數量,括號內的是對/錯題目的編號,為簡單起見,假設輸入的題目都是按照順序編號的符合規范的題目
2.PSP:
| PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
|---|---|---|---|
| Planning | 計劃 | 30 | 15 |
| · Estimate | · 估計這個任務需要多少時間 | 960 | 1365 |
| Development | 開發 | 840 | 1320 |
| · Analysis | · 需求分析 (包括學習新技術) | 30 | 15 |
| · Design Spec | · 生成設計檔案 | 20 | 20 |
| · Design Review | · 設計復審 (和同事審核設計檔案) | 10 | 5 |
| · Coding Standard | · 代碼規范 (為目前的開發制定合適的規范) | 10 | 10 |
| · Design | · 具體設計 | 10 | 10 |
| · Coding | · 具體編碼 | 720 | 1230 |
| · Code Review | · 代碼復審 | 10 | 10 |
| · Test | · 測驗(自我測驗,修改代碼,提交修改) | 30 | 20 |
| Reporting | 報告 | 40 | 30 |
| · Test Report | · 測驗報告 | 20 | 10 |
| · Size Measurement | · 計算作業量 | 10 | 10 |
| · Postmortem & Process Improvement Plan | · 事后總結, 并提出程序改進計劃 | 10 | 10 |
| 合計 | 910 | 1365 |
3.效能分析
隨著生成的題目數量不斷加大,這部分函式的消耗將會隨著題目數量增大而不斷增大
createQuestion(){//生成多道題目
//初始化資料串列
...
let questionData=https://www.cnblogs.com/brave-dreamer/p/[]
for(let i=0;i=0){
this.form.questionList[i]=question
this.form.answerList[i]=answer
let tag={}
tag.question=question+answer
tag.index=i+1
questionData[i]=tag
i++
}
}
this.tableData=questionData
},
4.實作思路

5.關鍵代碼分析
各函式功能基本在一個vue內實作,較為清晰,
new Vue({
...
beforeCreate() {
// 讀取檔案
FileReader.prototype.reading = function ({encode} = pms) {
let bytes = new Uint8Array(this.result); //無符號整型陣列
let text = new TextDecoder(encode || 'UTF-8').decode(bytes);
return text;
};
/* 重寫readAsBinaryString函式 */
FileReader.prototype.readAsBinaryString = function (f) {
if (!this.onload) //如果this未重寫onload函式,則創建一個公共處理方式
this.onload = e => { //在this.onload函式中,完成公共處理
let rs = this.reading();
console.log(rs);
};
this.readAsArrayBuffer(f); //內部會回呼this.onload方法
};
},
methods:{
...
tableRowClassName({row, rowIndex}) {//改變表格樣式
...
},
createOperationArr(arr1,arr2){//合并已生成的運算元陣列和運算子陣列
let operationArr=[]
let question=""
for(let i=0;i<arr2.length;i++){
question+=(arr1[i]+arr2[i])
operationArr.push(arr1[i])
operationArr.push(arr2[i])
if(i==(arr2.length-1)) {
question+=arr1[(i+1)]
operationArr.push(arr1[(i+1)])
}
}
return {operationArr,question}
},
createQuestionInfo(){//創建一道題目的運算子和運算元
let operation=[" + ", " ? ", " × ", " ÷ ", " / "," = "]//保存相關運算子
let operationTime=Math.floor(Math.random() * (3 - 1+1)+1)//運算次數
//隨機生成運算子
let operationSymbol=[]//保存生成的運算子
for(let k=0;k<operationTime;){
let i=Math.floor(Math.random() * (4 - 0+1))
if(i==4){
if(operationSymbol.length>0&&operationSymbol[operationSymbol.length-1]==operation[i]){
}else{
operationSymbol[operationSymbol.length]=operation[i]
}
}else{
operationSymbol[operationSymbol.length]=operation[i]
k++
}
}
// Math.floor(Math.random()*(n-m+1))+m 取m-n之間的亂數 [m,n]
//隨機生成運算元
let operationTagNumber=[]//保存生成的運算元
for(let k=0;k<=operationSymbol.length;){
let last=k-1
if(k>0&&(operationSymbol[last]==operation[4]||operationSymbol[last]==operation[3])){
let t=Math.floor(Math.random() * (Number(this.form.max) - Number(this.form.min))) +Number(this.form.min)
if(operationSymbol[last]==operation[4]&&t!=0&&operationTagNumber[last]<=t){
operationTagNumber[k]=t
k++
}
if(t!=0&&operationSymbol[last]==operation[3]){
operationTagNumber[k]=t
k++
}
}else{
operationTagNumber[k]=Math.floor(Math.random() * (Number(this.form.max) - Number(this.form.min))) +Number(this.form.min)
k++
}
}
let content=this.createOperationArr(operationTagNumber,operationSymbol)
let operationArr=content.operationArr
let question=content.question
question+=operation[5]
operationArr=this.getRPN(operationArr)
let answer=this.getResult(operationArr)
return{question,answer}
},
createQuestion(){//生成多道題目
//初始化資料串列
...
let questionData=https://www.cnblogs.com/brave-dreamer/p/[]
for(let i=0;i=0&&!isRepeat){
this.form.questionList[i]=question
this.form.answerList[i]=answer
let tag={}
tag.question=question+answer
tag.index=i+1
questionData[i]=tag
i++
}
}
this.tableData=questionData
},
getRPN(arr){//中綴運算式轉后綴運算式
let symbolPriority = {//確定運算優先級" # ": 0,
" + ": 1,
" ? ": 1,
" × ": 2,
" ÷ ": 2,
" / ": 3
}
let operand=[]//保存運算元的堆疊
let operator=[]//保存運算子的堆疊
arr.unshift(" # ")//方便進行運算優先級比較
for(let i=0;i<arr.length;i++){
if(typeof(arr[i])=="number"){
operand.push(arr[i])
}else{
switch (true){
case (arr[i]==' ( '||operator.slice(-1)[0]==' ( '):
operator.push(arr[i]);
break;
case (arr[i] == ' ) '):
do{
operand.push(operator.pop());
}while(operator.slice(-1)[0] != " ( ")
operator.pop()
break;
default:
if(operator.length == 0){
operator.push(arr[i]);
}else if(symbolPriority[operator.slice(-1)[0]]>=symbolPriority[arr[i]]){
do{
operand.push(operator.pop());
}while (symbolPriority[arr[i]]<=symbolPriority[operator[operator.length-1]])
operator.push(arr[i]);
}else {
operator.push(arr[i]);
}
break;
}
}
}
operator.forEach(function(){
operand.push(operator.pop());
});
operator.pop();//彈出"#"
return operand;
},
getResult(arr){//獲取計算結果
let result=[]//用于保存結果
let count
for(let i=0;i<arr.length;i++){
if(typeof(arr[i])=='string'){
....
}else{
result.push(arr[i])
}
}
return result[0]
},
downloadQuestion(){//下載題目和答案的txt檔案
let questionContent=""//題目內容
let answerContent=""//答案內容
if(this.form.questionList.length!=0){
let name1="Exercises"
let name2="Answers"
for(let i=0;i<this.form.questionList.length;i++){
questionContent+=(i+1)+"、"+this.form.questionList[i]+"\n"
answerContent+=(i+1)+"、"+this.form.answerList[i]+"\n"
}
this.download(name1,questionContent)
this.download(name2,answerContent)
}else{
this.$alert('題目串列為空,請重新生成題目', '', {
confirmButtonText: '確定',
});
}
},
download(filename, text){//下載TXT檔案
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
},
beforeUpload(file){//上傳檔案
this.fileList = [file]
console.log('選擇了檔案beforeUpload')
// 讀取資料
this.read(file);
return false
},
read(f) {//決議上傳過來的檔案
let rd = new FileReader();
rd.onload = e => {
//this.readAsArrayBuffer函式內,會回呼this.onload函式,在這里處理結果
let cont = rd.reading({encode: 'UTF-8'});
this.fileData.push(cont)
let formerData = https://www.cnblogs.com/brave-dreamer/p/this.textData;
this.textData = formerData +"\n" + cont;
};
rd.readAsBinaryString(f);
},
compareAnswer(){//檢查上傳過來的題目的答案的正確性并統計相關結果
let questionContent=[]//保存上傳過來的題目
let answerContent=[]//保存上傳過來的答案
let corretAnswer=[]//保存正確答案
let corretList=[]//保存題目正確答案的序號
let wrongList=[]//保存題目錯誤答案的序號
let corret=""
let wrong=""
//初始化資料串列
this.form.questionList=[]
this.form.answerList=[]
if(this.fileData.length!=0){
for(let i=0;i<this.fileData.length;i++){
if(this.fileData[i].includes("=")){
questionContent=this.fileData[i].split("\n")
for(let k=0;k<questionContent.length;k++){
for(let n=0;n<questionContent[i].length;n++){
if(questionContent[k][n]=="、")
questionContent[k]=questionContent[k].substr(n+1)
}
if(questionContent[k]==""){
questionContent.pop()
}else{
corretAnswer[k]=this.getCorrectAnswer(questionContent[k])//獲取正確答案
}
}
}else{
answerContent=this.fileData[i].split("\n")
for(let j=0;j<answerContent.length;j++){
for(let m=0;m<answerContent[j].length;m++){
if(answerContent[j][m]=="、")
answerContent[j]=answerContent[j].substr(m+1)
}
if(answerContent[j]!=""){
answerContent[j]=Number(answerContent[j])
}else{
answerContent.pop()
}
}
}
}
let questionData=https://www.cnblogs.com/brave-dreamer/p/[]
for(let n=0;n
6.測驗運行
-
界面整體如下:

-
控制引數可實作10000道題目生成,也可調節生成數值訪問

-
下載與上傳檔案均實作,無錯誤

-
批改作業

7. 小結
- 團隊專案合作比較重要,先做好計劃再動手不會很亂
- 選擇適當的工具有利于共同開發,比如github
3.兩人合作可以互動出新穎的想法
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/123526.html
標籤:JavaScript
上一篇:ECMAScript中的函式
