Automatically generate arithmetic topics

Source: Internet
Author: User

    • Main function: randomly generate arithmetic topics
    • Design ideas: First through a simple arithmetic, and then constantly improve
    • Source

#include"stdio.h"Main () {inta,b,c,d; printf ("auto-Generate arithmetic: \ n"); printf ("%d+%d= \ n", A, b); printf ("%d-%d= \ n", A, b); printf ("%d*%d= \ n", A, b); printf ("%d/%d= \ n", A, b);} 

This is just the beginning of the arithmetic, is the simplest arithmetic, but also has many shortcomings, such as: The value is too large, randomly generated numbers are pseudo-random numbers, can not generate a fraction of the arithmetic, subtraction can not be randomly generated.
To solve the problem that the random values are too large I used the function of the int rand (void) to generate the random number and added the Limit rand ()%100 so that it can only generate integers between 0~99. And in order to solve the pseudo-random number I used Srand ((unsigned) time (0)) this with segment code. I will subtraction four symbols also randomly generated with the rand () function.

#include"stdio.h"#include"stdlib.h"#include"time.h"Main () {Chari; inta,b,c,d,n,m,j,k,e; printf ("********** automatic generation of arithmetic ***********\n"); printf ("1. Start \ n"); printf ("2. Exit \ n"); printf ("Please select: \ n"); scanf ("%d",&N); if(n==1) {srand (unsigned) time (NULL)); printf ("Please enter the number of generated questions:"); scanf ("%d",&m);  for(j=0; j<m;j++) {e=rand ()%4; if(e==0) {i='+';} if(e==1) {i='-';} if(e==2) {i='*';} if(e==3) {i='/';} A=rand ()% -; b=rand ()% -; C=rand ()% -; D=rand ()% -; K=rand ()%2; Switch(k) { Case 0:p rintf ("%d%c%d=\n", a,i,b); Break;  Case 1:{if(A<=b&&c<=d) printf ("%d/%d%c%d/%d=\n", a,b,i,c,d); Else if(a>b&&c>d) printf ("%d/%d%c%d/%d=\n", B,a,i,d,c); Else if(a<=b&&c>d) printf ("%d/%d%c%d/%d=\n", A,b,i,d,c); Else if(A>b&&c<=d) printf ("%d/%d%c%d/%d=\n", b,a,i,c,d); } Break; }                            }                }     if(n==2) {exit (0); return 0; }}
    • Program run:

    • Summary:   

After this writing arithmetic, in my continuous revision process, I began to master the C language, and learned a lot from it, I obviously feel that I have been in a change in the progress of the time.

Automatically generate arithmetic topics

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.