Here you'll find C/C++ examples (of all sorts, may be a few of sorts as well).
Here you'll find C/C++ examples (of all sorts, may be a few of sorts as well).
Code:Another example. Checks for balanced ()s {}s []s <>s. They may be nested.
A Rational number class that supports +, -, *, /, <<, >> operators.
Simple audio capture using OpenAL:
See below where I referenced Buffers[0]? Im only read the FIRST value of a large buffer, the entire sample is there so consider that!
This was designed to be an application to test/alter the values I get from the capture... understand that.
For those that need it:
-lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32 -lopenal32
Code:#include <AL/al.h> #include <AL/alc.h> #include <iostream> using namespace std; const int SRATE = 44100, SSIZE = 1024; ALCdevice *alDevice; ALbyte alBuffer[SRATE/2]; ALint alSample; short dint = 0; int main(int argc, char *argv[]) { alGetError(); alDevice = alcCaptureOpenDevice(NULL, SRATE, AL_FORMAT_STEREO16, SSIZE); if (alGetError() != AL_NO_ERROR) { return 0; } alcCaptureStart(alDevice); while (true) { cout << " \r"; // ... is there a better way to clear the first line? lol. :-) alcGetIntegerv(alDevice, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &alSample); alcCaptureSamples(alDevice, (ALCvoid *)alBuffer, alSample); dint = ((((int *)alBuffer)[0] >> 0) & 0xFFFF); // Left Channel cout << dint << "\t"; dint = ((((int *)alBuffer)[0] >> 16) & 0xFFFF); // Right Channel cout << dint << "\r"; } alcCaptureStop(alDevice); alcCaptureCloseDevice(alDevice); return EXIT_SUCCESS; }
Last edited by simpleid; 08-14-2007 at 01:59 PM.
An FFT algorithm:
(vectors, floats, etc. are as is due to the fact that i designed it for my application, just change it to suit your needs... obviously.)
Code:#include <math.h> #include <vector> vector<float> fourierT (vector<float> & w) { vector<float> Sa, Im, Re; int N=w.size(), L= int(float(N)/2.0); float fIm=0.0, fRe=0.0, p=0.0, h=0.0; for (k=0; k<L; ++k) { Im.push_back(w.at(k*2)); Re.push_back(w.at((k*2)+1)); } p = (2 * M_PI / float(N+N)); for (j=0; j<N; ++j) { fIm = 0.0, fRe = 0.0; for (k=0; k<L; ++k) { h = j * k * p; fIm += Im.at(k) * sin(h); fRe += Re.at(k) * cos(h); } Sa.push_back( sqrt(fIm*fIm + fRe*fRe) ); } return Sa; }
Last edited by simpleid; 08-14-2007 at 01:43 PM.
Once I saw someone ask about this on the forum, so here's the code again- RGB->INT | INT->RGB:
Code:#include <iostream> #include <vector> using namespace std; int toINT (int r, int g, int b); vector<int> toRGB (int k); int main(int argc, char *argv[]) { int hClr=0; vector<int> vRGB; vector<int>::iterator vRGBi; hClr = toINT(40, 120, 255); cout << hClr <<endl; vRGB = toRGB(hClr); for (vRGBi = vRGB.begin(); vRGBi != vRGB.end(); ++vRGBi) { cout << (*vRGBi) << " "; } cout <<endl; system("PAUSE"); return EXIT_SUCCESS; } int toINT (int r, int g, int b) { // assuming rgb is > 0 and <= 255 int cR=0, cG=0, cB=0; cR = r; // cR = 0x000000RR cG = cR << 8; // cG = 0x0000RR00 cG = cG | g; // cG = 0x0000RRGG cB = cG << 8; // cB = 0x00RRGG00 cB = cB | b; // cB = 0x00RRGGBB return cB; } vector<int> toRGB (int k) { vector<int> sINT; sINT.push_back((k >> 16) & 0x000000FF); sINT.push_back((k >> 8) & 0x000000FF); sINT.push_back(k & 0x000000FF); return sINT; }
Last edited by simpleid; 08-14-2007 at 01:57 PM.
Oh and BTW, a forum thread makes for a very bad code repository, but I do know the above will be useful to certain individuals... that's why I posted anyway.
Last edited by simpleid; 08-14-2007 at 01:47 PM.
I find that rational class rather lacking.
For something a bit more satisfying, check the one on my homepage (Useful Classes)
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
How about DZone?
Or what about byteMyCode?
Yeah iMalc!
I am actually a An Idiot at C++ and posted my tiny examples here for my own reference and
any newbie like me (who may see them useful).
Thanks brewbuck!
My own auto_ptr implementation:
Code:#include <iostream> using namespace std; template <typename T> class ptr { T* p; public: ptr(T* q=0) { p = q; } ~ptr() { cout << *p << " eek.\n"; delete p; } T& operator*() { return *p; } }; int main() { ptr<int> ip = new int; *ip = 5; cout << *ip << endl; ptr<double> dp = new double; *dp = 5.5; cout << *dp << endl; return 0; }