Knot-Pair Project summary

Source: Internet
Author: User

The knot-pair project is finally over. Here is a summary of our group procedures.

Before hurriedly learned the initial use of MFC, and put it into practice, the effect is very satisfactory, the small program has a model.

The idea of our procedure is probably: take a 10-way title quiz, judge the right and wrong and see the answer. A total of 50 questions. Of course, we have the automatic question module, can automatically generate 50 new questions. There are also errors in judging the problem (such as 0 errors, symbolic errors, etc.), but we will not have these problems automatically generated topics >_<

This is our interface, the initial interface:

Clicking on the "Auto Quiz" button automatically generates 50 questions, and the popup dialog shows success, like this:

Then click "Start Test" to generate 10 questions on the left, like this:

Notice that the Start Test button becomes "test Again" and becomes grayed out, and cannot be pressed. Only after you have answered the question to confirm the submission will it light up and display the next question:

The answer is displayed in the Answer box on the right. If you click Test again, the interface will go back to the previous picture.

If you complete the 50-question test, you will see this:

Of course, you can continue testing by regenerating the 50 questions.

To exit the program, click Exit Test or the X in the upper-right corner.

Here is the specific code:

This is the core of the calculator class, including the calculation formula (infix suffix, suffix calculation, etc.), error detection and other functions:

Class calculator//Core Calculator classes {public://Auxiliary calculation parameter double result;//calculation result Fenshu fresult;//fraction calculation result myerror error;//error during calculation String str;//holds infix expression Calculator (string s): Fresult (1, 1) {//Calculator initialize u = new unit (); str = s;accuracy = 3;maxunit = 80;datera Nge = 100000;clear ();} Myerror Run () {//Calculate the value of the expression, deposit resultmyerror Temperror = Zzh (str), if (temperror! = error_no) {ERROR = Temperror;result =-1111 1;return Error;} int I;bool B = true;for (i = 0; I<str.size (); i++) {//No decimal point, calculate score result if (str[i] = = '. ') {b = False;break;}} if (b) {temperror = Getfresult ();//messagebox (Printerror (temperror). C_STR (), "Errorerror", Mb_iconhand); if (temperror ! = error_no) {Fenshu F ( -1,-1); fresult = f; Error = Temperror;return error;} else if (ABS (FRESULT.FZ) >daterange | | ABS (FRESULT.FM) >daterange) {error = Error_range;return error;}} else {temperror = GetResult (); if (temperror! = error_no) {error = Temperror;result = -11111;return error;} else if (ABS (Result) >daterange) {error = Error_range;return error;}} return error_no;} VOID Clear () {//Empty calculator all auxiliary calculation parameters num = 0; Error = Error_no;result = 0;fenshu F (1, 1), Fresult = F;str = "";d elete U;u = new Unit[maxunit];} void Recalculator (string s) {//Restart Calculator object clear (); str = s;}  String Getmyresult () {//Gets the result of the calculation, decimal result or fractional result int i = 0;char s[20];string ss;for (; i<str.size (); i++) {if (str[i] = = '. ') {if (accuracy! =-1)//judgment precision and output sprintf (s, "%.*lf", accuracy, result); elsesprintf (S, "%g", result); SS = S;return SS;}} SS = Fresult.getfenshu (); return SS;} Myerror setdaterange (int type) {//Set data range if (0<type) {daterange = Type;return error_no;} Elseerror = Error_set;return Error_set;} Myerror setmaxunit (int num) {//Set maximum number of recognized if (0<num&&num <=) {maxunit = Num;u = new Unit[maxunit];//empty suffix expression Type this->num = 0;return error_no;} Elseerror = Error_set;return Error_set;} Myerror setaccuracy (int a) {//Set precision if (a >=-1 && a <= 6) {accuracy = A;return error_no;} Elseerror = Error_set;return Error_set;} private://Non-auxiliary calculation parameter, after setting, unless set repeatedly, otherwise it will not be cleared such as clear int daterange;//equation parameterThe range of data in int maxunit;//The maximum number of characters that can be recognized by the equation parameter int accuracy;//decimal exact number of digits, 1 is imprecise, that is, all end 0 is removed, the other number is the number of digits retained after the decimal point//Auxiliary calculation parameter unit *u;//store suffix expression int num;//suffix expression Unit number Myerror Zzh (string s) {//Infix expression-to-suffix expression if (s.size () >maxunit) {return error_string;//error, The passed-in calculation length exceeds the maximum number of identifiers set}char C;char *temp1 = new Char[maxunit];d ouble temp;string stemp;stack<char> st;while (!s.empty ()) {///If the string is not empty then continue looping c = S[0];if (Isoperator (c)) {//Is the operator s.erase (0, 1);//Remove the operator from string if (Pushintostack (c, &st) = = E Rror_operator) return error_operator;} else if (Isnum (c)) {//is the number StringStream SST (s); SST >> temp;sprintf (Temp1, "%g", temp); stemp = temp1;s.erase (0, Stemp. Size ());//delete the number Sst.clear () from the STRING, U[num++].set (temp);//Store the number in the stack}else {return error_string;}} if (Pushintostack (' # ', &st) = = Error_operator) return Error_operator;return Error_no;} BOOL Isoperator (char c) {//Determines whether the operator if (c = = ' + ') return true;if (c = = '-') return true;if (c = = ' * ') return true;if (c = = '/ return True;if (c = = ' (') return true;if (c = = ') ') return True;retuRN false;} BOOL Isnum (char c) {if (c >= ' 0 ' &&c <= ' 9 ') return True;return false;} int Youxian (char C1, char C2) {//Determine two operator precedence if (C2 = = ' # ')//Terminator return 0;if (C2 = = ' (') return 1;if (C2 = = ') ') if (c1 = = ' (') Return 2;elsereturn 0;if (c1 = = ' (') if (C2 = = ' + ' | | c2 = '-' | | c2 = ' * ' | | c2 = '/') return 1;if (C1 = = ' * ' | | c1 = '/') return 0;if (C1 = = ' + ' | | c1 = = '-') if (C2 = = ' * ' | | c2 = '/') return 1;else if (C2 = = ' + ' | | c2 = '-') return 0;retur n-1;//illegal operator}myerror Pushintostack (char C, stack<char> *st) {//operator performs a series of in-Stack judgment operations char a;int y = 0;while (!st->empt Y ()) {a = St->top (), y = Youxian (A, C), if (y = = 0) {//later operator Priority Small St->pop (); U[num++].set (a);} else if (y = = 1) {//later operator precedence large break;} else if (y = = 2) {///Two operators are ' (' and ') ' St->pop (); return error_no;} Elsereturn Error_operator;} St->push (c); return error_no;} void Test () {//Output suffix expression, test with (persistence) int i;cout << num << endl;for (i = 0; i<num; i++) {if (U[i].kind = = 1) cout & lt;< u[i].op << ""; else if (u[i].kind = = 2) cout << u[i].num << "";}} Myerror GetResult () {//is called by the run function to get the decimal result, which is stored in result int I;char op;double NUM1, num2;stack<double> st;for (i = 0; i<n Um i++) {//processing suffix expression if (u[i].kind = = 2) {//If the number is in the stack St.push (u[i].num);} else if (U[i].kind = = 1) {//If it is an operator, then the stack of two digits op = u[i].op;if (St.empty ()) return error_string;//calculation illegal num2 = St.top (); St.pop (); if (St.empty ()) return error_string;//equation illegal num1 = st.top (); St.pop (); switch (OP) {case ' + ': St.push (NUM1 + num2); break;case '-': St.push (num1-num2); Break;case ' * ': St.push (num1*num2); Break;case '/': if (num2 = = 0) return error_zero;// Except for 0 error St.push (num1/num2); Elsereturn error_string;//formula Illegal}result = St.top (); return error_no;}  Myerror Getfresult () {//is called by the run function to get the score result, which is stored in Fresult int i;char Op;fenshu F1 (1, 1), F2 (1, 1);stack<fenshu> st;for (i = 0; i<num; i++) {if (U[i].kind = = 2) {//If the number is in the Stack St.push (Fenshu (U[i].num, 1));} else if (U[i].kind = = 1) {//If it is an operator, then the stack of two digits op = u[i].op;if (St.empty ()) return error_string;//calculation illegal F2 = St. Top (); St.pop (); if (St.empty ()) return error_string;//equation Illegal f1 = st.top (); St.pop (); switch (OP) {case ' + ': St.push (F1 + F2); Break;case '-': St.push (F1-F2); Break;case ' * ': St.push (F1*F2); Break;case '/': if (F2.fz = = 0) {return error_zero;//except 0 error} St.push (F1/F2); break;}} Elsereturn error_string;//formula Illegal}fresult = St.top (); return error_no;}

This is the defined error and the function that shows the error:

Enum Myerror {error_no = 0, Error_set = 1, Error_operator = 2, Error_zero = 4, error_string = 8, Error_range =};strin G Printerror (Myerror me) {if (me = = error_no) {return "no error";} else if (me = = Error_set) {return ' Set argument is illegal ';} else if (me = = error_operator) {return ' operator is illegal ';} else if (me = = Error_zero) {return "except 0 error";} else if (me = = error_string) {return "formula is illegal";} else if (me = = Error_range) {return ' calculation result out of range ';} else {return ' undefined error type ';} cout<< "error code:" << me << Endl << Endl;}

Because MFC was used instead, the previous cout were replaced with a return string, which is displayed directly in the MessageBox.

The following is a button that generates a calculation, as previously stated, to avoid the occurrence of various errors (such as the exception of 0 errors, which are illegal, etc.):

void Cmfctest4dlgonbnclickedauto () {TODO here adds control notification Handler code srand ((int) time (0)); string Equation;char Temp[100];ofstream Outf (equation.txt); int i = 50;cout Enter the number of the generated calculation:; cin I;while (i--) {equation.clear (); if (i% 2) {fractional operation char lastop = ' + '; previous operator int num = random (3) + 4; The calculation contains the number of operands -1 sprintf (temp,%d, random (20) + 1); The first operand equation.append (temp); while (num--) {int b;if (lastop = =) prevents the occurrence of continuous Division B = Random (2); elseb = random (n); switch (b) { Case 0case 4case 8lastop = temp[0] = ' + '; break;case 1case 5case 9lastop = temp[0] = '-'; break;case 2case 6case 10lastop = Temp[0] = "; Break;case 3case 7case 11lastop = temp[0] ="; break;} TEMP[1] = 0;equation.append (temp); sprintf (temp,%d, random () + 1); Equation.append (temp);} int k, a = 0;for (int j = 0; Jequation.size (); j + +) {Add parentheses if ((equation[j] = = ' + ' equation[j] = = '-') && a = = 0) {a++;} else if ((equation[j] = = ' + ' equation[j] = = '-') && a = = 1) {k = j-1; add opening parenthesis 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; add closing parenthesis while (!isoperator (equation[k + 1]) && k! = Equation.size ()-1) k++;equatio N.insert (k + 1,)); break;}} Coutequationendl;} else {decimal operation char lastop = ' + '; the previous operator int num = random (3) + 4; The calculation contains the number of operands -1 int temp1 = random (+) + 1;sprintf (temp,%g, tem P1 10.0); The first operand equation.append (temp); while (num--) {int b;if (lastop = =) prevents the occurrence of continuous Division B = Random (2); elseb = random (n); switch (b) { Case 0case 4case 8lastop = temp[0] = ' + '; break;case 1case 5case 9lastop = temp[0] = '-'; break;case 2case 6case 10lastop = Temp[0] = "; Break;case 3case 7case 11lastop = temp[0] ="; break;} TEMP[1] = 0;equation.append (temp), int temp2 = random ($) + 1;if (Equation[equation.size ()-1] = = && (temp1%te MP2)! = 0) {Temp2 = Temp1 5 + 1;while (TEMP1%TEMP2) {temp2++;}} Temp1 = temp2;sprintf (temp,%g, TEMP2 10.0); Equation.append (temp);} Coutequationendl;} Outf equation Endl;} cout Generating a calculation successfully Endl MessageBox (Generate 50 formula success, hint, mb_iconinformation); GetDlgItem (IDOK2)-enablewindow (true); In.close (); In.open (equation.txt); Outf.close ();}

Here's what happens when you click on the "Confirm Submit" button:

void Cmfctest4dlgonbnclickedbutton10 ()//Confirm commit button {int a = 0;//loop save user answer while (a) {Cal.recalculator (str_buf[a]);//Restart Calculator, and pass in the equation parameter Temperror = Cal.run (); MessageBox (Printerror (temperror). C_str (), ERROR, Mb_iconhand), if (Cal. Error! = error_no)//Display errors {MessageBox (Printerror (temperror). C_STR (), error, Mb_iconhand);} Switch (a) {case 0GetDlgItemText (idc_edit1, Answer[a]); Setdlgitemtext (IDC_A1, Cal.getmyresult (). C_STR ()); Break;case 1GetDlgItemText (Idc_edit2, Answer[a]); Setdlgitemtext (IDC_A2, Cal.getmyresult (). C_STR ()); Break;case 2GetDlgItemText (Idc_edit3, Answer[a]); Setdlgitemtext (Idc_a3, Cal.getmyresult (). C_STR ()); Break;case 3GetDlgItemText (IDC_EDIT4, Answer[a]); Setdlgitemtext (IDC_A4, Cal.getmyresult (). C_STR ()); Break;case 4GetDlgItemText (Idc_edit5, Answer[a]); Setdlgitemtext (Idc_a5, Cal.getmyresult (). C_STR ()); Break;case 5GetDlgItemText (Idc_edit6, Answer[a]); Setdlgitemtext (Idc_a6, Cal.getmyresult (). C_STR ()); Break;case 6GetDlgItemText (Idc_edit7, Answer[a]); Setdlgitemtext (Idc_a7, Cal.getmyresult (). C_STR ()); Break;case 7GetDlgItemText (Idc_edit8, Answer[a]); Setdlgitemtext (Idc_a8, Cal.getmyresult (). C_STR ()); Break;case 8GetDlgItemText (Idc_edit9, Answer[a]); Setdlgitemtext (IDC_A9, Cal.getmyresult (). C_STR ()); Break;case 9GetDlgItemText (idc_edit10, Answer[a]); Setdlgitemtext (Idc_a10,cal.getmyresult (). C_STR ()); break;} if (Answer[a]. GetString () = = Cal.getmyresult ()) {correct++;} else{incorrect++;} a++;} Char buf[2][3];sprintf (buf[0],%d, correct), sprintf (Buf[1],%d, incorrect); Setdlgitemtext (Idc_correct, buf[0]);//Display the correct number of questions Setdlgitemtext (Idc_incorrect, buf[1]);//Display the wrong number of problems GetDlgItem (IDOK2)- EnableWindow (TRUE);//Set the "Test Again" button to click GetDlgItem (IDC_BUTTON10)-enablewindow (FALSE);//Set yourself to not click}

where char two-dimensional arrays are stored correctly and incorrectly, I set two global variables, correct and incorrect, to record the correct number of incorrect questions.

Here is the "Start test" button and the "Test Again" button:

void Cmfctest4dlgonbnclickedok2 ()//Real Start button {//todo This adds control to the notification handler code int e = 0;last = False;correct = 0;incorrect = 0; Setdlgitemtext (idc_correct, 0); Setdlgitemtext (idc_incorrect, 0); (e) {if (in Str_buf[e]) {switch (e) {case 0SetDlgItemText (idc_edit1,); Setdlgitemtext (IDC_A1, answer to the first question); Setdlgitemtext (Idc_equality1, Str_buf[e].c_str ()); Break;case 1SetDlgItemText (Idc_edit2,); Setdlgitemtext (IDC_A2, answer to the second question); Setdlgitemtext (Idc_equality2, Str_buf[e].c_str ()); Break;case 2SetDlgItemText (Idc_edit3,); Setdlgitemtext (IDC_A3, answer to the third question); Setdlgitemtext (Idc_equality3, Str_buf[e].c_str ()); Break;case 3SetDlgItemText (IDC_EDIT4,); Setdlgitemtext (IDC_A4, answer to question fourth); Setdlgitemtext (Idc_equality4, Str_buf[e].c_str ()); Break;case 4SetDlgItemText (Idc_edit5,); Setdlgitemtext (Idc_a5, answer to question fifth); Setdlgitemtext (Idc_equality5, Str_buf[e].c_str ()); Break;case 5SetDlgItemText (Idc_edit6,); Setdlgitemtext (IDC_A6, answer to question sixth); Setdlgitemtext (Idc_equality6, Str_buf[e].c_str ()); Break;case 6SetDlgItemText (idc_edit7,); Setdlgitemtext (IDC_A7, sectionThe answer to seven questions); Setdlgitemtext (Idc_equality7, Str_buf[e].c_str ()); Break;case 7SetDlgItemText (Idc_edit8,); Setdlgitemtext (idc_a8, answer to question eighth); Setdlgitemtext (Idc_equality8, Str_buf[e].c_str ()); Break;case 8SetDlgItemText (Idc_edit9,); Setdlgitemtext (idc_a9, answer to question nineth); Setdlgitemtext (Idc_equality9, Str_buf[e].c_str ()); Break;case 9SetDlgItemText (idc_edit10,); Setdlgitemtext (idc_a10, answer to question tenth); Setdlgitemtext (Idc_equality10, Str_buf[e].c_str ()); break;}} Else{last = true;} e++;} After the start, changed to "test again" can not press Setdlgitemtext again (IDOK2, test again); GetDlgItem (IDOK2)-enablewindow (false); GetDlgItem (IDC_BUTTON10)-enablewindow (true); if (last) {MessageBox (no more questions!) The test is over! , hint, mb_iconinformation); GetDlgItem (IDC_BUTTON10)-enablewindow (FALSE);//When there are no more questions, set the Confirm Submit button to enable. In.close ();}}

The above is the main content of our project. This project I did not pull, paid their own efforts to complete after the refreshing. I hope the group project will be equally smooth.

Knot-Pair Project summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.