1 Star 0 Fork 0

闲来垂钓碧溪上/learngit

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
operations 7.12 KB
Copy Edit Raw Blame History
闲来垂钓碧溪上 authored 5 years ago . add operations.
//根据相关选择自动生成100道四则运算
#include <iostream>
#include<cstdlib>
#include<time.h>
using namespace std;
void main()
{
int a0[1000],b0[1000],c0[1000],a1[1000],b1[1000],c1[1000],a2[1000],b2[1000],c2[1000],a3[1000],b3[1000],c3[1000],C[1000],a,b,c;
int i,j,A0,A1,A2,A3,B0,B1,B2,B3;
int num,line,k;
int min,max,m;
int CC,FS,YS;
k=0;
A0=0;A1=0;A2=0;A3=0;
B0=0;B1=0;B2=0;B3=0;
cout<<"*************四则运算*************"<<endl;
int count=0 ;
srand(time(NULL));//用系统当前时间设置rand()随机序列种子,保证运行随机序列不一样
char ch;
while(count<30)
{
cout<<"选择年级(1~5年级)"<<endl;
cin>>ch;
if(ch=='1')
{cout<<"一年级四则运算:"<<endl;
break;
}
if(ch=='2')
{
cout<<"二年级四则运算:"<<endl;
break;
}
if(ch=='3')
{
cout<<"三年级四则运算:"<<endl;
break;
}
if(ch=='4')
{
cout<<"四年级四则运算:"<<endl;
break;
}
if(ch=='5')
{cout<<"五年级四则运算:"<<endl;
break;
}
}
cout<<"请依次输入算式数量和每行显示数量(用空格隔开):"<<endl;
cin>>num>>line;
cout<<"请问是否要有乘除法?有请输入\"1\",无请输入\"0\""<<endl;
cin>>CC;
while(1)
{
if(CC != 1&&CC != 0)
{
cout<<"输入不合法,请重新输入\"1\"或\"0\":";
cin>>CC;
}
else
break;
}
cout<<"请问是否要加减结果有负数?有请输入\"1\",无请输入\"0\""<<endl;
cin>>FS;
while(1)
{
if(FS != 1&&FS != 0)
{
cout<<"输入不合法,请重新输入\"1\"或\"0\":";
cin>>FS;
}
else
break;
}
cout<<"请问是否要除法结果有余数?有请输入\"1\",无请输入\"0\""<<endl;
cin>>YS;
while(1)
{
if(YS != 1&&YS != 0)
{
cout<<"输入不合法,请重新输入\"1\"或\"0\":";
cin>>YS;
}
else
break;
}
cout<<"请输入两个值确定算式中a,b的取值(a<b)(及算数的取值范围):"<<endl;
cin>>min>>max;
while(1)
{
if(min>=max)
{
cout<<"输入不合法,请重新输入:";
cin>>min>>max;
}
else
break;
}
m=max-min+1;
for(i=0;i<1000;i++)
{
a0[i]=0;b0[i]=0;
a1[i]=0;b1[i]=0;
a2[i]=0;b2[i]=0;
a3[i]=0;b3[i]=0;
}
for(i=0;;i++)
{
a=min+rand()%m;
b=min+rand()%m;
c=rand()%4;
if(CC==0) //若CC=0,去除c=2,3的情况
{
if(c==2||c==3)
continue;
}
if(FS==0) //若FS=0,去除c=0,1下结果小于0的情况
{
if(c==0)
{
if(a+b<0)
continue;
}
else if(c==1)
{
if(a-b<0)
continue;
}
}
if(YS==0) //若YS=0,且是除法,去除a,b相除有余数的情况
{
if(c==3)
if(0 != a%b)
continue;
}
//将算式分为加、减、乘、除四组
if(c==0) //加
{
for(j=0;j<=A0;j++)
{
if(a==a0[j]&&b==b0[j])
{
B0++;break;
}
}
if(B0>0)
{
B0=0;continue;
}
else
{
a0[A0]=a;
b0[A0]=b;
c0[A0]=a+b;
A0++;
}
}
else if(c==1) //减
{
for(j=0;j<=A1;j++)
{
if(a==a1[j]&&b==b1[j])
{
B1++;break;
}
}
if(B1>0)
{
B1=0;continue;
}
else
{
a1[A1]=a;
b1[A1]=b;
c1[A1]=a-b;
A1++;
}
}
else if(c==2) //乘
{
for(j=0;j<=A2;j++)
{
if(a==a2[j]&&b==b2[j])
{
B2++;break;
}
}
if(B2>0)
{
B2=0;continue;
}
else
{
a2[A2]=a;
b2[A2]=b;
c2[A2]=a*b;
A2++;
}
}
else //除
{
if(b==0)
continue;
for(j=0;j<=A3;j++)
{
if(a==a3[j]&&b==b3[j])
{
B3++;break;
}
}
if(B3>0)
{
B3=0;continue;
}
else
{
a3[A3]=a;
b3[A3]=b;
c3[A3]=a/b;
A3++;
}
}
if(num==A0+A1+A2+A3) //限制算式数量
break;
}
//输出
for(i=0;i<A0;i++) // 加
{
k++;
if(a0[i]<0)
cout<<"("<<a0[i]<<")";
else
cout<<a0[i]<<" ";
cout<<"+";
if(b0[i]<0)
cout<<"("<<b0[i]<<")";
else
cout<<" "<<b0[i]<<" ";
cout<<"=";
if(k%line==0)
cout<<endl;
else
cout<<"\t";
}
for(i=0;i<A1;i++) // 减
{
k++;
if(a1[i]<0)
cout<<"("<<a1[i]<<")";
else
cout<<a1[i]<<" ";
cout<<"-";
if(b1[i]<0)
cout<<"("<<b1[i]<<")";
else
cout<<" "<<b1[i]<<" ";
cout<<"=";
if(k%line==0)
cout<<endl;
else
cout<<"\t";
}
for(i=0;i<A2;i++) // 乘
{
k++;
if(a2[i]<0)
cout<<"("<<a2[i]<<")";
else
cout<<a2[i]<<" ";
cout<<"*";
if(b2[i]<0)
cout<<"("<<b2[i]<<")";
else
cout<<" "<<b2[i]<<" ";
cout<<"=";
if(k%line==0)
cout<<endl;
else
cout<<"\t";
}
for(i=0;i<A3;i++) //除
{
k++;
if(a3[i]<0)
cout<<"("<<a3[i]<<")";
else
cout<<a3[i]<<" ";
cout<<"÷";
if(b3[i]<0)
cout<<"("<<b3[i]<<")";
else
cout<<" "<<b3[i]<<" ";
cout<<"=";
if(k%line==0)
cout<<endl;
else
cout<<"\t";
}
cout<<"请依次输入结果:"<<endl;
for(i=0;i<num;i++)
cin>>C[i];
int t=0,corr=0;
//判断输入结果的对错
for(i=0;i<A0;i++)
{
if(c0[i]==C[t])
{t++;corr++;}
else
{
t++;
cout<<"第"<<t<<"题答错!"<<endl;
}
}
for(i=0;i<A1;i++)
{
if(c1[i]==C[t])
{t++;corr++;}
else
{
t++;
cout<<"第"<<t<<"题答错!"<<endl;
}
}
for(i=0;i<A2;i++)
{
if(c2[i]==C[t])
{t++;corr++;}
else
{
t++;
cout<<"第"<<t<<"题答错!"<<endl;
}
}
for(i=0;i<A3;i++)
{
if(c3[i]==C[t])
{t++;corr++;}
else
{
t++;
cout<<"第"<<t<<"题答错!"<<endl;
}
}
if(num==corr)
cout<<"恭喜你,全部正确"<<endl;
system("pause");
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cd_in_zz/learngit.git
git@gitee.com:cd_in_zz/learngit.git
cd_in_zz
learngit
learngit
master

Search