Path: C++ : Snipits :
Path: Snipits :
Code Snipits
Code snipits...
counting.cpp - simple counting problem solution
typedef double number_t;
number_t fact(number_t n)
{
n = round(n);
if(n > 1.0) {
return n * fact(n-1);
}
return 1;
}
number_t nCr(number_t n, number_t r)
{
return fact(n) / (fact(r)*fact(n - r));
}
number_t stars_and_bars(number_t n, number_t r)
{
return fact(n+r-1)/fact(fact(n-1)*fact(r));
}
admin.cpp - run cmd.exe as the administrator
#include <windows.h>
int
main()
{
ShellExecute( NULL,
"runas",
"c:\\windows\\system32\\cmd.exe",
"",
NULL, // default dir
SW_SHOWNORMAL
);
return 0;
}
soundex.cpp - soundex function
#include <stdio.h>
#include <ctype.h>
char*
soundex(char *instr, char *outstr)
{ /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
char *table = "01230120022455012623010202";
int count = 0;
while(!isalpha(instr[0]) && instr[0])
++instr;
if(!instr[0]) /* Hey! Where'd the string go? */
return(NULL);
if(toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H')
{
instr[0] = 'F';
instr[1] = 'A';
}
*outstr++ = (char)toupper(*instr++);
while(*instr && count < 5)
{
if(isalpha(*instr) && *instr != *(instr-1))
{
*outstr = table[toupper(instr[0]) - 'A'];
if(*outstr != '0')
{
++outstr;
++count;
}
}
++instr;
}
*outstr = '\0';
return(outstr);
}
hashpjw.cpp - hash PJW
int hashpjw(char* t)
{
unsigned h=0, g;
for(;*t;++t) {
h = (h << 4)+ *t;
if(g=h&0xf0000000) {
h ^= g>>24;
h ^= g;
}
}
return h;
}
hashpos.cpp - hash function for possition and value in a string of characters
int hashpos(const string & str)
{
int ret = 0;
for(int i = 0; i < str.size(); i++) {
ret += str[i] * i;
}
return ret;
}
atexit.cpp - at exit example
#include <iostream>
#include <cstdlib>
using namespace std;
void
die()
{
cout << "die..." << endl;
}
void
hello()
{
cout << "Hello, World!" << endl;
exit(0);
}
int
main()
{
atexit(die);
hello();
cout << "did hello..." << endl;
return 0;
}
atom.c - windows 32 API Atom example.
#include <windows.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
static char szAppName[] = "MyApp";
static char szWndClass[] = "MyWindowClass";
static char szAtomName[] = "<MyApp> Amy's birthday is 05-07-93 <MyApp>";
ATOM atom;
if (GlobalFindAtom (szAtomName))
{
MessageBox (NULL,
"Another instance of this application is running",
"Error", MB_OK |
MB_ICONSTOP);
return -1;
}
atom = GlobalAddAtom (szAtomName);
while(1)
Sleep(10);
return 0;
}
qe2.c - q * e
2
typedef unsigned long long uint64_t;
uint64_t qe2(uint64_t x, uint64_t y, uint64_t n)
{
uint64_t s,t,u;
int i;
s = 1; t = x; u = y;
while(u) {
if(u&1) s = (s*t)%n;
u <<= 1;
t = (t*t)%n;
}
return s;
}
fast_exp.c - fast exp function
typedef unsigned long long uint64_t;
uint64_t fast_exp(uint64_t x, uint64_t y, uint64_t N)
{
uint64_t tmp;
if(y == 1) return x % N;
if(1 ^ (x&1)) {
tmp = fast_exp(x,y/2,N);
return (tmp*tmp)%N;
}
else {
tmp = fast_exp(x,(y-1)/2,N);
tmp = (tmp*tmp)%N;
tmp = (tmp*x)%N;
return tmp;
}
}
gcd.c - gratest common denominator
(big-est number that divides without a remainder)
int gcd(int x, int y)
{
int g;
if(x < 0)
x = -x;
if(y < 0)
y = -y;
if(x+y == 0)
return -1;
g = y;
while(x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
unescape_url.c - URL decode a URL encoded string
void unescape_url(char *url) {
register int i,j;
for(i = 0,j=0; url[i]; i++,j++) {
if((url[i]=url[j]) == '%') {
url[i] = (url[j+1] >= 'A' ? ((url[j+1] & 0xdf) - 'A') + 10 : (url[j+1] - '0'));
url[i] *= 16;
url[i] += (url[j+2] >= 'A' ? ((url[j+2] & 0xdf) - 'A') + 10 : (url[j+2] - '0'));
j += 2;
}
else if (url[i] == '+') {
url[i] = ' ';
}
}
url[i] = '\0';
}
url_encode.cpp - URL encode a string
inline
string url_escape(const string & str)
{
string ret;
stringstream s;
string table = "abcdefhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for(size_t i = 0; i < str.size(); i++) {
if(str[i] == ' ')
s << "+";
else if(table.find(str[i]) != string::npos)
s << str[i];
else
s << "%" << hex << (int)str[i];
}
ret = s.str();
return ret;
}
form_encode.cpp - encode a string for usage in an HTML fill out form element
string form_encode(const char * str)
{
string ret;
const char * p = str;
while(*p) {
if(*p == '\"')
ret += """;
else if(*p == '\n')
ret += " ";
else if(*p == '\r')
ret += " ";
else
ret += *p;
p++;
}
return ret;
}
sin0.cpp - sin(x) function written in C++
double
sin0(double x)
{
double y = x;
double s = -1;
for(int i = 3; i <= 100; i += 2) {
y += s*(pow(x,i)/fact(i));
s *= -1;
}
return y;
}
getch.c - input a character from a terminal without
echoing the character to the screen
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
/*#include <curses.h>*/
int getch()
{
struct termios oldattr,newattr;
int ch;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
return ch;
}
vm_test.cpp memory maped file test program
Sort and Search - C/C++ example sort and searc program