Program implementation automatically generates 30 arithmetic topics (2)

Source: Internet
Author: User

After the last mission, this week the teacher gave the automatic question system a new request, I feel I and two pillars
As pitiful, the teacher's request is
1. Avoid duplication of topics
2. The topic can be customized (quantity/printing method)
3. You can control the parameters
(1) Whether there is a multiplication method
(2) whether there are parentheses (up to 10 numbers to participate in the operation)
(3) Range of values
(4) Increase or decrease with or without negative numbers
(5) Division has no number

For this task, we individually request, analyze
For topic avoidance repetition can be solved by creating string arrays, comparing strings to each other, in addition to the topic
You can customize the number of problems generated by random numbers and bring them into the loop, while printing can be used in the file input and output stream
Save the data in a file. For the multiplication method, you can put the +-*/deposit into the array, randomly call
The range of values can be controlled by a random number, the difficulty of the subject can be generated in parentheses,
Parentheses are used to raise the priority, and for this we can use circular calls to implement it, meaning
The arithmetic expression can be seen as the pattern of the expression 1+ symbol + expression 2, which is entered in the loop-open expression
Can be randomly selected on the expression 1 or expression 2, you can also decide whether to have parentheses, so that randomly generated
The number of operands determines the number of cycles, plus or minus negative numbers and division has no number, here only two
The expression of the operand is manipulated.
Here is the source code of the experiment:

//randomly generated arithmetic expression Yang 2016.3.12#include <iostream>#include<string>#include<time.h>#include<stdio.h>#include<fstream>using namespacestd;stringstr1[4]={"+","-","*","/"};intnum1,num2,num3,num4,num5,num6,num7,num8,num9,num10,n,m,a,b;Charstr2[ -];Charstr3[ -];stringstr4[ -];voidInput (intNintp) {m=N; stringstr;    Ofstream outfile; Outfile.open ("a.txt", Ios::app); if(!outfile) {Cerr<<"OPEN error!"<<Endl; Exit (0); } NUM1=rand ()% -+1; Num2=rand ()% -+1; NUM3=rand ()%4; NUM8=NUM1; NUM9=num2; NUM10=num3; Itoa (NUM1,STR2,Ten); Itoa (NUM2,STR3,Ten); STR=str2+str1[num3]+STR3; N=n-2;  while(n!=0)//when n is not equal to 0 o'clock, the loop generates STR, which is the form of the expression + symbol + expression{num4=rand ()%2; if(num4==0)//the previous STR is placed on the left side of the symbol{NUM5=rand ()%2; if(num5==0)//previous STR without parentheses{num3=rand ()%4; NUM1=rand ()% -+1; Itoa (NUM1,STR2,Ten); STR=str+str1[num3]+str2; }            Else                           //previous STR parentheses{num3=rand ()%4; NUM1=rand ()% -+1; Itoa (NUM1,STR2,Ten); STR="("+str+")"+str1[num3]+str2; }        }        Else                              //the previous STR is placed on the right side of the symbol{NUM5=rand ()%2; if(num5==0)//previous STR without parentheses{num3=rand ()%4; NUM1=rand ()% -+1; Itoa (NUM1,STR2,Ten); STR=str2+str1[num3]+str; }            Else                         //previous STR parentheses{num3=rand ()%4; NUM1=rand ()% -+1; Itoa (NUM1,STR2,Ten); STR=str2+str1[num3]+"("+str+")"; }} N--; } Str4[p]=STR;//storing str in the string array str4     for(intI=0; i<p;i++)//query for arithmetic type whether there are duplicates        if(str4[i]==Str4[p])    Input (M,P); cout<<str4[p]<<"                       "; OutFile<<str4[p]<<Endl; if(m==2)    {        if(num10==0)//addition has no negative number{a=num8+num9; if(a>0) cout<<"Results"<<a<<"is a positive"; Else if(a==0) cout<<"Results"<<a<<"to zero"; Else if(a<0) cout<<"Results"<<a<<"is negative"; }        Else if(num10==1)//subtraction with or without negative numbers{a=num8-num9; if(a>0) cout<<"Results"<<a<<"is a positive"; Else if(a==0) cout<<"Results"<<a<<"to zero"; Else if(a<0) cout<<"Results"<<a<<"is negative"; }        Else if(num10==2) {a=num8*num9; if(a>0) cout<<"Results"<<a<<"is a positive"; Else if(a==0) cout<<"Results"<<a<<"to zero"; Else if(a<0) cout<<"Results"<<a<<"is negative"; }        Else if(num10==3)//Division has no number{b=num8%num9; if(b==0) cout<<"the remainder is 0"; Elsecout<<"the remainder is not 0"; }} cout<<Endl;}voidChangenum (intN) {     for(intI=0; i<n;i++)//randomly generated topics{NUM6=rand ()%9+2; Switch(NUM6) { Case 2: Input (2, i); Break;  Case 3: Input (3, i); Break;  Case 4: Input (4, i); Break;  Case 5: Input (5, i); Break;  Case 6: Input (6, i); Break;  Case 7: Input (7, i); Break;  Case 8: Input (8, i); Break;  Case 9: Input (9, i); Break;  Case Ten: Input (Ten, i); Break; }    }}voidMain () {Ofstream outfile1; Outfile1.open ("a.txt"); if(!outfile1) {Cerr<<"OPEN error!"<<Endl; Exit (0);             } srand ((unsigned) time (NULL)); //Random Seedscout<<"Enter the number of topics you want to do:"; CIN>>N;    Changenum (n); cout<<"four expression is not duplicated"<<Endl;}

The following are the results of the number of different topics:

Number of topics is 5

The corresponding file output

Number of topics is 30

The corresponding file output

Summary: The experiment is a bit hasty, barely in line with, there are many flaws in the place, true scores did not get

0 value judgment is not written, more than two numbers of the results of the calculation is not calculated, after a perfect, hope to do better.

Program implementation automatically generates 30 arithmetic topics (2)

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.