Pair programming

Posted by hebicheng on May 3, 2018

A software class work.

实验目的及要求

生成小学算术题

满足如下条件

  • 题目避免重复
  • 可定制(数量、打印方式)
  • 可控制下面参数
    • 是否有乘除法
    • 整数范围
    • 除法有无余数
    • 是否支持小数
    • 有无括号
    • 加法有无负数
    • 是否支持分数
    • 打印间隔是否可调整

编程伙伴

  • 任力 2015110435

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <bits/stdc++.h>
#include<windows.h>
using namespace std;

string EXP[1000];
int expNumber = 0;
int numberRange = 0;
int numberType = 0;
int oprType = 0;
int colNumber = 0;
int rowHeight = 0;

//生成一定范围内的整数
int digitRand(int n){
    return rand()%n;
}

//生成一定范围内的小数
double doubleRand(int n){
    int i = digitRand(n);
    int d = digitRand(100);
    return i*1.0 + d/100.0;
}

//生成加减法
string oprLow(){
    string opr[] = {"+", "-"};
    return opr[digitRand(2)];

}

//生成加减乘除法
string oprAll(){
    string opr[] = {"+", "-", "*", "/"};
    return opr[digitRand(4)];

}

//生成分数
string fracRand(int n){
    ostringstream os;
    int i = digitRand(n);
    int d = digitRand(n);
    while(d == 0){
        d = digitRand(n);
    }
    os << i << '/' << d;
    return os.str();
}

//生成表达式
string createEXP(int numberRange, int numberType, int oprType){
    ostringstream os;
    int a[3] = {0, 0, 0};
    int b[2] = {0,0};
    if (numberType == 0) {}
    else if(numberType == 1) a[1] = 1;
    else a[2] = 2;

    if (oprType == 0){

    }else{
        b[1] = 1;
    }

    int numberIndex = a[digitRand(3)];
    int oprIndex = b[digitRand(2)];
    switch (numberIndex){
    case 0 :
        os << digitRand(numberRange);
        break;
    case 1:
        os << doubleRand(numberRange);
        break;
    case 2:
        os << fracRand(numberRange);
        break;
    default:
        os << 0;
        break;

    }
    switch (oprIndex){
    case 0:
        os << oprLow();
        break;
    case 1:
        os << oprAll();
        break;
    default:
        os << '+';
        break;

    }

    numberIndex = a[digitRand(3)];
    switch (numberIndex){
    case 0:
        os << digitRand(numberRange);
        break;
    case 1:
        os << doubleRand(numberRange);
        break;
    case 2:
        os << fracRand(numberRange);
        break;
    default:
        os << 0;
        break;
    }
    os << "=";
    return os.str();
}

void conT(){
    cout << "Please enter the number of EXP to be generated:";
    cin >> expNumber;
    cout << "Please enter the data range:";
    cin >> numberRange;
    cout << "Please enter the data type:(0.only integer, 1.integer&decimal, 2.integer&decimal&fraction)";
    cin >> numberType;
    cout << "Please enter the operation type:(0.only \"+,-\", 1.\"+,-,*,/\")";
    cin >> oprType;
    cout << "Please enter the number of EXP in each line:";
    cin >> colNumber;
    cout << "Please enter the number of blank line within two lines:";
    cin >> rowHeight;
}

void create(){
    for(int i = 0; i < expNumber; i++){
        EXP[i] = createEXP(numberRange, numberType, oprType);
    }
}

void print()
{
    int cnt = 0;
    for(int i = 0; i < expNumber; i++){
        cout << setw(15) << EXP[i] << "  ";
        cnt++;
        if(cnt == colNumber)
        {
            cnt = 0;
            for(int j = 0; j < rowHeight+1; j++){
                cout << "\n";
            }
        }
    }

}
int main(){
    srand((unsigned) time(NULL));
    conT();
    create();
    print();
    return 0;
}