The first chapter of modern software engineering introduction exercise 1th Li Yanwei

Source: Internet
Author: User

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

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.