initial commit; fizzbuzz complete
This commit is contained in:
commit
be94e873b0
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
bld/
|
||||
dbg/
|
||||
obj/
|
||||
target/
|
1
c/fizzbuzz/.gitignore
vendored
Normal file
1
c/fizzbuzz/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
bld
|
36
c/fizzbuzz/makefile
Normal file
36
c/fizzbuzz/makefile
Normal file
|
@ -0,0 +1,36 @@
|
|||
########################
|
||||
# (c) Ren Kararou 2023 #
|
||||
# All rights reserved #
|
||||
########################
|
||||
|
||||
# For use with GNU Make
|
||||
|
||||
CFLAGS ?= -Wall -Wextra -march=native -O3 -flto=thin -funroll-loops
|
||||
|
||||
.PHONY : all
|
||||
all : bld/fizzbuzz
|
||||
|
||||
.PHONY : debug
|
||||
debug : bld/fizzbuzz-dbg
|
||||
|
||||
obj/%.o : src/%.c
|
||||
@mkdir -p obj
|
||||
@cc $(CFLAGS) -c -o $@ $<
|
||||
|
||||
bld/fizzbuzz : obj/main.o
|
||||
@mkdir -p bld
|
||||
@cc $(CFLAGS) -o $@ $<
|
||||
@llvm-strip $@
|
||||
|
||||
dbg/%-dbg.o : src/%.c
|
||||
@mkdir -p dbg
|
||||
@cc --debug $(CFLAGS) -c -o $@ $<
|
||||
|
||||
bld/fizzbuzz-dbg : dbg/main-dbg.o
|
||||
@mkdir -p bld
|
||||
@cc --debug $(CFLAGS) -o $@ $<
|
||||
|
||||
.PHONY : clean
|
||||
clean :
|
||||
@rm -rf obj bld dbg
|
||||
|
49
c/fizzbuzz/src/main.c
Normal file
49
c/fizzbuzz/src/main.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
/************************
|
||||
* (c) Ren Kararou 2023 *
|
||||
* All rights reserved *
|
||||
************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define RUNTO 999999999
|
||||
#define MAXDIGITSYO 10
|
||||
#define MAXBUFFERYO (MAXDIGITSYO*2)
|
||||
static volatile uint8_t run = 1;
|
||||
|
||||
void siginthandl(int _notused) {
|
||||
run = 0;
|
||||
}
|
||||
|
||||
int main(/* int argc, char** argv */) {
|
||||
//setup signal handling
|
||||
signal(SIGINT, siginthandl);
|
||||
|
||||
for (int i = 1; i < RUNTO; i++) { // if we ever get this high we've won computing.
|
||||
if (run) {
|
||||
// setup a buffer, 10 digits is all we need, since we're only going up to 999999999 (plus a null terminator)
|
||||
char buf[MAXBUFFERYO] = "\0";
|
||||
// gotta get that return value.
|
||||
int pos = 0;
|
||||
if (i % 3 == 0) {
|
||||
pos = snprintf(buf+pos, MAXBUFFERYO-pos, "fizz");
|
||||
if (pos < 0) return ENOMEM;
|
||||
}
|
||||
if (i % 5 == 0) {
|
||||
pos = snprintf(buf+pos, MAXBUFFERYO-pos, "buzz");
|
||||
if (pos < 0) return ENOMEM;
|
||||
}
|
||||
if (pos == 0) {
|
||||
pos = snprintf(buf+pos, MAXBUFFERYO-pos, "%d", i);
|
||||
if (pos < 0) return ENOMEM;
|
||||
}
|
||||
puts(buf);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0; // SUCCESS!
|
||||
}
|
||||
|
7
rust/fizzbuzz/Cargo.lock
generated
Normal file
7
rust/fizzbuzz/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "fizzbuzz"
|
||||
version = "0.1.0"
|
8
rust/fizzbuzz/Cargo.toml
Normal file
8
rust/fizzbuzz/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "fizzbuzz"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
15
rust/fizzbuzz/src/main.rs
Normal file
15
rust/fizzbuzz/src/main.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
/************************
|
||||
* (c) Ren Kararou 2023 *
|
||||
* All rights reserved *
|
||||
************************/
|
||||
|
||||
// A memory safe fizzbuzz in rust is much smaller than the same in C, turns out.
|
||||
|
||||
fn main() {
|
||||
for i in 1..999999999 {
|
||||
if i % 3 == 0 && i % 5 == 0 { println!("fizzbuzz"); }
|
||||
else if i % 3 == 0 { println!("fizz"); }
|
||||
else if i % 5 == 0 { println!("buzz"); }
|
||||
else { println!("{i}"); }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue