Skip to content

Instantly share code, notes, and snippets.

View dvtate's full-sized avatar
🐧
chilling

Dustin Van Tate Testa dvtate

🐧
chilling
View GitHub Profile
@dvtate
dvtate / arg_parse.h
Created May 20, 2016 01:26
a simple [incomplete] command-line parsing library...
#ifndef ARG_PARSE_H
#define ARG_PARSE_H
#include <string.h> // strcmp, strcat
#include <stdlib.h> // atoi
char* findOccurance(char*,char*);
void getArgs(int, char**, char*, ...);
char hasArg(int,char**, char*);
char* strAfter(int, char**, char*);
@dvtate
dvtate / vector2.hpp
Last active June 4, 2016 22:51
pod vector2 class template
#ifndef VECTOR2_H
#define VECTOR2_H
template <class TYPE>
class Vector2 {
public:
TYPE x, y;
Vector2(TYPE xVal = 0, TYPE yVal = 0) : x(xVal), y(yVal) {}
#include <iostream> // std::string, std::cout, std::cin
//#include <inttypes.h>
//this function is recursive (calls itself)
long int fac(short int base, long int value = 1){ // factorial
// negative numbers give errors
if (base < 0)
throw "fac(): Can't factorial negative numbers.";
@dvtate
dvtate / fish2.cpp
Last active May 28, 2016 18:51
a really simple demonstration of OOP in C++
#include <iostream> //std::string std::cin std::cout
class Fish {
//these are private variables
std::string _type, _name;
int _length;
public: //these are public
@dvtate
dvtate / fish.cpp
Created May 28, 2016 19:51
tiny C++ OOP demonstration
#include <iostream> //the library we will be using
using namespace std;//no need to add the 'std::' scope
class Fish{
//Not the proper way to define a class
// should be in a separate fish.h file with an implementation fish.cpp file
private:
int _length;//how long
string _name, _type;
#include<iostream> // std::cin, std::cout, std::string()
///finds GCF using Euclid's Algorithm ( https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations (converted to C++))
template <class T>
T gcf(T a, T b){
//throw error if numbers aren't whole ( % = remainder: aka 'modulus')
if (a % 1 != 0 || b % 1 != 0) // could be simplified to `if (a % 1 || b % 1)` (as 0 == false)
throw std::string("GCF(): Numbers given weren\'t whole"); //error stops execution
@dvtate
dvtate / functions.cpp
Created May 28, 2016 19:54
this is for the n00bs :P
#include <iostream> //cin, cout
/**Functions:
*- Functions return a value (using the keyword "return").
*
*- Functions that don't return a value are called subroutines (subs),
* however they technically return a weird type called a void (holds no data)
*
*- In C++, functions are treated as variables in respect to their syntax.
*
@dvtate
dvtate / quit_prompt.cpp
Created May 28, 2016 20:00
more n00b practice...
#include<iostream> //std::cin, std::cout
int main(){
char response;//single letter that holds the user's response (y/n)
std::cout <<"\n Quit program (y/N)? ";
std::cin >>response;
//single expression if statement:
@dvtate
dvtate / reference_parameters.cpp
Created May 28, 2016 20:08
more stuff for n00bs
#include <iostream> //std::cout
//example function using pointers (C-compatable)
void increaseBy5(int* number);
//example function using reference parameters (C++ only)
void add5(int& number);
int main(){
@dvtate
dvtate / switch_statement.cpp
Last active May 28, 2016 20:26
more practice for the n00bs
#include <iostream> // std::xcin, std::cout,
// Here `unsigned char` is used to store a number and not a letter.
// A `char*` (character pointer) is a string, since a string is just an array of `char`s
// The character pointer points to the first element and you can increment/decrement it
// to see the rest of the elements.
char* numberToName(unsigned char monthNum){
/// returns a string which is the name of the month corresponding