Writing a simple program with C + + to randomly produce multiple two-digit arithmetic formulas

Source: Internet
Author: User

A design idea:

1. The first thing you can think of is the composition of a arithmetic formula: two operands and an operator;

2. The random function of two operands is generated by invoking random functions, in which the range of operands can be set;

3. The random generation of an operator can be divided into subtraction four cases, respectively, by generating four random numbers to determine which operator;

4. Finally, the two combine to complete the generation of stochastic formula;

Two program code:

#include"stdafx.h"#include"stdlib.h"      //call one of the random functions#include"iostream.h"#include"time.h"intMainintargcChar*argv[]) {srand (unsigned) time (NULL));//make a randomly generated formula non-repetitive    intK1,k2,n;//K1,k2 is a two random number, producing n formulas    intCh//CH has four different casescout<<"Please enter the number of question:"; CIN>>N;  for(intI=1; i<=n;i++) {K1=rand ()% -;//randomly generates a number within 100K2=rand ()% -; CH=rand ()%4;//randomly generates an operation symbol        Switch(CH) { Case 0: cout<<k1<<"+"<<k2<<"="<<Endl;  Break;  Case 1: cout<<k1<<"-"<<k2<<"="<<Endl;  Break;  Case 2: cout<<k1<<"*"<<k2<<"="<<Endl;  Break;  Case 3: cout<<k1<<"/"<<k2<<"="<<Endl;  Break; }    }    return 0;}

Three running results:

The number of formulas can be entered by the user themselves, here choose to produce 20:

Four classes not completed in time reasons:
1. First is the most basic random function of the header file if #include "stdlib.h", What I thought in class is "MATH.H".

2. Another is that the random generation of operational symbols has not been achieved, not just to randomly produce special symbols, you can convert it to the generation of random situations, that is, four of cases of production.

Writing a simple program with C + + to randomly produce multiple two-digit arithmetic formulas

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.