一、代码规范重写30道四则运算
/**
题目:30道2年纪四则运算题目
*/
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
//初始化
int a,b,c;
int num1,num2;
int num;
int chufa;
int k,jiange;
int zhengchu;
int j=0;
int chengfa;
int con;
//请输入数的范围,中间用空格隔开:
cout<<"请输入数的范围,中间用空格隔开:";
cin>>num1>>num2;
if(num1>num2)
{
cout<<"第一个数应该小于第二个数,请重输:";
cin>>num1>>num2;
}
//控制题目的个数,num
cout<<"其输入要出多少道题目:";
cin>>num;
if(num>(4*(num2-num1)^2))
{
cout<<"您输入的题目数量太多,会造成重复,请重新输入数目:";
cin>>num;
}
//打印格式
cout<<"一行几道题,行间距是几,中间空格:";
cin>>k>>jiange;
if(jiange>=5||jiange<0)
{
cout<<"您输入的间距太大或为负数,请重输:";
cin>>jiange;
}
//是否要除法
cout<<"是否要除法?是输入1,否输入0:";
cin>>chufa;
if(chufa==1)
{
cout<<"是否要有余数,是输入1,否输0:";
cin>>zhengchu;
}
//是否要乘法
cout<<"是否要乘法,是输入1,否输入0:";
cin>>chengfa;
cout<<endl;
for(int i=0;i<num;i++)
{
cout<<i+1<<"、";
a=(rand()%(num2-num1+1))+num1;
b=(rand()%(num2-num1+1))+num1;
if(chufa==1)
{
if(chengfa==1)
{
c=rand()%4;//0-3
}
else
{
c=rand()%3;//0-2
}
}
else
{
if(chengfa==1)
{
c=rand()%3+1;//1-3
}
else
{
c=rand()%2+1;//1-2
}
}
switch (c)
{
case 1:
cout<<a<<"+"<<b<<"="<<" ";
break;
case 2:
if(a>b)
{
cout<<a<<"-"<<b<<"="<<" ";
}
else
{
cout<<b<<"-"<<a<<"="<<" ";
}
break;
case 3:
cout<<a<<"*"<<b<<"="<<" ";
break;
case 0://除法
if(zhengchu==1)//有余数
{
if(b==0)
{
cout<<b<<"/"<<a<<"="<<" ";
}
else
{
cout<<a<<"/"<<b<<"="<<" ";
}
}
else
{
while((j<100000)&&(a!=0)&&(b!=0)&&(a%b==0))
{
cout<<a<<"/"<<b<<"="<<" ";
j++;
}
if((a!=0)&&(b!=0)&&(a%b==0))
{
cout<<a<<"/"<<b<<"="<<" ";
//i++;
}
else
{
int j=0;
while(j<100000)
{
a=(rand()%(num2-num1+1))+num1;
b=(rand()%(num2-num1+1))+num1;
if((a!=0)&&(b!=0)&&(a%b==0))
{
cout<<a<<"/"<<b<<"="<<" ";
//i++;
j=100000;
j++;
}
else
{
j++;
}
}
}
}
break;
}
if((i+1)%k==0)
{
cout<<endl;
for(int m=0;m<jiange;m++)
{
cout<<endl;
}
}
}
//继续吗?
cout<<"继续吗?是输入1,否输入0:"<<endl;
cin>>con;
if(con==1)
{
cout<<endl;
main();
}
else
{
return 0;
}
getch();
return 0;
}