Implementation of automatic quiz function:
The program has not fulfilled all the requirements and will continue to improve.
Implementation results:
Program code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <time.h>
using namespace Std;
#define N 100
BOOL Createproblem ()
{
int i,op,x;
Char st[100];
int dt[100];
int ct[2];
memset (dt,-1,sizeof (DT));
i=0;
ct[0]=ct[1]=0;
Op=rand ()%7;
if (op==0) {
St[i++]= ' (';
ct[op]++;
}
Dt[i++]=rand ()%N;
int count=0;
while (count<100)
{
Op=rand ()%7;
if (op==6)
{
st[i++]= ' = ';
if (Ct[0]!=ct[1]) {
return false;
}
Break
}
else if (op==0 | | op==1)
{
if (op==0) {
if (dt[i-1]==-1&&st[i-1]!= ') ') {//' (' There is no integer before ' (') ' (' before ' is an operation symbol or ' (') ' ('
St[i++]= ' (';
ct[op]++;
} else {
return false;
}
} else if (Op==1&&ct[0]>ct[1]) {
if (dt[i-1]!=-1 | | st[i-1]== ' (') {
St[i++]= ') ';
ct[op]++;
} else {
return false;
}
}
}
Else
{
if (dt[i-1]!=-1| | st[i-1]== ' (') {
if (op==2) {
st[i++]= ' + ';
} else if (op==3) {
st[i++]= '-';
} else if (op==4) {
st[i++]= ' * ';
} else if (op==5) {
st[i++]= '/'; The case of divisor 0 is not judged for the time being
} else {
return false;
}
X=rand ()%N;
int M=N/5;
while (x<=m) {
St[i++]= ' (';
ct[0]++;
X=rand ()%N;
}
Dt[i++]=rand ()%N;
}
}
count++;
}
if (count==100) {
return false;
}
int n=i;
st[n]= '? ';
for (i=0;i<=n;i++)
{
if (dt[i]==-1) {
cout<<st[i];
} else {
cout<<dt[i];
}
}
return true;
}
int main ()
{
int i;
Srand (Time (NULL));
Srand ((unsigned) time (NULL));
for (i=1;i<=20;i++)
{
while (1) {
if (Createproblem ()) {
cout<<endl;
Break
}
}
}
return 0;
}
The first chapter of modern software engineering introduction exercise 1th Li Yanwei