十以内的分数加减法(C++)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//获得分数
void PrintFenShu(int& a,int& b) {
//随机分母
int fenmu = rand()%7 + 3;
//根据分母随机分子
int fenzi =fenmu>5? rand()%(fenmu-3) + 1:rand()%fenmu + 1;
a = fenzi;
b = fenmu;
}
//打印算式
void PrintExpression() {
int a, b, c, d;
PrintFenShu(a,b);
PrintFenShu(c,d);
cout << a << "/" << b;
//随机加减
if ((float)a / b > (float)c / d)
if(rand()%2 == 1)
cout << " + ";
else
cout << " - ";
else
cout << " + ";
cout << c << "/" << d;
cout << " = \t\t";
}
int main() {
srand(time(NULL));
for (int i = 0; i < 500; i++) {
//5个算式一行
if (i % 5 == 0)
cout << "\n";
PrintExpression();
}
int a;
cin>>a;
return 0;
}