-
Notifications
You must be signed in to change notification settings - Fork 4
/
exercise1-22.c
86 lines (75 loc) · 1.32 KB
/
exercise1-22.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
#include<stdio.h>
#define MAXCOL 50
#define TABVAL 8
char line[MAXCOL];
int expandtab(int pos);
int printline(int pos);
int getblank(int pos);
int newposition(int pos);
int main(void)
{
int pos,c;
pos = 0;
while((c=getchar())!=EOF)
{
line[pos] = c;
if( c == '\t')
pos = expandtab(pos);
if( c == '\n')
{
printline(pos);
pos = 0;
}
else if( ++pos >= MAXCOL )
{
pos = getblank(pos);
printline(pos);
pos = newposition(pos);
}
}
return 0;
}
int expandtab(int pos)
{
line[pos] = ' ';
for(++pos; (pos < MAXCOL)&&((pos % TABVAL)!= 0); ++pos)
line[pos] = ' ';
if( pos >= MAXCOL)
{
printline(pos);
return 0;
}
else
return pos;
}
int printline(int pos)
{
int i;
for(i = 0; i < pos; ++i)
putchar(line[i]);
if( pos > 0)
putchar('\n');
}
int getblank(int pos)
{
if( pos > 0)
while( line[pos] != ' ')
--pos;
if(pos == 0)
return MAXCOL;
else
return pos + 1;
}
int newposition( int pos)
{
int i,j;
if(pos <= 0 || pos >= MAXCOL)
return 0;
else
{
i = 0;
for(j=pos;j < MAXCOL; ++j,++i)
line[i] = line[j];
}
return i;
}