working on it ...

Filters

Explore Public Snippets

Sort by

Found 25k snippets matching: c++

    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 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 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 DanielOgbuagu modified Jun 28, 2015  201728  1  5  0

    Adding a *.jar file (originating from java-wrapped C++ code) to an SBT project classpath

    I thought using unmanaged dependencies in SBT projects was simply dropping a jar file into the lib directory, until I bought an SDK for a biometric device. Both the C++ and the Java packages were provided. I opted for the Java package, copying the provided jar file and the JNI (*.so) files into the appropriate locations and configuring the ja
    /**
     * In my <code>build.sbt</code> file, I appended a blank line and then the following:
     */
    
    libraryDependencies += "groupID" % "artifactId" % "revision" from "file:///home/Danogbuagu/Projects/biometric-lib/filename.jar"
    
    // where,
    // groupID for example is like: com.zigzagcompany 
    // artifactId for example is like: zigzag
    // revision for example is like: 1.3.4
    // Remember the line befor the code and that after.

    public by David Grayson modified Aug 20, 2015  198563  1  3  0

    Woe unto any user that doesn't know that "pow" is a standard library function in C. The C standard allows this inconsistent behavior, sadly (Section 7.1.3 of N1124). I think C++ is better, at least based on how GCC behaves.

    Woe unto any user that doesn't know that "pow" is a standard library function in C. The C standard allows this inconsistent behavior, sadly (Section 7.1.3 of N1124). I think C++ is better, at least based on how GCC behaves.: pow_wtf.c
    #include <stdio.h>
    
    double pow(double x, double y)
    {
        return x + y;
    }
    
    int main()
    {
        volatile int y = 1;
        printf("%f\n", pow(10, y));  // prints 11.000000
        printf("%f\n", pow(10, 1));  // prints 10.000000
        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 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 sercani modified Apr 15, 2014  3241  14  7  5

    C++ Setter Getter Macros

    Add these macros to a header file and include this file wherever you need. If you want to use SetGetMacro for an integer named as "foo", you just need to add this line to your header file: SetGetMacro(foo, int);
    //--------------------------------------------------
    #define SetMacro(name,type)								\
    void Set##name (type _arg)								\
    {														\
      if (this->name != _arg)								\
    	{													\
    		this->name = _arg;								\
    	}													\
    } 
    //--------------------------------------------------
    #define GetMacro(name,type)								\
    type Get##name ()const									\
    {														\
    	return this->name;									\
    } 
    //--------------------------------------------------
    #define SetGetMacro(name,type)							\
    void Set##name (type _arg)								\
    {														\
      if (this->name != _arg)								\
    	{													\
    		this->name = _arg;								\
    	}													\
    }														\
    type Get##name ()const									\
    {														\
    	return this->name;									\
    }
    //--------------------------------------------------
    #define BooleanMacro(name,type)							\
    void name##On ()										\
    {														\
    	if(this->name != static_cast<type>(1))				\
    	{													\
    		this->name = static_cast<type>(1);				\
    	}													\
    }														\
    void name##Off ()										\
    {														\
    	if(this->name != static_cast<type>(0))				\
    	{													\
    		this->name = static_cast<type>(0);				\
    	}													\
    }														\
    type Is##name##On()										\
    {														\
    	return this->name;									\
    }
    //--------------------------------------------------
    #define BooleanSetGetMacro(name,type)					\
    void Set##name (type _arg)								\
    {														\
      if (this->name != _arg)								\
    	{													\
    		this->name = _arg;								\
    	}													\
    }														\
    type Get##name ()const									\
    {														\
    	return this->name;									\
    }														\
    void name##On ()										\
    {														\
    	if(this->name != static_cast<type>(1))				\
    	{													\
    		this->name = static_cast<type>(1);				\
    	}													\
    }														\
    void name##Off ()										\
    {														\
    	if(this->name != static_cast<type>(0))				\
    	{													\
    		this->name = static_cast<type>(0);				\
    	}													\
    }														\
    type Is##name##On()										\
    {														\
    	return this->name;									\
    }

    public by JonahM modified Apr 18, 2013  3272  1  5  0

    C++ ~ Menu

    Need's STRING input for the searchQuote() function.
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <windows.h>
    #include <h:\\Code\\C++\\input2010.h>
    const std::string TXTFILE = "file.dat";
    
    
    using namespace std;
    
    
    struct customer{
    	int opNo, age, engine, noClaims;
    	char area, name[21], sold;								//Sold Y or N
    	double bPremium, aPremium;//bPrem is total before discount. aPrem is total after discount.
    };
    
    void createQuote(customer cust[], int &rows, double &base, double &extras);
    void readFile(customer customers[], int &rows);
    void writeFile ( customer customers[], int rows);
    void showArray( customer cust[], int rows);
    void pick( char &choice, customer cust[], int &rows, double base, double &extras, double &sold, double &notSold);
    void searchQuote(customer cust[], int &rows);
    
    void inputCustomerDetails(customer &cust, double base, double extras)
    {	system("CLS");
    cout << "Enter your name: \n";
    Input(cust.name,20);
    cout << "Enter your age: \n";
    Input(cust.age);
    if(cust.age < 25)
    	extras += (.75*base);
    if ( cust.age > 50 )
    	extras += (.05*base);
    
    cout << "Please enter a Area (A,B or C): \n";
    Input(cust.area);
    
    while (cust.area !='A' && cust.area !='B' && cust.area !='C')
    {
    	cout << "Please enter a correct Area (A,B or C): \n";
    	Input(cust.area);
    }
    
    
    if (cust.area == 'A')
    	extras += 0.01*base;
    
    if (cust.area == 'B')
    	extras += .02*base;
    
    if (cust.area == 'C')
    	extras += (.03*base);
    
    cout << "Enter your engine size in CC: \n";
    Input(cust.engine);
    
    if(cust.engine >= 1300 && cust.engine <= 1500)
    	extras += (.03*base);
    
    if (cust.engine > 1500)
    	extras += (.04*base);
    
    
    cout << "Enter the amount of year without claims bonus:\n";
    Input(cust.noClaims);
    
    cust.bPremium = base+extras;
    cust.aPremium = cust.bPremium-(cust.noClaims*.10*cust.bPremium);
    cout << cust.aPremium << endl;
    cout << "The extras we pay you is: \n"<<cust.aPremium << endl;
    
    }
    
    void readFile(customer customers[], int &rows)
    {	  	
    	ifstream myfile(TXTFILE,ios::binary);
    	if (!myfile.is_open())
    		cout << "Unable to open file: " << TXTFILE << endl; 	  
    	else
    	{				
    		while ( !myfile.eof() )
    		{			
    			myfile.read((char *)&customers[rows], sizeof(customer));
    			rows++;			
    		}		
    		rows--;		
    		myfile.close();
    	}
    }
    
    // WRITE OUT 1 RECORD AT A TIME
    void writeFile (customer customers[], int rows) 
    {  
    	int r=0;
    	ofstream myfile(TXTFILE,ios::binary);
    	if (myfile.is_open())
    	{
    		while ( r < rows )
    		{
    			myfile.write((char *)&customers[r], sizeof(customer));
    			r++;
    		}
    		myfile.close();
    		cout << "Data written to " << TXTFILE << "\n";
    	}
    	else 
    	  cout << "Unable to open file: " << TXTFILE << endl;  
    }
    void displayAll(customer cust[], int rows)
    {
    	int sub=0;
    	while ( sub < rows )
    	{
    		cout
    			<< cust[sub].opNo << " "			//*
    			<< cust[sub].name << " "
    			<< cust[sub].age << " "
    			<< cust[sub].engine << " "
    			<< cust[sub].area << " "
    			<< cust[sub].sold << " "		//*
    			<< cust[sub].aPremium << " "
    			<< cust[sub].bPremium << endl;
    		sub++;
    	}
    }
    void searchQuote(customer cust[], int &rows)
    {	
    	clrscr();
    	char name[31];
    	cout << "Please enter search name" << endl;
    	Input(name,30);
    
    	for(int i=0;i<rows;i++)
    	{
    		if ( strnicmp(cust[i].name, name, strlen(name) ) == 0 ) 
    		{
    			cout << cust[i].name << endl;
    		}
    	}
    }
    
    void add( customer cust[], int &rows, double base, double &extras, double &sold, double &notSold)
    {
    	clrscr();
    	char quoteQuestion;
    	inputCustomerDetails(cust[rows], base, extras);
    
    	char yesno;
    	cout<<"do you want to sell this quote?" << endl;
    	Input(yesno);
    	cust[rows].sold = yesno;
    	if(yesno == 'y' || yesno == 'Y')
    		sold += cust[rows].aPremium;
    	else
    		notSold += cust[rows].bPremium;
    
    	rows++;
    }
    
    
    
    void menu(char &choice)
    {
    	cout << "\n\t------------Main Menu-----------";
    	cout << "\n\t|                               |";
    	cout << "\n\t|                               |";
    	cout << "\n\t|     (C)reate a QUOTE         |";
    	cout << "\n\t|     (D)isplay all QUOTE      |";
    	cout << "\n\t|     (S)earch by name           |";
    	cout << "\n\t|     (Q)uit                    |";
    	cout << "\n\t|                               |";
    	cout << "\n\t|                               |";
    	cout << "\n\t---------------------------------" << "\n\n>>>" ;
    	Input(choice);
    }
    
    void pick(char &choice, customer cust[], int &rows, double base, double &extras, double &sold, double &notSold)
    {
    	switch(choice)
    	{
    	case 'c' : add(cust, rows, base, extras, sold, notSold);
    		break;
    	case 'd' : displayAll(cust, rows);
    		break;
    	case 's' : searchQuote(cust, rows);
    		break;
    	case 'q' : 	
    		break;
    	default : "\n\tInvalid Input!";
    
    
    	}
    
    }
    
    void main()
    {
    	char choice ='x';
    	customer cust[99];
    	int rows = 0;
    	double extras = 0;
    	double base = 100;
    	char close = ' ';
    	int opNo = 0;
    	double gt[4] = {0,0,0,0};
    	char quoteQuestion = ' ';
    	double sold[4] = {0,0,0,0};
    	double notSold[4] = {0,0,0,0};
    
    	system("Color 0A");
    	readFile(cust,rows);
    	cout<<"Please enter your operator number..." << endl;
    	Input(opNo);
    
    	while (opNo !=0)
    	{
    		choice = 'x';
    		while (choice != 'q')
    		{
    			cust[rows].opNo = opNo;
    			menu(choice);
    			pick(choice,cust,rows,base,extras,sold[opNo],notSold[opNo]);
    			
    		}
    		cout<<"Please enter your operator number..." << endl;
    		Input(opNo);
    	}
    	cout << endl << endl;
    	cout << "Total Sold for Operator 1: " << sold[1] << endl;//Displaying the amount sold
    	cout << "Total Sold for Operator 2: " << sold[2] << endl;
    	cout << "Total Sold for Operator 3: " << sold[3] << endl << endl;
    	cout << "  Not Sold for Operator 1: " << notSold[1] << endl;//Displaying the amount not sold
    	cout << "  Not Sold for Operator 2: " << notSold[2] << endl;
    	cout << "  Not Sold for Operator 3: " << notSold[3] << endl << endl;
    	cout <<"The GTotal for Operator 1: " << gt[1] <<endl;//Displaying the grand total for each Operator.
    	cout <<"The GTotal for Operator 2: " << gt[2] <<endl;
    	cout <<"The GTotal for Operator 3: " << gt[3] <<endl;
    
    	writeFile(cust, rows);
    	system("pause");
    
    }
    
    
    
    
    • Public Snippets
    • Channels Snippets