辅助判卷程序项目的扩展--自动出题
生活随笔
收集整理的這篇文章主要介紹了
辅助判卷程序项目的扩展--自动出题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
既完成了主模塊---計算題目的設計后,我就開始了自動出題程序的設計,這個程序的思路比較簡單,并不是很完美
下面是程序截圖和生成的算式
題目中最多包含一對括號,此程序唯一的遺憾就是有時候計算結果會很大例如7736/4這樣的結果
下面是這個程序的全部代碼
#include <iostream> using namespace std; #include <string> #include <fstream> #include<time.h> #include<stdlib.h>#define random(x) (rand()%x)bool isoperator(char c){if(c=='+'||c=='-'||c=='*'||c=='/')return true;return false; }int main(int argc, char** argv) {srand((int)time(0));string equation;char temp[100];ofstream outf("equation.txt");int i=0;cout<<"輸入生成算式的數量:";cin>>i;while(i--){equation.clear();if(i%2){ //分數運算 char lastop='+'; //上一個運算符 int num=random(7)+4; //算式包含的操作數個數-1 sprintf(temp,"%d",random(20)+1); //第一個操作數 equation.append(temp);while(num--){int b; if(lastop=='/') //防止連續除法的出現 b=random(2);elseb=random(12);switch(b){case 0:case 4:case 8:lastop=temp[0]='+';break;case 1:case 5:case 9:lastop=temp[0]='-';break;case 2:case 6:case 10:lastop=temp[0]='*';break;case 3:case 7:case 11:lastop=temp[0]='/';break;} temp[1]=0;equation.append(temp);sprintf(temp,"%d",random(20)+1); equation.append(temp);}int k,a=0;for(int j=0;j<equation.size();j++){ //添加括號 if((equation[j]=='+'||equation[j]=='-')&&a==0){a++;} else if((equation[j]=='+'||equation[j]=='-')&&a==1){k=j-1; //添加左括號 while(!isoperator(equation[k-1])&&k!=0) k--;if(equation[k-1]=='/'){k--;while(!isoperator(equation[k-1])&&k!=0) k--;}equation.insert(k,"(");k=j+2; //添加右括號 while(!isoperator(equation[k+1])&&k!=equation.size()-1) k++;equation.insert(k+1,")");break;}}//cout<<equation<<endl; }else{ //小數運算 char lastop='+'; //上一個運算符 int num=random(7)+4; //算式包含的操作數個數-1 int temp1=random(200)+1;sprintf(temp,"%g",temp1/10.0); //第一個操作數 equation.append(temp);while(num--){int b; if(lastop=='/') //防止連續除法的出現 b=random(2);elseb=random(12);switch(b){case 0:case 4:case 8:lastop=temp[0]='+';break;case 1:case 5:case 9:lastop=temp[0]='-';break;case 2:case 6:case 10:lastop=temp[0]='*';break;case 3:case 7:case 11:lastop=temp[0]='/';break;} temp[1]=0;equation.append(temp);int temp2=random(200)+1;if(equation[equation.size()-1]=='/'&&(temp1%temp2)!=0){temp2=temp1/5+1;while(temp1%temp2){temp2++;}}temp1=temp2;sprintf(temp,"%g",temp2/10.0); equation.append(temp);}//cout<<equation<<endl; }outf<<equation<<endl; }cout<<"生成算式成功"<<endl; outf.close();return 0; }?
轉載于:https://www.cnblogs.com/chengyu404/p/5295935.html
總結
以上是生活随笔為你收集整理的辅助判卷程序项目的扩展--自动出题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: node源码详解(四) —— js代码如
- 下一篇: oracle学习笔记系列------or