四则运算2
/*四则运算生成 要求: 1、题目避免重复 2、可定制(数量/打印方式) 3、可以控制下列参数 是否有乘除法 是否有括号· 数值范围 加减有无负数 除法有无余数 设计思想:题目避免重复用数组,每次输出时与之前输出的比较是否相等。 输出数量由循环决定。 打印方式用if else 语句:可以直接输出,也可以输出到txt文件当中。 是否有乘除法,加减有无负数,除法有无余数都用if else语句。 正负数可以在每个数之前加一个运算符号:负号用“-”,正号用“ ”空格表示。 判断余数用z=x%y语句,判断z是否为0。 括号输出的考虑比较不周全,还不能达到随机随意添加括号。数字最多10个,符号最多9个, 创建符号的数组,如果符号数是奇数,则在没俩个数字之间添加括号;如果符号数是偶数,则第 一个数字不考虑,剩下的奇数个符号考虑与之前相同。 */ #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, pcc, pccs, numf, pjj, pys, scfs; //pcc判断乘除,pccs判断随机数,numf数值范围,pf判断有无负数,pys判断有无余数,scfs输出方式 char fh1, fh2; //fh符号正负 int pdkh,pdkhs; //pdkh判断是否有括号,pdkhs括号随机数 int szgs, fhgs; //szgs数字个数,fhgs符号个数 int szsz[20]; //szsz数字数组,fhsz符号数组 char fh[4] = { '+', '-', '*', '/' }, fhsz[10]; cout << "请输入要打印的数量:"; cin >> number; cout << "请输入打印方式: 1、直接输出 2、输入到txt文件 :"; cin >> scfs; cout << "请输入数值范围:0-"; cin >> numf; cout << "是否有括号: 1、有 2、无 :"; cin >> pdkh; cout << "加减有无负数: 1、有 2、无 :"; cin >> pjj; cout << "是否有乘除法: 1、有 2、无 :"; cin >> pcc; if (pcc == 1) { cout << "除法有无余数: 1、有 2、无 :"; cin >> pys; } srand((unsigned)time(NULL));//时间种子,每次出现随机数不同 if (pdkh == 1) //判断是否有括号 { pdkhs = 3; } if (pdkh == 2) { pdkhs = 2; } if (pcc == 1) //判断是否有乘除 { pccs = 4; } if (pcc == 2) { pccs = 2; } for (i = 0; i < number; i++) { a = rand() % numf; b = rand() % numf; e = rand() % numf; f = rand() % numf; if (pjj == 1) { l = rand() % 2; m = rand() % 2; if (l == 0) //加减有负数时,随机生成正负号 { fh1 = '-'; } if (l == 1) { fh1 = ' '; } if (m == 0) { fh2 = '-'; } if (m == 1) { fh2 = ' '; } } else { fh1 = fh2 = ' '; } if (scfs == 1) //判断输出方式为直接输出 { switch (d = rand() % pdkhs) { case 0: //整数 { switch (c = rand() % pccs) { case 0:cout << fh1 << a << "+" << fh2 << b << "=" << endl; break; case 1:cout << fh1 << a << "-" << fh2 << b << "=" << endl; break; case 2:cout << a << "×" << b << "=" << endl; break; case 3: { if (pys == 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() % pccs) { case 0: cout << fh1 << a << "/" << b << "+" << fh2 << e << "/" << f << "=" << endl; break; case 1: cout << fh1 << a << "/" << b << "-" << fh2 << 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: { szgs = 3 + rand() % 8; //有括号时,数字和符号数组 fhgs = szgs - 1; for (y = 0; y < szgs; y++) { szsz[y] = rand() % numf; } for (y = 0; y < fhgs; y++) { x = rand() % 4; fhsz[y] = fh[x]; } fhsz[fhgs] = '='; switch (z=fhgs % 2) { case 0: { cout << szsz[0]; cout << fhsz[0]; for (y = 1; y < fhgs; y++) { cout << "("; cout << szsz[y]; cout << fhsz[y]; cout << szsz[y + 1]; cout << ")"; cout << fhsz[y + 1]; y = y + 1; }cout<<endl; }break; case 1: { for (y = 0; y < fhgs; y++) { cout << "("; cout << szsz[y]; cout << fhsz[y]; cout << szsz[y + 1]; cout << ")"; cout << fhsz[y + 1]; y = y + 1; } cout<<endl; }break; } }break; } } else //选择输出方式为 输出到文件 { outfile.open("myfile.txt", ios_base::out | ios_base::app); switch (d = rand() % pdkhs) { case 0: //整数 { switch (c = rand() % pccs) { case 0:outfile << fh1 << a << "+" << fh2 << b << "=" << endl; break; case 1:outfile << fh1 << a << "-" << fh2 << b << "=" << endl; break; case 2:outfile << a << "×" << b << "=" << endl; break; case 3: { if (pys == 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; } } outfile << 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() % pccs) { case 0: outfile << fh1 << a << "/" << b << "+" << fh2 << e << "/" << f << "=" << endl; break; case 1: outfile << fh1 << a << "/" << b << "-" << fh2 << e << "/" << f << "=" << endl; break; case 2: outfile << a << "/" << b << "×" << e << "/" << f << "=" << endl; break; case 3: outfile << a << "/" << b << "÷" << e << "/" << f << "=" << endl; break; } }break; case 2: { szgs = 3 + rand() % 8; //有括号时,数字和符号数组 fhgs = szgs - 1; for (y = 0; y < szgs; y++) { szsz[y] = rand() % numf; } for (y = 0; y < fhgs; y++) { x = rand() % 4; fhsz[y] = fh[x]; } fhsz[fhgs] = '='; switch (z = fhgs % 2) { case 0: { outfile << szsz[0]; outfile << fhsz[0]; for (y = 1; y < fhgs; y++) { outfile << "("; outfile << szsz[y]; outfile << fhsz[y]; outfile << szsz[y + 1]; outfile << ")"; outfile << fhsz[y + 1]; y = y + 1; }outfile << endl; }break; case 1: { for (y = 0; y < fhgs; y++) { outfile << "("; outfile << szsz[y]; outfile << fhsz[y]; outfile << szsz[y + 1]; outfile << ")"; outfile << fhsz[y + 1]; y = y + 1; } outfile << endl; }break; } }break; } outfile.close(); } } }
周活动总结表
姓名:王春帆 日期:2016/03/12
|
听课 |
编写程序 |
阅读课本 |
|
|
|
日总结 |
周日3.6 |
|
|
|
|
|
|
|
周一 |
100 |
|
30 |
|
|
|
130 |
周二 |
|
|
|
|
|
|
|
周三 |
|
|
|
|
|
|
|
周四 |
100 |
60 |
|
|
|
|
160 |
周五 |
|
|
30 |
|
|
|
30 |
周六 |
|
450 |
|
|
|
|
450 |
周总结 |
200 |
510 |
60 |
|
|
|
770 |
时间记录日志
学生:王春帆 日期:3/12/2016
教师:王建民 课程:软件工程
日期 |
开始 时间 |
结束时间 |
中断时间 |
净时间 |
活动 |
备注 |
C |
U |
3/7 |
14:00 |
15:50 |
10 |
100 |
上课 |
理论学时 |
|
|
|
19:00 |
19:30 |
|
30 |
看书 |
《构建之法》 |
|
|
3/10 |
14:00 |
15:50 |
10 |
100 |
上课 |
理论学时 |
|
|
|
20:00 |
21:00 |
|
60 |
编写程序 |
四则运算2 |
|
|
3/11 |
19:00 |
19:30 |
|
30 |
看课件 |
上课所讲 |
|
|
3/12 |
9:00 |
12:00 |
|
180 |
编写程序 |
四则运算2 |
|
|
|
13:00 |
15:00 |
|
120 |
编写程序 |
四则运算2 |
|
|
|
15:30 |
18:00 |
|
150 |
编写程序 |
四则运算2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
缺陷记录日志
学生:王春帆
日期:3/12/2016
教员:先生王
程序号:四则运算2
日期 |
编号 |
类型 |
引入阶段 |
排除阶段 |
修复时间 |
3/12 |
1 |
|
生成 |
错误列表 |
5min |
|
描述 |
Char类型的赋值方式错误,应该用char a=’-’ 小括号 |
|||
|
2 |
|
生成 |
调试 |
10min |
|
描述 |
a/b 中b可能为0 |
|||
|
3 |
|
生成 |
错误列表 |
1min |
|
描述 |
If else 语句的括号错误 |
|||
|
4 |
|
执行 |
修改代码 |
10min |
|
描述 |
输出到文件中只有一道运算题,输出方式需要修改 |
|||
|
|
|
|
|
|