#include<iostream> #include<stdio.h> #include<time.h> #include<fstream> using namespace std; int main() { ofstream outfile; int i, a, b, c, d, e, f, g, l, m, o, x, y, z; int number, muldiv, rnum, numf, neg, rem; //muldiv判断乘除,rnum判断随机数,numf数值范围,neg判断有无负数,rem判断有无余数 char psign, msign; //符号正负 int bra, braran; //bra判断是否有括号 int num, signnum; //num数字个数,signnum符号个数 int numarray[20]; //szsz数字数组 char fh[4] = { '+', '-', '*', '/' }, signarray[10];//fhsz符号数组 cout << "请输入要打印的数量:"; cin >> number; cout << "请输入数值范围:0-"; cin >> numf; cout << "是否有括号: 1、有 2、无 :"; cin >> bra; cout << "加减有无负数: 1、有 2、无 :"; cin >> neg; cout << "是否有乘除法: 1、有 2、无 :"; cin >> muldiv; if (muldiv == 1) { cout << "除法有无余数: 1、有 2、无 :"; cin >> rem; } srand((unsigned)time(NULL));//时间种子,每次出现随机数不同 if (bra == 1) //判断是否有括号 { braran = 3; } if (bra == 2) { braran = 2; } if (muldiv == 1) //判断是否有乘除 { rnum = 4; } if (muldiv == 2) { rnum = 2; } for (i = 0; i < number; i++) { a = rand() % numf; b = rand() % numf; e = rand() % numf; f = rand() % numf; if (neg == 1) { l = rand() % 2; m = rand() % 2; if (l == 0) //加减有负数时,随机生成正负号 { psign = '-'; } if (l == 1) { psign = ' '; } if (m == 0) { msign = '-'; } if (m == 1) { msign = ' '; } } else { psign = msign = ' '; } switch (d = rand() % braran) { case 0: //整数 { switch (c = rand() % rnum) { case 0:cout << psign << a << "+" << msign << b << "=" << endl; break; case 1:cout << psign << a << "-" << msign << b << "=" << endl; break; case 2:cout << a << "×" << b << "=" << endl; break; case 3: { if (rem == 2) { while (b == 0) { b = rand() % numf; } o = a%b; while (o != 0) { b = rand() % numf; while (b == 0) { a = rand() % numf; b = rand() % numf; } o = a%b; } } else { while (b == 0) { b = rand() % numf; } } cout << a << "÷" << b << "=" << endl; }break; } }break; case 1://真分数 { while (a == 0 || b == 0 || a >= b) { a = rand() % numf; b = rand() % numf; } while (e == 0 || f == 0 || e >= f) { e = rand() % numf; f = rand() % numf; } switch (g = rand() % rnum) { case 0: cout << psign << a << "/" << b << "+" << msign << e << "/" << f << "=" << endl; break; case 1: cout << psign << a << "/" << b << "-" << msign << e << "/" << f << "=" << endl; break; case 2: cout << a << "/" << b << "×" << e << "/" << f << "=" << endl; break; case 3: cout << a << "/" << b << "÷" << e << "/" << f << "=" << endl; }break; }break; case 2: { num = 3 + rand() % 8; //有括号时,数字和符号数组 signnum = num - 1; for (y = 0; y < num; y++) { numarray[y] = rand() % numf; } for (y = 0; y < signnum; y++) { x = rand() % 4; signarray[y] = fh[x]; } signarray[signnum] = '='; switch (z = signnum % 2) { case 0: { cout << numarray[0]; cout << signarray[0]; for (y = 1; y < signnum; y++) { cout << "("; cout << numarray[y]; cout << signarray[y]; cout << numarray[y + 1]; cout << ")"; cout << signarray[y + 1]; y = y + 1; }cout << endl; }break; case 1: { for (y = 0; y < signnum; y++) { cout << "("; cout << numarray[y]; cout << signarray[y]; cout << numarray[y + 1]; cout << ")"; cout << signarray[y + 1]; y = y + 1; } cout << endl; }break; } }break; } } }
本次作业的题目:
老师又给二柱子增加了几个条件,对程序再做进一步的改进,要求满足如下条件:
1.题目避免重复。
2.可制定。(数量/打印方式)
3.可以控制下列参数:
· 是否有乘除法
· 是否有括号(最多可支持10个数参与计算)
· 数值范围
· 加减有无负数
· 乘除有无余数
设计思想:
这次的内容有点多,而且个别功能有点难度,我没有把握全部实现,只能从最容易完成的功能入手,这次程序在第一次的基础上完成的,主要构建的功能如下
1.利用循环可指定输出的题目数量
2.分类乘除法与加减法
3.在乘除法中,判断条件,余数为0,得到整除的除法式子
4.在加减法中设置条件,使减数不小于被减数,得到的结果不为负数
5.利用循环嵌套结构