设计思路:
在第一次实验的基础上(第一次并没有建类),建立Arithmetic类,然后依次添加新的功能模块(添加新的函数体或者在原有函数体上做改动)
通过switch语句实现功能的选择不同的case语句调用不同的函数体实现功能
通过对随机数对4求余数来随机产生不同的运算符
通过随机数对10求余来确定括号的个数
通过减去余数(a=(a-a%b))来确保没有余数
通过加一个数来确保没有余数是产生有余数的运算式
通过随机函数确定负号位置
Arithmetic.h
#pragma once
#include<iostream>
using namespace std;
class Arithmetic
{
protected:
int a,b,c,d,y,m;
public:
Arithmetic(void);
~Arithmetic(void);
void Intnumber(int N,int m,int F,int O,int P);
void Mixnumber(int N,int m,int F,int O);
void show1(int y,int a,int b,int o,int P);
void show2(int y,int a,int b,int c,int d);
void Havespace(int F,int N);
};
Arithmetic.cpp
#include "Arithmetic.h"
Arithmetic::Arithmetic(void)
{
}
Arithmetic::~Arithmetic(void)
{
}
void Arithmetic::Intnumber(int N,int m,int F,int O,int P)
{
for(int i=0;i<N;i++)
{
a=rand()%F;
b=rand()%F;
if(m==1)
{ y=rand()%2;}
else if(m==2)
{ y=rand()%4;} //判断是否有乘除
if(b==0||(a==b&&b==c)) //避免重复且除数不为0
{
i--;
}
else
{
show1(y,a,b,O,P);
}
}
}
void Arithmetic::Mixnumber(int N,int m,int F,int O)
{
for(int i=0;i<N;i++)
{
a=rand()%F;
b=rand()%F;
if(m==1)
{ y=rand()%2;}
else if(m==2)
{ y=rand()%4;} //判断是否有乘除
c=rand()%100;
d=rand()%100;
if(b==0||b>=a||d>=c||(a==b&&b==c)) //确保假分数避免重复且除数不为0
{
i--;
}
else
{
show2(y,a,b,c,d);
}
}
}
void Arithmetic::show1(int y,int a,int b,int o,int p)
{
if(y==0)
{
if(o==1)//判断有无负数
{cout<<a<<"+"<<b<<"="<<endl;}
else
{
if(rand()%3==0)//随机确定负号位置
{cout<<"(-"<<a<<")+"<<b<<"="<<endl;}
if(rand()%3==1)
{cout<<a<<"+(-"<<b<<")="<<endl;}
else
{cout<<"(-"<<a<<")+(-"<<b<<")="<<endl;}
}
}
else if(y==1)//判断有无负数
{
if(o==1)//随机确定负号位置
{cout<<a<<"-"<<b<<"="<<endl;}
else
{
if(rand()%3==0)
{cout<<"(-"<<a<<")-"<<b<<"="<<endl;}
if(rand()%3==1)
{cout<<a<<"-(-"<<b<<")="<<endl;}
else
{cout<<"(-"<<a<<")-(-"<<b<<")="<<endl;}
}
}
else if(y==2)
{ cout<<a<<"*"<<b<<"="<<endl;}
else
if(p==1)//判断是否要求有余数
{
if(a%b!=0)
{a=(a-a%b);}//确保没有余数
cout<<a<<"/"<<b<<"="<<endl;
}
else
{
if(b==1)
{b=b+1;}
if(a%b==0)
{a=(a+rand()%b);}//确保有余数
cout<<a<<"/"<<b<<"="<<endl;
}
}
void Arithmetic::show2(int y,int a,int b,int c,int d)
{
if(y==0)
{cout<<"("<<a<<"/"<<b<<")+("<<c<<"/"<<d<<")="<<endl;}
else if(y==1)
{ cout<<"("<<a<<"/"<<b<<")-("<<c<<"/"<<d<<")="<<endl;}
else if(y==2)
{ cout<<"("<<a<<"/"<<b<<")*("<<c<<"/"<<d<<")="<<endl; }
else
{cout<<"("<<a<<"/"<<b<<")/("<<c<<"/"<<d<<")="<<endl; }
}
void Arithmetic::Havespace(int F,int N)
{
for(int i=0;i<N;i++)
{
char s;
int n=rand()%4;
if(n==0)
{s=‘+‘;}
else if(n==1)
{s=‘-‘;}
else if(n==2)
{s=‘*‘;}
else
{s=‘/‘;}
cout<<"("<<rand()%F<<s<<rand()%F<<")"<<s<<"("<<rand()%F<<s<<rand()%F<<")="<<endl;
}
}
main.cpp
#include "Arithmetic.h"
void main()
{
P:
Arithmetic A;
int N;
cout<<"请输入要打印的题目数量:"<<endl;
cin>>N;
int n,m,f,o,p;
cout<<"请输入四则运算数的种类(1为整数,2为假分数,3有括号运算):"<<endl;
cin>>n;
cout<<"生成f值以内的运算题目(输入f值):"<<endl;
cin>>f;
if(n==1||n==2)
{
cout<<"有无乘除运算?(1没有,2有):"<<endl;
cin>>m;
cout<<"加减运算有无负数?(1没有,2有):"<<endl;
cin>>o;
cout<<"除法有无余数?(1没有,2有)"<<endl;
cin>>p;
}
switch(n)
{
case 1:
{A.Intnumber(N,m,f,o,p);break;}
case 2:
{A.Mixnumber(N,m,f,o);break;}
case 3:
{A.Havespace(f,N);}
}
goto P;
}
原文:http://www.cnblogs.com/liying123/p/5291644.html