第一次作业

四则运算题目生成器
本文介绍了一个简单的C++程序,用于生成包含加减乘除的四则运算题目。该程序能够生成整数或真分数运算题,并可根据需求输出答案。
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

void DealFenshu(int m, int a[][2])
{
for(int p=0;p<m;p++)
{
int i=(int)rand()%10;
int j=(int)rand()%10;
while(j==0||i>=j)
{
 i=(int)rand()%10;
 j=(int)rand()%10;
}
int x=(int)rand()%10;
int y=(int)rand()%10;
while(y==0||x>=y)
{
 x=(int)rand()%10;
 y=(int)rand()%10;
}
int k=(int)rand()%100/25;
switch(k)
{
 case 0:
 cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";
 a[p][0]=i*y+x*j;
 a[p][1]=j*y;
 break;
 case 1:
 int m=0,n=0;
 cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";
 a[p][0]=i*y-x*j;
 a[p][1]=j*y;
 m=i*y;
 n=x*j;
 m>>n;
 break;
case 2:
cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";
a[p][0]=i*x;
a[p][1]=j*y;
break;
case 3:
a[p][0]=i*y;
a[p][1]=j*x;
cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";
}
            
if(p%5==4)
{
 cout<<endl;
}
 else
{
 cout<<‘\t‘;
}
}
}
void DisplayFenshu(int a[][2],int w,int m)
{
 if(w==1)
{
 for(int q=0;q<m;q++)
 {
  if(a[q][0]==0)
cout<<"0"<<‘\t‘;
else
cout<<a[q][0]<<"/"<<a[q][1]<<‘\t‘;
if(q%5==4)
{
cout<<endl;
}
}
}
else
{};
}
void DealInt(int m,int a[])
{
for(int p=0;p<m;p++)
{
int i=(int)rand()%10;
int j=(int)rand()%10;
int k=(int)rand()%100/25;
switch(k)
{
case 0:
cout<<i<<"+"<<j<<"=";
a[p]=i+j;
break;
case 1:
cout<<i<<"-"<<j<<"=";
i>>j;
a[p]=i-j;
break;
case 2:
cout<<i<<"*"<<j<<"=";
a[p]=i*j;
break;
case 3:
try
{
a[p]=i/j;
cout<<i<<"/"<<j<<"=";
}
catch(...)
{
p--;
}
        

}
        
if(p%5==4)
{
cout<<endl;
}
else
{
cout<<‘\t‘;
}
}
}
void DisplayInt(int a[],int w,int m)
{
if(w==1)
{
for(int q=0;q<m;q++)
{
cout<<a[q]<<‘\t‘;
if(q%5==4)
{
cout<<endl;
}
}
}
else
{};
}
void main()
{
int p;
do
{
system("cls");
int a[1000],b[1000][2];
int m,n,w;
cout<<"请输入生成的四则运算题个数:";
cin>>m;
cout<<endl;
cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):";
cin>>n;
cout<<endl;
if(n==1)
{
DealInt(m,a);
cout<<endl;
}
else
{
DealFenshu(m,b);
cout<<endl;
}
cout<<"是否输出答案(输入1则输出答案否则不输出答案)"<<endl;
cin>>w;
if(n==1)
{
DisplayInt(a,w,m);        
}
else
{
DisplayFenshu(b,w,m);
}
cout<<endl;
cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl;
cin>>p;
cout<<endl;
}while(1==p);
}

 



说实话,基础差,这个如啊年弄了很久,最后还是在各种同学高人的帮助下慢慢完成的,累计用时超过一天。

 

转载于:https://www.cnblogs.com/linweixiya/p/5269637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值