-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.txt
More file actions
53 lines (43 loc) · 1.16 KB
/
test_parser.txt
File metadata and controls
53 lines (43 loc) · 1.16 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_OPERAND 10
#define MIN_OPERAND 1
int generateRandomNumber(int min, int max) {
return min + rand() % (max - min + 1);
}
char getRandomOperator() {
char operators[] = {'+', '-', '*', '/'};
int index = rand() % 4;
return operators[index];
}
void generateExpression(int depth) {
if (depth == 0) {
int operand = generateRandomNumber(MIN_OPERAND, MAX_OPERAND);
printf("%d", operand);
return;
}
if (rand() % 2 == 0) {
int operand = generateRandomNumber(MIN_OPERAND, MAX_OPERAND);
printf("%d", operand);
} else {
printf("(");
generateExpression(depth - 1);
printf(")");
}
if (depth > 1) {
char operator = getRandomOperator();
printf(" %c ", operator);
}
}
int main() {
srand(time(NULL));
int numExpressions = 5; // Number of expressions to generate
int maxDepth = 3; // Maximum depth of parentheses
for (int i = 0; i < numExpressions; i++) {
int depth = generateRandomNumber(1, maxDepth);
generateExpression(depth);
printf("\n");
}
return 0;
}