Post a C or C++ program that you have written

From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.

Announcements Posted on
Ask me ANYTHING - Andrew O'Neill - Buzzcocks comedian, amateur occultist, veggie... 22-05-2013
Sign in to Reply
  1. The_Last_Melon's Avatar
    • Overlord in Training
    Post a C or C++ program that you have written
    I think there should be a thread where people share their ideas like this. I am very new to C++ so I wont post anything until later, assuming this thread becomes popular.

    Post a C or C++ program that you wrote that you think could be useful to someone else browsing this thread.

  2. JGR's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,239
    Re: Post a C or C++ program that you have written
    What do you mean by useful? Also "ideas" is very different to code.
    What exactly are you looking for?

    As you asked though, I have a few C/C++ apps of varying quality on my sourceforge account.
  3. The Polymath's Avatar
    • TSR Demigod
    Re: Post a C or C++ program that you have written
    (Original post by The_Last_Melon)
    I think there should be a thread where people share their ideas like this. I am very new to C++ so I wont post anything until later, assuming this thread becomes popular.

    Post a C or C++ program that you wrote that you think could be useful to someone else browsing this thread.

    Couple of little programs, MS Office, Firefox, nothing major

    All I've done is a little maths tool which was basically a little window with buttons for popular maths symbols like Pi, square root, powers etc. and there was a drop-down menu to let you select the active window for input (e.g. you could select your open Word document for the text to appear in there rather than the Notepad file you have open). I had it installed on my old computer but forgot to back it up! All gone
  4. Popppppy's Avatar
    • Section Moderator
    • Overlord in Training
    • Location: Glasgow
    • Posts: 2,819
    Re: Post a C or C++ program that you have written
    I wrote a timetabling program that will take in a template from Excel, edit it (as per the user requirements) and then save it. Nothing out of the world but it was my uni project
  5. Chrosson's Avatar
    • PS Helper
    • Vengeful, Imperial Overlord of The Student Room
    • Posts: 4,189
    Re: Post a C or C++ program that you have written
    Wrote a rolling hash as a library for another program, because it turns out extensive binary manipulation and garbage collected languages don't play well together.
  6. kelvinscor's Avatar
    • New Member
    • Posts: 12
    Re: Post a C or C++ program that you have written
    I wrote a program to automate my college software audit.

    WinAudit will generate .txt for every pc in my college, all of them are stored in the same directory. My program will read very single line from every single file in that directory and generate an excel table as a report (a report for one lab).
  7. CJKay's Avatar
    • Overlord in Training
    • Location: England
    • Posts: 2,407
    Re: Post a C or C++ program that you have written
  8. Mrkingpenguin's Avatar
    • Benevolent Member
    • Posts: 726
    Re: Post a C or C++ program that you have written
    (sorry I couldn't get line numbers)


    #include <cassert>
    #include <cstdlib>
    #include <iostream>

    class Cpu {
    public:
    virtual int dummy( int, int ) {}
    private:
    virtual int add_( int a, int b ) { return a + b; } /* A */
    virtual int sub_( int a, int b ) { return a - b; } /* B */
    virtual int mul_( int a, int b ) { return a * b; } /* C */
    virtual int div_( int a, int b ) { return a / b; } /* D */
    virtual int mod_( int a, int b ) { return a % b; } /* E */
    virtual int and_( int a, int b ) { return a & b; } /* F */
    virtual int or_( int a, int b ) { return a | b; } /* G */
    virtual int xor_( int a, int b ) { return a ^ b; } /* H */
    };

    int main( int argc, char* argv[] ) {
    typedef int (Cpu::*Memfun)( int, int );

    union {
    Memfun fn;
    unsigned char ptr[6];
    } u;

    Cpu cpu;

    u.fn = &Cpu::dummy;

    assert( argc == 4 );

    int p1 = atoi( argv[ 1 ] );
    int p2 = atoi( argv[ 3 ] );
    char op = argv[2][0];

    assert( op >= 'A' && op <= 'H' );

    u.ptr[0] = 1 + 4 * ( op - 'A' + 1 ); // Don't look here!

    std::cout << "The answer is " << ( (cpu.*(u.fn))( p1, p2 ) ) << std::endl;
  9. Mrkingpenguin's Avatar
    • Benevolent Member
    • Posts: 726
    Re: Post a C or C++ program that you have written
    Here's one that should be familiar (fibonacci anyone?)

    #include <iostream>
    using namespace std;

    int main()
    {
    int count;
    // long long is HUGE! alast and blast are initialized.
    long long alast, blast, sum, n;
    int totamnt;
    alast = 0;
    blast = 1;
    // Determines place in the sequence.
    cout << "Enter time: ";
    cin >> n;
    cout << "\n";
    // Determines inital amount (what to multiply final sum by).
    cout << "Enter total amount: ";
    cin >> totamnt;
    cout << "\n";
    switch (n)
    {
    // Catches 0 before it outputs a negative number.
    case 0: cout << "0"; break;
    // Catches 1 before it outputs 0.
    case 1: cout << 1 * totamnt; break;
    default:
    // Determines place in the sequence.
    for (count= 0; count < n-1; count++)
    {
    // "Rolls" numbers around to make the sequece work.
    sum = alast + blast;
    alast = blast;
    blast = sum;
    };
    // Outputs the place in the sequence multiplied by the initial ammount.
    cout << "\n" << sum * totamnt << " is the total amount."; break;
    }
    return 0;
    }

    -----
  10. Xarren's Avatar
    • Benevolent Member
    • Location: London
    • Posts: 854
    Re: Post a C or C++ program that you have written
    I've written an autonomous navigational system in C for an ARM9 platform.
  11. roblee's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,062
    Re: Post a C or C++ program that you have written
    Link/Cut trees
    Code:
    // solves Codeforces #123 problem E
    
    #ifndef SOURCE_IS_SPLICED
    #include <unordered_set>
    #include <unordered_map>
    #include <functional>
    #include <algorithm>
    #include <iostream>
    #include <numeric>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <list>
    #include <set>
    #include <map>
    using namespace std;
    typedef long long ll;
    #ifdef seventeen
    #define localfun(x) x
    #else
    #define localfun(x)
    #endif
    void run();int main(){localfun(freopen("e.in","r",stdin));ios::sync_with_stdio(false);run();}
    #endif
    
    ll const l=100000;
    ll const mod=1000000007;
    
    struct LinkCut{
      LinkCut *p,*l,*r;
      ll weight;
      ll sum;
    
      inline LinkCut(LinkCut* p=nullptr): p(p){
        l=r=nullptr;
      }
    
      inline bool root(){
        return not p or p->l != this and p->r != this;
      }
    
      inline void push(){
        sum = weight + (l? l->sum: 0) + (r? r->sum: 0);
      }
    
      inline void increase(ll by){
        splay();
        (weight+=by)%=mod;
        push();
      }
    
      inline void rot(LinkCut* LinkCut::*a,LinkCut* LinkCut::*b){
        if (not root()) (p->l==this?p->l:p->r)=(this->*a);
        (this->*a)->p=p, p=(this->*a);
        (this->*a)=(p->*b), (p->*b)=this;
        if (this->*a) (this->*a)->p=this;
    
        if (l) l->push();
        if (r) r->push();
        push();
      }
    
      inline void rotate(){
        if (p->l==this) p->rot(&LinkCut::l,&LinkCut::r);
        else            p->rot(&LinkCut::r,&LinkCut::l);
      }
    
      inline void splay(){
        push();
        while (not root()){
          if (not p->root()) p->p->push(); p->push(); push();
          if (not p->root()) ((p->l==this)==(p->p->l==p)? p:this)->rotate();
          rotate();
        }
        push();
      }
    
      inline void link(LinkCut* x){
        splay(); p=x; p->splay();
      }
    
      inline void expose(){
        for (LinkCut* i=this,*j=nullptr; i; j=i,i=i->p){
          i->splay();
          i->l=j;
          i->push();
        }
        splay();
      }
    
      inline ll depth(){
        splay();
        return sum;
      }
    
      inline LinkCut* findroot(){
        expose();
        LinkCut* x=this;
        while (x->r) x=x->r, x->push();
        return x;
      }
    };
    
    LinkCut tree[l];
    ll w[l];
    
    void run(){
      ll res=0;
    
      int n; cin>>n;
      for (int i=0; i<n; i++){
        int k; cin>>k;
        for (int j=0; j<k; j++){
          ll v,x; cin>>x>>v; --x;
          while (v<0) v+=mod;
    
          ll plus=tree[x].findroot()->depth();
          ll y=tree[x].findroot()-tree;
    
          tree[y].link(tree+i);
          tree[y].increase(v+plus);
          (res+=v+plus)%=mod;
        }
      }
    
      cout<<res<<endl;
    }
  12. Fallen's Avatar
    • Overlord in Training
    Re: Post a C or C++ program that you have written
    Its an open source project, and others have contributed a lot more than myself, but this C++ server software.
  13. rock_climber86's Avatar
    • Peer Of The TSR Realm
    • Posts: 1,828
    Re: Post a C or C++ program that you have written
    hmmm, i want to learn c or c++. Which one should i learn? Which one is more useful? i've got some vba programming experience but that's it
  14. Fallen's Avatar
    • Overlord in Training
    Re: Post a C or C++ program that you have written
    (Original post by rock_climber86)
    hmmm, i want to learn c or c++. Which one should i learn? Which one is more useful? i've got some vba programming experience but that's it
    They are both incredibly useful, and both heavily used.

    You can't really go wrong with either. I suppose C is conceptually simpler to understand (no Object Orientation), but so might be a slightly better choice if you are going to self-teach yourself. With C there are arguably less bad habits you can get yourself into.
  15. rock_climber86's Avatar
    • Peer Of The TSR Realm
    • Posts: 1,828
    Re: Post a C or C++ program that you have written
    (Original post by Fallen)
    They are both incredibly useful, and both heavily used.

    You can't really go wrong with either. I suppose C is conceptually simpler to understand (no Object Orientation), but so might be a slightly better choice if you are going to self-teach yourself. With C there are arguably less bad habits you can get yourself into.
    is C a subset of C++? If i learn C, say, can i progress onto C++ or is the syntax totally different as is the code between vba and C?
  16. Psyk's Avatar
    • TSR Royalty
    • Location: Leamington Spa
    • Posts: 19,075
    Re: Post a C or C++ program that you have written
    (Original post by rock_climber86)
    hmmm, i want to learn c or c++. Which one should i learn? Which one is more useful? i've got some vba programming experience but that's it
    I'd start with C then move onto C++. They are very closely related, so much so that when you first go to C++ from C you'll barely notice the difference. But when you start using C++'s features that C doesn't have you'll realise the difference between them.
  17. Fallen's Avatar
    • Overlord in Training
    Re: Post a C or C++ program that you have written
    (Original post by rock_climber86)
    is C a subset of C++? If i learn C, say, can i progress onto C++ or is the syntax totally different as is the code between vba and C?
    For the most part, yes. C++ is an extension of C.

    The syntax is almost identical where they overlap, although obviously the new features C++ adds require new syntax. Almost all valid C code is valid C++ code.

    I should note, however, that C++ being an extension of C does not mean that C is worse or less useful, just different.
  18. When you see it...'s Avatar
    • Peer Of The TSR Realm
    • Posts: 1,910
    Re: Post a C or C++ program that you have written
    I've got an idea - you could have a 'game' thread similar to this where the OP posts a simple program and then challenges the forum to achieve a simple goal (i.e. solving a maths problem) using a program. The firsty reply solves the problem and then challenges the forum to solve another and so on. I suppose it would take a while for people to respond but if we could get it working then it would be good.
  19. Fried Sock's Avatar
    • Full Member
    • Location: Preston
    • Posts: 97
    Re: Post a C or C++ program that you have written
    (Original post by When you see it...)
    I've got an idea - you could have a 'game' thread similar to this where the OP posts a simple program and then challenges the forum to achieve a simple goal (i.e. solving a maths problem) using a program. The firsty reply solves the problem and then challenges the forum to solve another and so on. I suppose it would take a while for people to respond but if we could get it working then it would be good.
    This is actually a brilliant idea, although maybe the program could be stored in a github repo to avoid pasting the entire program/changes into replies.
  20. When you see it...'s Avatar
    • Peer Of The TSR Realm
    • Posts: 1,910
    Re: Post a C or C++ program that you have written
    (Original post by Fried Sock)
    This is actually a brilliant idea, although maybe the program could be stored in a github repo to avoid pasting the entire program/changes into replies.
    I originally meant seperate programs, but it would be even better if it was one program being continuously improved with relevant enhancements. I cba settting up such a thread though - maybe you could?
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.