程序1:自动生成小学四则运算题目
本程序改变自:http://blog.csdn.net/qq_27926481/article/details/50836061
语言:C++
编译环境:visual studio 2015
运行环境:Win10
做出修改的地方:在原码基础上增加了可自主选择生成题目的顺序
修改后的代码如下:
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<time.h>
using namespace std;
#define random() (rand()%100)
int main()
{
int i, j, m, a, b, c, d,count;
char k;
srand((int)time(NULL)); //设置时间种子 ,使得程序每次运行的结果都不同
cout << "请输入生成多少道题目:" ;
cin >> count;
for (i = 0;i<count;i++)
{
a = random();
b = random();
c = random();
d = random();
j = random();
m = random();
j = j % 4;
m = m % 2;
if (m == 0) //m=0时,生成整数四则运算
{
if (j == 0) k = '+';
else if (j == 1) k = '-';
else if (j == 2) k = '*';
else
{
for (;;)
{
if (b == 0) b = random();
else break;
}
k = '/';
}
cout << setw(3) << a << setw(2) << k << setw(3) << b << setw(3) << "= " << endl;
}
else //m=1时,生成真分数四则运算
{
for (;;) //位于分母的c不能为0
{
if (c <= a)
{
c = random();
}
else break;
}
for (;;) //位于分母的d不能为0
{
if (d <= b)
{
d = random();
}
else break;
}
if (j == 0) k = '+';
else if (j == 1) k = '-';
else if (j == 2) k = '*';
else //当b为0时,b/d为除数,所以b也不能为0
{
for (;;)
{
if (b == 0)
{
b = random();
}
else if (b <= d)
{
d = random();
}
else break;
}
k = '/';
}
cout << setw(3) << a << "/" << c << setw(2) << k << setw(3) << b << "/" << d << setw(3) << "= " << endl;
}
}
return 0;
}
本意是想增加支持多个运算符的功能,可是一改就出bug,没调正确。
GitHub上的源码:https://github.com/dangzhen/hello-world/blob/dangzhen-patch-1/main.cpp