-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
146 lines (126 loc) · 2.93 KB
/
helper.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
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "helper.h"
#include <ctype.h>
// Random number
int call_random (void) {
int value;
value=rand();
return value;
}
// Add two numbers
int call_add (int value1, int value2) {
return (value1 + value2);
}
// Substruct two numbers
int call_sub (int value1, int value2) {
return (value1 - value2);
}
// Multiply two numbers
int call_mul (int value1, int value2) {
return (value1 * value2);
}
// Divide two numbers
int call_div (int value1, int value2) {
if (value2==0)
printf("Error! Devide by zero!\n");
return (value1 / value2);
}
// num1 mod num2
int call_rem (int value1, int value2) {
return (value1 % value2);
}
// Read number from input
int call_read (void) {
char s[80];
fgets(s, 80, stdin);
return atoi(s);
}
// Function write: print a number
void call_write (int value) {
printf("%d ", value);
}
// Function writeln: print a number and go to the next line
void call_writeln (int value) {
printf("%d\n", value);
}
// Function nl: leave a blanck line
void call_nl (void) {
printf("\n");
}
// Deal with the operators
int call_eval_expression (int value1, int value2, char* oper) {
int value=0;
// For the operators (<, <=, >, >=, ==, !=): if value=1, then the condition is real
if (!strcmp(oper, "<"))
value = (value1 < value2);
else if (!strcmp(oper, "<="))
value = (value1 <= value2);
else if (!strcmp(oper, ">"))
value = (value1 > value2);
else if (!strcmp(oper, ">="))
value = (value1 >= value2);
else if (!strcmp(oper, "=="))
value = (value1 == value2);
else if (!strcmp(oper, "!="))
value = (value1 != value2);
// Arithmetic operations
else if (!strcmp(oper, "+"))
value = call_add(value1, value2);
else if (!strcmp(oper, "-"))
value = call_sub(value1, value2);
else if (!strcmp(oper, "*"))
value = call_mul(value1, value2);
else if (!strcmp(oper, "/"))
value = call_div(value1, value2);
else if (!strcmp(oper, "%"))
value = call_rem(value1, value2);
else
printf("Error! Invalid delimiter!\n"); // Non-existent operator
return value;
}
// Print error
void syntax_error (int row, char* error) {
printf("Row: %d, Syntax Error: %s\n", row, error);
exit(1);
}
// Print error
void runtime_error (int row, char* error) {
printf("Row: %d, Runtime Error: %s\n", row, error);
exit(1);
}
// Search for a delimiter
int isdelim (char c) {
if (strchr(" !+-<>/*%=[]", c) || c==9 || c=='\r' || c=='\n' || c==0)
return 1;
else
return 0;
}
// Skip over white space
int iswhite (char c) {
if (c==' ' || c=='\t')
return 1;
else
return 0;
}
// Check if it's a number or a valuable
int isnumber (char* s) {
int i;
for (i=0; i<strlen(s); i++)
if (!isdigit(s[i]))
return 0;
return 1;
}
// Check if the variable has a valid name based on the rules
int is_valid_variable (char* s) {
int i;
for (i=0; i<strlen(s); i++) {
if (i==0)
if (!isalpha(s[i]))
return 0;
if (!isdigit(s[i]) && !isalpha(s[i]) && s[i]!='_')
return 0;
}
return 1;
}