Skip to content

Instantly share code, notes, and snippets.

View cgmb's full-sized avatar

Cory Bloor cgmb

  • Advanced Micro Devices, Inc.
  • Canada
View GitHub Profile
@cgmb
cgmb / Add.java
Created February 2, 2015 23:26
An adding calculator in Java
class Add {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Add can only add two numbers!");
System.exit(1);
}
int a = 0;
int b = 0;
try {
@cgmb
cgmb / RandomNumberGenerator.java
Created February 2, 2015 23:33
A random number generator in Java
import java.util.Random;
class RandomNumberGenerator {
public static String usage = "Usage: java RandomNumberGenerator <lines> [seed]";
public static void main(String[] args) {
if (args.length > 2) {
System.err.println("Too many arguments!");
System.err.println(usage);
System.exit(1);
@cgmb
cgmb / BenchmarkRandomNumberWriting.java
Last active August 29, 2015 14:14
A benchmark for how quickly random numbers can be written to file in Java
import java.util.Random;
import java.io.FileWriter;
import java.io.IOException;
class BenchmarkRandomNumberWriting {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Incorrect number of arguments!");
System.err.println(
"Usage: java BenchmarkRandomNumberGenerator <output file> <line count>");
@cgmb
cgmb / acceptance_test.sh
Created February 6, 2015 04:43
An end-to-end test implemented in bash
#!/bin/bash
# the above line specifies the program used to run this script
# run like: ./acceptance_test.sh ascending 100 merge
# or: bash acceptance_test.sh ascending 100 merge
# bash is a very easy language to make mistakes in
# let's set a few flags that help catch errors
set -e # exit on error
set -u # make the use of non-existant variables an error
set -o pipefail # propogate errors through | pipelines
@cgmb
cgmb / acceptance_test.ps1
Last active August 29, 2015 14:14
An end-to-end test implemented in powershell
# call this script like so:
# .\acceptance_test.ps1 -order random -size 1000 -algorithm merge
# call our program
java Assign1 $order $size $algorithm output.txt
# read our output file, convert each line to an integer, then sort, and output to an ascii text file
get-content output.txt | foreach-object { [Int] $_ } | sort-object | out-file -encoding ascii sorted.txt
# unfortunately, compare-object does not care about order, so it can't check the sort
@cgmb
cgmb / LinkedList.java
Last active August 29, 2015 14:15
A singly-linked list in Java
import java.util.NoSuchElementException;
public class ForwardList<T> {
public ForwardList() {
head = tail = null;
}
public void insertAtHead(T data) {
Node<T> n = new Node<T>(data, head);
head = n;
@cgmb
cgmb / Add.java
Created March 5, 2015 03:57
Adding big numbers using a stack of digits in Java
public class Add {
public static int charToInt(char c) {
return Integer.parseInt(Character.toString(c));
}
public static LLStack<Integer> stackDigits(String s) {
LLStack<Integer> stack = new LLStack<Integer>();
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
try {
@cgmb
cgmb / main.cpp
Last active August 29, 2015 14:19 — forked from hostilefork/main.cpp
#include <iostream>
#include <QCoreApplication>
#include <QTimer>
#include "param.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
@cgmb
cgmb / main.cpp
Created August 19, 2015 19:47
AntTweakBar's TwGetKeyCode SSCCE
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define _stricmp strcasecmp
#define _strdup strdup
typedef enum ETwKeyModifier
@cgmb
cgmb / copy.c
Created September 22, 2015 16:47
strcpy example
#include <stdio.h>
#include <string.h>
int main() {
char buffer[6];
char* h = "hello";
// copy the string from 'h' into 'buffer'
strcpy(buffer, h);
// now change the first letter in the buffer