标签:分析 time 保存 使用 include 小朋友 交互 输入 编写
针对本次题目的要求,我和我的结对小伙伴郭金鑫商量之后,在此次作业中我选择了驾驶员这一角色,当然了,不是开车的老司机那种,而是开飞机的呢。
拿到题目之后,我第一感觉是这次的作业已经开始真正的“软件工程”了。本次题目对我来说并不陌生,相信对很多人来说也都并不陌生。在上小学的时候,我的数学老师就要求我们买四则运算册子,每一篇都是同样的一种运算题型,针对性训练,一篇的题目量大约在50道左右,天天做,对于提升小学的运算水平有很大的提升。
回到题目,这次作业需要的就是一个小程序可以基本满足于小学数学四则运算的题目生成,若使用的人群是小学数学老师,还应该满足自动生成题目之后可以选择打印出来,方便学生们进行演算。
针对用户可以选择运算符这一需求,我想起了我当初做运算题册的时候,是针对性训练,即这一篇是加法,下一篇就是减法,这一篇全是乘法,下一篇就全是除法,这给了我启发,针对这一要求,我可以使用switch语句,每一个case就可以对应每一个运算,这样需求就实现了。功能代码如下:
switch(inmain)
{
case 1: //加法
for(int i=0;i<num;++i)
{
A[i] = rand()%10;
B[i] = rand()%10;
fout<<A[i]<<"+"<<B[i]<<"="<<endl;
cout<<A[i]<<"+"<<B[i]<<"="<<endl;
}break;
case 2: //减法
for(int i=0;i<num;++i)
{
A[i] = rand()%10;
B[i] = rand()%10;
cout<<A[i]<<"-"<<B[i]<<"="<<endl;
fout<<A[i]<<"-"<<B[i]<<"="<<endl;
}break;
case 3: //乘法
for(int i=0;i<num;++i)
{
A[i] = rand()%10;
B[i] = rand()%10;
cout<<A[i]<<"*"<<B[i]<<"="<<endl;
fout<<A[i]<<"*"<<B[i]<<"="<<endl;
}break;
case 4: //除法
for(int i=0;i<num;++i)
{
A[i] = (rand()%10)+1;
B[i] = (rand()%10)+1;
cout<<A[i]<<"/"<<B[i]<<"="<<endl;
fout<<A[i]<<"/"<<B[i]<<"="<<endl;
}break;
接下来就是解决混合型运算也就是带括号的运算,但凡一个运算式带了括号,一定是三元即三元以上,先来考虑没有括号的一个式子:a+b* c= 以及这个式子加了括号:(a+b) c= 、a+(bc)=;可以看到第二个式子所加的括号完全是没有必要的,也就是说在加括号时还必须考虑到所加括号是否有实际意义,也就是得把运算符的优先级考虑进来。之前说的其实是建立在某个基础之上的,这个基础就是首先得把整个运算式转换成字符串,之后使用针对字符串的遍历才能实现括号在正确的位置正确加入,而将数字转换为字符串的一个函数是itoa函数,
悲剧的是linux下此函数用不了,我又看到了sprintf函数,想到这儿,我跪了,太费事了...于是我转换了思路,一个简单的不能再简单的思路...先贴代码:
case 9:
for(int i=0;i<num;++i)
{
A[i] = rand()%100;
B[i] = rand()%100;
C[i] = rand()%100;
D[i] = rand()%100;
if(i%2==0)
{
cout<<A[i]<<"*"<<"("<<B[i]<<"-"<<C[i]<<")"<<"+"<<D[i]<<endl;
fout<<A[i]<<"*"<<"("<<B[i]<<"-"<<C[i]<<")"<<"+"<<D[i]<<endl;
}
else if(i%3==0)
{
cout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%5==0)
{
cout<<"("<<A[i]<<"+"<<B[i]<<")"<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<"("<<A[i]<<"+"<<B[i]<<")"<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%7==0)
{
cout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
fout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
}
else
{
cout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
fout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
}
}break;
可以看到这个样子就无比的简单,因为是小学运算,所以我默认混合运算设置为四元运算,同时我的括号是直接输出的,这样就解决了我的括号无论何时都是有意义的,潜在的问题就是在这个case下,我如果不对题目进行调整,一个循环出来后所有的题目假设30道,这30道题目所有的括号位置,包括题目格式都是一样的...想到这儿,我必须对题目做一个分类,其实也很简单,就是对用户输入进来的题目总数,假设还是30道,将这个按照能分别被2、3、5、7整除进行分类,调整了题目格式和括号所在的位置,这样,小朋友们做卷子时就不会有“哇,怎么题目都是千篇一律的!”的感受了...这么整,我觉得既简单的同时又很好的满足了需要。
A[i] = (rand()%100)/10.0+1;
B[i] = (rand()%100)/10.0+1;
C[i] = (rand()%100)/10.0+1;
D[i] = (rand()%100)/10.0+1;
A[i] = (rand()%1000)/10.0+1;
B[i] = (rand()%1000)/10.0+1;
C[i] = (rand()%1000)/10.0+1;
D[i] = (rand()%1000)/10.0+1;
/*************************************************************************
> File Name: 四则运算生成.cpp
> Author: liililiu
> Created Time: 2018年04月12日 星期四 10时52分09秒
************************************************************************/
#include<iostream>
#include<ctime>
#include<stdlib.h>
#include<fstream>
using namespace std;
int srand(int);
int main()
{
srand((unsigned int)time(0));
int inmain;
float A[1000];
float B[1000];
float C[1000];
float D[1000];
int num;
char a;
int file;
cout<<"请选择你想要哪种运算?"<<endl
<<"1 为十以内加法运算;"<<endl
<<"2 为十以内减法运算;"<<endl
<<"3 为十以内乘法运算;"<<endl
<<"4 为十以内除法运算;"<<endl
<<"5 为百以内加法运算;"<<endl
<<"6 为百以内减法运算;"<<endl
<<"7 为百以内乘法运算;"<<endl
<<"8 为百以内除法运算;"<<endl
<<"9 为多元混合型运算;"<<endl
<<"10 为十以内小数运算;"<<endl
<<"11 为百以内小数运算;"<<endl
<<"0 为退出程序!"<<endl;
cin>>inmain;
if(inmain==0)
{
cout<<"谢谢使用"<<endl;
exit(0);
}
cout<<"请输入出题数量:"<<endl;
cin>>num;
cout<<"题目是否需要打印? 1是,0否!"<<endl;
cin>>file;
ofstream fout;
if(file==1)
{
fout.open("四则运算.txt");
fout<<num<<"道四则运算题如下:"<<endl;
}
else
{
cout<<num<<"道四则运算题如下:"<<endl;
}
switch(inmain)
{
case 1:
for(int i=0;i<num;++i)
{
A[i] = rand()%10;
B[i] = rand()%10;
fout<<A[i]<<"+"<<B[i]<<"="<<endl;
cout<<A[i]<<"+"<<B[i]<<"="<<endl;
}break;
case 2:
for(int i=0;i<num;++i)
{
A[i] = rand()%10;
B[i] = rand()%10;
cout<<A[i]<<"-"<<B[i]<<"="<<endl;
fout<<A[i]<<"-"<<B[i]<<"="<<endl;
}break;
case 3:
for(int i=0;i<num;++i)
{
A[i] = rand()%10;
B[i] = rand()%10;
cout<<A[i]<<"*"<<B[i]<<"="<<endl;
fout<<A[i]<<"*"<<B[i]<<"="<<endl;
}break;
case 4:
for(int i=0;i<num;++i)
{
A[i] = (rand()%10)+1;
B[i] = (rand()%10)+1;
cout<<A[i]<<"/"<<B[i]<<"="<<endl;
fout<<A[i]<<"/"<<B[i]<<"="<<endl;
}break;
case 5:
for(int i=0;i<num;++i)
{
A[i] = rand()%100;
B[i] = rand()%100;
cout<<A[i]<<"+"<<B[i]<<"="<<endl;
fout<<A[i]<<"+"<<B[i]<<"="<<endl;
}break;
case 6:
for(int i=0;i<num;++i)
{
A[i] = rand()%100;
B[i] = rand()%100;
cout<<A[i]<<"-"<<B[i]<<"="<<endl;
fout<<A[i]<<"-"<<B[i]<<"="<<endl;
}break;
case 7:
for(int i=0;i<num;++i)
{
A[i] = rand()%100;
B[i] = rand()%100;
cout<<A[i]<<"*"<<B[i]<<"="<<endl;
fout<<A[i]<<"*"<<B[i]<<"="<<endl;
}break;
case 8:
for(int i=0;i<num;++i)
{
A[i] = (rand()%100)+1;
B[i] = (rand()%100)+1;
cout<<A[i]<<"/"<<B[i]<<"="<<endl;
fout<<A[i]<<"/"<<B[i]<<"="<<endl;
}break;
case 9:
for(int i=0;i<num;++i)
{
A[i] = rand()%100;
B[i] = rand()%100;
C[i] = rand()%100;
D[i] = rand()%100;
if(i%2==0)
{
cout<<A[i]<<"*"<<"("<<B[i]<<"-"<<C[i]<<")"<<"+"<<D[i]<<endl;
fout<<A[i]<<"*"<<"("<<B[i]<<"-"<<C[i]<<")"<<"+"<<D[i]<<endl;
}
else if(i%3==0)
{
cout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%5==0)
{
cout<<"("<<A[i]<<"+"<<B[i]<<")"<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<"("<<A[i]<<"+"<<B[i]<<")"<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%7==0)
{
cout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
fout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
}
else
{
cout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
fout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
}
}break;
case 10:
for(int i=0;i<num;++i)
{
A[i] = (rand()%100)/10.0+1;
B[i] = (rand()%100)/10.0+1;
C[i] = (rand()%100)/10.0+1;
D[i] = (rand()%100)/10.0+1;
if(i%2==0)
{
cout<<A[i]<<"*"<<B[i]<<"-"<<C[i]<<"+"<<D[i]<<endl;
fout<<A[i]<<"*"<<B[i]<<"-"<<C[i]<<"+"<<D[i]<<endl;
}
else if(i%3==0)
{
cout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%5==0)
{
cout<<A[i]<<"+"<<B[i]<<"*"<<C[i]<<"-"<<D[i]<<endl;
fout<<A[i]<<"+"<<B[i]<<"*"<<C[i]<<"-"<<D[i]<<endl;
}
else if(i%7==0)
{
cout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
fout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
}
else
{
cout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
fout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
}
}break;
case 11:
for(int i=0;i<num;++i)
{
A[i] = (rand()%1000)/10.0+1;
B[i] = (rand()%1000)/10.0+1;
C[i] = (rand()%1000)/10.0+1;
D[i] = (rand()%1000)/10.0+1;
if(i%2==0)
{
cout<<A[i]<<"*"<<"("<<B[i]<<"-"<<C[i]<<")"<<"+"<<D[i]<<endl;
fout<<A[i]<<"*"<<"("<<B[i]<<"-"<<C[i]<<")"<<"+"<<D[i]<<endl;
}
else if(i%3==0)
{
cout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<A[i]<<"+"<<B[i]<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%5==0)
{
cout<<"("<<A[i]<<"+"<<B[i]<<")"<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
fout<<"("<<A[i]<<"+"<<B[i]<<")"<<"*"<<"("<<C[i]<<"-"<<D[i]<<")"<<endl;
}
else if(i%7==0)
{
cout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
fout<<A[i]<<"+"<<"("<<B[i]<<"-"<<C[i]<<")"<<"/"<<D[i]<<endl;
}
else
{
cout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
fout<<A[i]<<"/"<<"("<<B[i]<<"-"<<C[i]<<")"<<"*"<<D[i]<<endl;
}
}break;
}
if(file==1)
{
fout.close();
}
return 0;
}
liililiu@liililiu-PC:~/Desktop$
首先我们来看下编译运行后的样子:
拿几个例子试验一下:
我们再来看下是否能保存到文件以备需求我的数学老师为可爱的小朋友们打印:
最后看一下是否友好交互:
到此,大吉大利!此次作业完成。
标签:分析 time 保存 使用 include 小朋友 交互 输入 编写
原文地址:https://www.cnblogs.com/liililiu/p/8822665.html