Download code

Jump to: navigation, search

Back to Sierpinski_triangle_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

sierpinski.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Sierpinski_triangle_(C)?oldid=11926
14 */
15 
16 #include<stdio.h>
17 #include<string.h>
18 
19 char *apply_rules(char *new_sl, const char *sl, size_t width)
20 {
21 	int i;
22 
23 	new_sl[0]=(sl[0] || sl[1]);
24 	new_sl[width-1]=(sl[width-2] || sl[width-1]);
25 	for(i=1; i<width-1; ++i) {
26 		new_sl[i]=!((sl[i-1] && sl[i] && sl[i+1]) || (!sl[i-1] && !sl[i] && !sl[i+1]));
27 	}
28 	return new_sl;
29 }
30 
31 void print_statelist(const char *sl, size_t width)
32 {
33 	int i;
34 	for(i=0; i<width; ++i) putchar(sl[i]?'@':' ');
35 	putchar('\n');
36 }
37 
38 #define WIDTH 80
39 void run_and_display(size_t niters)
40 {
41 	char statelist1[WIDTH];
42 	char statelist2[WIDTH];
43 	char *statelist, *new_statelist;
44 	int i;
45 
46 	memset(statelist1, 0, WIDTH);
47 	statelist1[WIDTH/2]=1;
48 
49 	statelist=statelist1;
50 	new_statelist=statelist2;
51 
52 	for(i=0; i<niters; ++i) {
53 		print_statelist(statelist, WIDTH);
54 		if((statelist=apply_rules(new_statelist, statelist, WIDTH))==statelist1)
55 			new_statelist=statelist2;
56 		else
57 			new_statelist=statelist1;
58 	}
59 }
60 
61 int main()
62 {
63 	run_and_display(32);
64 	
65 	return 0;
66 }


hijacker
hijacker
hijacker
hijacker