Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

netfish

« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS 

OprOvrcmplx.cpp


#include <iostream>

using namespace std;


class complex
{
public:
complex () {};
complex (float, float);
complex operator+(complex c);
complex operator-(complex c);
complex operator*(complex c);
complex operator/(complex c);
friend ostream& operator<<(ostream& os, complex x);
private:
float real,imag;
};

complex::complex(float real, float imag)
{
(*this).real = real;
(*this).imag = imag;
}

complex complex::operator+(complex c)
{
complex sum;
sum.real = real + c.real;
sum.imag = imag + c.imag;

return sum;
}

complex complex::operator-(complex c)
{
complex diff;
diff.real = real - c.real;
diff.imag = imag - c.imag;

return diff;
}

complex complex::operator*(complex c)
{
complex prod;

prod.real = real*c.real - imag*c.imag;
prod.imag = imag*c.real + real*c.imag;

return prod;
}

complex complex::operator/(complex c)
{
complex quot;

quot.real = real*c.real + imag*c.imag;
quot.imag = imag*c.real - real*c.imag;

quot.real /= (c.real*c.real+c.imag*c.imag);
quot.imag /= (c.real*c.real+c.imag*c.imag);

return quot;
}

ostream& operator<<(ostream& os, complex x)
{
if (x.imag < 0.0)
os << x.real << " - i" << abs(x.imag) << endl;
else
os << x.real << " + i" << x.imag << endl;
return os;
}




void main (void)

{

complex a(1,2);
complex b(3,4);

cout << " a = " << a << endl;
cout << " b = " << b << endl;
cout << "a+b = " << a+b << endl;
cout << "a-b = " << a-b << endl;
cout << "a*b = " << a*b << endl;
cout << "a/b = " << a/b << endl;

}

quickSort.cpp

#include <iostream>

using namespace std;


void show (int size, float *a)
{
int i;
for (i = 0; i < size; i++)
cout << a[i] << " ";
cout << endl;
}
void sort (int size, float *a, int pivotindex)
{
int smidx, idx;
float temp;
float pivot = a[pivotindex];
show (9, a);
a[pivotindex] = a[0];
a[0] = pivot;
cout << "swap(" << 0 << ", " << pivotindex << ")";
cout << " (pivot = " << pivot << ")" << endl;
show (9, a);
smidx = 0;
for (idx = 1; idx < size; idx++)
{
if ( a[idx] < pivot )
{
smidx++;
temp = a[smidx];
a[smidx] = a[idx];
a[idx] = temp;
cout << "swap(" << smidx << ", " << idx << ")" << endl;
show (9,a);
}
}
temp = a[smidx];
a[smidx] = a[0];
a[0] = temp;
cout << "swap(" << 0 << ", " << smidx << ")" << endl;
show (9,a);
}


void main(void)
{
static float a[] = {45, 82, 25, 94, 50, 60, 78, 32, 92};
sort (9, a, 4);
}

besselFunc.cpp

#include <iostream>
#include <iomanip>

using namespace std;

float zero (float x, int n)
{
if (n==0)
return 1;
else
return x*zero(x,n-1)/2/n;
}

float term(float x, int n)
{
if (n==0)
return 1;
else
return -x*term(x,n-1);
}

float bsum (int n, float x, int k, float a)
{
float w;
w = -x*x/4/(k+1)/(n+k+1);
a *= w;
if (fabs(a) < 0.0000001)
return 1;
else
return (1 + w*bsum (n, x, k+1, a));
}

float bessel (int n, float x)
{
return bsum (n, x, 0, 1)*zero(x,n);
}

void main (void)
{
int n;
float x;
for (x=0; x<10; x++)
{
for (n=0; n<7; n++)
cout << setiosflags(ios::fixed) << setprecision(4) << setw(10) << bessel (n, x);
cout << endl;
}
}

displayNode.cpp

#include <iostream>

using namespace std;

class node
{
public:
node (int data);
int data;
node *left, *right;
private:
};

class tree
{
public:
tree (int data);
void add (int data);
void display ();
private:
node *first;
};

node::node (int data)
{
left = NULL;
this->data = data;
right = NULL;
};

tree::tree (int data)
{
first = new node(data);
}

void addnode(node *l, int data)
{
if (data < l->data)
if (l->left == NULL)
l->left = new node (data);
else
addnode (l->left, data);
else
if (l->right == NULL)
l->right = new node (data);
else
addnode (l->right, data);
}

void tree::add(int data)
{
addnode (first, data);
}

void displaynode (node *l)
{
if (l == NULL)
return;
else
{

// Display all the elements in the binary tree in a visual fashion

cout << l->data << endl; // Left Element
cout << "\t\t\t " << "/ \\" << endl;
cout << "\t\t\t " << l->left->data << "\t " << l->right->data << endl;
cout << "\t\t\t " << "/" << " " << "/" << endl;
cout << "\t\t\t " << l->left->left->data << "\t " << l->right->left->data << endl;

}
}

void tree::display()
{
cout << "\t\t\t\t";
displaynode(first);
}


main ()
{



tree b(3); // The "First" root node is initialized using the tree function.
b.add (2); // The elements of the binary tree are recursively added to the binary tree
b.add (1); // These elemets are by no means sorted yet - they simply "grow" the tree.
b.add (5); // They, simply, are being appended to the tree by magnitude of order
b.add (4); // The relatively smaller elements are pushed to the left nodes.
cout << endl << endl << endl;

cout << "\tBefore the nodes of the binary tree were pushed into tree" << endl;
cout << "\tcausing it to grow, they were in the order : " << endl;
cout << "\t3 .. 2.. 1.. 5.. 4.." << endl << endl;
cout << "\tPress [ENTER] to visualize the (Binary) Tree" << endl;
getchar(); cout << endl << endl;

b.display(); cout << endl << endl;

cout << "\t\"It is an immutable law in business that words are words, explanations are explanations," << endl;
cout << "\tpromises are promises but only performance is reality.\" - Harold S. Green" << endl << endl;

getchar();
}

combinations.cpp

#include <iostream>

using namespace std;

double factorial(long n)
{
if (n==0)
return 1;
else
return n*factorial(n-1);
}

double combination_0(int n, int k)
{
return factorial(n)/factorial(n-k)/factorial(k);
}

double combination_1(int n, int k)
{
double result = 1;
while(k > 0)
{
result *= (double)(n--);
result /= (double)(k--);
}
return result;
}

double combination_r(int n, int k)
{
if (k == 0)
return 1;
else
return ((double)n/(double)k)*combination_r(n-1, k-1);
}

double power (int n, double p)
{
if (n==0)
return 1;
else
return p*power(n-1,p);
}

double binomial (int n, int k, double p)
{
return combination_r(n,k)*power(k,p)*power(n-k,1-p);
}

double pi (int n)
{
if(n==0)
return 4;
else
if (n%2 == 0)
return pi(n-1) + 4/(double)(2*n+1);
else
return pi(n-1) - 4/(double)(2*n+1);
}

main (void)
{
int i;
for (i=0; i<5; i++)
cout << i << "! = " << factorial(i) << endl;
cout << endl;
cout << (int)combination_0(52,5) << endl;
cout << (int)combination_1(52,5) << endl;
cout << (int)combination_r(52,5) << endl << endl;
cout << power(2,0.5) << endl;
cout << binomial(3,2,0.5) << endl;
for (i=0; i<1000200; i++)
cout << i << " places " << pi(i) << endl;
getchar();
}

netGlyph.cpp

#include <iostream>

using namespace std;

void function1 ()
{
int i=1;
cout << i++ << endl;
function1();
}

void function2 ()
{
static int i=1;
cout << (i+=10) << (i+=10) << (i+=2) << (i+=100) << (i+=50) << (i+=4) << (i+=670) << (i+=20);
if (i<100000000) function2();
}

main()
{
int i;
function2();
}

termxRecursion.cpp

#include <iostream>

using namespace std;

float term(float x, int n)
{
if (n==0)
return 1;
else
return -x*term(x,n-1);
}

float logr(float x, int n)
{
if (n==3)
return 0;
else
return term(x,n)/(float)n + logr(x, n+1);
}

float log(float x)
{
return logr(x-1,1);
}

main()
{
cout << term(1,1) << " " << term(3,1) << endl;
cout << log(1.0) << " " << log(2.71828) << endl;
getchar();
}

stringPermute.cpp

#include <iostream>
#include <string>

using namespace std;

string swtch(string topermute, int x, int y)
{
string newstring = topermute;
newstring[x] = newstring[y];
newstring[y] = topermute[x]; //avoids temp variable
return newstring;
}
void permute(string topermute, int place)
{
if(place == topermute.length() - 1)
{
cout<<topermute<<endl;
}
for(int nextchar = place; nextchar <
topermute.length(); nextchar++)
{
permute(swtch(topermute, place, nextchar),
place+1);
}
}

int main(int argc, char* argv[])
{
if(argc!=2)
{
cout<<"Proper input is 'permute string'";
}
else
{
permute(argv[1], 0);
}
getchar();
return 0;
}

coloredSlowPrint.cpp

#include <iostream>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <windows.h>

using namespace std;

int main()
{
int i;
char tmp[124];
char ar[] = "WARNING: Array Is Getting Filled!";
strcpy(tmp,ar);
system("color 1f");
for(i=0;i<strlen(ar);i++)
{
cout << ar[i];
Sleep(25);
}
getchar();
MessageBox(NULL, tmp, "Good Job!", MB_OK);
getchar();
}

vortx.pl

#!/usr/bin/perl -w
# Vortex0.pl
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 5842;
my $server = "vortex.labs.pulltheplug.org";

# create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2])
or die "Can't create a socket $!\n";
connect( SOCKET, pack( 'Sn4x8', AF_INET, $port, $server ))
or die "Can't connect to port $port! \n";


my $comm;
while($comm=<STDIN>)
{
chomp $comm;


if($comm ne '')
{

print "\n Received 4-Byte Password '", $pass,"'\n";
$SOCKET->recv($pass,4);

print "\n Sending message '",$pass,"'";
if($SOCKET->send($pass))
{
print "[Done] CONGRATS! Time for some Vodka","\n";
}
}


else
{
# Send an empty message to server and exit
$SOCKET->send($pass);
exit 1;
}
}


close SOCKET or die "close: $!";
« Newer Snippets
Older Snippets »
Showing 1-10 of 14 total  RSS