This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
140 lines (128 loc) · 4.2 KB
/
app.js
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
var inquirer = require('inquirer')
var fs = require('fs')
var Generator = require('./tools/generator')
var Fractional = require('./tools/fractional')
var Solver = require('./tools/solver')
var QuestionBot = require('./tools/questionbot')
function main () {
let doWhat = [{
type: 'list',
name: 'doWhat',
message: 'Solve problems or generate problems?',
choices: [
'Generate',
'Solve',
'Quiz mode!!!'
]
}]
let questionType = [{
type: 'list',
name: 'type',
message: 'Integer arithmetic or fractional arithmetic?',
choices: [
'Integer',
'Fractional'
]
}]
let questionCount = [{
type: 'input',
name: 'count',
message: 'How many problems do you want to generate?',
validate: function (number) {
// Problem count must be a positive integer
let pattern = /^[1-9]\d*$/
let pass = number.match(pattern)
if (pass) {
return true
}
return 'Please enter a positive integer.'
}
}]
inquirer.prompt(doWhat).then(doThis => {
if (doThis['doWhat'] === 'Generate') {
inquirer.prompt(questionType).then(doThis => {
if (doThis['type'] === 'Integer') {
inquirer.prompt(questionCount).then(answers => {
let problemFile = 'problems.txt'
let problemCount = answers['count']
let generator = new Generator()
let problemSet = generator.generate(problemCount)
try {
let stream = fs.createWriteStream(problemFile, {
flags: 'w'
})
problemSet.forEach((item) => {
console.log(item)
stream.write(item + '\n')
})
console.log('Saved ' + problemCount + ' problems to file: ' + problemFile)
} catch (error) {
throw error
}
console.log('Generated ' + problemCount + ' problems.')
})
}
if (doThis['type'] === 'Fractional') {
inquirer.prompt(questionCount).then(answers => {
let problemFile = 'problems.txt'
let problemCount = answers['count']
let fractional = new Fractional()
let problemSet = fractional.fractional(problemCount)
try {
let stream = fs.createWriteStream(problemFile, {
flags: 'w'
})
problemSet.forEach((item) => {
console.log(item)
stream.write(item + '\n')
})
console.log('Saved ' + problemCount + ' problems to file: ' + problemFile)
} catch (error) {
throw error
}
console.log('Generated Fractional ' + problemCount + ' problems.')
})
}
})
}
if (doThis['doWhat'] === 'Solve') {
// let solveProblemFile = 'solve_problems.txt'
let solveProblemFile = 'problems.txt'
let solver = new Solver()
console.log('----------------------------------------')
console.log('Solving problems from', solveProblemFile)
console.log('----------------------------------------')
fs.readFile(solveProblemFile, (err, data) => {
if (err) throw err
let problemSet = data.toString().split('\n')
problemSet.forEach((problem) => {
try {
if (problem !== '') {
let answer = solver.solve(problem)
if (answer === 'DivisionByZero') {
console.log('[ERR!] ' + problem + ' False expression: ' + answer)
} else {
console.log(problem + ' = ' + answer.toFraction())
}
}
} catch (error) {
throw error
}
})
console.log('----------------------------------------')
console.log('Done!')
console.log('----------------------------------------')
})
}
if (doThis['doWhat'] === 'Quiz mode!!!') {
let solveProblemFile = 'solve_problems.txt'
let questionBot = new QuestionBot()
fs.readFile(solveProblemFile, (err, data) => {
if (err) throw err
let problemSet = data.toString().split('\n')
questionBot.startQuestioning(problemSet)
})
}
})
}
main()