-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions_2.c
89 lines (73 loc) · 1.8 KB
/
helper_functions_2.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
/* these functions are helper functions */
#include "shell_head.h"
#include "shell_builtins.h"
int string_to_integer(char *s)
{
int size, answer, mod;
char newString[11];
answer = 0;
mod = 0;
while (*s && (*s < '0' || *s > '9')) {
if (*s == '-') {
mod++;
}
s++;
}
if (mod % 2) {
mod = -1;
}
else {
mod = 1;
}
for (size = 0; s[size] >= '0' && s[size] <= '9'; size++) {
}
if (size > 10) {
return 0;
}
cut_string(s, size, newString);
answer = make_number(newString, size, mod);
return answer;
}
void cut_string(char *s, int size, char *newString)
{
int ndx;
for (ndx = 0; ndx < size; ndx++) {
newString[ndx] = s[ndx];
}
newString[ndx] = '\0';
}
int make_number(char *newString, int size, int mod)
{
int number, ndx;
number = 0;
size--;
for (ndx = 0; ndx <= size; ndx++) {
number += ((newString[size - ndx] - '0') * expon(ndx));
}
number *= mod;
if ((mod == 1 && number > 0) || (mod == -1 && number < 0)) {
return number;
}
return 0;
}
int expon(int n)
{
int x;
x = 1;
while (n--) {
x *= 10;
}
return x;
}
/* function compares str1 to str2, and returns an int */
int string_compare(char *str1, char *str2)
{
int i;
for(i = 0; str1[i] == str2[i] && str1[i] && str2[i]; i++) {}
if (str1[i] == '\0' && str2[i] == '\0') {
return (0);
}
else {
return (1);
}
}