-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_expression.c
executable file
·135 lines (114 loc) · 3.67 KB
/
check_expression.c
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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//Checks the brackets of input expression
int CheckBrackets(char *expression) {
int count_left_brackets = 0, count_right_brackets = 0, i;
//let's count a number of each type of brackets
for(i = 0; i < strlen(expression); i++) {
if(expression[i] == '(')
count_left_brackets++;
if(expression[i] == ')')
count_right_brackets++;
}
if(count_right_brackets == count_left_brackets) {
int total_count_brackets = count_left_brackets + count_right_brackets;
//exclude occasions for ")("
if(total_count_brackets != 0) {
int check_open_close = 0;
for(i = 0; i < strlen(expression); i++) {
if(check_open_close < 0)
return -1;
else {
if(expression[i] == '(')
check_open_close++;
if(expression[i] == ')')
check_open_close--;
}
}
}
return 1;
}
else
return -1;
//will never reach this "return"
return 1;
}
//The function checks comma's right position in the expression
int CheckComma(char *expression) {
int i;
if(expression[0] == '.' || expression[strlen(expression) - 1] == '.')
return -1;
for(i = 1; i < strlen(expression) - 1; i++) {
if(expression[i] == '.') {
if((expression[i - 1] >= '0' && expression[i - 1] <= '9') && (expression[i + 1] >= '0' && expression[i + 1] <= '9'))
continue;
else
return -1;
}
}
return 1;
}
//Checks the correct order of '*' and '/' signs
int CheckFirstActionSigns(char *expression, char sign) {
int i;
if(expression[0] == sign || expression[strlen(expression) - 1] == sign)
return -1;
for(i = 1; i < strlen(expression) - 1; i++) {
if(expression[i] == sign) {
if(((expression[i - 1] >= '0' && expression[i - 1] <= '9') && (expression[i + 1] >= '0' && expression[i + 1] <= '9')) ||
((expression[i - 1] >= '0' && expression[i - 1] <= '9') && (expression[i + 1] == '(')) ||
((expression[i + 1] >= '0' && expression[i + 1] <= '9') && (expression[i - 1] == ')')) ||
((expression[i - 1] == ')') && ((expression[i + 1] == '('))) )
continue;
else
return -1;
}
}
return 1;
}
//Checks the correct order of '+' and '-'
int CheckSecondActionSigns(char *expression, char sign) {
int i;
//if we meet this sign at the end of expression - then stupidly return -1
if(expression[strlen(expression) - 1] == sign)
return -1;
for(i = 1; i < strlen(expression) - 1; i++) {
if(expression[i] == sign) {
if((((expression[i - 1] >= '0' && expression[i - 1] <= '9') && (expression[i + 1] >= '0' && expression[i + 1] <= '9'))) ||
(strchr("+-/*.",expression[i - 1]) == NULL && expression[i + 1] >= '0' && expression[i + 1] <= '9') ||
(strchr("+-/*.",expression[i + 1]) == NULL && expression[i - 1] >= '0' && expression[i - 1] <= '9') ||
(expression[i - 1] == ')' || (expression[i + 1] == '('))) {
continue;
}
else
return -1;
}
}
return 1;
}
//Checks the content of operands in expression(excludes occasions for "3.2.4" numbers)
int CheckOperandsOnCommas(char *expression) {
int i; //runner through expression
int dot_counter; //counter for commas
for(i = 0; i < strlen(expression); i++) {
if(strchr("1234567890", expression[i]) != NULL) {
dot_counter = 0;
while(strchr("1234567890", expression[i]) != NULL || expression[i] == '.') {
if(expression[i] == '.') {
dot_counter++;
if(dot_counter > 1)
return -1; //in this case operand contains more than 1 comma
}
i++;
}
}
}
return 1;
}
//Probably, some cases will be added with time
int CheckOtherStrangeCases(char *expression) {
if(strstr(expression, "()"))
return -1;
return 1;
}