Skip to content

Commit 6564a3d

Browse files
committed
initial commit
Organize the code. Declarations in functions.h, implementation in functions.c and program code in main.c. Also, included Makefile to build the program.
1 parent b527c7c commit 6564a3d

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

chapter05/5-6/functions.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include <ctype.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include "functions.h"
6+
7+
/* atoi: convert s to integer (chapter 3) */
8+
int atoi(const char *s)
9+
{
10+
int n, sign;
11+
12+
while (isspace(*s)) /* skip whitespace */
13+
++s;
14+
sign = (*s == '-') ? -1 : 1;
15+
if (*s == '+' || *s == '-') /* skip sign */
16+
++s;
17+
for (n = 0; isdigit(*s); ++s)
18+
n = 10 * n + (*s - '0');
19+
return sign * n;
20+
}
21+
22+
/* atof: convert string s to double (chapter 4) */
23+
double atof(const char *s)
24+
{
25+
int sign, expSign;
26+
double val, power, exponent;
27+
28+
while (isspace(*s)) /* skip whitespace */
29+
++s;
30+
sign = (*s == '-') ? -1 : 1;
31+
if (*s == '+' || *s == '-')
32+
++s;
33+
for (val = 0.0; isdigit(*s); ++s)
34+
val = 10.0 * val + (*s - '0');
35+
if (*s == '.')
36+
++s;
37+
for (power = 1.0; isdigit(*s); ++s) {
38+
val = 10.0 * val + (*s - '0');
39+
power *= 10.0;
40+
}
41+
if (tolower(*s) == 'e') /* handle scientific notation */
42+
++s;
43+
expSign = (*s == '-') ? -1 : 1; /* record exponent's sign */
44+
if (*s == '+' || *s == '-')
45+
++s;
46+
for (exponent = 0.0; isdigit(*s); s++) /* extract the exponent */
47+
exponent = 10.0 * exponent + (*s - '0');
48+
while (exponent-- != 0) /* adjust power according to exponent */
49+
power = (expSign > 0) ? power / 10: power * 10;
50+
return sign * val / power;
51+
}
52+
53+
/* itoa: convert n to characters in s (chapter 3) */
54+
void itoa(int n, char *s)
55+
{
56+
int sign;
57+
58+
sign = n; /* record sign */
59+
60+
do { /* generate digits in reverse order */
61+
*s++ = abs(n % 10 + '0'); /* get the next digit */
62+
} while ((n /= 10) > 0); /* delete it */
63+
if (sign < 0)
64+
*s++ = '-';
65+
*s = '\0';
66+
reverse(s - strlen(s));
67+
}
68+
69+
/* getLine function: read a line into s, return length (chapter 4) */
70+
int getLine(char *s, int lim)
71+
{
72+
int c;
73+
char *len;
74+
75+
len = s;
76+
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
77+
*s++ = c;
78+
if (c == '\n')
79+
*s++ = c;
80+
*s = '\0';
81+
return strlen(len);
82+
}
83+
84+
/* getop: get next operator or numeric operand (chapter 4) - getch/ungetch version */
85+
int getop(char *s)
86+
{
87+
int c;
88+
char *var;
89+
90+
while (isblank(s[0] = c = getchar()))
91+
;
92+
s[1] = '\0';
93+
if (c == '-') /* check sign */
94+
if (!isdigit(*++s = c = getchar())) {
95+
ungetc(c, stdin);
96+
c = *--s; /* not a sign */
97+
}
98+
if (isalpha(c)) { /* string command */
99+
var = s;
100+
while (isalpha(*++s = c = getchar()))
101+
;
102+
*s = '\0';
103+
ungetc(c, stdin);
104+
return (strlen(var) == 1) ? var[0] : NAME;
105+
}
106+
if (!isdigit(c) && c != '.')
107+
return c; /* not a number */
108+
if (isdigit(c))
109+
while (isdigit(*++s = c = getchar()))
110+
;
111+
if( c == '.') /* collect fraction part */
112+
while (isdigit(*++s = c = getchar()))
113+
;
114+
*s = '\0';
115+
ungetc(c, stdin);
116+
return NUMBER;
117+
}
118+
119+
/* reverse: reverses the string s in place (chapter 3) */
120+
void reverse(char *s)
121+
{
122+
char *p, *q, tmp;
123+
124+
q = s + strlen(s) - 1;
125+
for (p = s; p < q; ++p, --q) {
126+
tmp = *p;
127+
*p = *q;
128+
*q = tmp;
129+
}
130+
}
131+
132+
/* strindex: return index of t in s, -1 if none (chapter 4) */
133+
int strindex(const char *s, const char *t)
134+
{
135+
int pos;
136+
const char *j, *k;
137+
138+
for (pos = 0; *s; ++s, ++pos) {
139+
for (j = s, k = t; *k && (*j == *k); ++j, ++k)
140+
;
141+
if (k > t && *k == '\0')
142+
return pos;
143+
}
144+
return -1;
145+
}

chapter05/5-6/functions.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef FUNCTIONS_H
2+
#define FUNCTIONS_H
3+
4+
#include <stddef.h>
5+
6+
#define NUMBER '0' /* signal that a number was found */
7+
#define NAME '1' /* signal that a string command was found */
8+
9+
int atoi(const char *);
10+
double atof(const char *);
11+
void itoa(int , char *);
12+
int getLine(char *, int);
13+
int getop(char *);
14+
void reverse(char *);
15+
int strindex(const char *, const char *);
16+
17+
#endif /* FUNCTIONS_H */

chapter05/5-6/main.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Exercise 5-6. Rewrite appropriate programs from earlier chapters and
3+
* exercises with pointers instead of array indexing. Good possibilities
4+
* include getLine (chapter 1 and 4), atoi, itoa, and their variants (Chapters
5+
* 2, 3 and 4), reverse (Chapter 3), and strindex and gettop (Chapter 4)
6+
*
7+
* By Faisal Saadatmand
8+
*/
9+
10+
/*
11+
* Just use any function listed above in main.c to test its code. Below are
12+
* some examples.
13+
*
14+
* Instructions:
15+
* To Build:
16+
* make main
17+
* To run:
18+
* ./main
19+
* To clean object files:
20+
* make clean
21+
*
22+
*/
23+
24+
#include <stdio.h>
25+
#include "functions.h"
26+
27+
28+
int main(void)
29+
{
30+
char str[] = "It's not personal";
31+
const char *pattern = "son";
32+
char str2[] = "5892";
33+
int pos, number;
34+
35+
/* strindex */
36+
if ((pos = strindex(str, pattern)) < 0)
37+
printf("Not found\n");
38+
else
39+
printf("%i\n", pos);
40+
/* reverse */
41+
reverse(str);
42+
printf("%s\n", str);
43+
/* atoi */
44+
number = atoi(str2);
45+
printf("%i\n", number);
46+
printf("%i\n", atoi("4092"));
47+
return 0;
48+
}

0 commit comments

Comments
 (0)