testing conditions for consistency

In the below program, when we have 4 consecutive spaces, the following comparison will not be equal: (x / tabstop != (x + spaces) / tabstop). If we have, let's say, only 2 consecutive spaces, then the comparison will be equal. So we know to print a tab with 4 consecutive spaces and not print a tab otherwise. How do we know this kind of division comparison would work in every case?

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
#include <stdio.h>

#define TABSTOP 4


int main(void)

{

    size_t spaces = 0;

    int ch;

    size_t x = 0;               /* position in the line */

    size_t tabstop = TABSTOP;   /* get this from the command-line 

                                 * if you want to */



    while ((ch = getchar()) != EOF)

    {

        if (ch == ' ')

        {

            spaces++;

        }

        else if (spaces == 0) /* no space, just printing */

        {

            putchar(ch);

            x++;

        }

        else if (spaces == 1) /* just one space, never print a tab */

        {

            putchar(' ');

            putchar(ch);

            x += 2;

            spaces = 0;

        }

        else

        {

            while (x / tabstop != (x + spaces) / tabstop) 

                /* are the spaces reaching behind the next tabstop ? */

            {

                putchar('\t');

                x++;

                spaces--;

                while (x % tabstop != 0)

                {

                    x++;

                    spaces--;

                }

            }



            while (spaces > 0) /* the remaining ones are real space */

            {

                putchar(' ');

                x++;

                spaces--;

            }

            putchar(ch); /* now print the non-space char */

            x++;

        }

        if (ch == '\n')

        {

            x = 0; /* reset line position */

        }

    }



    return 0;

}
http://www.cplusplus.com/forum/general/118435/

> when we have 4 consecutive spaces,
> the following comparison will not be equal: (x / tabstop != (x + spaces) / tabstop).
> If we have, let's say, only 2 consecutive spaces, then the comparison will be equal
wrong.
If spaces >= tabstop-x%tabstop the comparison would not be equal
If you surpass the marker, a TAB would be inserted (especial case if there is only one space)
Can you elaborate a little more on this:

If spaces >= tabstop-x%tabstop
Suppose tabstop=4, that is: there is a mark each 4 characters.

x%tabstop is how much you are past the last mark. By instance, if x=42, you are over 2 characters of the mark 40.

tabstop - x%tabstop would be how much you need to advance in order to reach the next mark. So, if x=42, you need to advance 2 characters to reach the mark 44.

(x / tabstop != (x + spaces) / tabstop)
Both sides would be equal if `x' and `x+spaces' are between the same marks [)
So x=42 is between the mark 40 and 44
Now add the spaces,
if spaces=1 -> x+spaces = 43. It's between marks 40 and 44 (same)
if spaces=2 -> x+spaces = 44. It's between marks 44 and 48 (different)
if spaces=3 -> x+spaces = 45. It's between marks 44 and 48 (different)

So you would want to know the number of spaces to reach the next mark,
that is tabstop - x%tabstop

If you reach the next mark, you need to insert a TAB
Topic archived. No new replies allowed.