C/C++ examples

This is a discussion on C/C++ examples within the C++ Programming forums, part of the General Programming Boards category; Here you'll find C/C++ examples (of all sorts, may be a few of sorts as well)....

  1. #1
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31

    C/C++ examples

    Here you'll find C/C++ examples (of all sorts, may be a few of sorts as well).

  2. #2
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Reduce Rational Numbers
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    So what ?

  4. #4
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Quote Originally Posted by Desolation View Post
    So what ?
    Well nothing special though!
    But it may help someone out there, anyway it will serve as a repository of quick codes for me.

  5. #5
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Code:
    Another example. Checks for balanced ()s {}s []s <>s. They may be nested.
    Attached Files Attached Files

  6. #6
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    A Rational number class that supports +, -, *, /, <<, >> operators.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    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.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    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.

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    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.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    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.

  11. #11
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    Thanks simpleid!
    All your examples look very useful (have not run yet)!
    Quote Originally Posted by simpleid View Post
    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.
    Please suggest a better alternative for that!

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    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"

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by msp View Post
    Please suggest a better alternative for that!
    How about DZone?

    Or what about byteMyCode?

  14. #14
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    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!

  15. #15
    msp
    msp is offline
    Registered User msp's Avatar
    Join Date
    Jul 2007
    Location
    in
    Posts
    31
    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;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows - how to get started... examples don't help
    By nonoob in forum Windows Programming
    Replies: 6
    Last Post: 09-26-2008, 05:45 AM
  2. Thread Pool libraries or examples
    By Mastadex in forum Windows Programming
    Replies: 6
    Last Post: 08-24-2008, 08:58 PM
  3. Serial/Activation Key Validation Examples
    By Welder in forum C Programming
    Replies: 0
    Last Post: 11-04-2007, 09:34 PM
  4. Examples of C++ variables
    By CrazYjoe in forum C++ Programming
    Replies: 9
    Last Post: 01-20-2007, 12:33 PM
  5. Treeview examples.
    By Sebastiani in forum Windows Programming
    Replies: 0
    Last Post: 09-21-2003, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21