Software Engineering after-work 30-way Random four operation program 3

Source: Internet
Author: User
Tags mul

First, the topic requirements:

The user enters the answer, the program automatically determines the correct error, and finally gives the total number of pairs/errors.

Second, the design idea:

On the basis of the original program to increase the arithmetic results and save with result, after the user entered a result and compared with it, the equality is displayed correctly, answer the wrong to display the correct answer.

Third, the code:

#include <iostream.h> #include <stdlib.h> #include <time.h>int answer=0;int score=0;int result=0; void display (int number,int mul,int rage,int neg)//Custom output function {int first[1000],second[1000],opt[1000];//, respectively, representing the first number, The second number and the operator for (int i=0;i<number;i++)//random two build operand {int m=1;//avoid duplication of title and initialize First[i]=rand ()%rage;//randomly produce the first number second[i]= Rand ()%rage;//randomly generates the second number if (mul==0)//There is no multiplication method {Opt[i]=rand ()%2;//randomly generate 0~1 numbers, respectively representing the plus and minus}if (mul==1) {Opt[i]=rand ()%4;//randomly generated 0 The number of subtraction}for (int j=0;j<i;j++) {if (first[j]==first[i]&&second[j]==second[i]&&opt[j]==opt [i]) Compare the newly generated calculation with the original or not repeat {i=i-1;m=0;}} while (m)//If the calculation is not repeated then output {switch (Opt[i]) {case 0:cout<<first[i]<< "+" <<second[i]<< "="; result= First[i]+second[i];break;case 1:if (neg==0)//0 indicates that the subtraction result does not allow negative numbers, and 1 indicates that a negative number {int temp;//intermediate variable if (First[i]<second[i]) is allowed { Temp=first[i];first[i]=second[i];second[i]=temp;} cout<<first[i]<< "-" <<second[i]<< "="; result=first[i]-second[i];break;} else{cout<<first[i]<< "-" <<second[i]<< "="; result=first[i]-second[i];break;} Case 2:cout<<first[i]<< "*" <<second[i]<< "="; Result=first[i]*second[i];break;case 3:if ( second[i]==0)//Avoid a denominator of zero processing {i=i-1;break;} else{cout<<first[i]<< "/" <<second[i]<< "="; result=first[i]/second[i];break;} break;} Cin>>answer;if (Answer==result) {score+=1;cout<< "right" <<endl;break;} else{cout<< "Wrong!the answer is:" <<result<<endl;break;}} cout<< "You answered the question number is:" &LT;&LT;SCORE&LT;&LT;ENDL;} void Main () {srand (unsigned) time (NULL));//Initialize the random number generator so that each run generates a different number of random numbers int number;//custom topic quantity int mul;//need multiplication method int rage;// Control numeric range int neg;//Controls whether the subtraction result allows a negative int remainder;//to control whether division allows the remainder cout<< "Please enter the number of topics to be printed:"; cin>>number;if >0) {cout<<endl;cout<< "Please enter the range of values (i.e. the maximum number) of positive integers:";cin>>rage;cout<<endl;cout<< "whether there is multiplication method ( 0 means no, 1 indicates there are no negative numbers for ";cin>>mul;cout<<endl;cout<<" subtraction (0 means no; 1 is indicated): ";cin>>neg;cout<< Endl;} else{cout<< "Input error, please re-enter!" <<endL        cout<< "Please enter the number of topics to print:";cin>>number;cout<<endl; cout<< "Please enter a range of values for positive integers (i.e. maximum number):";cin>>rage;cout<<endl;cout<< "whether there is a multiplication method (0 means no; 1 indicates):";cin> >mul;cout<<endl;cout<< "Subtraction has no negative number (0 means no; 1 is indicated):"; Cin>>neg;cout<<endl;} Display (Number,mul,rage,neg);//Call Function}

Iv. Testing and:

Five, the experiment Summary:

The experiment realizes the error that can automatically answer the user's input answer, but when it comes to the arithmetic of surplus number, there are some defects, and other aspects are basically realized.

六、三个 Log table:

Software Engineering after-work 30-way Random four operation program 3

Related Article

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.