working on it ...

Filters

Explore Public Snippets

Sort by

Found 25k snippets matching: cpp

    public by ihsanarifr modified May 25, 2015  253327  0  4  0

    Belajar Class CPP

    Belajar Class CPP: belajar_class.cpp
    // Program Belajar Class pada CPP
    // Soal Toki Nomor 24  http://apps.cs.ipb.ac.id/lx
    // @author : Ihsan Arif Rahman
    #include<ios>
    #include<iostream>
    #include<iomanip>
    #include<list>
     
    using namespace std;
    class Person {
       private:
          string nama;
          int usia;
          int tinggi;
          double berat;
          double imt;
          string status;
          double rataan;
          double IMT() { double t=(double)tinggi/100.0; return (berat/(t*t)); }
     
       public:
          Person() { nama=""; usia=tinggi=0; berat=0.0; }
          void setPerson(string nm, int u, int t, double b) {
             nama=nm; usia=u; tinggi=t; berat=b;
             imt=IMT(); status=StatusGizi();
          }
          string getNama() { return nama; }
          int setBerat(){ return berat; }
          int getUsia() { return usia; }
          int getTinggi() { return tinggi; }
          double getBerat() { return berat; }
          double getIMT() { return imt; }
          string getStatusGizi() { return status; }
     
          string StatusGizi() {
             double t=IMT();
             if (t<17.0) return "sangat kurus";
             else if (t<18.5) return "kurus";
             else if (t<25.0) return "normal";
             else if (t<28.0) return "gemuk";
             else return "sangat gemuk"; }
     
          void cetak() {
             cout << nama << " " << usia << " " << tinggi << " ";
             cout << fixed << setprecision(2) << berat << " " << imt << " " << status << endl;
          }
          void cetak2(){
              cout << nama << " " << usia << endl;
          }
    };
     
    int main(){
        list<Person> p;
        Person orang;
        int iterasi,i;
        int status[5] = {0};
        string na;
        int us,ti,banyak=0;
        double jti=0.0, be,rataan=0;
        cin >> iterasi;
        for(i=0;i<iterasi;i++){
            cin >> na >> us >> ti >> be;
            orang.setPerson(na,us,ti,be);
            p.push_back(orang);
        }
        list<Person>::iterator it;
        for(it=p.begin();it!=p.end();++it){
                (*it).cetak();
                jti = jti + (*it).getTinggi();
        }
        rataan = jti/double(iterasi);
        cout << fixed << setprecision(2) << rataan << endl;
     
        for(it=p.begin();it!=p.end();++it){
                //(*it).cetak2();
                if((*it).getTinggi() > rataan){
                    //cout << (*it).getTinggi();
                    banyak++;
                }
                if((*it).StatusGizi() == "sangat kurus"){
                    status[0]++;
                }else if((*it).StatusGizi() == "kurus"){
                    status[1]++;
                }else if((*it).StatusGizi() == "normal"){
                    status[2]++;
                }else if((*it).StatusGizi() == "gemuk"){
                    status[3]++;
                }else if((*it).StatusGizi() == "sangat gemuk"){
                    status[4]++;
                }
        }
        cout << banyak << endl;
        cout << status[0] << " " << status[1] << " " << status[2] << " " << status[3] << " " << status[4] << endl;
        return 0;
    }
    
    

    public by aashcool modified Dec 4, 2014  310776  2  5  2

    Why we need assignment operator overloading

    c++
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <map>
    #include<set>
    using namespace std;
    
    class Person {
    protected:
        string* name_p;
        int age;
    public:
        Person () : name_p(new string("anonymus")), age(30) {}
        ~Person() {delete name_p;}
        void set_age(int agep) {age = agep;}
        void set_name(string agep) {*name_p = agep;}
        void get_name() {cout<<*name_p<<endl;}
    };
    
    class Developer: public Person {
    };
    
    int main() {
        
        Person obj1;
        Person obj2;
        obj1.set_name("Rajesh");
        obj2.set_name("Mahesh");
        obj1 = obj2;
        delete &obj2;
        obj1.get_name();
        return 0;
    }
    
    /*This program should output "mahesh" but outputs nothing. 
    The reason is that on line 32 shallow copy happened.
    www.cplusplus.com/doc/tutorial/classes2/

    public by Ihsan Arif modified May 25, 2015  236039  1  4  0

    Belajar C++

    Belajar C++: penjumlahan.cpp
    // Program Penjumlahan pada CPP
    // Soal Toki Nomor 21  http://apps.cs.ipb.ac.id/lx
    // @author : Ihsan Arif Rahman
    #include <iostream>
    #include <iomanip> //std::setprecision
    using namespace std;
    int main() {
        double a,b,c;
        cin >> a >> b ;
        c = a + b;
        cout << fixed << setprecision(2) << c << endl;
        //std::cout << std::setprecision(3) << c << endl;
        return 0;
    }
    
    

    public by avtukhoff modified Oct 17, 2012  143964  0  5  1

    low-level mouse hook

    low-level mouse hook
    #pragma once
    #include "StdAfx.h"
    
    
    extern
    HINSTANCE g_hApplication;
    
    
    
    HHOOK				g_hMouseHook;
    
    
    LRESULT CALLBACK MouseHookProcedure (INT	iHookCode,
    									 WPARAM wParameter,
    									 LPARAM lParameter)
    {
    
    	if (iHookCode == HC_ACTION)
    	{
    		// Обработка хука
    
    
    		if (wParameter == WM_MOUSEWHEEL || wParameter == WM_MOUSEHWHEEL)
    		{
    			
    
    			MSLLHOOKSTRUCT *psInformation;
    
    			psInformation = (MSLLHOOKSTRUCT *) lParameter;
    
    
    			HWND hWindow;
    
    			hWindow = ::WindowFromPoint (psInformation->pt);
    
    			if (hWindow)
    			{
    				::SendMessage (hWindow, wParameter, wParameter, lParameter);
    
    
    			}
    			
    
    		}
    		
    
    	}
    	
    
    
    
    	// Иначе вернем управление
    	return (::CallNextHookEx (g_hMouseHook, iHookCode, wParameter, lParameter));
    }
    
    
    
    
    
    
    
    
    BOOL_MAXIMUM SetMouseHook (VOID)
    {
    	
    	g_hMouseHook = ::SetWindowsHookEx (WH_MOUSE_LL,
    									   MouseHookProcedure,
    									   g_hApplication,
    									   ::GetCurrentThreadId ());
    
    
    
    
    
    	if (g_hMouseHook)
    	{
    		return (TRUE);
    	}
    	
    	return (FALSE);
    }
    
    
    
    
    
    
    
    BOOL_MAXIMUM RemoveMouseHook (VOID)
    {
    	if (g_hMouseHook)
    	{
    		return (::UnhookWindowsHookEx (g_hMouseHook));
    	}
    
    	return (FALSE);
    }
    
    
    
    
    

    public by DavidEGrayson modified Sep 11, 2015  118351  0  3  1

    libusb test transitions

    libusb test transitions: ltt.cpp
    #include <libusb.h>
    #include <iostream>
    #include <iomanip>
    #include <chrono>
    #include <cassert>
    #include <unistd.h>
    
    typedef std::chrono::steady_clock clock_type;
    
    std::ostream & log()
    {
        return std::cerr
            << clock_type::now().time_since_epoch().count()
            << ": ";
    }
    
    void check_device(libusb_device * device)
    {
        static std::string current_status;
        std::string status;
    
        if (device)
        {
            status = "Found device.  ";
    
            // Try to open a handle.
            libusb_device_handle * handle;
            int result = libusb_open(device, &handle);
            if (result != 0)
            {
                status += libusb_error_name(result);
            }
            else
            {
                status += "Success.";
                libusb_close(handle);
            }
        }
        else
        {
            status = "Not found.";
        }
    
        //if (current_status != status)
        {
            log() << status << std::endl;
            current_status = status;
        }
    }
    
    int main(int argc, char ** argv)
    {
        libusb_init(NULL);
        libusb_set_debug(NULL, 4);
    
        std::cout
            << "Clock tick period: "
            << clock_type::period::num
            << "/"
            << clock_type::period::den
            << " seconds" << std::endl;
    
        while(1)
        {
            libusb_device ** list;
            ssize_t count = libusb_get_device_list(NULL, &list);
            libusb_device * device = NULL;
            for(ssize_t i = 0; i < count; i++)
            {
                libusb_device * candidate = list[i];
                libusb_device_descriptor desc;
                int result = libusb_get_device_descriptor(candidate, &desc);
                assert(result == 0);
                if (desc.idVendor == 0x1FFB && desc.idProduct == 0xDA01)
                {
                    device = candidate;
                    break;
                }
            }
            check_device(device);
            libusb_free_device_list(list, 1);
            usleep(10);
        }
        return 0;
    }
    
    
    

    public by Ihsan Arif modified May 25, 2015  2585  0  4  0

    Belajar Clas CPP

    Belajar Clas CPP: class_person.cpp
    // Program Belajar Class pada CPP
    // Soal Toki Nomor 23  http://apps.cs.ipb.ac.id/lx
    // @author : Ihsan Arif Rahman
    #include<ios>
    #include<iostream>
    #include<iomanip>
    #include<list>
     
    using namespace std;
    class Person {
       private:
          string nama;
          int usia;
          int tinggi;
          double berat;
          double imt;
          string status;
          double rataan;
          double IMT() { double t=(double)tinggi/100.0; return (berat/(t*t)); }
          string StatusGizi() {
             double t=IMT();
             if (t<17.0) return "sangat kurus";
             else if (t<18.5) return "kurus";
             else if (t<25.0) return "normal";
             else if (t<28.0) return "gemuk";
             else return "sangat gemuk"; }
       public:
          Person() { nama=""; usia=tinggi=0; berat=0.0; }
          void setPerson(string nm, int u, int t, double b) {
             nama=nm; usia=u; tinggi=t; berat=b;
             imt=IMT(); status=StatusGizi();
          }
          string getNama() { return nama; }
          int setBerat(){ return berat; }
          int getUsia() { return usia; }
          int getTinggi() { return tinggi; }
          double getBerat() { return berat; }
          double getIMT() { return imt; }
          string getStatusGizi() { return status; }
     
          void cetak() {
             cout << nama << " " << usia << " " << tinggi << " ";
             cout << fixed << setprecision(2) << berat << " " << imt << " " << status << endl;
          }
          void cetak2(){
              cout << nama << " " << usia << endl;
          }
    };
     
    int main(){
        list<Person> p;
        Person orang;
        
        int iterasi,i;
        string na;
        int us,ti,banyak=0;
        double jti=0.0, be,rataan=0;
        cin >> iterasi;
        for(i=0;i<iterasi;i++){
            cin >> na >> us >> ti >> be;
            orang.setPerson(na,us,ti,be);
            p.push_back(orang);
        }
        list<Person>::iterator it;
        for(it=p.begin();it!=p.end();++it){
                (*it).cetak2();
                jti = jti + (*it).getTinggi();
        }
        rataan = jti/double(iterasi);
        cout << fixed << setprecision(2) << rataan << endl;
     
        for(it=p.begin();it!=p.end();++it){
                //(*it).cetak2();
                if((*it).getTinggi() > rataan){
                    //cout << (*it).getTinggi();
                    banyak++;
                }
        }
        cout << banyak << endl;
        return 0;
    }
    
    
    

    public by ihsanarifr modified May 26, 2015  2462  1  4  0

    Belajar Vector CPP

    Belajar Vector CPP: belajar_vector.cpp
    // Program Belajar Vector pada CPP
    // Soal Toki Nomor 25 http://apps.cs.ipb.ac.id/lx
    // @author : Ihsan Arif Rahman
    #include <iostream>
    #include<iomanip>
    #include <vector>
    using namespace std;
    
    int main ()
    {
      int i;
    
      vector<int> v6;
      int t,hapus,CountOrigin,CountModified, hasilOrigin=0,hasilModified=0;
      double RataOrigin, RataModified;
    
      cin >> t;
      do{
        v6.push_back(t);
        cin >> t;
      }while (t!= -9);
    
      for (i=0; i < v6.size(); i++)
        hasilOrigin += v6[i];
    
      CountOrigin = v6.size();
      RataOrigin = double(hasilOrigin) / double(CountOrigin);
    
      cin >> hapus;
      i=0;
      do{
        if(hapus == 1){
            v6.erase(v6.begin());
            cin >> hapus;
            i++;
        }else{
            v6.erase(v6.begin()+hapus-i-1);
            cin >> hapus;
            i++;
        }
      }
      while(hapus != -9);
    
      for (i=0; i < v6.size(); i++)
        hasilModified += v6[i];
    
      CountModified = v6.size();
      RataModified = double(hasilModified) / double(CountModified);
    
      cout << CountOrigin << " " << CountModified << endl;
    
      if(v6.empty())
        cout << -9.99 << endl;
      else
        cout << fixed << setprecision(2) << RataOrigin << " " << fixed << setprecision(2) << RataModified << endl;
    
      return 0;
    }
    
    
    

    public by FelipeTRM modified Jul 23, 2015  17145  186  7  0

    Codingame - Temperatures - C++

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    /**
     * Auto-generated code below aims at helping you parse
     * the standard input according to the problem statement.
     **/
    int main()
    {
        int N,c; // the number of temperatures to analyse
        scanf("%d", &N);
        int number[N],ptemps,i=0,FLAG=0,mark;
    
        for(i=0;i<N;i++){
            scanf("%d",&number[i]);
           }
    
        sort(number,number+N);
    
        for(i=0;i<N;i++){
           if(number[i]>0)FLAG++;
           if(FLAG==1)mark=i;
        }
    
        if(N==0)printf("0");
        else if(-1*number[mark-1]<number[mark])printf("%d",number[mark-1]);
        else printf("%d",number[mark]);
    
    }

    public by axydus modified Jan 27, 2017  20115  2  4  0

    C++_SwitchCase(axyd)

    Simple switch...case for menu choice
    int choice= 0;
    cin>>choice;
    
    switch(choice){
    	case 1:
    		
    		break;
    	case 2:
    		
    		break;
    	case 3:
    		
    		break;
    	case 4:
    		
    		break;
    	default :
    		
    		break;		
    }

    public by evandrix modified Feb 26, 2013  2654  2  4  0

    Generating all partitions of a given number: A partition of a positive integer number "n" is a way of writing "n" as a sum of positive integers.

    Generating all partitions of a given number: A partition of a positive integer number "n" is a way of writing "n" as a sum of positive integers.: int_partition.cpp
    C++
    #include <iostream>
    
    // Output a partition:
    void output_partition(const int n, const int *x, const int how_many_partitions)
    {
        std::cout << "Partition(" << how_many_partitions << ")" << " = ";
        for (int i = 1; i <= n; i++)
        {
            // Can't show negative numbers:
            if (x[i] > 0)
            {
                if (i == n)
                    std::cout << x[i];
                else
                    std::cout << x[i] << " + ";
            }
        }
        std::cout << std::endl;
    }
    
    // This is the function which generates the partitions of a given number "n".
    void generate_partitions(int *x, const int n)
    {
        int k, s = 0; // s represents the sum of already-generated components
        // Initialize the solution vector's elements with -1
        int how_many_partitions = 0;
        for (k = 1; k < n; k++)
            x[k] = -1;
        k = 1;
        while (k > 0)
        {
            // Generated a solution, let's output it then:
            if (k == n)
            {
                how_many_partitions++; // Increase the number of generated partitions
                x[n] = n - s;
                output_partition(n, x, how_many_partitions);
                k--;
                s = s - x[k];
            }
            else
            {
                // Check for another solution:
                if (((n - k + 1) * (x[k] + 1)) <= n - s)
                {
                    x[k]++;
                    if (x[k] >= x[k - 1])
                    {
                        s = s + x[k];
                        k++;
                    }
                }
                else
                {
                                    // Didn't find any solutions, reset x[k] to -1
                    x[k] = -1;
                    k--;
                    s = s - x[k];
                }
            }
        }
    }
    
    // Test:
    int main()
    {
        int number;
        std::cout << "n = ";
        std::cin >> number;
        int *x = new int[number + 1];
        x[0] = 0;
        std::cout << "Partitions of " << number << ":\n\n";
        generate_partitions(x, number);
        delete [] x;
        std::cin.ignore();
        std::cin.get();
        return 0;
    }
    
    
    
    • Public Snippets
    • Channels Snippets