- 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; }}
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