working on it ...

Filters

Explore Public Snippets

Sort by

Found 25k snippets matching: cpp

    public by ihsanarifr modified May 25, 2015  253349  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  310791  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  236061  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  143986  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  118372  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  2606  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  2481  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 DinhoPutz modified Feb 5, 2019  72  0  3  -1

    An array of integers that returns colors

    Given an array with and objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not supposed to use the library's sort function for this
    /*
    Given an array with and objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
    Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
    Note: You are not supposed to use the library's sort function for this problem.
    
    Example:
    
    Input: [2,0,2,1,1,0]
    Output: [0,0,1,1,2,2]
    Follow up:
    
    A rather straight forward solution is a two-pass algorithm using counting sort.
    First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
    Could you come up with a one-pass algorithm using only constant space?
    */
    #include<iostream>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    class Solution {
    public:
    	static void sortColors(vector<int>&& nums) {
    		map <int, int> m_int;
    		for (const auto& num : nums) {
    			m_int[num]++;
    		}
    		nums.clear();
    		for (auto& m : m_int) {
    			for (int i = 0;i < m.second;++i) {
    				cout << m.first<<" ";
    				nums.push_back(m.first);
    			}
    		}
    
    	}
    };
    int main() {
    	Solution::sortColors({ 2,0,2,1,1,0 });
    }

    public by FelipeTRM modified Jul 23, 2015  17210  194  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  20122  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 Snippets
    • Channels Snippets