在本次编写关于小学生4则运算项目时,我采用了之前学习的c++来编写的,在本次题目编写过程中由于我好久没用c++写过东西,所以在编写过程中好多关于细节东西已经忘记了,幸 好我有一个叫做百度的老师,在好多细节方面我在百度文库上查找了资料,例如在关于枚举类型enum运用上我好多知识参考了百度,在本次写项目过程中由于我 好多知识忘记,所以我在查找资料观看视频方面大约花了我50%的时间,在捋顺思路,确定函数方面大约算25%时间吧,编写代码大约花了10%的时 间,15%时间是用在调试代码了。
在本次我采用了将主要内容分函数,采用函数调用的方法来完成关于4则运算的,其中关于一些函数是参考网上大神的思路,自己编写完成。另外在
这我说一件另外尴尬事情,由于我英语不好,所以我好多单词是百度的,在这里让一些英语好的同学见笑了
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <conio.h>
using std::cin;
using std::cout;
using std::endl;
int const num = 300;
int const Maxnum = 100;
enum sym{Jia,Jian,Cheng,Chu};
char Fuhao(sym f)
{
switch (f)
{
case Jia:
return '+';
case Jian:
return '-';
case Cheng:
return 'x';
case Chu:
return '/';
}
return ' ';
}
int ferate(int a,int b,sym f);
bool Verify(int a,int b,sym f);
void getFormula(int* a,int*b,sym f);
int main()
{
int i;
int a[num];
int b[num];
int result[num];
sym fer[num];
int answerb = 0;
cout<<"正在生成,请稍等。"<<endl;
srand(time(NULL));
for(i=0;i<num;i++)
{
fer[i] = sym(rand()%4);
getFormula(&a[i],&b[i],fer[i]);
}
cout<<"生成计算式完成,开始答题!"<<endl;
for(i=0;i<num;i++)
{
cout<<"第 "<<i+1<<" 题: \n";
cout<<a[i]<<' '<<Fuhao(fer[i])<<' '<<b[i]<< " = ";
//获取用户答案
cin>>result[i];
if(result[i] == ferate(a[i],b[i],fer[i]))
{
cout<<"正确";
answerb++;
}
else
{
cout<<"小宝贝打错了,别灰心哈哈想想啊\n";
}
}
cout<<"共 "<<num<<" 题,答对 "<<answerb<<" 题 ";
//屏幕停止。
getch();
return 0;
}
int ferate(int a,int b,sym f)//运算
{
switch (f)
{
case Jia:
return a+b;
case Jian:
return a-b;
case Cheng:
return a *b;
case Chu:
return a/ b;
}
return 0;
}
bool Verify(int a,int b,sym f)//判断是否符合规范
{
switch (f)
{
case Jia:
{
return true;
}
case Jian://不能为负数
{
if(a < b)
return false;
return true;
}
case Cheng:
{
return true;
}
case Chu://分母不为0
{
if(b == 0)
return false;
if(double(a/b) != double(double(a)/double(b)))
return false;
return true;
}
}
return false;
}
void getFormula(int *a,int *b,sym f)
{
for(;;)
{
*a = 1 + rand() % Maxnum;
*b = 1 + rand() % Maxnum;
if(Verify(*a,*b,f) && ferate(*a,*b,f)<= 100 && ferate(*a,*b,f)>= 0)
break;
}
}