Skip to content

Instantly share code, notes, and snippets.

View Prince781's full-sized avatar
⚙️
Compiling a compiler

Princeton Ferro Prince781

⚙️
Compiling a compiler
View GitHub Profile
@Prince781
Prince781 / isprime.c
Last active August 29, 2015 14:02
Check prime.
char is_prime(long n) { long i=1; while (n > 1 && n % ++i); return i >= n; }
@Prince781
Prince781 / bintoint.c
Created July 7, 2014 06:14
Binary string to number converter
#include <stdio.h>
unsigned bintoint(char s[]) {
int i, l;
unsigned v = 0;
for (l=-1; s[l+1] != '\0'; ++l);
for (i=l; i>=0; --i)
v += (s[i]-'0')<<(l-i);
return v;
}
@Prince781
Prince781 / gendata.sh
Created September 22, 2014 03:16
Generates data from a prime list.
#!/bin/sh
# gendata.sh
function print_ij() {
# print j'th field (column) of the i'th line
awk -v i=$1 -v j=$2 'FNR == i {print $j}' $3
}
function gendata() {
n=$(($1 - 1))
@Prince781
Prince781 / genlist.c
Created September 28, 2014 01:14
Linked List generator in C
#include "genlist.h"
#include <stdio.h>
#include <stdlib.h>
void init_list(struct list **p, unsigned tail, unsigned loop) {
if (*p != NULL) {
fprintf(stderr, "init_list: base pointer not NULL\n");
return;
}
@Prince781
Prince781 / prob_benchmark.h
Last active August 29, 2015 14:07
Small benchmark suite for Data Structures homework.
#ifndef _PROB_BENCHMARK_H_
#define _PROB_BENCHMARK_H_
#define _GNU_SOURCE
#include <stdio.h>
#include <time.h>
#define KNRM "\x1B[0m" // normal console color
#define KRED "\x1B[31m"
@Prince781
Prince781 / mat.c
Last active August 29, 2015 14:08
Basic matrix functionality.
#include <stdio.h>
#include <stdlib.h>
typedef struct mat {
int rows;
int cols;
int **e;
} mat;
mat *create_matrix(int i, int j);
@Prince781
Prince781 / heapsort.c
Created November 24, 2014 17:47
Heapsort implementation in C
#include <stdio.h>
void heapify(int *a, int len);
void heapsort(int *a, int len);
void siftdown(int *a, int p, int len);
void printa(int *a, int len);
int main(int argc, char *argv[])
{
int arr[10] = {1,2,4,-41,4,13,3,2,40,-4};
@Prince781
Prince781 / fsize.sh
Last active August 29, 2015 14:11
file size function
#!/bin/sh
# put this in .bashrc or /etc/bashrc
function fsize() {
kB=1000
val=$(stat --printf="%s" $1)
if [ $val -lt $kB ]; then
printf "$val B\n"
elif [ $val -lt $(($kB*$kB)) ]; then
printf "%0.2lf KB\n" $(bc -l <<< "$val/$kB")
@Prince781
Prince781 / Makefile
Last active August 29, 2015 14:11
GLFW demo
# makefile for glfw
CFLAGS=$(shell pkg-config --cflags glfw3)
LDFLAGS=$(shell pkg-config --libs glfw3) -lGL -lm
all:
$(CC) $(CFLAGS) glfwtest.c -o glfw_test $(LDFLAGS)
clean:
$(RM) glfw_test
@Prince781
Prince781 / threads.c
Created December 27, 2014 21:23
pthreads example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#define UBOUND 1000000000
void *tfunc(void *arg);
int main(int argc, char *argv[])