Rotating bits, one shift at a time.
Posts tagged C++
ore-may ig-pay atenizing-lay
Dec 7th
I got bored. So sue me. Redone in C++ (correctly this time, and probably more efficient than this or this.)
#include <iostream> #include <string> #include <vector> #include <cctype> using namespace std; bool isPunctuation( char c ); string latinize( string s ); bool isVowel( char c ); int main( ) { string input, output, temp; vector<string> words; vector<char> punctuation; cout< < "Enter a phrase you would like to latenize" << endl << "-> "; getline( cin, input ); for( int i = 0; i < input.length(); i++ ) { char c = tolower( input.at( i ) ); if( isPunctuation( c ) ) { words.push_back( temp ); punctuation.push_back( c ); temp.clear(); } else temp.append( 1, c ); } if( !temp.empty() ) { words.push_back( temp ); punctuation.push_back( ' ' ); } for( int i = 0; i < words.size(); i++ ) { string l = latinize( words.at( i ) ); output.append( l ); output.append( 1, punctuation.at( i ) ); } cout<< output << endl; return 0; } bool isPunctuation( char c ) { char punctuation[33] = { ',', '.', '/', '<', '>', '?', ';', '\'', ':', '"', '[', ']', '\\', '{', '}', '|', '`', '-', '=', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', ' ' }; for( int i = 0; i < 33; i++ ) if( c == punctuation[i] ) return true; return false; } string latinize( string s ) { string postfix, temp; for( int i = 0; i < s.length(); i++ ) { char c = s.at( i ); if( isVowel( c ) ) { postfix.append( "ay" ); temp = s.substr( i ); temp.append( "-" ); temp.append( postfix ); return temp; } else postfix.append( 1, c ); } } bool isVowel( char c ) { char vowels[5] = { 'a', 'e', 'i', 'o', 'u' }; for( int i = 0; i < 5; i++ ) if( c == vowels[i] ) return true; return false; }
Rotating Bits
Nov 25th
Under normal circumstances, you can only ask for a bit rotation using assembly code, however, it is possible to imitate rotation through code.
First an explanation on rotating, and bits:
Goofy Sort
Nov 18th
You know it’s an easy algorithm to program when you’re done writing it, before the teacher is done explaining it…
8b74cb8a9c4c5b23277826fcbfe51db8005
DiceRoller v2
Apr 21st
I decided my first version wasn’t exactly flexible, so I rewrote it to parse out strings of dice rolls and then roll them.
/* * dice.c * * Created on: Apr 21, 2009 * Author: Mike Sherwood (coolhand2@gmail.com) * Modified on: Nov 18, 2009 * Author: Mike Sherwood (coolhand2@gmail.com) * Changes: Changed the if-series in modifySum * to be a select statement instead. */ #include <stdio .h> #include <time .h> #include <stdlib .h> int rollDice( int, int ); void modifySum( char, int, int* ); int main( int argc, char* argv[] ) { char* diceString = argv[1]; int amount, sides, modifier, sum; char mod; amount = sides = modifier = sum = 0; mod = '\0'; sscanf( diceString, "%dd%d%c%d", &amount, &sides, &mod, &modifier ); sum = rollDice( amount, sides ); printf( "Sum: %d\n", sum ); modifySum( mod, modifier, &sum ); printf( "Total: %d\n", sum ); return 0; } int rollDice( int amount, int sides ) { srand( time(NULL)/2 ); int i, value, sum; for( i = value = sum =0; i < amount; i++ ) { value = ( rand() % sides ) + 1; printf( "Dice %d: %d\n", i + 1, value ); sum += value; } return sum; } void modifySum( char mod, int modifier, int* sum ) { switch( mod ) { case '\0': printf( "No Modification\n" ); break; case '+': printf( "Add %d\n", modifier ); *sum += modifier; break; case '-': printf( "Subtract %d\n", modifier ); *sum -= modifier; break; case '*': case 'x': case 'X': printf( "Multiply %d\n", modifier ); *sum *= modifier; break; case '/': printf( "Divide %d\n", modifier ); *sum /= modifier; break; } return; }
Dice Roller v1
Apr 20th
A bit of C code I russelled up to roll a specific die based off the arguments passed to it through the command line. It’s basically just a random number generator with addition properties.
/* * dice.c * * Created on: Apr 20, 2009 * Author: Mike Sherwood <coolhand2 @gmail.com> */ #include <stdio .h> #include <stdlib .h> #include <time .h> int main( int argc, char* argv[] ) { int number, sides, i, val, sum; number = atoi( argv[1] ); sides = atoi( argv[2] ); val = sum = 0; srand( time( NULL )/2 ); printf( "Rolling: %dd%d\n", number, sides ); for( i = 0; i < number; i++ ) { val = (rand() % sides) + 1; printf( "Dice %d: %d\n", i+1, val); sum += val; } printf( "Sum: %d\n", sum ); return 0; }
C++ Pig Latinizer
Sep 28th
About three lines smaller actually. This version is only 94 lines, instead of the 97 lines of the php version… It confuses me, but eventually I’ll figure it out… I’m sure it has to do with amount of comments or something… Or the fact that it can only take one word…